-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconcatenateNexus.py
executable file
·426 lines (377 loc) · 11.2 KB
/
concatenateNexus.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
#!/usr/bin/python
import sys
import os
import getopt
import io
import glob
from Bio import AlignIO
from Bio import SeqIO
def main():
params = parseArgs()
if params.files:
alignments = list()
lookup = dict()
parts = dict()
for f in glob.iglob(params.files):
#Read file into dict of dicts
this_one = dict()
this_length = None
#for each tuple in NEXUS file
print("Reading file:",f)
#Read nexus as DNAalignment
this_one = DNAalignment(f, "nexus")
if this_one.length < params.min:
print("Alignment",this_one.name,"has length",this_one.length,", skipping.")
continue
# if params.part:
# this_one.loadPartitions(f)
#add names to lookup table
for name in this_one.alignment.keys():
lookup[name] = "NULL"
#Add this alignment to our alignment list
alignments.append(this_one)
#if -t, read and validate popmap
if params.popmap is not None:
popmap=parsePopmap(params.popmap)
for ind in lookup.keys():
if ind not in popmap:
print("Sample", ind, "not found in popmap. Treating as separate pop.")
popmap[ind] = ind
blacklist=list()
for ind in popmap.keys():
if ind not in lookup:
print("Sample", ind, "found in popmap but not in data. Deleting it.")
blacklist.append(ind)
for ind in blacklist:
del popmap[ind]
#make flattened popmap
flatmap=make2Dpopmap(popmap)
#For each alignment, print individual NEXUS if required
if params.ind_files:
for aln in alignments:
#print NEXUS header information
new_nex = aln.name + "_filled.nex"
print("Writing",new_nex)
print(new_nex)
with open(new_nex, 'w') as fh:
try:
slen = aln.length
header = "#NEXUS\n\nBEGIN DATA;\nDIMENSIONS NTAX=" + str(len(lookup.keys())) + " NCHAR=" + str(slen) + ";\n"
header = header + "FORMAT DATATYPE=DNA MISSING=N GAP=-;\n\nMATRIX\n"
fh.write(header)
if params.popmap is not None:
for pop in sorted(list(flatmap.keys())):
for samp in flatmap[pop]:
l = samp + "\t"
for aln in alignments:
#if sample not in alignment, write Ns
if samp not in aln.alignment.keys():
l += str(Nrepeats("N", aln.length))
else:
l += str(aln.alignment[samp])
l += "\n"
fh.write(l)
else:
for samp in sorted(lookup.keys()):
#if sample not in alignment, write Ns
if samp not in aln.alignment.keys():
l = str(samp) + "\t" + str(Nrepeats("N", aln.length)) + "\n"
fh.write(l)
else:
l = str(samp) + "\t" + str(aln.alignment[samp]) + "\n"
fh.write(l)
last = ";\nEND;\n"
fh.write(last)
partline1 = "\nbegin sets;"
partline2 = "\ttaxpartition popmap ="
fh.write("{}\n{}\n".format(partline1, partline2))
#if -t, write taxpartition block
if params.popmap is not None:
popnum = 1
sample_number = 1
range_begin = 1
prev = None
for pop in sorted(list(flatmap.keys())):
start = sample_number
for ind in flatmap[pop]:
sample_number += 1
end=sample_number
if prev is not None:
write_taxpart(prev[0], fh, tuple([prev[1], prev[2]-1]))
prev=[pop, start, end]
fh.write("\t\t{}\t: {}-{};\n".format(str(prev[0]), str(prev[1]), str(prev[2]-1)))
fh.write("end;\n")
except IOError as e:
print("Could not write to file:",new_nex)
print("Caught IOError: ",e)
sys.exit(1)
except Exception as e:
print("Unexpected error writing to file:",new_nex)
print("Caught Exception: ",e)
sys.exit(1)
finally:
fh.close()
#write concatenated file with CHARSETS
concat = "concat.nex"
with open(concat, 'w') as fh:
try:
#write header
print ("Writing concatenated alignment to concat.nex...")
full_len = 0
for a in alignments:
full_len += a.length
header = "#NEXUS\n\nBEGIN DATA;\nDIMENSIONS NTAX=" + str(len(lookup.keys())) + " NCHAR=" + str(full_len) + ";\n"
header = header + "FORMAT DATATYPE=DNA MISSING=N GAP=-;\n\nMATRIX\n"
fh.write(header)
if params.popmap is not None:
for pop in sorted(list(flatmap.keys())):
for samp in flatmap[pop]:
l = samp + "\t"
for aln in alignments:
#if sample not in alignment, write Ns
if samp not in aln.alignment.keys():
l += str(Nrepeats("?", aln.length))
else:
l += str(aln.alignment[samp])
l += "\n"
fh.write(l)
else:
for samp in sorted(lookup.keys()):
l = samp + "\t"
for aln in alignments:
#if sample not in alignment, write Ns
if samp not in aln.alignment.keys():
l += str(Nrepeats("?", aln.length))
else:
l += str(aln.alignment[samp])
l += "\n"
fh.write(l)
#end of data block
l = "\n;END;\n"
fh.write(l)
#write charsets
print("Adding CHARSET blocks...")
out = "\nBEGIN sets;\n"
cur = 0
for a in alignments:
out += "\t" + "CHARSET " + str(a.name) + " = " + str(cur + 1) + "-" + str(cur + a.length) + ";\n"
cur += a.length
out += "END;\n\n"
fh.write(out)
partline1 = "\nbegin sets;"
partline2 = "\ttaxpartition popmap ="
fh.write("{}\n{}\n".format(partline1, partline2))
#if -t, write taxpartition block
if params.popmap is not None:
popnum = 1
sample_number = 1
range_begin = 1
prev = None
for pop in sorted(list(flatmap.keys())):
start = sample_number
for ind in flatmap[pop]:
sample_number += 1
end=sample_number
if prev is not None:
write_taxpart(prev[0], fh, tuple([prev[1], prev[2]-1]))
prev=[pop, start, end]
fh.write("\t\t{}\t: {}-{};\n".format(str(prev[0]), str(prev[1]), str(prev[2]-1)))
fh.write("end;\n")
except IOError as e:
print("Could not write to file:",concat)
print("Caught IOError: ",e)
sys.exit(1)
except Exception as e:
print("Unexpected error writing to file:",concat)
print("Caught Exception: ",e)
sys.exit(1)
finally:
fh.close()
print("\nDone!\n")
else:
print("No inputs provided.\n")
sys.exit(0)
def write_taxpart(pattern, outfile, range_tupl):
outfile.write("\t\t{}\t: {}-{},\n".format(str(pattern), str(range_tupl[0]), str(range_tupl[1])))
#function reads a tab-delimited popmap file and return dictionary of assignments
def parsePopmap(popmap):
ret = dict()
if os.path.exists(popmap):
with open(popmap, 'r') as fh:
try:
contig = ""
seq = ""
for line in fh:
line = line.strip()
if not line:
continue
else:
stuff = line.split()
ret[stuff[0]] = stuff[1]
return(ret)
except IOError:
print("Could not read file ",pairs)
sys.exit(1)
finally:
fh.close()
else:
raise FileNotFoundError("File %s not found!"%popmap)
#Makes a dict of lists from a popmap
def make2Dpopmap(p):
ret = dict()
for s in p:
if p[s] not in ret:
ret[p[s]] = list()
ret[p[s]].append(s)
return(ret)
def Nrepeats(pattern, N):
ret = ""
for i in range(int(N)):
ret = ret + str(pattern)
return(ret)
#Object to hold an alignment
class DNAalignment():
def __init__(self, aln_file, aln_type):
self.length = int()
self.alignment = dict()
self.filepath = aln_file
self.name = self.getName()
if aln_type.lower() in ["nexus", "nex"]:
for sample in self.readNexus():
#Add to this alignment
if sample[0] not in self.alignment.keys():
self.alignment[sample[0]] = sample[1]
if not self.length:
self.length = len(sample[1])
else:
if len(sample[1]) != self.length:
print("Sequences not of equal length:",f)
sys.exit(1)
def getName(self):
return(os.path.splitext(os.path.basename(self.filepath))[0])
#method to read alignment from NEXUS
def readNexus(self):
with open(self.filepath, 'r') as fh:
try:
start = False
for line in fh:
line = line.strip()
if not line:
continue
#line = line.replace(" ","")
if line.lower() == "matrix":
start = True
continue
if start: #we're in the matrix!
if line in [";", "END;", "end;"]:
start = False
break
else:
line = line.replace("\'","")
line = line.replace("\"","")
stuff = line.split()
yield([stuff[0],stuff[1]])
except IOError as e:
print("Could not read file:",e)
sys.exit(1)
except Exception as e:
print("Unexpected error:",e)
sys.exit(1)
finally:
fh.close()
#Function to read partitions from a NEXUS-formatted SETS block
# def loadPartitions(self, f):
# with open(f, 'r') as fh:
# try:
# start = False
# for line in fh:
# line = line.strip()
# if not line:
# continue
# #line = line.replace(" ","")
#
# if "begin" and "sets" in line.lower():
# start = True
# continue
#
# if start: #we're in the charsets block
# if "charset" in line.lower():
#
#
# except IOError as e:
# print("Could not read file:",e)
# sys.exit(1)
# except Exception as e:
# print("Unexpected error:",e)
# sys.exit(1)
# finally:
# fh.close()
#Object to parse command-line arguments
class parseArgs():
def __init__(self):
#Define options
try:
options, remainder = getopt.getopt(sys.argv[1:], 'hf:ipmM:t:', \
["help", "files=","ind", "part", "miss",'min', "tax="])
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.files = None
self.ind_files = False
self.part = False
self.recode = False
self.min = 1
self.popmap=None
#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 ('f','files'):
self.files = arg
elif opt in ('i','ind'):
self.ind_files = True
elif opt in ('p','part'):
self.part = True
elif opt in ('m', 'miss'):
self.recode = True
elif opt in ('M', 'min'):
self.min=int(arg)
elif opt == "t" or opt == "tax":
self.popmap=arg
else:
assert False, "Unhandled option %r"%opt
#Check manditory options are set
if not self.files:
self.display_help("No files provided.")
def display_help(self, message=None):
if message is not None:
print()
print (message)
print ("\nconcatenateNexus.py\n")
print("Author: Tyler K Chafin, University of Arkansas")
print ("Contact: tkchafin@uark.edu")
print ("Description: Concatenates NEXUS gene alignments and fills in gaps for missing taxa")
print("""
Options:
-f,--files : \"/path/to/*.nex\" MUST BE QUOTED, NO \'~\'!!
-i,--ind : Toggle on to print individual NEXUS files [default=off]
-p,--part : Toggle on to retain existing partition information (CHARSETS)
-m,--miss : Toggle on to code missing data as "?"
NOTE: This will also convert terminal gaps as "?"
-M,--min : Minimum alignment length to keep an alignment
-t, --tax : Popmap to write taxpartition
-h,--help : Displays help menu
""")
print()
sys.exit()
#Call main function
if __name__ == '__main__':
main()