Note
Go to the end to download the full example code.
ASW inference - Two pulses model#
This example implements inference for the ASW population under a two pulses model of admixture, using the tracts package. Inference is performed using autosomal and X chromosome data, allowing for the specification of sex-biased admixture.
To implement this example, we use the following driver file:
samples:
directory: ./ASW_TrioPhased
individual_names: [
"NA19625","NA19700","NA19701","NA19703","NA19704","NA19707","NA19711","NA19712","NA19713","NA19818","NA19819",
"NA19834","NA19835","NA19900","NA19901","NA19904","NA19908","NA19909","NA19913","NA19914","NA19916","NA19917",
"NA19920","NA19921","NA19922","NA19923","NA19982","NA19984","NA20126","NA20127","NA20274","NA20276","NA20278",
"NA20281","NA20282","NA20287","NA20289","NA20291","NA20294","NA20296","NA20298","NA20299","NA20314","NA20317",
"NA20318","NA20320","NA20321","NA20332","NA20334","NA20339","NA20340","NA20342","NA20346","NA20348","NA20351",
"NA20355","NA20356","NA20357","NA20359","NA20362","NA20412"]
male_names : [
"NA19700","NA19703","NA19711","NA19818","NA19834","NA19900","NA19904","NA19908","NA19916","NA19920",
"NA19922","NA19982","NA19984","NA20126","NA20278","NA20281","NA20291","NA20298","NA20318","NA20340",
"NA20342","NA20346","NA20348","NA20351","NA20356","NA20362"] #see Readme_dataprocessing.md for how this was generated
filename_format: "{name}_{label}_final.bed"
labels: [A, B] #If this field is omitted, 'A' and 'B' will be used by default
chromosomes: 1-22 #The chromosomes to use for analysis. Can be specified as a list or a range
allosomes: [X]
models:
model_filename: ../models/ppp_pxx.yaml
ad_model_autosomes: M
ad_model_allosomes: DC
start_params:
t1: 14:16
REUR: 0.1:0.9
REUR_sex_bias: 0.1:0.3
t2: 4:8
REUR2: 0.1:0.8
REUR2_sex_bias: 0.1:0.3
RNAT: 0.1:0.9
RNAT_sex_bias: 0.1:0.3
optim:
repetitions: 5
seed: 100
maximum_iterations: 1000
npts: 50
exclude_tracts_below_cm: 2
unknown_labels_for_smoothing: ["UNK", "centromere","miscall"] # segments with these labels will be smoother over, that is, will be filled with neighbouring ancestries up to their midpoints.
fix_parameters_from_ancestry_proportions: ['REUR2', 'RNAT', 'REUR2_sex_bias', 'RNAT_sex_bias']
output:
output_directory: ./output_two_pulses/
output_filename_format: "ASW_test_output_{label}"
log_filename: 'ASW_two_pulses.log'
verbose_log: 1
verbose_screen: 30
log_scale: True
Complete results from this analysis are saved in the output directory specified in the driver file. Below, we display the optimal parameters estimated from this analysis, as well as the plots illustrating the inferred tract length distributions, compared to the observed histograms, for every source population and chromosome type (autosomes and X chromosome).
Optimal parameters#
parameter |
value |
|---|---|
REUR |
0.11652118627342288 |
REUR_sex_bias |
0.14106245227227787 |
RNAT |
0.041815376914786304 |
RNAT_sex_bias |
-0.09003584652900054 |
t1 |
11.008511204151068 |
REUR2 |
0.08610552943841718 |
REUR2_sex_bias |
-1.0 |
t2 |
5.035390532997822 |
X_AFR_rate |
0.8416634368117908 |
X_AFR_sex_bias |
-0.08003130271237033 |
likelihood -1704.25 |
Optimal migration matrices#
Tract length histograms#
Autosomal admixture#
X chromosome admixture in females#
X chromosome admixture in males#
------------------------------------------------------------------------------------------------
Running tracts 2.0 with driver file: /home/runner/work/tracts/tracts/example/documentation_examples/ASW/ASW_two_pulses.yaml
------------------------------------------------------------------------------------------------
Results will be written to: output_two_pulses.
Using log file: output_two_pulses/ASW_two_pulses.log.
excluding_tracts_below set to 2.0 cM.
Re-optimization will be performed until convergence or maximum 5 times.
Ancestries: EUR, NAT, AFR
Data autosome proportions: [0.19578862 0.03825495 0.76595643]
Data allosome proportions: [0.16839124 0.03818939 0.79341937]
The following parameters have been fixed from ancestry proportions: REUR2, RNAT, REUR2_sex_bias, RNAT_sex_bias
Model parameters and bounds:
--------------------------------------------
Parameter | Lower bound | Upper bound
--------------------------------------------
REUR | 1e-09 | 1
REUR_sex_bias | -1 | 1
RNAT | 1e-09 | 1
RNAT_sex_bias | -1 | 1
t1 | 1 | inf
REUR2 | 1e-09 | 1
REUR2_sex_bias | -1 | 1
t2 | 1 | inf
--------------------------------------------
Multiple starting parameters will be generated and used for multiple optimization runs.
------------------------------------------------------------------------------
Step 1 : Optimizing autosomal likelihood over parameters ['REUR', 't1', 't2'].
------------------------------------------------------------------------------
Starting parameters for step 1 optimization
------------------------------------------------
Run | REUR | t1 | t2
------------------------------------------------
1 | 0.4537 | 15.41 | 6.681
2 | 0.1272 | 15.14 | 5.513
Warning: starting parameters are out of bounds.
3 | 0.1737 | 14.55 | 7.734
4 | 0.2105 | 15.42 | 6.904
5 | 0.4494 | 14.44 | 7.182
------------------------------------------------
Optimization run #1
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.391. This can happen when no sex bias parameter allows for the observed ancestry proportions.
28 , -19955.1 , array([ 0.457825 , 0 , 0.0321764 , 0.989574 , 15.2266 , 2.43362e-09, -0.964696 , 6.69511 ]), Autosomes
Optimization completed.
-----------------------
Optimization run #2
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
30 , -2441.54 , array([ 0.120178 , 0 , 0.0417856 , -0.092409 , 12.6393 , 0.0830782 , -1 , 5.2017 ]), Autosomes
60 , -1495.26 , array([ 0.116749 , 0 , 0.0419934 , -0.100824 , 10.9967 , 0.0885383 , -1 , 5.03317 ]), Autosomes
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.001. This can happen when no sex bias parameter allows for the observed ancestry proportions.
72 , -1439.54 , array([ 0.116521 , 0 , 0.0420032 , -0.0992152 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Autosomes
Optimization completed.
-----------------------
Optimization run #3
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
30 , -5295.56 , array([ 0.168303 , 0 , 0.0392788 , -0.0334845 , 13.8038 , 0.0228821 , -1 , 7.56728 ]), Autosomes
60 , -5222.54 , array([ 0.167946 , 0 , 0.0392712 , -0.0306462 , 13.7474 , 0.0233436 , -1 , 7.56924 ]), Autosomes
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.015. This can happen when no sex bias parameter allows for the observed ancestry proportions.
89 , -5195.35 , array([ 0.167952 , 0 , 0.0392662 , -0.0302609 , 13.7216 , 0.0233293 , -1 , 7.56843 ]), Autosomes
Optimization completed.
-----------------------
Optimization run #4
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
30 , -8810.29 , array([ 0.206837 , 0 , 0.0441383 , -0.999999 , 14.888 , 4.5738e-06 , -1 , 6.87633 ]), Autosomes
60 , -8586.93 , array([ 0.20609 , 0 , 0.044111 , -1 , 14.7655 , 6.09348e-06, -1 , 6.87173 ]), Autosomes
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.04. This can happen when no sex bias parameter allows for the observed ancestry proportions.
80 , -8552.81 , array([ 0.205973 , 0 , 0.0441087 , -1 , 14.7465 , 6.34222e-06, -1 , 6.87145 ]), Autosomes
Optimization completed.
-----------------------
Optimization run #5
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.377. This can happen when no sex bias parameter allows for the observed ancestry proportions.
29 , -16881 , array([ 0.448449 , 0 , 0.032164 , 0.98731 , 14.0891 , 4.9149e-09 , -1 , 7.10882 ]), Autosomes
Optimization completed.
-----------------------
In Step 1: Results from multiple optimization runs with different starting parameters:
----------------------------------------------------------------------------------------------------------------------------------------------
Run | LogLik | REUR | REUR_sex_bias | RNAT | RNAT_sex_bias | t1 | REUR2 | REUR2_sex_bias | t2
----------------------------------------------------------------------------------------------------------------------------------------------
1 | -19955.1 | 0.4578 | 0 | 0.03218 | 0.9896 | 15.23 | 2.434e-09 | -0.9647 | 6.695
2 | -1439.54 | 0.1165 | 0 | 0.042 | -0.09922 | 11.01 | 0.08878 | -1 | 5.035
3 | -5195.35 | 0.168 | 0 | 0.03927 | -0.03026 | 13.72 | 0.02333 | -1 | 7.568
4 | -8552.81 | 0.206 | 0 | 0.04411 | -1 | 14.75 | 6.342e-06 | -1 | 6.871
5 | -16881 | 0.4484 | 0 | 0.03216 | 0.9873 | 14.09 | 4.915e-09 | -1 | 7.109
----------------------------------------------------------------------------------------------------------------------------------------------
Selecting best parameters from step 1 and proceeding to step 2 optimization.
-----------------------------------------------------------------------------
Step 2 : Optimizing allosomal likelihood over parameters : ['REUR_sex_bias'].
-----------------------------------------------------------------------------
Starting parameters for step 2 optimization (non-sex-bias parameters are fixed to the best step 1 estimates).
-------------------------------------------------------------------------------------------------------------------------------
Run | REUR | REUR_sex_bias | RNAT | RNAT_sex_bias | t1 | REUR2 | REUR2_sex_bias | t2
-------------------------------------------------------------------------------------------------------------------------------
Warning: starting parameters are out of bounds.
1 | 0.1165 | 0.2193 | 0.042 | -0.09562 | 11.01 | 0.08878 | -1 | 5.035
Warning: starting parameters are out of bounds.
2 | 0.1165 | 0.138 | 0.042 | -0.09599 | 11.01 | 0.08878 | -1 | 5.035
3 | 0.1165 | 0.2004 | 0.042 | -0.09576 | 11.01 | 0.08878 | -1 | 5.035
4 | 0.1165 | 0.2599 | 0.042 | -0.09383 | 11.01 | 0.08878 | -1 | 5.035
5 | 0.1165 | 0.2312 | 0.042 | -0.09395 | 11.01 | 0.08878 | -1 | 5.035
-------------------------------------------------------------------------------------------------------------------------------
Optimization run #1
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.008. This can happen when no sex bias parameter allows for the observed ancestry proportions.
15 , -170.927 , array([ 0.116521 , 0.21477 , 0.0420032 , -0.0903096 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Female allosomes
15 , -105.602 , array([ 0.116521 , 0.21477 , 0.0420032 , -0.0903096 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Male allosomes
Optimization completed.
-----------------------
Optimization run #2
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.005. This can happen when no sex bias parameter allows for the observed ancestry proportions.
12 , -171.244 , array([ 0.116521 , 0.141062 , 0.0420032 , -0.0900358 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Female allosomes
12 , -105.079 , array([ 0.116521 , 0.141062 , 0.0420032 , -0.0900358 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Male allosomes
Optimization completed.
-----------------------
Optimization run #3
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.007. This can happen when no sex bias parameter allows for the observed ancestry proportions.
16 , -171.055 , array([ 0.116521 , 0.185949 , 0.0420032 , -0.0951576 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Female allosomes
16 , -105.374 , array([ 0.116521 , 0.185949 , 0.0420032 , -0.0951576 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Male allosomes
Optimization completed.
-----------------------
Optimization run #4
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.009. This can happen when no sex bias parameter allows for the observed ancestry proportions.
12 , -170.797 , array([ 0.116521 , 0.257532 , 0.0420032 , -0.104996 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Female allosomes
12 , -105.85 , array([ 0.116521 , 0.257532 , 0.0420032 , -0.104996 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Male allosomes
Optimization completed.
-----------------------
Optimization run #5
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.007. This can happen when no sex bias parameter allows for the observed ancestry proportions.
17 , -170.955 , array([ 0.116521 , 0.207427 , 0.0420032 , -0.0894919 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Female allosomes
17 , -105.552 , array([ 0.116521 , 0.207427 , 0.0420032 , -0.0894919 , 11.0085 , 0.0887846 , -1 , 5.03539 ]), Male allosomes
Optimization completed.
-----------------------
In Step 2: Results from multiple optimization runs with different starting parameters:
----------------------------------------------------------------------------------------------------------------------------------------------
Run | LogLik | REUR | REUR_sex_bias | RNAT | RNAT_sex_bias | t1 | REUR2 | REUR2_sex_bias | t2
----------------------------------------------------------------------------------------------------------------------------------------------
1 | -276.529 | 0.1165 | 0.2148 | 0.042 | -0.09031 | 11.01 | 0.08878 | -1 | 5.035
2 | -276.323 | 0.1165 | 0.1411 | 0.042 | -0.09004 | 11.01 | 0.08878 | -1 | 5.035
3 | -276.428 | 0.1165 | 0.1859 | 0.042 | -0.09516 | 11.01 | 0.08878 | -1 | 5.035
4 | -276.647 | 0.1165 | 0.2575 | 0.042 | -0.105 | 11.01 | 0.08878 | -1 | 5.035
5 | -276.507 | 0.1165 | 0.2074 | 0.042 | -0.08949 | 11.01 | 0.08878 | -1 | 5.035
----------------------------------------------------------------------------------------------------------------------------------------------
Selecting best parameters from step 2.
Step 2 used allosomal data only. Final likelihood is evaluated on autosomal + allosomal data at the selected optimal parameters.
--------------------------------------------------------------------------------------------------
Launching re-optimization until convergence is achieved or 5 re-optimizations have been performed.
--------------------------------------------------------------------------------------------------
Re-optimization 1/5: re-optimizing starting from the current optimal parameters (likelihood = -1715.858940).
------------------------------------------------------------------------------
Step 1 : Optimizing autosomal likelihood over parameters ['REUR', 't1', 't2'].
------------------------------------------------------------------------------
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.006. This can happen when no sex bias parameter allows for the observed ancestry proportions.
21 , -1428.06 , array([ 0.116521 , 0.141062 , 0.0418154 , -0.0900358 , 11.0085 , 0.0861055 , -1 , 5.03539 ]), Autosomes
Optimization completed.
-----------------------
-----------------------------------------------------------------------------
Step 2 : Optimizing allosomal likelihood over parameters : ['REUR_sex_bias'].
-----------------------------------------------------------------------------
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.005. This can happen when no sex bias parameter allows for the observed ancestry proportions.
13 , -171.265 , array([ 0.116521 , 0.141062 , 0.0418154 , -0.0900358 , 11.0085 , 0.0861055 , -1 , 5.03539 ]), Female allosomes
13 , -104.927 , array([ 0.116521 , 0.141062 , 0.0418154 , -0.0900358 , 11.0085 , 0.0861055 , -1 , 5.03539 ]), Male allosomes
Optimization completed.
-----------------------
Change in likelihood from -1715.858940 to -1704.250511 after re-optimizing.
Re-optimization 2/5: re-optimizing starting from the current optimal parameters (likelihood = -1704.250511).
------------------------------------------------------------------------------
Step 1 : Optimizing autosomal likelihood over parameters ['REUR', 't1', 't2'].
------------------------------------------------------------------------------
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.006. This can happen when no sex bias parameter allows for the observed ancestry proportions.
21 , -1428.06 , array([ 0.116521 , 0.141062 , 0.0418154 , -0.0900358 , 11.0085 , 0.0861055 , -1 , 5.03539 ]), Autosomes
Optimization completed.
-----------------------
-----------------------------------------------------------------------------
Step 2 : Optimizing allosomal likelihood over parameters : ['REUR_sex_bias'].
-----------------------------------------------------------------------------
Iter. Log-likelihood Model parameters Transmission
-------------------------------------------------------------
At the end of the current optimization step: Could not solve for parameters fixed by ancestry proportions. Final error: 0.005. This can happen when no sex bias parameter allows for the observed ancestry proportions.
13 , -171.265 , array([ 0.116521 , 0.141062 , 0.0418154 , -0.0900358 , 11.0085 , 0.0861055 , -1 , 5.03539 ]), Female allosomes
13 , -104.927 , array([ 0.116521 , 0.141062 , 0.0418154 , -0.0900358 , 11.0085 , 0.0861055 , -1 , 5.03539 ]), Male allosomes
Optimization completed.
-----------------------
No further improvement in likelihood after 2 repetitions. Re-optimization completed.
Final parameters and corresponding likelihood computed on autosomal + allosomal data:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LogLik | REUR | REUR_sex_bias | RNAT | RNAT_sex_bias | t1 | REUR2 | REUR2_sex_bias | t2 | X_AFR_rate | X_AFR_sex_bias
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-1704.25 | 0.1165 | 0.1411 | 0.04182 | -0.09004 | 11.01 | 0.08611 | -1 | 5.035 | 0.8417 | -0.08003
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Parameters X_AFR_rate, X_AFR_sex_bias correspond to the dependent ancestry and were not free in the optimization.
Predicted autosome proportions: [0.19286176 0.03820215 0.76893609]
Predicted allosome proportions: [0.17277316 0.03821172 0.78901512]
Results saved to : output_two_pulses
{'destination_dir': PosixPath('/home/runner/work/tracts/tracts/docs/source/auto_examples/ASW/output_two_pulses'), 'table_file': PosixPath('/home/runner/work/tracts/tracts/docs/source/auto_examples/ASW/output_two_pulses/ASW_test_output_optimal_parameters.txt')}
import sys
from pathlib import Path
from tracts.driver import run_tracts
# Read files automatically for online documentation
sys.path.append('.')
script_dir = Path.cwd()
driver_filename = script_dir / "ASW_two_pulses.yaml"
run_tracts(
driver_filename=str(driver_filename),
script_dir=str(script_dir),
)
Total running time of the script: (8 minutes 29.092 seconds)