|
8 | 8 | import os |
9 | 9 | import matplotlib.patches as mpatches |
10 | 10 | from matplotlib import pyplot as plt |
| 11 | +import stdpopsim |
11 | 12 | import numpy as np |
12 | 13 | # Force matplotlib to not use any Xwindows backend. |
13 | 14 | matplotlib.use('Agg') |
@@ -61,52 +62,99 @@ def plot_compound_msmc(infiles, outfile): |
61 | 62 | ax.plot(nt['x'], nt['y'], c="red") |
62 | 63 | f.savefig(outfile, bbox_inches='tight') |
63 | 64 |
|
| 65 | +def plot_compound_smcsmc_with_guide(infiles, outfile, generation_time, pop_id = 0, nhaps = 1, model = None, steps = None): |
| 66 | + f, ax = plt.subplots(figsize=(7, 7)) |
| 67 | + ax.set(xscale="log", yscale="log") |
64 | 68 |
|
65 | | -def plot_all_ne_estimates(sp_infiles, smcpp_infiles, msmc_infiles, outfile, |
66 | | - model, n_samp, generation_time, species, |
67 | | - pop_id=0, steps=None): |
| 69 | + if model is not None: |
| 70 | + ddb = msprime.DemographyDebugger(**model.asdict()) |
| 71 | + if steps is None: |
| 72 | + end_time = ddb.epochs[-2].end_time + 10000 |
| 73 | + steps = np.exp(np.linspace(1,np.log(end_time),31)) |
| 74 | + num_samples = [0 for _ in range(ddb.num_populations)] |
| 75 | + num_samples[pop_id] = 20 |
| 76 | + coal_rate, P = ddb.coalescence_rate_trajectory(steps=steps, |
| 77 | + num_samples=num_samples, double_step_validation=False) |
| 78 | + steps = steps * generation_time |
| 79 | + ax.plot(steps, 1/(2*coal_rate), c="black", drawstyle = 'steps-pre') |
68 | 80 |
|
69 | | - ddb = msprime.DemographyDebugger(**model.asdict()) |
70 | | - if steps is None: |
71 | | - end_time = ddb.epochs[-2].end_time + 10000 |
72 | | - steps = np.linspace(1, end_time, end_time+1) |
73 | | - num_samples = [0 for _ in range(ddb.num_populations)] |
74 | | - num_samples[pop_id] = n_samp |
75 | | - coal_rate, P = ddb.coalescence_rate_trajectory(steps=steps, |
76 | | - num_samples=num_samples, |
77 | | - double_step_validation=False) |
78 | | - steps = steps * generation_time |
79 | | - num_msmc = set([os.path.basename(infile).split(".")[0] for infile in msmc_infiles]) |
80 | | - num_msmc = sorted([int(x) for x in num_msmc]) |
81 | | - f, ax = plt.subplots(1, 2+len(num_msmc), sharex=True, sharey=True, figsize=(14, 7)) |
82 | | - for infile in smcpp_infiles: |
| 81 | + |
| 82 | + for infile in infiles: |
83 | 83 | nt = pandas.read_csv(infile, usecols=[1, 2], skiprows=0) |
84 | | - line1, = ax[0].plot(nt['x'], nt['y'], alpha=0.8) |
85 | | - ax[0].plot(steps, 1/(2*coal_rate), c="black") |
86 | | - ax[0].set_title("smc++") |
87 | | - for infile in sp_infiles: |
88 | | - nt = pandas.read_csv(infile, sep="\t", skiprows=5) |
89 | | - line2, = ax[1].plot(nt['year'], nt['Ne_median'], alpha=0.8) |
90 | | - ax[1].plot(steps, 1/(2*coal_rate), c="black") |
91 | | - ax[1].set_title("stairwayplot") |
92 | | - for i, sample_size in enumerate(num_msmc): |
93 | | - for infile in msmc_infiles: |
94 | | - fn = os.path.basename(infile) |
95 | | - samp = fn.split(".")[0] |
96 | | - if(int(samp) == sample_size): |
97 | | - nt = pandas.read_csv(infile, usecols=[1, 2], skiprows=0) |
98 | | - line3, = ax[2+i].plot(nt['x'], nt['y'], alpha=0.8) |
99 | | - ax[2+i].plot(steps, 1/(2*coal_rate), c="black") |
100 | | - ax[2+i].set_title(f"msmc, ({sample_size} samples)") |
101 | | - plt.suptitle(f"{species}, population id {pop_id}", fontsize=16) |
102 | | - for i in range(2+len(num_msmc)): |
103 | | - ax[i].set(xscale="log", yscale="log") |
104 | | - ax[i].set_xlabel("time (years ago)") |
105 | | - red_patch = mpatches.Patch(color='black', label='Coalescence rate derived Ne') |
106 | | - ax[0].legend(frameon=False, fontsize=10, handles=[red_patch]) |
107 | | - ax[0].set_ylabel("population size") |
108 | | - f.savefig(outfile, bbox_inches='tight', alpha=0.8) |
| 84 | + ax.step(nt['x'], nt['y'], c="red") |
| 85 | + |
| 86 | + ax.set_ylim([1e3,1e6]) |
| 87 | + ax.set_xlabel('Years before present') |
| 88 | + ax.set_ylabel('Effective population size') |
| 89 | + h_string = "".join(nhaps) |
| 90 | + ax.set_title(f"SMCSMC Estimated Ne ({h_string} samples)") |
| 91 | + |
| 92 | + f.savefig(outfile, bbox_inches='tight') |
| 93 | + |
109 | 94 |
|
| 95 | +def plot_all_ne_estimates(sp_infiles, smcpp_infiles, msmc_infiles, smcsmc_infiles, outfile, |
| 96 | + model, n_samp, generation_time, species, |
| 97 | + pop_id = 0, steps=None): |
| 98 | + |
| 99 | + ddb = msprime.DemographyDebugger(**model.asdict()) |
| 100 | + if steps is None: |
| 101 | + end_time = ddb.epochs[-2].end_time + 10000 |
| 102 | + steps = np.linspace(1,end_time,end_time+1) |
| 103 | + num_samples = [0 for _ in range(ddb.num_populations)] |
| 104 | + num_samples[pop_id] = n_samp |
| 105 | + coal_rate, P = ddb.coalescence_rate_trajectory(steps=steps, |
| 106 | + num_samples=num_samples, double_step_validation=False) |
| 107 | + steps = steps * generation_time |
| 108 | + |
| 109 | + num_msmc = set([os.path.basename(infile).split(".")[0] for infile in msmc_infiles]) |
| 110 | + num_smcsmc = set([infile.split("/")[-2].split(".")[0] for infile in smcsmc_infiles]) |
| 111 | + |
| 112 | + num_msmc = sorted([int(x) for x in num_msmc]) |
| 113 | + num_smcsmc = sorted([int(x) for x in num_smcsmc]) |
| 114 | + |
| 115 | + f, ax = plt.subplots(1,2+len(num_msmc) + len(num_smcsmc), sharex=True,sharey=True,figsize=(14, 7)) |
| 116 | + for infile in smcpp_infiles: |
| 117 | + nt = pandas.read_csv(infile, usecols=[1, 2], skiprows=0) |
| 118 | + line1, = ax[0].plot(nt['x'], nt['y'], alpha=0.8) |
| 119 | + ax[0].plot(steps, 1/(2*coal_rate), c="black") |
| 120 | + ax[0].set_title("smc++") |
| 121 | + for infile in sp_infiles: |
| 122 | + nt = pandas.read_csv(infile, sep="\t", skiprows=5) |
| 123 | + line2, = ax[1].plot(nt['year'], nt['Ne_median'],alpha=0.8) |
| 124 | + ax[1].plot(steps, 1/(2*coal_rate), c="black") |
| 125 | + ax[1].set_title("stairwayplot") |
| 126 | + |
| 127 | + plot_counter=2 |
| 128 | + for i,sample_size in enumerate(num_msmc): |
| 129 | + for infile in msmc_infiles: |
| 130 | + fn = os.path.basename(infile) |
| 131 | + samp = fn.split(".")[0] |
| 132 | + if(int(samp) == sample_size): |
| 133 | + nt = pandas.read_csv(infile, usecols=[1, 2], skiprows=0) |
| 134 | + line3, = ax[plot_counter].plot(nt['x'], nt['y'],alpha=0.8) |
| 135 | + ax[plot_counter].plot(steps, 1/(2*coal_rate), c="black") |
| 136 | + ax[plot_counter].set_title(f"msmc, ({sample_size} samples)") |
| 137 | + plot_counter+=1 |
| 138 | + |
| 139 | + for i,sample_size in enumerate(num_smcsmc): |
| 140 | + for infile in smcsmc_infiles: |
| 141 | + samp = infile.split("/")[-2].split(".")[0] |
| 142 | + if(int(samp) == sample_size): |
| 143 | + nt = pandas.read_csv(infile, usecols=[1, 2], skiprows=0) |
| 144 | + line3, = ax[plot_counter].plot(nt['x'], nt['y'],alpha=0.8) |
| 145 | + ax[plot_counter].plot(steps, 1/(2*coal_rate), c="black") |
| 146 | + ax[plot_counter].set_title(f"smcsmc, ({sample_size} samples)") |
| 147 | + plot_counter+=1 |
| 148 | + plt.suptitle(f"{species}, population id {pop_id}", fontsize = 16) |
| 149 | + for i in range(2+len(num_msmc)+len(num_smcsmc)): |
| 150 | + ax[i].set(xscale="log", yscale="log") |
| 151 | + ax[i].set_xlabel("time (years ago)") |
| 152 | + |
| 153 | + |
| 154 | + red_patch = mpatches.Patch(color='black', label='Coalescence rate derived Ne') |
| 155 | + ax[0].legend(frameon=False, fontsize=10, handles=[red_patch]) |
| 156 | + ax[0].set_ylabel("population size") |
| 157 | + f.savefig(outfile, bbox_inches='tight', alpha=0.8) |
110 | 158 |
|
111 | 159 | def plot_stairwayplot_coalrate(sp_infiles, outfile, |
112 | 160 | model, n_samp, generation_time, species, |
|
0 commit comments