-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathms2something.py
executable file
·609 lines (485 loc) · 21.3 KB
/
ms2something.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/usr/bin/env python
import os, sys
import numpy as np
__infinite_site_model__ = True
## Finite site Transition probabilities from nucleotide u to nucleotide v
__FiniteSite_T_mat__ = [ [0.503, 0.082, 0.315, 0.100 ], \
[0.186, 0.002, 0.158, 0.655 ], \
[0.654, 0.158, 0, 0.189 ], \
[0.097, 0.303, 0.085, 0.515 ] ]
## Infinite site Transition probabilities from nucleotide u to nucleotide v
__InFiniteSite_T_mat__ = [ [0, 1/float(3) , 1/float(3), 1/float(3) ], \
[1/float(3), 0, 1/float(3), 1/float(3) ], \
[1/float(3), 1/float(3), 0, 1/float(3) ], \
[1/float(3), 1/float(3), 1/float(3), 0 ] ]
__eq_prob__ = [ .25, .25, .25, .25 ]
def transition_prob_of(u):
"""
Args:
u: string of nucleotide
Returns:
Conditional on the value of __infinite_site_model__
return the transition probability of nucleotide u
"""
if ( u == "A" ): return __InFiniteSite_T_mat__ [0] if __infinite_site_model__ else __FiniteSite_T_mat__[0]
elif ( u == "C" ): return __InFiniteSite_T_mat__ [1] if __infinite_site_model__ else __FiniteSite_T_mat__[1]
elif ( u == "G" ): return __InFiniteSite_T_mat__ [2] if __infinite_site_model__ else __FiniteSite_T_mat__[2]
elif ( u == "T" ): return __InFiniteSite_T_mat__ [3] if __infinite_site_model__ else __FiniteSite_T_mat__[3]
else:
print "Nucleotide is invalid"
exit( 1 )
def set_to_infinite_site ( model ):
"""
Does what it says
"""
__infinite_site_model__ = model
def get_position( seqlen, position_file_name, scaled = True ):
"""
Args:
seqlen: sequence length
position_file_name: position of mutation on sequence between 0 and 1
Returns:
position: rescaled mutation position on sequence between 0 and seqlen
"""
print position_file_name
position_file = open( position_file_name, 'r' )
position = [ float(x) for x in position_file.read().split() ]
position_file.close()
if scaled:
position = [ round( x * float(seqlen) ) for x in position ]
return position
def get_seg(seg_file_name):
"""
Args:
seg_file_name: segregating site data
Returns:
seg: list of segregating site data
num_taxa: number of taxa
"""
seg = []
num_taxa = 0
seg_file = open( seg_file_name, 'r' )
for line in seg_file:
seg.append( [ int(x) for x in line.strip() ] )
num_taxa += 1
seg_file.close()
return seg, num_taxa
def random_pick( probabilities ):
"""
Args:
probabilities: Probability array of nucleotide
Returns:
Randomly return the nucleotide according to the probability
"""
nucleotide = ["A", "C", "G", "T"]
x = np.random.uniform()
cumulative_probability = 0.0
for item, item_probability in zip( nucleotide, probabilities ):
cumulative_probability += item_probability
if x < cumulative_probability: break
return item
## @ingroup group_compare_msmc
def generate_msmc_in ( msmc_input_file_prefix, position, seqlen, seg, num_taxa, python_seed = 0 ):
"""
Generate msmc input data
Args:
msmc_input_file_prefix: msmc input data file prefix
position: rescaled mutation position on sequence between 0 and seqlen
seqlen: sequence length
seg: list of segregating site data
num_taxa: number of taxa
Returns:
pass
"""
if python_seed > 0: np.random.seed( python_seed )
msmc = open( msmc_input_file_prefix + ".msmc_in", 'w' )
num_seg = len( position )
previous_position = 0
for i in range( num_seg ):
num_homozygous = int(round( position[i] - previous_position - 2 ))
if num_homozygous > 0 :
line = `1` + "\t" + `int(position[i])` + "\t" + `num_homozygous` + "\t"
msmc.write(line)
ref = random_pick( __eq_prob__ )
alt = random_pick( transition_prob_of(ref) )
for allele in range( num_taxa ):
variant = ref if seg[allele][i] == 0 else alt
msmc.write(variant)
msmc.write("\n")
previous_position = position[i]
msmc.close()
## @ingroup group_compare_msmc
def To_msmc(arg1, arg2, arg3, arg4, python_seed = 0):
seqlen = int(arg1)
position_file_name = arg2
seg_file_name = arg3
msmc_input_file_prefix = arg4
position = get_position ( seqlen, position_file_name )
seg, num_taxa = get_seg ( seg_file_name )
generate_msmc_in (msmc_input_file_prefix, position, seqlen, seg, num_taxa, python_seed)
## @ingroup group_compare_msmc
def Help_msmc():
print " %s msmc <seqlen> <position_file_name> <segsites_file_name> <msmc_input_file_prefix>" % sys.argv[0]
## @ingroup group_compare_pfarg
def generate_vcf( vcf_prefix, position, seqlen, seg, num_taxa, file_type, python_seed = 0 ):
"""
Generate sequence data file in vcf/gvcf format
Args:
vcf_prefix: vcf data file prefix
position: rescaled mutation position on sequence between 0 and seqlen
seqlen: sequence length
seg: list of segregating site data
num_taxa: number of taxa
Returns:
pass
"""
if python_seed > 0: np.random.seed( python_seed ) ## \todo to be implemented
if file_type == "vcf":
vcf = open( vcf_prefix + ".vcf", 'w' )
vcf.write( "##fileformat=VCFv4.1\n" )
vcf.write( "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT" )
elif file_type == "gvcf":
vcf = open( vcf_prefix + ".gvcf", 'w' )
vcf.write( "##fileformat=GVCF\n" )
vcf.write( "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT" )
elif file_type == "rgvcf":
vcf = open( vcf_prefix + ".rgvcf", 'w' )
vcf.write( "##fileformat=RGVCF\n" )
vcf.write( "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT" )
else:
print "oops, check generate_vcf()"
sys.exit(1)
for j in range(num_taxa/2):
name = "\tNA" + str(j+1)
vcf.write(name)
vcf.write("\n")
previous_position = int(0);
for i, position_i in enumerate( position ):
next_position = position[i+1] if ( i < len(position)-1 ) else seqlen
if next_position == int(position_i):
continue
if (file_type == "gvcf") & (previous_position < int(position_i)-1):
line = str(1) + "\t" + `previous_position+1` + "\t" +".\t.\t.\t0\tREFCALL;\tEND="+`int(position_i)-1`+";\tGT" # ignoring mutations type, only from A to T
#line = str(1) + "\t" + `previous_position+1` + "\t" +".\tN\tT\t0\tREFCALL;\tEND="+`int(position_i)-1`+";\tGT" # ignoring mutations type, only from A to T
#line = str(1) + "\t" + `previous_position+1` + "\t" +".\tT\tN\t0\tREFCALL;\tEND="+`int(position_i)-1`+";\tGT" # ignoring mutations type, only from A to T
vcf.write(line)
for j in range(0, num_taxa, 2):
data = "\t./."
vcf.write(data)
vcf.write( "\n" )
line = str(1) + "\t" + `int(position_i)` + "\t" +"rs0\tA\tT\t67\tPASS\tNS=2;\tGT" # ignoring mutations type, only from A to T
vcf.write(line)
for j in range(0, num_taxa, 2):
data = "\t" + str(seg[j][i])
vcf.write(data)
data = "|" + str(seg[j+1][i])
vcf.write(data)
vcf.write( "\n" )
previous_position = int(position_i)
if (file_type == "gvcf") & (previous_position <= int(seqlen) ):
line = str(1) + "\t" + `previous_position+1` + "\t" +".\t.\t.\t0\tREFCALL;\tEND="+`int(seqlen)`+";\tGT" # ignoring mutations type, only from A to T
#line = str(1) + "\t" + `previous_position+1` + "\t" +".\tN\tT\t0\tREFCALL;\tEND="+`int(position_i)-1`+";\tGT" # ignoring mutations type, only from A to T
#line = str(1) + "\t" + `previous_position+1` + "\t" +".\tT\tN\t0\tREFCALL;\tEND="+`int(position_i)-1`+";\tGT" # ignoring mutations type, only from A to T
vcf.write(line)
for j in range(0, num_taxa, 2):
data = "\t./."
vcf.write(data)
vcf.write( "\n" )
vcf.close()
## @ingroup group_compare_pfarg
def To_vcf( seqlen_in, position_file_name_in, seg_file_name_in, vcf_prefix_in, file_type_in, python_seed = 0 ):
seqlen = int(seqlen_in)
position = get_position ( seqlen, position_file_name_in )
seg, num_taxa = get_seg ( seg_file_name_in )
generate_vcf ( vcf_prefix_in, position, seqlen, seg, num_taxa, file_type_in, python_seed )
def To_seg( seqlen_in, position_file_name_in, seg_file_name_in, segment_prefix_in, missing_data = False ):
seqlen = int(seqlen_in)
position = get_position ( seqlen, position_file_name_in )
seg, num_taxa = get_seg ( seg_file_name_in )
generate_seg ( segment_prefix_in, position, seqlen, seg, num_taxa, missing_data )
def generate_seg_variant_at_beginning ( segment_prefix_in, position, seqlen, seg, num_taxa, missing_data = False):
"""
Generate segment data
Each line consist with
Site(Segment start) Segment_length Segment_state genetic_break allelic_state
Args:
segment_prefix_in: segment data file prefix
position: rescaled mutation position on sequence between 0 and seqlen
seg: list of segregating site data
num_taxa: number of taxa
Returns:
pass
"""
segfile_name = segment_prefix_in + ("missing" if missing_data else "") + ".seg"
sefement_file = open( segfile_name, 'w' )
num_seg = len( position )
position.append( float(seqlen) )
num_homozygous = int(round( position[0] - 1 ))
seg_state = "F" if missing_data else "T"
line = `int(1)` + "\t" + `num_homozygous` + "\t" + seg_state + "\t" + "F\t1\t"
sefement_file.write(line)
for allele in range( num_taxa ):
sefement_file.write( "." ) # let 2 denote missing, easier to read in
sefement_file.write("\n")
for i in range( num_seg ):
num_homozygous = int(round( position[i+1] - position[i] ))
if num_homozygous > 0 :
line = `int(position[i])` + "\t" + `num_homozygous` + "\t" + seg_state + "\t" + "F\t1\t"
sefement_file.write(line)
for allele in range( num_taxa ):
seg_contant = "." if missing_data else `seg[allele][i]`
sefement_file.write( seg_contant )
#if missing_data:
#sefement_file.write( "." )
#else:
#sefement_file.write( `seg[allele][i]` )
sefement_file.write("\n")
sefement_file.close()
def generate_seg ( segment_prefix_in, position, seqlen, seg, num_taxa, missing_data = False):
"""
Generate segment data
Each line consist with
Site(Segment start) Segment_length Segment_state genetic_break allelic_state
Args:
segment_prefix_in: segment data file prefix
position: rescaled mutation position on sequence between 0 and seqlen
seg: list of segregating site data
num_taxa: number of taxa
Returns:
pass
"""
segfile_name = segment_prefix_in + ("missing" if missing_data else "") + ".seg"
sefement_file = open( segfile_name, 'w' )
num_seg = len( position )
position.append( float(seqlen) )
num_homozygous = int(round( position[0] - 1 ))
seg_state = "F" if missing_data else "T"
line = `int(1)` + "\t" + `num_homozygous` + "\t" + seg_state + "\t" + "F\t1\t"
sefement_file.write(line)
for i in range( num_seg ):
if num_homozygous > 0 :
for allele in range( num_taxa ):
seg_contant = "." if missing_data else `seg[allele][i]`
sefement_file.write( seg_contant )
sefement_file.write("\n")
num_homozygous = int(round( position[i+1] - position[i] ))
if num_homozygous > 0 :
line = `int(position[i])` + "\t" + `num_homozygous` + "\t" + seg_state + "\t" + "F\t1\t"
sefement_file.write(line)
# Use missing data at the end of the seg file
for allele in range( num_taxa ):
sefement_file.write( "." ) # let 2 denote missing, easier to read in
sefement_file.write("\n")
sefement_file.close()
## @ingroup group_compare_pfarg
def Help_vcf():
print " %s vcf <seqlen> <position_file_name> <segsites_file_name> <vcf_file_prefix>" % sys.argv[0]
## @ingroup group_compare_dical
def generate_diCal_data ( diCal_input_prefix, position, seqlen, seg, num_taxa, python_seed = 0 ):
"""
Generate sequence data file in fasta format
Args:
diCal_input_prefix: data file prefix
position: rescaled mutation position on sequence between 0 and seqlen
seqlen: sequence length
seg: list of segregating site data
num_taxa: number of taxa
Returns:
pass
"""
if python_seed > 0: np.random.seed( python_seed )
diCal_input_file_name = diCal_input_prefix + ".fasta"
if os.path.isfile( diCal_input_file_name ):
os.remove( diCal_input_file_name )
diCal = open( diCal_input_file_name, 'w' )
#seqlist = [[],[],[],[]]
seqlist = [[] for i in range(num_taxa)]
index = 0
for mut, snp_site in enumerate( position ):
#print mut, snp_site
while index <= snp_site:
ref = random_pick( __eq_prob__ )
for allele in range( num_taxa ):
#print allele
seqlist[allele].append(ref)
index += 1
# Problem, alt could be the same as the ref, even there is a mutation there ...
alt = random_pick( transition_prob_of(ref) )
for allele in range( num_taxa ):
variant = ref if seg[allele][mut] == 0 else alt
seqlist[allele].append(variant)
#print index, [seg[allele][mut] for allele in range(num_taxa)]
#print [seqlist[allele][index] for allele in range(num_taxa)]
index += 1
while index < seqlen: # add more sequence data, as previsou process ended at the last mutation, but not the end of the sequence
ref = random_pick( __eq_prob__)
for allele in range( num_taxa ):
seqlist[allele].append(ref)
index += 1
#seq = ["","","",""]
seq = ["" for i in range(num_taxa)]
for allele in range(num_taxa):
seq[allele] = "".join(seqlist[allele])
diCal.write('>'+`allele`+'\n')
diCal.write(seq[allele] + '\n')
diCal.close()
## @ingroup group_compare_dical
def generate_diCal_param (diCal_input_prefix, mu, rho):
"""
Generate diCal parameter file
Args:
diCal_input_prefix: parameter file prefix
mu: mutation rate (per basepair per generation) scaled by 4Ne.
mu = 4Ne*u, mu is mutation rate per basepair per individual
rho: recombination rate (per basepair per generation) scaled by 4Ne.
rho = 4Ne*r, r is the recombination per basepair per individual
Returns:
"""
diCal_input_file_name = diCal_input_prefix + ".param"
if os.path.isfile( diCal_input_file_name ):
os.remove( diCal_input_file_name )
diCal = open( diCal_input_file_name, 'w' )
diCal.write( "# mutation rate: mu (per site per generation)" + '\n' )
diCal.write( mu + '\n\n' )
diCal.write( "# stochastic mutation matrix (A,C,G,T)" + '\n' )
if __infinite_site_model__:
diCal.write( "0, 0.3333, 0.3333, 0.3334 | 0.3333, 0, 0.3333, 0.3334 | 0.3333, 0.3333, 0, 0.3334 | 0.3333, 0.3333, 0.3334, 0" + '\n\n' )
else:
diCal.write( "0.503, 0.082, 0.315, 0.100 | 0.186, 0.002, 0.158, 0.655 | 0.654, 0.158, 0, 0.189 | 0.097, 0.303, 0.085, 0.515" + '\n\n' )
diCal.write( "# recombination rate: rho (per adjacent loci breakpoint, per generation)" + '\n' )
diCal.write( rho + '\n\n' )
diCal.close()
## @ingroup group_compare_dical
def To_diCal(arg1, arg2, arg3, arg4, arg5, arg6, python_seed = 0):
seqlen = int(arg1)
position_file_name = arg2
seg_file_name = arg3
mu = arg4
rho = arg5
diCal_input_prefix = arg6
position = get_position ( seqlen, position_file_name )
seg, num_taxa = get_seg ( seg_file_name )
generate_diCal_data ( diCal_input_prefix, position, seqlen, seg, num_taxa, python_seed )
generate_diCal_param ( diCal_input_prefix, mu, rho )
print "diCal data generated"
## @ingroup group_compare_dical
def Help_diCal():
print " %s diCal <seqlen> <position_file_name> <segsites_file_name> <mu> <rho> <diCal_input_prefix> " % sys.argv[0]
print " mu: Mutation rate (per site per generation) "
print " rho: Recombination rate (per adjacent loci breakpoint, per generation)"
## @ingroup group_compare_psmc
def generate_psmc_concatenate (psmc_input_file_prefix, position, seqlen, seg, num_taxa, python_seed):
"""
Concaternate multiple sequences for Generating single nucleotide mutation data for psmc
"""
if python_seed > 0: np.random.seed( python_seed )
seqchar = ""
for seq_i in range (0 , num_taxa, 2):
#print "sequence", seq_i
seqchar_tmp = "T" * seqlen
seqlist = list( seqchar_tmp )
for mut, snp_site in enumerate( position ):
if seg[seq_i][mut] != seg[seq_i+1][mut]:
ref = random_pick( __eq_prob__ )
if __infinite_site_model__:
seqlist[int(snp_site)] = "K"
elif ref != random_pick ( transition_prob_of(ref) ):
seqlist[int(snp_site)] = "K"
seqchar_tmp = "".join(seqlist)
seqchar += seqchar_tmp
#print len(seqchar)
psmc = open( psmc_input_file_prefix + ".psmcfa" , 'w' )
psmc.write( '>loci' + `1` + '\n' )
psmc.write( seqchar + '\n' )
psmc.close()
## @ingroup group_compare_psmc
def generate_psmc_in ( seqlen, position, psmc_input_file_prefix, python_seed = 0 ):
"""
Generate single nucleotide mutation data for psmc
"""
if python_seed > 0: np.random.seed( python_seed )
seqchar = "T" * seqlen
seqlist = list( seqchar )
for index in position:
ref = random_pick( __eq_prob__ )
if __infinite_site_model__:
seqlist[int(index)] = "K"
elif ref != random_pick ( transition_prob_of(ref) ):
seqlist[int(index)] = "K"
seqchar = "".join(seqlist)
# Write the sequence string into file
psmc = open( psmc_input_file_prefix + ".psmcfa" , 'w' )
psmc.write( '>loci' + `1` + '\n' )
psmc.write( seqchar + '\n' )
psmc.close()
## @ingroup group_compare_psmc
def To_psmc( arg1, arg2, arg3, python_seed = 0 ):
seqlen = int(arg1)
position_file_name = arg2
psmc_input_file_prefix = arg3
position = get_position ( seqlen, position_file_name )
generate_psmc_in ( seqlen, position, psmc_input_file_prefix, python_seed )
## @ingroup group_compare_psmc
def To_psmc_concatenate(arg1, arg2, arg3, arg4, python_seed = 0):
seqlen = int(arg1)
position_file_name = arg2
seg_file_name = arg3
psmc_input_file_prefix = arg4
position = get_position ( seqlen, position_file_name )
seg, num_taxa = get_seg ( seg_file_name )
generate_psmc_concatenate (psmc_input_file_prefix, position, seqlen, seg, num_taxa, python_seed)
## @ingroup group_compare_psmc
def Help_psmc():
print " %s psmc <seqlen> <position_file_name> <psmc_input_file_prefix>" % sys.argv[0]
if __name__ == "__main__":
#try:
if sys.argv[1] == "diCal":
To_diCal( sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7] )
elif sys.argv[1] == "psmc":
To_psmc ( sys.argv[2], sys.argv[3], sys.argv[4] )
elif sys.argv[1] == "msmc":
To_msmc ( sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5] )
elif sys.argv[1] == "seg":
To_seg ( seqlen_in = sys.argv[2],
position_file_name_in = sys.argv[3],
seg_file_name_in = sys.argv[4],
segment_prefix_in = sys.argv[5] )
elif sys.argv[1] == "vcf":
To_vcf ( seqlen_in = sys.argv[2],
position_file_name_in = sys.argv[3],
seg_file_name_in = sys.argv[4],
vcf_prefix_in = sys.argv[5],
file_type_in = sys.argv[1] )
elif sys.argv[1] == "gvcf":
To_vcf ( seqlen_in = sys.argv[2],
position_file_name_in = sys.argv[3],
seg_file_name_in = sys.argv[4],
vcf_prefix_in = sys.argv[5],
file_type_in = sys.argv[1] )
elif sys.argv[1] == "rgvcf":
To_vcf ( seqlen_in = sys.argv[2],
position_file_name_in = sys.argv[3],
seg_file_name_in = sys.argv[4],
vcf_prefix_in = sys.argv[5],
file_type_in = sys.argv[1] )
else:
print "method is not recognised"
#except:
#print "Usage: "
#print " %s <Method> <seqlen> <position_file_name> [ options ] <output_prefix> " % sys.argv[0]
#print " Method: diCal, psmc, msmc, or vcf"
#print " seqlen: Sequence length"
#print " "
#print "Method: "
#print " diCal"
#Help_diCal()
#print " "
#print " psmc"
#Help_psmc()
#print " "
#print " msmc"
#Help_msmc()
#print " "
#print " vcf"
#Help_vcf()
#sys.exit(1)