-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-dolores.py
578 lines (533 loc) · 20.9 KB
/
run-dolores.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#!/usr/bin/env python
# coding: utf-8
import sys
import os
import getopt
import gc
import pickle
from tqdm import tqdm
import tskit
import tszip
import msprime
import stdpopsim
import dolores.cladespans
import dolores.simulations
import dolores.viz
def main(argv):
chromosome = None
name = None
trees_loc = None
species = "HomSap"
Ne_file = None
genetic_map_stdpopsim = "HapMapII_GRCh37"
genetic_map_loc = None
use_genotype_id = True
overwrite = False
cM_limit = 0.01
muts_per_kb = 0.05
use_muts = True
plot_on = True
try:
opts, args = getopt.getopt(
argv,
"hC:n:t:s:G:g:u:c:m:M:op",
[
"chromosome=",
"name=",
"trees_loc=",
"species_stdpopsim=",
"genetic_map_stdpopsim=",
"genetic_map_loc=",
"use_genotype_id=",
"cM_limit=",
"muts_per_kb=",
"mut_spans=",
"overwrite",
"plot_off",
],
)
except getopt.GetoptError:
print("For usage: python -m run-dolores -h")
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print("")
print("python -m run-dolores -C <chromosome> -n <name> -t <filepath> ")
print("-" * 100)
print("Example:")
print("python -m run-dolores -C chr21 -n chr21_100_relate -t example")
print("-" * 100)
print("Options:")
print("-" * 100)
print("-C", "--chromosome", ": Chromosome (e.g. chr21), str")
print("-n", "--name", ": Name of trees file (without extension), str")
print(
"-t",
"--trees_loc",
": Location of trees file (output directory will be created here), str",
)
print(
"-s",
"--species",
": Species in stdpopsim catalogue (for getting Ne and genetic map), str",
)
print(
"-G",
"--genetic_map_stdpopsim",
": Name of genetic map to use from stdpopsim (default: HapMapII_GRCh37), str",
)
print(
"-g",
"--genetic_map_loc",
": Location of genetic map to load from file, str",
)
print(
"-u",
"--use_genotype_id",
": Whether to use genotype ID (or sample ID; if yes, must have a .poplabels file",
"\n in the same directory as the trees; default: 1), 0 or 1",
)
print(
"-c",
"--cM_limit",
": How far apart can clades be for them to be merged together (default: 0.01), float",
)
print(
"-m",
"--muts_per_kb",
": Required number of mutations per kb, float (default: 0.05)",
)
print(
"-M",
"--mut_spans",
": Whether to use mutations to calculate clade span, 0 or 1 (default: 1)",
)
print(
"-o",
"--overwrite",
": If output files already exist, whether to recompute p-values (default: False)",
)
print(
"-p",
"--plot_off",
": Whether to skip plotting the results (default: False)",
)
print("-" * 100)
sys.exit()
elif opt in ("-C", "--chromosome"):
chromosome = arg
elif opt in ("-n", "--name"):
name = arg
elif opt in ("-t", "--trees_loc"):
trees_loc = arg
elif opt in ("-s", "--species"):
species = arg
elif opt in ("-G", "--genetic_map_stdpopsim"):
genetic_map_stdpopsim = arg
elif opt in ("-g", "--genetic_map_loc"):
genetic_map_loc = arg
elif opt in ("-u", "--use_genotype_id"):
use_genotype_id = bool(int(arg))
elif opt in ("-c", "--cM_limit"):
cM_limit = float(arg)
elif opt in ("-m", "--muts_per_kb"):
muts_per_kb = float(arg)
elif opt in ("-M", "--mut_spans"):
use_muts = bool(int(arg))
elif opt in ("-o", "--overwrite"):
overwrite = True
elif opt in ("-p", "--plot"):
plot_on = False
if (
chromosome is None
or trees_loc is None
or (genetic_map_loc is None and genetic_map_stdpopsim is None)
):
print("For usage: python -m run-dolores -h")
sys.exit(2)
if not os.path.exists(trees_loc + "/" + name + ".popsize"):
print("Error: must have a .popsize file")
sys.exit(2)
if use_genotype_id and not os.path.exists(trees_loc + "/" + name + ".poplabels"):
print("Warning: option set to use genotype IDs, but .poplabels file not found")
print("Defining clades through sample IDs instead")
use_genotype_id = False
epoch_starts = []
Ne_list = []
Ne_file = trees_loc + "/" + name + ".popsize"
with open(Ne_file, "r") as file:
for line in file:
line = line.strip().split(",")
if line[1] != "NA":
epoch_starts.append(float(line[0]))
Ne_list.append(float(line[1]))
else:
epoch_starts.append(1000000000.0)
if genetic_map_loc is None:
if species is None:
print("Must specify recombination map file or species")
sys.exit(2)
if genetic_map_loc is None:
print("Getting recombination map from stdpopsim...")
species_ = stdpopsim.get_species(species)
contig = species_.get_contig(
chromosome=chromosome, genetic_map=genetic_map_stdpopsim
)
recombination_map = contig.recombination_map
else:
print("Getting recombination map from file...")
recombination_map = msprime.RateMap.read_hapmap(genetic_map_loc)
print("=" * 100)
print("Inputs:")
print("Chromosome:", chromosome)
print("Trees location:", trees_loc)
print("Species:", species)
print("Recombination map from stdpopsim:", genetic_map_stdpopsim)
print("Recombination map location:", genetic_map_loc)
print("Using genotype IDs:", use_genotype_id)
print("cM limit for merging clades:", cM_limit)
print("Mutations per kb:", muts_per_kb)
print("Overwrite:", overwrite)
print("=" * 100)
output_dir = trees_loc + "/" + name + "_output"
if not os.path.exists(output_dir):
print("Making results directory: " + output_dir)
os.mkdir(output_dir)
print("-" * 100)
# ----------------------------------------------------------------------------------------------------------------------
# GETTING METADATA
# ----------------------------------------------------------------------------------------------------------------------
if (
os.path.exists(output_dir + "/" + name + ".clades.gz")
and os.path.exists(output_dir + "/" + name + ".pvalues.gz")
and not overwrite
):
print("Results already exist, loading. Specify -o option to overwrite.")
results = dolores.cladespans.read_from_file(output_dir + "/" + name)
else:
if use_genotype_id:
print("Getting metadata...")
md, populations, groups = dolores.simulations.read_in_pop_metadata(
trees_loc + "/" + name + ".poplabels"
)
print("Groups and populations:")
print(groups, populations)
filename = output_dir + "/" + name + "_samp_to_ind.pickle"
if not os.path.exists(filename):
print("Getting map of samples to individuals...")
samp_to_ind = {}
inds = {}
i = 0
for key, val in md.items():
if val["ID"] not in inds:
inds[val["ID"]] = i
i += 1
samp_to_ind[key] = inds[val["ID"]]
with open(filename, "wb") as file:
pickle.dump(samp_to_ind, file)
del samp_to_ind
gc.collect()
else:
samp_to_ind = None
# ------------------------------------------------------------------------------------------------------------------
# SPLITTING UP TREES
# ------------------------------------------------------------------------------------------------------------------
print("Splitting up trees for " + name + "...")
filename_trees = trees_loc + "/" + name + ".trees"
if os.path.exists(filename_trees + ".tsz"):
ts = tszip.decompress(filename_trees + ".tsz")
elif os.path.exists(filename_trees):
ts = tskit.load(filename_trees)
else:
sys.exit("No tree sequence file found in " + output_dir)
num_samples = ts.num_samples
num_trees = ts.num_trees
bps = ts.breakpoints(as_array=True)
num_chunks = int(len(bps) / 1000) + 1
for i, j in enumerate(range(num_chunks)):
filename_chunk = output_dir + "/" + name + "_chunk" + str(j) + ".trees"
if not os.path.exists(filename_chunk) and not os.path.exists(
filename_chunk + ".tsz"
):
print("...chunk", j)
left = bps[i * 1000]
if (i + 1) * 1000 >= len(bps):
right = bps[-1]
else:
right = bps[(i + 1) * 1000]
ts_sub = ts.keep_intervals([[left, right]])
tszip.compress(ts_sub, filename_chunk + ".tsz")
del ts_sub
gc.collect()
else:
print("...chunk", j, "already done")
del ts
gc.collect()
print("Done")
print("-" * 100)
if not os.path.exists(output_dir + "/" + name + "_treeinfo.txt"):
print("Getting tree info...")
with open(output_dir + "/" + name + "_treeinfo.txt", "w") as file:
file.write("chr;chunk;chunk_tree_index;tree_start;tree_end\n")
for i in range(num_chunks):
print("Loading chunk " + str(i) + "...")
filename = output_dir + "/" + name + "_chunk" + str(i) + ".trees"
if os.path.exists(filename + ".tsz"):
ts = tszip.decompress(filename + ".tsz")
else:
ts = tskit.load(filename)
for t in ts.trees():
if t.num_roots == 1:
file.write(
name
+ ";"
+ str(i)
+ ";"
+ str(t.index)
+ ";"
+ str(int(t.interval[0]))
+ ";"
+ str(int(t.interval[1]))
+ "\n"
)
print("-" * 100)
filename = output_dir + "/" + name + "_unmerged"
if not os.path.exists(filename + ".clades.gz"):
if use_genotype_id:
samp_to_ind = pickle.load(
open(output_dir + "/" + name + "_samp_to_ind.pickle", "rb")
)
ts_handles = [
output_dir + "/" + name + "_chunk" + str(i) + ".trees"
for i in range(num_chunks)
]
_, duplicate_clades = dolores.cladespans.clade_span(
ts_handles,
num_trees,
num_samples,
samp_to_ind=samp_to_ind,
write_to_file=filename,
write_to_file_freq=50000,
)
print("-" * 100)
print("Clades that are duplicates based on genotype ID:")
print(
"chunk;chunk_tree_index;tree_index;node_id;cladesize;sample_ids;individual_ids"
)
for d in duplicate_clades:
print(*d, sep=";")
print("-" * 100)
del ts_handles
gc.collect()
else:
print("Calculating clade spans already done")
print("-" * 100)
# ------------------------------------------------------------------------------------------------------------------
# MERGING CLADES
# ------------------------------------------------------------------------------------------------------------------
filename = output_dir + "/" + name
if not os.path.exists(filename + ".clades.gz"):
filename_ = output_dir + "/" + name + "_unmerged"
print("Reading in unmerged clades...")
results = dolores.cladespans.read_from_file(filename_)
results.merge_clades(recombination_map, cM_limit=cM_limit)
print("Done - storing merged results...")
results.write_to_file(filename)
else:
results = dolores.cladespans.read_from_file(filename)
results_chunk = []
chunk = 0
with tqdm(
total=results.num, bar_format="{l_bar}{bar:10}{r_bar}{bar:-10b}"
) as pbar:
for i in range(results.num):
if results.on[i] == 1:
if results.chunkindex[i] != chunk:
with open(
output_dir
+ "/"
+ name
+ "_chunk"
+ str(chunk)
+ ".results.pickle",
"wb",
) as file:
pickle.dump(results_chunk, file)
results_chunk = []
chunk = results.chunkindex[i]
results_chunk.append(
[
results.id[i],
results.nodeid[i],
results.treeindex[i],
results.tbl[i],
results.mut_span[i],
results.left_mut[i],
results.right_mut[i],
results.span[i],
results.start[i],
results.end[i],
results.num_mutations[i],
]
)
pbar.update(1)
with open(
output_dir + "/" + name + "_chunk" + str(chunk) + ".results.pickle",
"wb",
) as file:
pickle.dump(results_chunk, file)
num_chunks = chunk + 1
# Free up RAM and will reload later
del results
del results_chunk
gc.collect()
print("-" * 100)
# ------------------------------------------------------------------------------------------------------------------
# COMPUTING p-VALUES
# ------------------------------------------------------------------------------------------------------------------
print(num_chunks)
for chunk in range(num_chunks):
filename = (
output_dir + "/" + name + "_chunk" + str(chunk) + ".cladesinfo.pickle"
)
if not os.path.exists(filename):
print("Loading chunk " + str(chunk) + "...")
results_chunk = pickle.load(
open(
output_dir
+ "/"
+ name
+ "_chunk"
+ str(chunk)
+ ".results.pickle",
"rb",
)
)
filename_ = output_dir + "/" + name + "_chunk" + str(chunk) + ".trees"
if os.path.exists(filename_ + ".tsz"):
ts_chunk = tszip.decompress(filename_ + ".tsz")
else:
ts_chunk = tskit.load(filename_)
trees_chunk = dolores.edgespans.compute_trees(
ts_chunk, epoch_starts, Ne_list
)
print("Computing clade p-values for chunk " + str(chunk) + "...")
cladesInfo = dolores.cladespans.calculate_q(
results_chunk,
recombination_map,
ts_chunk,
trees_chunk,
bps,
use_muts=use_muts,
muts_per_kb=muts_per_kb,
destroy_trees=True,
)
with open(filename, "wb") as file:
pickle.dump(cladesInfo, file)
del results_chunk
del ts_chunk
del trees_chunk
del cladesInfo
gc.collect()
else:
print("Chunk", chunk, "already done.")
print("Loading clade span results...")
results = dolores.cladespans.read_from_file(output_dir + "/" + name)
results.reset()
for chunk in range(num_chunks):
print("Loading results chunk " + str(chunk) + "...")
results_chunk = pickle.load(
open(
output_dir
+ "/"
+ name
+ "_chunk"
+ str(chunk)
+ ".cladesinfo.pickle",
"rb",
)
)
print("Adding chunk to results...")
dolores.cladespans.add_info(results, results_chunk)
print("Saving...")
results.write_to_file(output_dir + "/" + name)
# Tidy up
if os.path.exists(filename):
chunk_filenames = output_dir + "/" + name + "_chunk*.results.pickle"
os.system("rm " + chunk_filenames)
chunk_filenames = output_dir + "/" + name + "_chunk*.cladesinfo.pickle"
os.system("rm " + chunk_filenames)
print("=" * 100)
# ----------------------------------------------------------------------------------------------------------------------
# OUTPUTTING RESULTS
# ----------------------------------------------------------------------------------------------------------------------
print("Outputting results...")
with open(output_dir + "/" + name + "_output.csv", "w") as file:
file.write(
"name,genetic_map,total_clades,clade_num,clade_id,"
+ "nlog10p_test1,nlog10p_test2,cladesize,span,start,end,mut_span,"
+ "left_mut,right_mut,num_mutations,merged,chunk_index,tree_index,node_id\n"
)
for i, clade_id in enumerate(results.ids):
file.write(
name
+ ","
+ "HapMapII_GRCh37"
+ ","
+ str(results.num)
+ ","
+ str(clade_id)
+ ","
+ str(i)
+ ","
+ str(-results.log10sf[i])
+ ","
+ str(-results.log10sf_event[i])
+ ","
+ str(results.cladesize[clade_id])
+ ","
+ str(int(results.span[clade_id]))
+ ","
+ str(int(results.start[clade_id]))
+ ","
+ str(int(results.end[clade_id]))
+ ","
+ str(int(results.mut_span[clade_id]))
+ ","
+ str(int(results.left_mut[clade_id]))
+ ","
+ str(int(results.right_mut[clade_id]))
+ ","
+ str(results.num_mutations[clade_id])
+ ","
+ str(results.merged[clade_id])
+ ","
+ str(results.chunkindex[clade_id])
+ ","
+ str(results.treeindex[clade_id])
+ ","
+ str(results.nodeid[clade_id])
+ "\n",
)
# ----------------------------------------------------------------------------------------------------------------------
# PLOTS
# ----------------------------------------------------------------------------------------------------------------------
if plot_on:
print("Plotting results...")
dolores.viz.qqplot(
[results],
edges=False,
legend_labels=["Sim, no inv (Relate)"],
save_to_file=output_dir + "/" + name + "_qq.png",
)
dolores.viz.outliers_plot(
results,
save_to_file=output_dir + "/" + name + "_outliers.png",
)
dolores.viz.pvalues_plot(
results,
save_to_file=output_dir + "/" + name + "_pvalues.png",
)
print("=" * 100)
if __name__ == "__main__":
main(sys.argv[1:])