-
Notifications
You must be signed in to change notification settings - Fork 171
/
convert.py
executable file
·667 lines (634 loc) · 27.4 KB
/
convert.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#!/usr/local/bin/python3
# Michael Matschiner, 2016-06-19
# Import libraries and make sure we're on python 3.
import sys
if sys.version_info[0] < 3:
print('Python 3 is needed to run this script!')
sys.exit(0)
import argparse, textwrap, random, os, re
from subprocess import call
# Parse the command line arguments.
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
%(prog)s
-----------------------------------------
Converts sequence data into multiple formats.
Accepted input formats: phylip, fasta, nexus, ped
Accepted output formats: phylip, fasta, nexus, genepop,
stampp, treemix, treemix_bi (as treemix, but only
biallelic SNPs).
'''))
parser.add_argument(
'-v', '--version',
action='version',
version='%(prog)s 0.95'
)
parser.add_argument(
'-p', '--populations',
nargs='*',
type=str,
help="One or more population identifiers. If specified, only sequences\
matching a population identifier will be included.")
parser.add_argument(
'-x', '--exclude',
nargs=1,
type=str,
default=[-1],
dest='exclude',
help="Name of a single individual to be excluded from the data set."
)
parser.add_argument(
'-f', '--format',
nargs=1,
type=str,
default=[-1],
dest='output_format',
help="Ouput format."
)
parser.add_argument(
'--phased',
action='store_true',
dest='phased',
help="Assume that each pair of sequences are from the same individual \
(with phylip, fasta, nexus input formats), or that each consecutive \
pair of bases is sorted according to haplotype (ped input format). \
Only has an effect with genepop, stampp, or treemix output formats."
)
parser.add_argument(
'-m', '--mind',
nargs=1,
type=float,
default=[-1],
dest='missingness_per_individual',
help="Allowed missingness per individual."
)
parser.add_argument(
'-g', '--geno',
nargs=1,
type=float,
default=[-1],
dest='missingness_per_marker',
help="Allowed missingness per marker."
)
parser.add_argument(
'infile',
nargs='?',
type=argparse.FileType('r'),
default='-',
help='The input file name.'
)
parser.add_argument(
'outfile',
nargs='?',
type=argparse.FileType('w'),
default=sys.stdout,
help='The output file name.'
)
# Get the command line arguments.
args = parser.parse_args()
pops = args.populations
if pops == None:
pops = []
exclude = args.exclude[0]
output_format = args.output_format[0]
if output_format == -1:
print("ERROR: No output format specified!")
sys.exit(1)
phased = args.phased
missingness_per_individual = args.missingness_per_individual[0]
missingness_per_marker = args.missingness_per_marker[0]
infile = args.infile
outfile = args.outfile
# Read the input.
if infile.isatty():
print('No input file specified, and no input piped through stdin!')
sys.exit(0)
instring = infile.read()
inlines = instring.split('\n')
# Determine the input format.
input_format = None
if inlines[0].strip().lower() == "#nexus":
input_format = "nexus"
elif inlines[0][0] == ">":
input_format = "fasta"
elif len(inlines[0].split()) == 2 and int(inlines[0].split()[0]) > 0 and int(inlines[0].split()[1]) > 0:
input_format = "phylip"
elif "[[Samples]]" in instring:
input_format = "arlequin"
else:
# Check wether it could be ped format.
is_ped = True
first_line_num_cols = len(inlines[0].strip().split())
if first_line_num_cols < 10:
is_ped = False
for inline in inlines:
if inline.strip() != "":
if len(inline.strip().split()) != first_line_num_cols:
is_ped = False
if is_ped:
input_format = "ped"
else:
print("ERROR: Input format could not be recognized!")
sys.exit(1)
# Parse the input lines.
record_ids = []
record_seqs = []
if input_format == "nexus":
in_matrix = False
for line in inlines:
if line.strip().lower() == 'matrix':
in_matrix = True
elif line.strip() == ';':
in_matrix = False
in_tree = False
elif in_matrix and line.strip() is not '':
record_ary = line.split()
record_ids.append(record_ary[0])
record_seqs.append(record_ary[1].upper())
elif input_format == "fasta":
for line in inlines:
if line.strip() != "":
if line[0] == ">":
record_ids.append(line[1:].strip())
record_seqs.append("")
elif line.strip() != "":
record_seqs[-1] = record_seqs[-1].upper() + line.strip()
elif input_format == "phylip":
ntax = int(inlines[0].split()[0])
nchar = int(inlines[0].split()[1])
for line in inlines[1:]:
if line.strip() != "":
record_ary = line.split()
record_ids.append(record_ary[0])
record_seqs.append(record_ary[1].upper())
if len(record_ids) < ntax:
print("ERROR: Found less sequences than specified in the phylip header!")
sys.exit(1)
elif len(record_ids) > ntax:
print("ERROR: Found more sequences than specified in the phylip header!")
sys.exit(1)
if len(record_seqs[0]) != nchar:
print("ERROR: The sequence length does not match the length specified in the phylip header!")
elif input_format == "ped":
for line in inlines:
line = line.strip()
if line != "" and line[0] != "#":
record_ary = line.split()
record_seq_ary = record_ary[6:]
if len(record_seq_ary)%2 != 0:
print("ERROR: With ped input format, the number of alleles per individual should be an even number (is " + str(len(record_seq_ary)) + ")!")
sys.exit(1)
for x in range(round(len(record_seq_ary)/2)):
allele_a_missing = True
allele_b_missing = True
if record_seq_ary[2*x] == "A":
allele_a_missing = False
elif record_seq_ary[2*x] == "C":
allele_a_missing = False
elif record_seq_ary[2*x] == "G":
allele_a_missing = False
elif record_seq_ary[2*x] == "T":
allele_a_missing = False
if record_seq_ary[2*x+1] == "A":
allele_b_missing = False
elif record_seq_ary[2*x+1] == "C":
allele_b_missing = False
elif record_seq_ary[2*x+1] == "G":
allele_b_missing = False
elif record_seq_ary[2*x+1] == "T":
allele_b_missing = False
if allele_a_missing == True or allele_b_missing == True:
record_seq_ary[2*x] = "N"
record_seq_ary[2*x+1] = "N"
if phased == True:
record_ids.append(record_ary[1] + "_a")
record_ids.append(record_ary[1] + "_b")
record_seq_a = ""
record_seq_b = ""
for x in range(round(len(record_seq_ary)/2)):
record_seq_a += record_seq_ary[2*x]
record_seq_b += record_seq_ary[2*x+1]
record_seqs.append(record_seq_a)
record_seqs.append(record_seq_b)
else:
record_ids.append(record_ary[1])
record_seq = ""
for x in range(round(len(record_seq_ary)/2)):
allele_a = record_seq_ary[2*x]
allele_b = record_seq_ary[2*x+1]
if allele_a == "A" and allele_b == "A":
record_seq += "A"
elif allele_a == "A" and allele_b == "C":
record_seq += "M"
elif allele_a == "A" and allele_b == "G":
record_seq += "R"
elif allele_a == "A" and allele_b == "T":
record_seq += "W"
elif allele_a == "C" and allele_b == "A":
record_seq += "M"
elif allele_a == "C" and allele_b == "C":
record_seq += "C"
elif allele_a == "C" and allele_b == "G":
record_seq += "S"
elif allele_a == "C" and allele_b == "T":
record_seq += "Y"
elif allele_a == "G" and allele_b == "A":
record_seq += "R"
elif allele_a == "G" and allele_b == "C":
record_seq += "S"
elif allele_a == "G" and allele_b == "G":
record_seq += "G"
elif allele_a == "G" and allele_b == "T":
record_seq += "K"
elif allele_a == "T" and allele_b == "A":
record_seq += "W"
elif allele_a == "T" and allele_b == "C":
record_seq += "Y"
elif allele_a == "T" and allele_b == "G":
record_seq += "K"
elif allele_a == "T" and allele_b == "T":
record_seq += "T"
else:
record_seq += "N"
record_seqs.append(record_seq)
elif input_format == 'arlequin':
in_alignment = False
for inline in inlines:
if len(inline) > 0:
if inline.strip() == "SampleData= {":
in_alignment = True
elif inline.strip() == "}":
in_alignment = False
elif in_alignment == True:
record_id = inline.split()[0]
record_seq = inline.split()[2].replace("0","A").replace("1","C")
record_ids.append(record_id)
record_seqs.append(record_seq)
else:
print("ERROR: Unsupported input format: " + str(input_format) + "!")
sys.exit(1)
# Make sure the same number of records and sequences are found.
if len(record_ids) != len(record_seqs):
print("ERROR: The number of record ids and sequences differ!")
sys.exit(1)
# Remove records specified for exclusion.
if exclude != -1:
exclude_taxon_found = False
reduced_record_ids = []
reduced_record_seqs = []
for x in range(len(record_ids)):
if exclude in record_ids[x]:
exclude_taxon_found = True
print("INFO: Excluding " + record_ids[x])
else:
reduced_record_ids.append(record_ids[x])
reduced_record_seqs.append(record_seqs[x])
record_ids = reduced_record_ids
record_seqs = reduced_record_seqs
if exclude_taxon_found == False:
print("WARNING: The taxon specified to be exluded (" + exclude + ") could not be found.")
# Make sure that all sequences have the same length.
lengths_differ = False
for x in range(1,len(record_seqs)):
if len(record_seqs[0]) != len(record_seqs[x]):
lengths_differ = True
if lengths_differ == True:
print("WARNING: Not all sequences have the same length!")
longest_length = 0
for record_seq in record_seqs:
if len(record_seq) > longest_length:
longest_length = len(record_seq)
for x in range(len(record_seqs)):
record_length = len(record_seqs[x])
if record_length != longest_length:
for z in range(longest_length-record_length):
record_seqs[x] += "-"
# Remove records that do not fit population identifiers, if population identifiers
# have been specified.
if pops != []:
filtered_record_ids = []
filtered_record_seqs = []
for x in range(len(record_ids)):
record_should_be_included = False
for pop in pops:
if pop in record_ids[x]:
record_should_be_included = True
if record_should_be_included == True:
filtered_record_ids.append(record_ids[x])
filtered_record_seqs.append(record_seqs[x])
record_ids = filtered_record_ids
record_seqs = filtered_record_seqs
# Remove individuals based on missing data, if the "mind" option is used.
if missingness_per_individual != -1:
filtered_record_ids = []
filtered_record_seqs = []
for x in range(len(record_ids)):
missing = (record_seqs[x].count("N") + record_seqs[x].count("?") + record_seqs[x].count("-"))/len(record_seqs[x])
if missing <= missingness_per_individual:
filtered_record_ids.append(record_ids[x])
filtered_record_seqs.append(record_seqs[x])
record_ids = filtered_record_ids
record_seqs = filtered_record_seqs
if len(record_ids) == 0:
print("ERROR: No individuals left after filtering!")
sys.exit(1)
# Remove sites based on missing data, if the "geno" option is used.
if missingness_per_marker != -1:
filtered_record_seqs = []
for x in range(len(record_ids)):
filtered_record_seqs.append("")
for pos in range(len(record_seqs[0])):
missing = 0
for x in range(len(record_ids)):
if record_seqs[x][pos] == "N":
missing += 1
elif record_seqs[x][pos] == "?":
missing += 1
elif record_seqs[x][pos] == "-":
missing += 1
if missing/len(record_ids) <= missingness_per_marker:
for x in range(len(record_ids)):
filtered_record_seqs[x] += record_seqs[x][pos]
record_seqs = filtered_record_seqs
if len(record_seqs[0]) == 0:
print("ERROR: No markers left after filtering!")
sys.exit(0)
# Prepare the output string.
output_string = ""
if output_format == "phylip":
output_string += str(len(record_ids)) + " " + str(len(record_seqs[0])) + "\n"
max_id_length = 0
for record_id in record_ids:
if len(record_id) > max_id_length:
max_id_length = len(record_id)
for x in range(len(record_ids)):
output_string += record_ids[x].ljust(max_id_length+2) + record_seqs[x] + "\n"
elif output_format == "fasta":
for x in range(len(record_ids)):
output_string += ">" + record_ids[x] + "\n"
output_string += record_seqs[x] + "\n"
elif output_format == "nexus":
output_string += "#nexus\n"
output_string += "begin data;\n"
output_string += " dimensions ntax=" + str(len(record_ids)) + " nchar=" + str(len(record_seqs[0])) + ";\n"
output_string += " format datatype=dna missing=? gap=-;\n"
output_string += " matrix\n"
max_id_length = 0
for record_id in record_ids:
if len(record_id) > max_id_length:
max_id_length = len(record_id)
for x in range(len(record_ids)):
output_string += " " + record_ids[x].ljust(max_id_length+2) + record_seqs[x] + "\n"
output_string += " ;\n"
output_string += "end;\n"
elif output_format == "genepop":
variable_positions = []
for pos in range(len(record_seqs[0])):
bases_at_this_site = []
for record_seq in record_seqs:
if record_seq[pos] in ["A","C","G","T"]:
bases_at_this_site.append(record_seq[pos])
bases_at_this_site = list(set(bases_at_this_site))
if len(bases_at_this_site) > 1:
variable_positions.append(pos)
genepop_record_ids = []
genepop_record_seqs = []
if phased == False:
for x in range(len(record_ids)):
genepop_record_id = record_ids[x]
genepop_record_seq = ""
for variable_position in variable_positions:
if record_seqs[x][variable_position] == "A":
genepop_record_seq += "\t01"
elif record_seqs[x][variable_position] == "C":
genepop_record_seq += "\t02"
elif record_seqs[x][variable_position] == "G":
genepop_record_seq += "\t03"
elif record_seqs[x][variable_position] == "T":
genepop_record_seq += "\t04"
else:
genepop_record_seq += "\t00"
genepop_record_ids.append(genepop_record_id)
genepop_record_seqs.append(genepop_record_seq)
else:
for x in range(round(len(record_ids)/2)):
genepop_record_id = record_ids[2*x]
genepop_record_seq = ""
for variable_position in variable_positions:
if record_seqs[2*x][variable_position] == "A":
genepop_record_seq += "\t01"
elif record_seqs[2*x][variable_position] == "C":
genepop_record_seq += "\t02"
elif record_seqs[2*x][variable_position] == "G":
genepop_record_seq += "\t03"
elif record_seqs[2*x][variable_position] == "T":
genepop_record_seq += "\t04"
else:
genepop_record_seq += "00"
if record_seqs[(2*x)+1][variable_position] == "A":
genepop_record_seq += "01"
elif record_seqs[(2*x)+1][variable_position] == "C":
genepop_record_seq += "02"
elif record_seqs[(2*x)+1][variable_position] == "G":
genepop_record_seq += "03"
elif record_seqs[(2*x)+1][variable_position] == "T":
genepop_record_seq += "04"
else:
genepop_record_seq += "00"
genepop_record_ids.append(genepop_record_id)
genepop_record_seqs.append(genepop_record_seq)
output_string += "converted from " + input_format + " format by converter.py" + "\n"
for variable_position in variable_positions:
output_string += "SNP" + str(variable_position).rjust(len(str(variable_positions[-1]))).replace(" ","0") + "\n"
max_id_length = 0
for genepop_record_id in genepop_record_ids:
if len(genepop_record_id) > max_id_length:
max_id_length = len(genepop_record_id)
if pops == []:
output_string += "pop\n"
for x in range(len(genepop_record_ids)):
output_string += genepop_record_ids[x].ljust(max_id_length) + "\t," + genepop_record_seqs[x] + "\n"
else:
for pop in pops:
output_string += "pop\n"
for x in range(len(genepop_record_ids)):
if pop in genepop_record_ids[x]:
output_string += genepop_record_ids[x].ljust(max_id_length) + "\t," + genepop_record_seqs[x] + "\n"
elif output_format == "stampp":
if phased == False:
print("ERROR: When output format stampp is chosen, phased sequences are required. Specify with \'--phased\'.")
sys.exit(1)
variable_positions = []
alleles_at_variable_positions = []
for pos in range(len(record_seqs[0])):
bases_at_this_site = []
for record_seq in record_seqs:
if record_seq[pos] in ["A","C","G","T"]:
bases_at_this_site.append(record_seq[pos])
bases_at_this_site = list(set(bases_at_this_site))
if len(bases_at_this_site) == 2:
variable_positions.append(pos)
alleles_at_variable_positions.append(sorted(bases_at_this_site))
stampp_record_ids = []
stampp_record_seqs = []
for x in range(round(len(record_ids)/2)):
stampp_record_id = record_ids[2*x]
stampp_record_seq = ""
for z in range(len(variable_positions)):
if record_seqs[2*x][variable_positions[z]] == alleles_at_variable_positions[z][0]:
if record_seqs[2*x+1][variable_positions[z]] == alleles_at_variable_positions[z][0]:
stampp_record_seq += ",AA"
elif record_seqs[2*x+1][variable_positions[z]] == alleles_at_variable_positions[z][1]:
stampp_record_seq += ",AB"
else:
stampp_record_seq += ",A-"
elif record_seqs[2*x][variable_positions[z]] == alleles_at_variable_positions[z][1]:
if record_seqs[2*x+1][variable_positions[z]] == alleles_at_variable_positions[z][0]:
stampp_record_seq += ",AB"
elif record_seqs[2*x+1][variable_positions[z]] == alleles_at_variable_positions[z][1]:
stampp_record_seq += ",BB"
else:
stampp_record_seq += ",B-"
else:
if record_seqs[2*x+1][variable_positions[z]] == alleles_at_variable_positions[z][0]:
stampp_record_seq += ",A-"
elif record_seqs[2*x+1][variable_positions[z]] == alleles_at_variable_positions[z][1]:
stampp_record_seq += ",B-"
else:
stampp_record_seq += ",--"
stampp_record_ids.append(stampp_record_id)
stampp_record_seqs.append(stampp_record_seq)
output_string += "Inds,Pop,Ploidy,Format"
for variable_position in variable_positions:
output_string += ",SNP" + str(variable_position).rjust(len(str(variable_positions[-1]))).replace(" ","0")
output_string += "\n"
if pops == []:
for x in range(len(stampp_record_ids)):
output_string += stampp_record_ids[x] + ","
output_string += "all,2,BiA"
output_string += stampp_record_seqs[x] + "\n"
else:
for pop in pops:
for x in range(len(stampp_record_ids)):
if pop in stampp_record_ids[x]:
output_string += stampp_record_ids[x] + ","
output_string += pop + ",2,BiA"
output_string += stampp_record_seqs[x] + "\n"
elif output_format == "treemix" or output_format == "treemix_bi":
if pops == []:
print("ERROR: Population assignments must be specified with output format 'treemix' (option -p)!")
sys.exit(0)
first_pop = True
for pop in pops:
if first_pop == True:
output_string += pop
first_pop = False
else:
output_string += " " + pop
output_string += "\n"
for pos in range(len(record_seqs[0])):
# First find out what the two alleles are at this pos, for which individuals from all populations must be read.
alleles_at_this_pos = []
for x in range(len(record_ids)):
for pop in pops:
if pop in record_ids[x]:
if record_seqs[x][pos] == "A":
alleles_at_this_pos.append("A")
alleles_at_this_pos.append("A")
elif record_seqs[x][pos] == "C":
alleles_at_this_pos.append("C")
alleles_at_this_pos.append("C")
elif record_seqs[x][pos] == "G":
alleles_at_this_pos.append("G")
alleles_at_this_pos.append("G")
elif record_seqs[x][pos] == "T":
alleles_at_this_pos.append("T")
alleles_at_this_pos.append("T")
elif record_seqs[x][pos] == "M":
alleles_at_this_pos.append("A")
alleles_at_this_pos.append("C")
elif record_seqs[x][pos] == "R":
alleles_at_this_pos.append("A")
alleles_at_this_pos.append("G")
elif record_seqs[x][pos] == "W":
alleles_at_this_pos.append("A")
alleles_at_this_pos.append("T")
elif record_seqs[x][pos] == "S":
alleles_at_this_pos.append("C")
alleles_at_this_pos.append("G")
elif record_seqs[x][pos] == "Y":
alleles_at_this_pos.append("C")
alleles_at_this_pos.append("T")
elif record_seqs[x][pos] == "K":
alleles_at_this_pos.append("G")
alleles_at_this_pos.append("T")
unique_alleles_at_this_pos = sorted(list(set(alleles_at_this_pos)))
# Only use biallelic SNPs when treemix_bi is chosen, or both mono-
# and biallelic SNPs when treemix is chosen.
use_this_marker = False
if len(unique_alleles_at_this_pos) == 2:
use_this_marker = True
elif len(unique_alleles_at_this_pos) == 1 and output_format == "treemix":
use_this_marker = True
if use_this_marker == True:
first_pop = True
for pop in pops:
alleles_of_this_pop_at_this_pos = []
if phased == True:
for x in range(len(record_ids)):
if pop in record_ids[x]:
alleles_of_this_pop_at_this_pos.append(record_seqs[x][pos])
else:
for x in range(len(record_ids)):
if pop in record_ids[x]:
if record_seqs[x][pos] == "A":
alleles_of_this_pop_at_this_pos.append("A")
alleles_of_this_pop_at_this_pos.append("A")
elif record_seqs[x][pos] == "C":
alleles_of_this_pop_at_this_pos.append("C")
alleles_of_this_pop_at_this_pos.append("C")
elif record_seqs[x][pos] == "G":
alleles_of_this_pop_at_this_pos.append("G")
alleles_of_this_pop_at_this_pos.append("G")
elif record_seqs[x][pos] == "T":
alleles_of_this_pop_at_this_pos.append("T")
alleles_of_this_pop_at_this_pos.append("T")
elif record_seqs[x][pos] == "M":
alleles_of_this_pop_at_this_pos.append("A")
alleles_of_this_pop_at_this_pos.append("C")
elif record_seqs[x][pos] == "R":
alleles_of_this_pop_at_this_pos.append("A")
alleles_of_this_pop_at_this_pos.append("G")
elif record_seqs[x][pos] == "W":
alleles_of_this_pop_at_this_pos.append("A")
alleles_of_this_pop_at_this_pos.append("T")
elif record_seqs[x][pos] == "S":
alleles_of_this_pop_at_this_pos.append("C")
alleles_of_this_pop_at_this_pos.append("G")
elif record_seqs[x][pos] == "Y":
alleles_of_this_pop_at_this_pos.append("C")
alleles_of_this_pop_at_this_pos.append("T")
elif record_seqs[x][pos] == "K":
alleles_of_this_pop_at_this_pos.append("G")
alleles_of_this_pop_at_this_pos.append("T")
num_occurrences_allele_a = 0
num_occurrences_allele_b = 0
for allele in alleles_of_this_pop_at_this_pos:
if allele == unique_alleles_at_this_pos[0]:
num_occurrences_allele_a += 1
else:
num_occurrences_allele_b += 1
allele_frequency_string = str(num_occurrences_allele_a) + "," + str(num_occurrences_allele_b)
if first_pop == True:
output_string += allele_frequency_string
first_pop = False
else:
output_string += " " + allele_frequency_string
output_string += "\n"
else:
print("ERROR: Unsupported output format: " + str(output_format) + "!")
sys.exit(1)
# Write the output string to file or STDOUT.
outfile.write(output_string)