88import os
99import matplotlib .patches as mpatches
1010from matplotlib import pyplot as plt
11+ import stdpopsim
1112import numpy as np
1213# Force matplotlib to not use any Xwindows backend.
1314matplotlib .use ('Agg' )
@@ -59,8 +60,40 @@ def plot_compound_msmc(infiles, outfile):
5960 ax .plot (nt ['x' ], nt ['y' ], c = "red" )
6061 f .savefig (outfile , bbox_inches = 'tight' )
6162
63+ def plot_compound_smcsmc_with_guide (infiles , outfile , generation_time , pop_id = 0 , nhaps = 1 , model = None , steps = None ):
64+ f , ax = plt .subplots (figsize = (7 , 7 ))
65+ ax .set (xscale = "log" , yscale = "log" )
66+
67+ if model == "ooa" :
68+ model = getattr (stdpopsim .homo_sapiens ,"GutenkunstThreePopOutOfAfrica" )()
69+
70+ if model is not None :
71+ ddb = msprime .DemographyDebugger (** model .asdict ())
72+ if steps is None :
73+ end_time = ddb .epochs [- 2 ].end_time + 10000
74+ steps = np .exp (np .linspace (1 ,np .log (end_time ),31 ))
75+ num_samples = [0 for _ in range (ddb .num_populations )]
76+ num_samples [pop_id ] = 20
77+ coal_rate , P = ddb .coalescence_rate_trajectory (steps = steps ,
78+ num_samples = num_samples , double_step_validation = False )
79+ steps = steps * generation_time
80+ ax .plot (steps , 1 / (2 * coal_rate ), c = "black" , drawstyle = 'steps-pre' )
81+
82+
83+ for infile in infiles :
84+ nt = pandas .read_csv (infile , usecols = [1 , 2 ], skiprows = 0 )
85+ ax .step (nt ['x' ], nt ['y' ], c = "red" )
86+
87+ ax .set_ylim ([1e3 ,1e6 ])
88+ ax .set_xlabel ('Years before present' )
89+ ax .set_ylabel ('Effective population size' )
90+ h_string = "" .join (nhaps )
91+ ax .set_title (f"SMCSMC Estimated Ne ({ h_string } samples)" )
92+
93+ f .savefig (outfile , bbox_inches = 'tight' )
6294
63- def plot_all_ne_estimates (sp_infiles , smcpp_infiles , msmc_infiles , outfile ,
95+
96+ def plot_all_ne_estimates (sp_infiles , smcpp_infiles , msmc_infiles , smcsmc_infiles , outfile ,
6497 model , n_samp , generation_time , species ,
6598 pop_id = 0 , steps = None ):
6699
@@ -73,9 +106,14 @@ def plot_all_ne_estimates(sp_infiles, smcpp_infiles, msmc_infiles, outfile,
73106 coal_rate , P = ddb .coalescence_rate_trajectory (steps = steps ,
74107 num_samples = num_samples , double_step_validation = False )
75108 steps = steps * generation_time
109+
76110 num_msmc = set ([os .path .basename (infile ).split ("." )[0 ] for infile in msmc_infiles ])
111+ num_smcsmc = set ([os .path .basename (infile ).split ("." )[0 ] for infile in smcsmc_infiles ])
112+
77113 num_msmc = sorted ([int (x ) for x in num_msmc ])
78- f , ax = plt .subplots (1 ,2 + len (num_msmc ),sharex = True ,sharey = True ,figsize = (14 , 7 ))
114+ num_smcsmc = sorted ([int (x ) for x in num_msmc ])
115+
116+ f , ax = plt .subplots (1 ,2 + len (num_msmc ) + len (num_smcsmc ), sharex = True ,sharey = True ,figsize = (14 , 7 ))
79117 for infile in smcpp_infiles :
80118 nt = pandas .read_csv (infile , usecols = [1 , 2 ], skiprows = 0 )
81119 line1 , = ax [0 ].plot (nt ['x' ], nt ['y' ], alpha = 0.8 )
@@ -86,6 +124,7 @@ def plot_all_ne_estimates(sp_infiles, smcpp_infiles, msmc_infiles, outfile,
86124 line2 , = ax [1 ].plot (nt ['year' ], nt ['Ne_median' ],alpha = 0.8 )
87125 ax [1 ].plot (steps , 1 / (2 * coal_rate ), c = "black" )
88126 ax [1 ].set_title ("stairwayplot" )
127+
89128 for i ,sample_size in enumerate (num_msmc ):
90129 for infile in msmc_infiles :
91130 fn = os .path .basename (infile )
@@ -99,6 +138,22 @@ def plot_all_ne_estimates(sp_infiles, smcpp_infiles, msmc_infiles, outfile,
99138 for i in range (2 + len (num_msmc )):
100139 ax [i ].set (xscale = "log" , yscale = "log" )
101140 ax [i ].set_xlabel ("time (years ago)" )
141+
142+ for i ,sample_size in enumerate (num_smcsmc ):
143+ for infile in smcsmc_infiles :
144+ fn = os .path .basename (infile )
145+ samp = fn .split ("." )[0 ]
146+ if (int (samp ) == sample_size ):
147+ nt = pandas .read_csv (infile , usecols = [1 , 2 ], skiprows = 0 )
148+ line3 , = ax [2 + len (num_msmc ) + i ].plot (nt ['x' ], nt ['y' ],alpha = 0.8 )
149+ ax [2 + i ].plot (steps , 1 / (2 * coal_rate ), c = "black" )
150+ ax [2 + i ].set_title (f"smcsmc, ({ sample_size } samples)" )
151+ plt .suptitle (f"{ species } , population id { pop_id } " , fontsize = 16 )
152+ for i in range (2 + len (num_msmc )):
153+ ax [i ].set (xscale = "log" , yscale = "log" )
154+ ax [i ].set_xlabel ("time (years ago)" )
155+
156+
102157 red_patch = mpatches .Patch (color = 'black' , label = 'Coalescence rate derived Ne' )
103158 ax [0 ].legend (frameon = False , fontsize = 10 , handles = [red_patch ])
104159 ax [0 ].set_ylabel ("population size" )
0 commit comments