-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphylip2biNumNex.py
executable file
·331 lines (296 loc) · 7.94 KB
/
phylip2biNumNex.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
#!/usr/bin/python
import re
import sys
import os
import getopt
import operator
import random
def main():
params = parseArgs()
if params.phylip:
#Get sequences as dict of lists
seqs = readPhylip(params.phylip)
#get list of columns and list of samplenames
alen = getSeqLen(seqs)
columns = [[]for i in range(alen)]
names = list()
for key, value in seqs.items():
names.append(key)
for i, nuc in enumerate(value):
columns[i].append(nuc)
#For each column, delete those which are not bi-allelic
dels=list()
for i, col in enumerate(columns):
if not isBiallelic(col):
dels.append(i)
#print(i,"not biallelic:",col)
print("Deleting",len(dels),"non-biallelic columns.")
for col in sorted(dels,reverse=True): #reverse sorted so subsequent deletes aren't thrown off
#print(col,":",columns[col])
del columns[col]
#Then, convert to 012 format
print("Converting to 012 format...")
formatted = [[]for i in range(alen-len(dels))]
for i, col in enumerate(columns):
#print(col)
#print(nucs2numeric(col))
if params.nohet:
formatted[i] = nucs2numericNohet(col)
else:
formatted[i] = nucs2numeric(col)
#sys.exit()
final_data = dict()
for i, samp in enumerate(names):
seqs = list()
for k,nuc in enumerate(formatted):
seqs.append(nuc[i])
final_data[samp] = "".join(seqs)
print("Writing NEXUS output file...")
dict2nexus(params.out, final_data)
else:
print("No input provided.")
sys.exit(1)
#Function takes biallelic list of nucleotides and converts to numeric
#0 = major allele
#1 = minor allele
#2 = het
#? = - or N
def nucs2numeric(nucs):
if isBiallelic(nucs):
#print(nucs)
ret = list()
counts = {"A":0, "G":0, "C":0, "T":0}
#find major allele
for nuc in nucs:
if nuc not in ("-", "N"):
for exp in get_iupac_caseless(nuc):
counts[exp] += 1
#sort dict, to list of tuples (b/c dicts are orderless, can't keep as dict)
sorted_x = sorted(counts.items(), key=operator.itemgetter(1), reverse=True)
majA = sorted_x[0][0]
minA = sorted_x[1][0]
het = reverse_iupac(''.join(sorted(set([majA, minA])))) #get het code
#print(majA, minA, het)
for nuc in nucs:
nuc = nuc.upper()
if nuc == majA:
ret.append("0")
elif nuc == minA:
ret.append("1")
elif nuc == het:
ret.append("2")
elif nuc == "-":
ret.append("-")
else:
ret.append("?")
return(ret)
else:
print("Warning: Data is not biallelic:",nucs)
return(None)
#Function takes biallelic list of nucleotides and converts to numeric
#0 = major allele
#1 = minor allele
#2: Randomly samples heterozygous sites as 0 or 1
def nucs2numericNohet(nucs):
if isBiallelic(nucs):
#print(nucs)
ret = list()
counts = {"A":0, "G":0, "C":0, "T":0}
#find major allele
for nuc in nucs:
if nuc not in ("-", "N"):
for exp in get_iupac_caseless(nuc):
counts[exp] += 1
#sort dict, to list of tuples (b/c dicts are orderless, can't keep as dict)
sorted_x = sorted(counts.items(), key=operator.itemgetter(1), reverse=True)
majA = sorted_x[0][0]
minA = sorted_x[1][0]
het = reverse_iupac(''.join(sorted(set([majA, minA])))) #get het code
#print(majA, minA, het)
for nuc in nucs:
nuc = nuc.upper()
if nuc == majA:
ret.append("0")
elif nuc == minA:
ret.append("1")
elif nuc == het:
ret.append(random.randint(0,1))
elif nuc == "-":
ret.append("-")
else:
ret.append("?")
return(ret)
else:
print("Warning: Data is not biallelic:",nucs)
return(None)
#Function to translate a string of bases to an iupac ambiguity code
def reverse_iupac(char):
char = char.upper()
iupac = {
'A':'A',
'N':'N',
'-':'-',
'C':'C',
'G':'G',
'T':'T',
'AG':'R',
'CT':'Y',
'AC':'M',
'GT':'K',
'AT':'W',
'CG':'S',
'CGT':'B',
'AGT':'D',
'ACT':'H',
'ACG':'V',
'ACGT':'N'
}
return iupac[char]
#Function takes a list of nucleotides, and returns True if the column is biallelic
#ignores gaps and Ns
#expands uipac codes using a call to external function
def isBiallelic(nucs):
expanded = list()
for nuc in nucs:
if nuc not in ("-", "N"):
for exp in get_iupac_caseless(nuc):
expanded.append(exp)
uniq_sort = sorted(set(expanded))
if len(uniq_sort) != 2:
#print(nucs)
#print(uniq_sort, len(uniq_sort))
return(False)
else:
return(True)
#Function to split character to IUPAC codes, assuing diploidy
def get_iupac_caseless(char):
if char.islower():
char = char.upper()
iupac = {
"A" : ["A"],
"G" : ["G"],
"C" : ["C"],
"T" : ["T"],
"N" : ["A", "C", "G", "T"],
"-" : ["-"],
"R" : ["A","G"],
"Y" : ["C","T"],
"S" : ["G","C"],
"W" : ["A","T"],
"K" : ["G","T"],
"M" : ["A","C"],
"B" : ["C","G","T"],
"D" : ["A","G","T"],
"H" : ["A","C","T"],
"V" : ["A","C","G"]
}
ret = iupac[char]
return ret
#Function to read a phylip file. Returns dict (key=sample) of lists (sequences divided by site)
def readPhylip(phy):
if os.path.exists(phy):
with open(phy, 'r') as fh:
try:
num=0
ret = dict()
for line in fh:
line = line.strip()
if not line:
continue
num += 1
if num == 1:
continue
arr = line.split()
ret[arr[0]] = list(arr[1])
return(ret)
except IOError:
print("Could not read file ",fas)
sys.exit(1)
finally:
fh.close()
else:
raise FileNotFoundError("File %s not found!"%fas)
#Function to write an alignment as DICT to NEXUS
def dict2nexus(nex, aln):
with open(nex, 'w') as fh:
try:
slen = getSeqLen(aln)
header = "#NEXUS\n\nBegin data;\nDimensions ntax=" + str(len(aln)) + " nchar=" + str(slen) + ";\n"
header = header + "Format datatype=dna symbols=\"012\" missing=? gap=-;\nMatrix\n\n"
fh.write(header)
for seq in aln:
sline = str(seq) + " " + aln[seq] + "\n"
fh.write(sline)
last = ";\nEnd;\n"
fh.write(last)
except IOError:
print("Could not read file ",nex)
sys.exit(1)
finally:
fh.close()
#Goes through a dict of sequences and get the alignment length
def getSeqLen(aln):
length = None
for key in aln:
if not length:
length = len(aln[key])
else:
if length != len(aln[key]):
print("getSeqLen: Alignment contains sequences of multiple lengths.")
return(length)
#Object to parse command-line arguments
class parseArgs():
def __init__(self):
#Define options
try:
options, remainder = getopt.getopt(sys.argv[1:], 'p:ho:n', \
["phylip=","phy=","out=","nohet"])
except getopt.GetoptError as err:
print(err)
self.display_help("\nExiting because getopt returned non-zero exit status.")
#Default values for params
#Input params
self.phylip=None
self.out="out.nex"
self.nohet=False
#First pass to see if help menu was called
for o, a in options:
if o in ("-h", "-help", "--help"):
self.display_help("Exiting because help menu was called.")
#Second pass to set all args.
for opt, arg_raw in options:
arg = arg_raw.replace(" ","")
arg = arg.strip()
opt = opt.replace("-","")
#print(opt,arg)
if opt in ('p', 'phylip', 'phy'):
self.phylip = arg
elif opt in ('h', 'help'):
pass
elif opt in ('o','out'):
self.out = arg
elif opt in ('n','nohet'):
self.nohet=True
else:
assert False, "Unhandled option %r"%opt
#Check manditory options are set
if not self.phylip:
self.display_help("Error: Missing required phylip file (-p, --phylip)")
def display_help(self, message=None):
if message is not None:
print ("\n",message)
print ("\nphylip2biNumNex.py\n")
print ("Contact:Tyler K. Chafin, University of Arkansas,[email protected]")
print ("\nUsage: ", sys.argv[0], "-p /path/to/phylip \n")
print ("Description: Converts PHYLIP file to NEXUS file of only bi-allelic markers, coded with 012. As inputs for PhyloNetworks MLE_biMarkers or SNAPP")
print("""
Arguments:
-p,--popmap : Path to tab-delimited population map
-o,--out : Output file name <default = out.nex>
-n,--nohet : Randomly sample one allele from all heterozygous sites
-h,--help : Displays help menu
""")
sys.exit()
#Call main function
if __name__ == '__main__':
main()