This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join_dna_files_with_indels.py
executable file
·389 lines (284 loc) · 11.1 KB
/
join_dna_files_with_indels.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
#!/usr/bin/env python
import os, sys, string
from random import *
from optparse import OptionParser, OptionGroup
import pysam
import time
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC, Gapped, generic_dna, SingleLetterAlphabet
from modules.Si_SeqIO import *
#################################
# Simple Error Printing Funtion #
#################################
def DoError(ErrorString):
print "!!!Error:", ErrorString,"!!!"
sys.exit()
##########################################
# Function to Get command line arguments #
##########################################
def main():
usage = "usage: %prog [options] <list of mfa files>"
parser = OptionParser(usage=usage)
parser.add_option("-r", "--reference", action="store", dest="ref", help="Reference fasta (or multifasta). Must be the one used for mapping", default="")
parser.add_option("-o", "--output", action="store", dest="output", help="output file prefix", default="")
# parser.add_option("-m", "--muscle", action="store_true", dest="muscle", help="Try to align regions with muscle [default=%default]", default=False)
parser.add_option("-c", "--curate", action="store_true", dest="curate", help="Manually curate added insertions using seaview [default=%default]", default=False)
parser.add_option("-t", "--textfile", action="store", dest="textfile", help="textfile containing input mfa files", default="")
return parser.parse_args()
################
# Main program #
################
if __name__ == "__main__":
#Get command line arguments
(options, args) = main()
if options.ref=="":
DoError("Reference file (-r) required")
if options.output=="":
DoError("Output file (-o) required")
if options.textfile=="":
DoError("No textfile of input mfa files selected")
if not os.path.isfile(options.textfile):
DoError("Cannot find file "+options.textfile)
#Read the reference file
if not os.path.isfile(options.ref):
DoError("Cannot find file "+options.ref)
#read the reference fasta file
try:
fasta=SeqIO.parse(open(options.ref), "fasta")
except StandardError:
DoError("Cannot open file "+options.ref)
if options.textfile!="" and not os.path.isfile(options.textfile):
DoError("Cannot find file "+options.textfile)
for line in open(options.textfile, "rU"):
line=line.strip()
args.append(line)
reforder=[]
refseq={}
for sequence in fasta:
if not refseq.has_key(sequence.id):
refseq[sequence.id]={}
reforder.append(sequence.id)
refseq[sequence.id]=sequence
refseq[sequence.id].id='.'.join(options.ref.split("/")[-1].split(".")[:-1])
#Get all insertion locations
Insertions={}
Insertion_locations={}
Deletions={}
Deletion_locations={}
for arg in args:
if arg.split(".")[-1]=="mfa":
indelfile=arg.replace(".mfa", "_indels.txt")
elif arg.split(".")[-1]=="dna":
indelfile=arg.replace(".dna", "_indels.txt")
else:
DoError("Input files must end in .dna or .mfa")
if not os.path.isfile(indelfile):
print "Cannot find ", indelfile
else:
try:
lines=open(indelfile, "rU").readlines()
except StandardError:
print "Cannot open ", indelfile
for line in lines:
contig=line.split()[0]
location=int(line.split()[1])-1
#note that insertions must be +1, as they are AFTER the base
indeltype=line.strip().split()[2]
change=line.strip().split()[3]
if indeltype=="+":
if not Insertion_locations.has_key(contig):
Insertion_locations[contig]=[]
Insertions[contig]={}
if not Insertions[contig].has_key(location):
Insertion_locations[contig].append(location)
Insertions[contig][location]={}
Insertions[contig][location][arg]=change
elif indeltype=="-":
if not Deletion_locations.has_key(contig):
Deletion_locations[contig]=[]
Deletions[contig]={}
if not Deletions[contig].has_key(location):
Deletion_locations[contig].append(location)
Deletions[contig][location]={}
Deletions[contig][location][arg]=change
#Read the mfa files for each isolate
sequences={}
for arg in args:
try:
fasta=SeqIO.parse(open(arg), "fasta")
except StandardError:
DoError("Cannot open file "+arg)
for sequence in fasta:
seqid=""
if sequence.id in reforder:
seqid=sequence.id
else:
x=0
while x<len(sequence.id.split("_")):
if "_".join(sequence.id.split("_")[x:]) in reforder:
seqid="_".join(sequence.id.split("_")[x:])
break
#print reforder, sequence.id, seqid
x+=1
if seqid=="":
DoError(seqid+" and "+sequence.id+" not in reference genome")
if not sequences.has_key(seqid):
sequences[seqid]={}
sequences[seqid][arg]=sequence
sequences[seqid][arg].id='.'.join(arg.split("/")[-1].split(".")[:-1])
chars = string.ascii_letters + string.digits
tmpname='tmp'+"".join(choice(chars) for x in range(randint(8, 10)))
#Deal with the deletions
align_size=20
for contig in Deletion_locations:
Deletion_locations[contig].sort()
Deletion_locations[contig].reverse()
for contig in sequences:
if not contig in Deletion_locations:
continue
for location in Deletion_locations[contig]:
#print location
maxdellen=0
#lens=[]
for sequence in sequences[contig]:
if Deletions[contig][location].has_key(sequence):
if len(Deletions[contig][location][sequence])>maxdellen:
maxdellen=len(Deletions[contig][location][sequence])
#lens.append(Deletions[contig][location][sequence])
alignlen=maxdellen+align_size
if location>=align_size and location<=len(refseq[contig])-(align_size+1):
start=location-align_size
end=location+alignlen
elif location<align_size:
start=0
end=location+alignlen
else:
end=len(refseq[contig])
start=location-align_size
# print end-start, maxdellen, align_size, alignlen, lens
tempseqs=[refseq[contig][start:end]]
for sequence in sequences[contig]:
tempseqs.append(sequences[contig][sequence][start:end])
#print sequences[contig][sequence][start:end]
tempseqs[-1].id=sequence
if Deletions[contig][location].has_key(sequence):
deletionlen=len(Deletions[contig][location][sequence])
#sequences[contig][sequence].seq=sequences[contig][sequence].seq[:location]+"-"*deletionlen+sequences[contig][sequence].seq[location+deletionlen:]
if start==0:
distfromstart=align_size-location
else:
distfromstart=align_size
startbit=tempseqs[-1].seq[:distfromstart+1].upper()
if distfromstart+deletionlen<len(tempseqs[-1].seq):
endbit=tempseqs[-1].seq[distfromstart+1+deletionlen:].upper()
else:
endbit=""
# print tempseqs[-1].seq
# print location>=align_size , location<=len(refseq[contig])-(align_size+1)
# print location, len(refseq[contig]), Deletions[contig][location][sequence]
# print distfromstart, start, end, startbit, endbit, distfromstart+deletionlen, len(tempseqs[-1].seq)
# print tempseqs[-1].seq
##
# print startbit+"-"*deletionlen+endbit
# sys.exit()
tempseqs[-1].seq=startbit+"-"*deletionlen+endbit
SeqIO.write(tempseqs, open(tmpname+".aln","w"), "fasta")
if options.curate:
os.system("seaview "+tmpname+".aln")
muscleout=open(tmpname+".aln", "rU").read().split(">")[1:]
for line in muscleout:
words=line.split("\n")
name=words[0].split()[0]
if not sequences[contig].has_key(name):
refseq[contig].seq=refseq[contig].seq[:start]+''.join(words[1:])+refseq[contig].seq[end:]
continue
else:
# seqline=''.join(words[1:])
#
# x=0
# for y, base in enumerate(seqline):
# if base=="-":
# continue
# elif base!=tempseqscopy[name][x]:
# seqline=seqline[:y]+tempseqscopy[name][x]+seqline[y+1:]
# x+=1
sequences[contig][name].seq=sequences[contig][name].seq[:start]+''.join(words[1:])+sequences[contig][name].seq[end:]
refseqnewlen=len(refseq[contig].seq)
for name in sequences[contig]:
if len(sequences[contig][name].seq)!=refseqnewlen:
print "D", start, end, location, refseqnewlen, len(sequences[contig][name].seq)
sys.exit()
os.system("seaview "+tmpname+".aln")
#sort the inserts
for contig in Insertion_locations:
Insertion_locations[contig].sort()
Insertion_locations[contig].reverse()
#Deal with the inserts (most complicated bit)
for contig in sequences:
if not contig in Insertion_locations:
continue
for location in Insertion_locations[contig]:
if location>=align_size and location<=len(refseq[contig])-(align_size+1):
start=location-align_size
end=location+align_size
elif location<align_size:
start=0
end=align_size*2
else:
end=len(refseq[contig])-1
start=len(refseq[contig])-(align_size*2)+1
tempseqs=[refseq[contig][start:end]]
# tempseqs[-1].id=tmpname
for sequence in sequences[contig].keys():
tempseqs.append(sequences[contig][sequence][start:end])
tempseqs[-1].id=sequence
if Insertions[contig][location].has_key(sequence):
tempseqs[-1].seq=tempseqs[-1].seq[:align_size+1]+Insertions[contig][location][sequence]+tempseqs[-1].seq[align_size+1:]
# if options.muscle:
SeqIO.write(tempseqs, open(tmpname+".fasta","w"), "fasta")
os.system("muscle -in "+tmpname+".fasta -out "+tmpname+".aln > /dev/null 2>&1 ")
# else:
# SeqIO.write(tempseqs, open(tmpname+".aln","w"), "fasta")
# if options.curate:
# os.system("mv "+tmpname+".aln "+tmpname+"b.aln")
# muscleout=open(tmpname+"b.aln", "rU").read().split(">")[1:]
# output=open(tmpname+".aln", "w")
# for line in muscleout:
# words=line.split("\n")
# print >> output, ">"+words[0]
# print >> output, words[1].replace("N-","NN").replace("-N","NN")
#
# output.close()
# os.system("seaview "+tmpname+".aln")
if options.curate:
os.system("seaview "+tmpname+".aln")
muscleout=open(tmpname+".aln", "rU").read().split(">")[1:]
for line in muscleout:
words=line.split("\n")
name=words[0].split()[0]
if not sequences[contig].has_key(name):
refseq[contig].seq=refseq[contig].seq[:start]+''.join(words[1:])+refseq[contig].seq[end:]
else:
seqline=''.join(words[1:])
while ("N-" in seqline) or ("-N" in seqline):
seqline=seqline.replace("N-","NN").replace("-N","NN")
sequences[contig][name].seq=sequences[contig][name].seq[:start]+seqline+sequences[contig][name].seq[end:]
refseqnewlen=len(refseq[contig].seq)
for name in sequences[contig]:
if len(sequences[contig][name].seq)!=refseqnewlen:
print "I", start, end, location
print tempseqs
sys.exit()
os.system("seaview "+tmpname+".aln")
final_sequences=[]
final_sequences.append(refseq[reforder[0]])
for contig in reforder[1:]:
final_sequences[-1].seq=final_sequences[-1].seq+refseq[contig].seq
for sequence in sequences[contig].keys():
final_sequences.append(sequences[reforder[0]][sequence])
for contig in reforder[1:]:
final_sequences[-1].seq=final_sequences[-1].seq+sequences[contig][sequence].seq
SeqIO.write(final_sequences, open(options.output,"w"), "fasta")
os.system("rm -f "+tmpname+"*")