-
Notifications
You must be signed in to change notification settings - Fork 0
/
drbagel.py
341 lines (278 loc) · 9.51 KB
/
drbagel.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
#drbagel.py
#Clara Liu
#2017/06/12
import Bio
from Bio.Blast import NCBIXML
from Bio import SeqIO
from Bio import pairwise2
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from parameters import *
import os
counter = 0
#return all the name of files in the given dir
def file_names(dir):
f = []
for (dirpath, dirnames, filenames) in os.walk(dir):
f.extend(filenames)
break
for files in f:
if 'DS' in files:
f.remove(files)
return f
def folder_names(dir):
f = []
for (dirpath, dirnames, filenames) in os.walk(dir):
f.extend(dirnames)
break
return f
def makemydir(dir):
try:
os.makedirs(dir)
except OSError:
pass
def rename(dir,old_ext,new_ext):
f = []
for (dirpath, dirnames, filenames) in os.walk(dir):
f.extend(filenames)
break
for file_name in f:
if file_name[-len(old_ext):]==old_ext:
file = dir+"/"+file_name
base = os.path.splitext(file)[0]
os.rename(file, base.lower() + new_ext)
def create_db_win(input_dir,file_list):
makemydir("db_%s/" %extension)
makemydir("temp/")
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
make_db = "makeblastdb -in %s%s.%s -dbtype nucl \
-out db_%s/%s/%s -title '%s'\n" %(input_dir,genome,extension,extension,genome,genome,genome)
#with open("temp/test.sh", "w") as text_file:
#text_file.write(make_db)
os.system("sh %s" %make_db)
#os.system("sh temp/test.sh")
#os.system("rm -r temp/test.sh")
def create_db_mac(input_dir,file_list):
makemydir("db_%s/" %extension)
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
make_db = "makeblastdb -in %s%s.%s -dbtype nucl \
-out db_%s/%s/%s -title '%s'\n" %(input_dir,genome,extension,extension,genome,genome,genome)
os.system(make_db)
def create_blast_win(input_dir,file_list,ref_gene_name):
counter = 0
if len(file_list)/100>0:
num_d_100 = len(file_list)/100
else:
num_d_100 = 1
#print ref_gene_name
outdir = "blast_%s/%s/" %(extension,ref_gene_name)
makemydir(outdir)
makemydir("temp/")
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
if counter%num_d_100 ==0:
print "blast: %d/100" %(counter/num_d_100)
counter += 1
make_blast = "blastn -query ref_genes/%s.fasta -task megablast -db db_%s/%s/%s \
-outfmt 5 -out %s%s.xml -num_threads 4" %(ref_gene_name,extension,genome,genome,outdir,genome)
with open("temp/test.sh", "w") as text_file:
text_file.write(make_blast)
os.system("sh temp/test.sh")
os.system("rm -r temp")
def create_blast_mac(input_dir,file_list,ref_gene_name):
counter = 0
if len(file_list)/100>0:
num_d_100 = len(file_list)/100
else:
num_d_100 = 1
print ref_gene_name
outdir = "blast_%s/%s/" %(extension,ref_gene_name)
makemydir(outdir)
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
if counter%num_d_100 ==0:
print "blast: %d/100" %(counter/num_d_100)
counter += 1
make_blast = "blastn -query ref_genes/%s.fasta -task megablast -db db_%s/%s/%s \
-outfmt 5 -out %s%s.xml -num_threads 4" %(ref_gene_name,extension,genome,genome,outdir,genome)
os.system(make_blast)
def process_result_handle(genome,ref_gene_name):
ref_gene = SeqIO.read('ref_genes/%s.fasta' %ref_gene_name,"fasta",IUPAC.unambiguous_dna)
genome_records = SeqIO.index('%s/%s' %(extension,genome+'.'+extension),"fasta",IUPAC.unambiguous_dna)
result_handle = open("blast_%s/%s/%s.xml" %(extension,ref_gene_name,genome))
blast_record = NCBIXML.read(result_handle)
#find the best hit (filter the off-target hits)
try:
alignment = sorted(blast_record.alignments,cmp=lambda x,y: x.length-y.length)[-1]
id_l = alignment.title.find(id_spliter)
id_r = alignment.title.find(id_spliter,id_l+1)
id = alignment.title[id_l+1:id_r]
sbjct_gene = genome_records[id].seq
title = alignment.title
hsp = sorted(alignment.hsps,cmp=lambda x,y: x.identities-y.identities)[-1]
query = hsp.query.upper()
len_upstream = hsp.query_start - 1
len_downstream = len(ref_gene)-hsp.query_end
#print (hsp.sbjct_start,hsp.sbjct_end)
if hsp.sbjct_start<hsp.sbjct_end:
sbjct_left_s = hsp.sbjct_start-len_upstream
sbjct_left_e = hsp.sbjct_start
sbjct_left = (sbjct_gene)[sbjct_left_s-1:sbjct_left_e-1].upper()
sbjct_right_s = hsp.sbjct_end+1
sbjct_right_e = hsp.sbjct_end+1+len_downstream
sbjct_right = (sbjct_gene)[sbjct_right_s-1:sbjct_right_e-1].upper()
#sbjct_from = hsp.sbjct_start-len_upstream
#sbjct_to = sbjct_from+len(query)-1
#sbjct = (sbjct_gene)[sbjct_from-1:sbjct_to].upper()
else:
sbjct_left_s = hsp.sbjct_end+1+len_upstream
sbjct_left_e = hsp.sbjct_end+1
sbjct_left = (sbjct_gene)[sbjct_left_e-1:sbjct_left_s-1].reverse_complement().upper()
sbjct_right_s = hsp.sbjct_start
sbjct_right_e = hsp.sbjct_start-len_downstream
sbjct_right = (sbjct_gene)[sbjct_right_e-1:sbjct_right_s-1].reverse_complement().upper()
sbjct = sbjct_left+hsp.sbjct.upper()+sbjct_right
query = (ref_gene[:hsp.query_start-1] + query + ref_gene[hsp.query_end:]).upper()
return [str(sbjct),str(query),len_upstream-len(sbjct_left),len_downstream-len(sbjct_right),title]
except IndexError:
return ['','',0,0,'']
class Blasted:
def dash_at_beginning(str):
ls = 0
le = 0
rs = len(str)-1
re = len(str)-1
if (len(str)==0):
return 0
while (str[le]=='-' or str[rs]=='-'):
if (str[le]=='-'):
le += 1
else:
rs -= 1
return le-ls+re-rs
def __init__(self,genome,ref_gene_name,option):
self.ref_gene_name = ref_gene_name
self.genome = genome
self.left_miss = 0
self.right_miss = 0
self.sbjct = ''
if option=='xml':
[s,q,l,r,t] = process_result_handle(genome,ref_gene_name)
#complete sbjct sequence (no dashes added even if incomplete)
self.sbjct = s
#complete query sequence with possible dash in the middle
#self.query = q
self.title = t
self.left_miss = l
self.right_miss = r
elif option=='fasta':
txt = 'output_seq/%s/%s' %(ref_gene_name,genome)
file = open(txt)
str_file = file.read()
newline = str_file.find('\r\n')
self.title = str_file[str_file.find('>')+1:newline]
str_seq = ''
newline += 2
while str_file.find('\r\n',newline)>=0:
r = str_file.find('\r\n',newline)
str_seq += str_file[newline:r]
newline = r+2
self.sbjct = str_seq.upper()
l = 0
while(self.sbjct[l]=='-'):
l += 1
r = len(self.sbjct)-1
while(self.sbjct[r]=='-'):
r -= 1
self.left_miss = l
self.right_miss = len(self.sbjct)-1-r
def validify(self):
if self.sbjct == '':
return False
def get_ref_gene(self):
return SeqIO.read('ref_genes/%s.fasta' %self.ref_gene_name,"fasta",IUPAC.unambiguous_dna)
def get_ref_protein(self):
return self.get_ref_gene().translate(to_stop=True)
def get_title(self):
return self.title
def get_seq(self):
return ''.join(self.sbjct.split('-'))
def get_print_seq(self):
return '-'*self.left_miss + self.sbjct + '-'*self.right_miss
def get_protein(self):
try:
return str(Seq(self.get_seq(), IUPAC.unambiguous_dna).translate(to_stop=True))
except Bio.Data.CodonTable.TranslationError:
return ''
except:
return ''
# make sure dir 'output_seq/AA' and 'output_seq/DNA' exist
def write_single_seq(self):
dir = 'output_seq/%s/' %(self.ref_gene_name)
txt = '%s.fasta' %(self.genome)
blast_error = []
translate_error = []
protein = self.get_protein()
sbjct = self.get_print_seq()
if sbjct!='' :
with open(dir+"DNA/"+txt, "w") as text_file:
text_file.write('>%s\n' %self.genome)
n = 0
while n<len(sbjct):
text_file.write(sbjct[n:n+100]+'\n')
n += 100
else:
blast_error += [self.genome]
protein = self.get_protein()
if protein!='' :
with open(dir+"AA/"+txt, "w") as text_file:
text_file.write('>%s\n' %self.genome)
n = 0
while n<len(protein):
text_file.write(protein[n:n+100]+'\n')
n += 100
else:
translate_error += [self.genome]
return [blast_error,translate_error]
def attach_exact_alignment(self,txt):
pass
def write_seq(file_list,ref_gene_name):
counter = 0
if len(file_list)/100>0:
num_d_100 = len(file_list)/100
else:
num_d_100 = 1
makemydir('output_seq/%s/' %ref_gene_name)
makemydir('output_seq/%s/DNA/' %ref_gene_name)
makemydir('output_seq/%s/AA/' %ref_gene_name)
#print '%s:' %ref_gene_name
for file_name in file_list:
if "DS" in file_name:
continue
if counter%num_d_100 == 0:
print "write: %d/100" %(counter/num_d_100)
counter += 1
genome = file_name[:file_name.find('.'+extension)]
blasted = Blasted(genome,ref_gene_name,'xml')
[bE,tE] = blasted.write_single_seq()
if len(bE)!=0 or len(tE)!=0:
with open('output_seq/%s/error_seqs.txt' %ref_gene_name,'a') as txt:
txt.write('Seqs not writen for %s: \n' %ref_gene_name)
txt.write('BLAST error (no alignment):\n')
for item in bE:
txt.write(item+'\n')
txt.write('Translation error: \n')
for item in tE:
txt.write(item+'\n')
def setup(species):
makemydir(species)