-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
756 lines (594 loc) · 22.3 KB
/
test.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#Dr.Bagel Blast and Analysis Script
#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
num_d_10 = 11
#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 create_db(input_dir,file_list):
makemydir("db_%s/" %extension)
#os.system("mkdir -p 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 -parse_seqids \
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:
#windows
#text_file.write("#!/bin/sh\n")
text_file.write(make_db)
os.system(make_db)
os.system("sh temp/test.sh")
os.system("rm -r temp/test.sh")
def create_blast(input_dir,file_list,ref_gene_name):
global counter
counter = 0
print ref_gene_name
outdir = "blast_%s/%s/" %(extension,ref_gene_name)
#os.system("mkdir -p temp")
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_10 ==0:
print "blast: %d/100" %(counter/num_d_10)
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)
#windows:
os.system(make_blast)
#macos:
os.system("sh temp/test.sh")
#os.system("rm -r temp")
def dash_at_beginning2(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
#str has only ' ' and at least one 'x'.
def onlyinvalid(str):
l = -1
lm = l
r = len(str)
rm = r
while (lm!=rm):
lm = str.find('x',l+1,r)
rm = str.rfind('x',l+1,r)
#print "l=%d lm=%d rm=%d r=%d" %(l,lm,rm,r)
if lm==l+1:
l += 1
elif rm==r-1:
r -= 1
else:
return False
return True
class Blasted:
def __init__(self,result_handle,ref_gene_name):
self.ref_gene_name = ref_gene_name
self.valid = True
[s,q,l,r,t] = process_result_handle(result_handle,ref_gene)
#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
if q == '':
self.valid = False
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 process_result_handle(result_handle,ref_gene):
ref_gene = ref_gene.seq
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
#print id
hsp = sorted(alignment.hsps,cmp=lambda x,y: x.identities-y.identities)[-1]
#query = ref_gene.seq.upper()
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 [sbjct,query,len_upstream-len(sbjct_left),len_downstream-len(sbjct_right),title]
except IndexError:
return ['','',0,0,'']
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 get_ref_gene(self).translate(to_stop=True)
def get_title(self):
return title
def get_seq(self):
return ''.join(sbjct.split('-'))
def get_print_seq(self):
return '-'*lelf_miss + sbjct + '-'*right_miss
def get_protein(self):
try:
return str(Seq(get_seq(self), IUPAC.unambiguous_dna).translate(to_stop=True))
except Bio.Data.CodonTable.TranslationError:
return ''
def parse_single_blast(result_handle,ref_gene,genome_records,txt):
global mut_genome
global counter
if counter%num_d_10 ==0:
print "parse: %d/100" %(counter/num_d_10)
counter += 1
ref_gene = ref_gene.seq
ref_protein = ref_gene.translate()
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
#print id
hsp = sorted(alignment.hsps,cmp=lambda x,y: x.identities-y.identities)[-1]
#query = ref_gene.seq.upper()
query = hsp.query.upper()
len_add = hsp.query.count('-')+hsp.sbjct.count('-')
len_upstream = hsp.query_start - 1
len_downstream = len(ref_gene)-(hsp.query_end-hsp.query_start+1)-len_upstream
#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()
if len(sbjct_left)<len_upstream:
sbjct_left = '='*(len_upstream-len(sbjct_left))+sbjct_left
if len(sbjct_right)<len_downstream:
sbjct_right = sbjct_right+'='*(len_downstream-len(sbjct_right))
sbjct = sbjct_left+hsp.sbjct.upper()+sbjct_right
#sbjct_to = hsp.sbjct_start+len_upstream
#sbjct_from = sbjct_to-len(query)+1
#print "sbjct_to,sbjct_from: %d %d" %(sbjct_to,sbjct_from)
#sbjct = (sbjct_gene)[sbjct_from-1:sbjct_to].reverse_complement().upper()
query = (ref_gene[:hsp.query_start-1] + query + ref_gene[hsp.query_end:]).upper()
# print (id,len(query),len(sbjct),counter)
mismatch_g = []
match_g = ''
dash_count_g = dash_at_beginning(sbjct)
for n in range(len(query)):
if query[n]!=sbjct[n]:
match_g += 'x'
mismatch_g += [(n+1,query[n],sbjct[n])]
else:
match_g += ' '
if write_al_gene:
with open(txt, "a") as text_file:
text_file.write('**** DNA Alignment ****\n')
text_file.write('Sequence:\n%s\n' %alignment.title)
text_file.write('Identities: %d/%d\n' %(match_g.count(' '),len(ref_gene)))
#text_file.write('Positives: %d/%d\n' %(hsp.positives,len(hsp.query)))
text_file.write('Mismatches:\n')
print_parsed_blast([query,match_g,sbjct,mismatch_g],text_file)
if dash_count_g>0.1*len(ref_gene):
text_file.write('##### Invalid DNA > 10% #####\n')
mismatch_g = []
dash_count_g = 0
sbjct_dna = ''.join(str(sbjct) .split('-'))
sbjct_dna = ''.join(str(sbjct_dna) .split('='))
protein = Seq(sbjct_dna, IUPAC.unambiguous_dna).translate(to_stop=True)
alignments = pairwise2.align.globalms(ref_protein, protein,2,0,-200,-100)
align = pairwise2.format_alignment(*alignments[0])
lst = align.split('\n')
match_p = ''
mismatch_p = []
dash_count_p = dash_at_beginning(lst[2])
for n in range(len(lst[0])):
if lst[0][n] == lst[2][n]:
match_p += ' '
else:
match_p += 'x'
mismatch_p += [(n+1,lst[0][n],lst[2][n])]
if write_all_protein:
with open(txt, "a") as text_file:
if lst[2] == ref_protein:
pass
elif onlyinvalid(match_p):
pass
else:
text_file.write('**** Protein Alignment ****\n')
text_file.write('Sequence:\n%s\n' %alignment.title)
text_file.write('Identities: %d/%d\n' %(match_p.count(' '),len(ref_protein)))
if dash_count_g>0.1*len(ref_gene):
mismatch_p = []
dash_count_p = 0
else:
print_parsed_blast([lst[0],match_p,lst[2],mismatch_p],text_file)
text_file.write(' \n')
return [len(mismatch_g),dash_count_g,len(mismatch_p),dash_count_p]
except Bio.Data.CodonTable.TranslationError:
open(txt,"a").write("Invalid DNA seq. Fail to translate\n \n")
return [0,0,0,0]
except IndexError:
open(txt,"a").write("No alignment\n \n")
return [0,0,0,0]
def write_single_seq(blasted,genome_records,txt):
global counter
if counter%num_d_10 ==0:
print "parse: %d/100" %(counter/num_d_10)
counter += 1
ref_protein = blasted.get_ref_protein()
sbjct = blasted.get_sbjct()
query = blasted.get_seq
title = blasted.get_title()
if query!=''
with open("DNA_"+txt, "w") as text_file:
text_file.write('>%s\n' %title)
n = 0
while n<len(sbjct):
text_file.write(sbjct[n:n+100]+'\n')
n += 100
protein = blasted.get_protein()
with open("AA_"+txt, "w") as text_file:
text_file.write('>%s\n' %title)
n = 0
while n<len(protein):
text_file.write(protein[n:n+100]+'\n')
n += 100
except Bio.Data.CodonTable.TranslationError:
open(txt,"w").write("Invalid DNA seq. Fail to translate\n \n")
except IndexError:
open(txt,"w").write("No alignment\n \n")
def count_single_invalid(result_handle,ref_gene,genome_records,txt):
global counter
if counter%num_d_10 ==0:
print "parse: %d/100" %(counter/num_d_10)
counter += 1
ref_gene = ref_gene.seq
ref_protein = ref_gene.translate()
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
#print id
hsp = sorted(alignment.hsps,cmp=lambda x,y: x.identities-y.identities)[-1]
#query = ref_gene.seq.upper()
query = hsp.query.upper()
len_add = hsp.query.count('-')+hsp.sbjct.count('-')
len_upstream = hsp.query_start - 1
len_downstream = len(ref_gene)-(hsp.query_end-hsp.query_start+1)-len_upstream
#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()
if len(sbjct_left)<len_upstream:
sbjct_left = '-'*(len_upstream-len(sbjct_left))+sbjct_left
if len(sbjct_right)<len_downstream:
sbjct_right = sbjct_right+'-'*(len_downstream-len(sbjct_right))
sbjct = sbjct_left+hsp.sbjct.upper()+sbjct_right
query = (ref_gene[:hsp.query_start-1] + query + ref_gene[hsp.query_end:]).upper()
dash_count_g = dash_at_beginning(sbjct)
if dash_count_g>0.1*len(ref_gene):
return 1
else:
return 0
except Bio.Data.CodonTable.TranslationError:
return 1
except IndexError:
return 1
def combine_parse_single_blast(result_handle,ref_gene,genome_records,txt):
global counter
if counter%num_d_10 ==0:
print "parse: %d/100" %(counter/num_d_10)
counter += 1
ref_gene = ref_gene.seq
ref_protein = ref_gene.translate()
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
#print id
hsp = sorted(alignment.hsps,cmp=lambda x,y: x.identities-y.identities)[-1]
#query = ref_gene.seq.upper()
query = hsp.query.upper()
len_add = hsp.query.count('-')+hsp.sbjct.count('-')
len_upstream = hsp.query_start - 1
len_downstream = len(ref_gene)-(hsp.query_end-hsp.query_start+1)-len_upstream
#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()
if len(sbjct_left)<len_upstream:
sbjct_left = '='*(len_upstream-len(sbjct_left))+sbjct_left
if len(sbjct_right)<len_downstream:
sbjct_right = sbjct_right+'='*(len_downstream-len(sbjct_right))
sbjct = sbjct_left+hsp.sbjct.upper()+sbjct_right
#sbjct_to = hsp.sbjct_start+len_upstream
#sbjct_from = sbjct_to-len(query)+1
#print "sbjct_to,sbjct_from: %d %d" %(sbjct_to,sbjct_from)
#sbjct = (sbjct_gene)[sbjct_from-1:sbjct_to].reverse_complement().upper()
query = (ref_gene[:hsp.query_start-1] + query + ref_gene[hsp.query_end:]).upper()
# print (id,len(query),len(sbjct),counter)
dash_count_g = dash_at_beginning2(sbjct)
g = ''
for n in range(len(query)):
if query[n]!=sbjct[n]:
g += sbjct[n]
else:
g += ' '
if dash_count_g>0.1*len(ref_gene):
g = ''
sbjct_dna = ''.join(str(sbjct) .split('='))
sbjct_dna = ''.join(str(sbjct) .split('-'))
protein = Seq(sbjct_dna, IUPAC.unambiguous_dna).translate()
protein = protein[:protein.find('*')+1]
alignments = pairwise2.align.globalms(ref_protein, protein,2,0,-200,-100)
align = pairwise2.format_alignment(*alignments[0])
lst = align.split('\n')
p = ''
dash_count_p = dash_at_beginning(lst[2])
for n in range(len(lst[0])):
if lst[0][n] == lst[2][n]:
p += ' '
else:
p += lst[2][n]
if dash_count_g>0.1*len(ref_gene):
p = ''
return [g,query,p,lst[0]]
except Bio.Data.CodonTable.TranslationError:
return ['']*4
except IndexError:
return ['']*4
def print_parsed_blast(lst,text_file):
query = lst[0]
match = lst[1]
sbjct = lst[2]
mismatch = lst[3]
#for (n,q,s) in mismatch:
#text_file.write('%d: %s->%s\n' %(n,q,s))
if len(mismatch)>0:
n = 0
while (n<len(query)):
text_file.write('%s\n' %query[n:n+75])
text_file.write('%s\n' %match[n:n+75])
text_file.write('%s\n' %sbjct[n:n+75])
n += 75
def parse_blast(file_list,ref_gene_name):
#E_VALUE_THRESH = 0.001
global counter
ref_gene = SeqIO.read('ref_genes/%s.fasta' %ref_gene_name,"fasta",IUPAC.unambiguous_dna)
output_name = "blast_%s/%s.txt" %(extension,ref_gene_name)
with open(output_name, "w") as f:
f.write("%s %s\n" %(species, ref_gene_name))
mut_sum = [0,0,0,0]
#seq_cum = [[0]*len(ref_gene)]*5 #A,T,G,C
counter = 0
mut_genome = 0
#try:
file_name = "no genome input"
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
genome_records = SeqIO.index('%s/%s' %(extension,file_name),"fasta",IUPAC.unambiguous_dna)
#print records["JLSA01000036"]
result_handle = open("blast_%s/%s/%s.xml" %(extension,ref_gene_name,genome))
[g,dg,p,dp] = parse_single_blast(result_handle,ref_gene,genome_records,output_name)
mut_sum[0] += g
mut_sum[1] += dg
mut_sum[2] += p
mut_sum[3] += dp
print "%s: #genome w/ mutation:%d" %(ref_gene_name,mut_genome)
return mut_sum
#except:
#print "fail at %s" %file_name
def count_invalid(file_list,ref_gene_name):
#E_VALUE_THRESH = 0.001
global counter
ref_gene = SeqIO.read('ref_genes/%s.fasta' %ref_gene_name,"fasta",IUPAC.unambiguous_dna)
output_name = "blast_%s/%s.txt" %(extension,ref_gene_name)
with open(output_name, "w") as f:
f.write("%s %s\n" %(species, ref_gene_name))
invalid = 0
#seq_cum = [[0]*len(ref_gene)]*5 #A,T,G,C
counter = 0
file_name = "no genome input"
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
genome_records = SeqIO.index('%s/%s' %(extension,file_name),"fasta",IUPAC.unambiguous_dna)
result_handle = open("blast_%s/%s/%s.xml" %(extension,ref_gene_name,genome))
invalid += count_single_invalid(result_handle,ref_gene,genome_records,output_name)
return invalid
def combine_parse_blast(file_list,ref_gene_name):
global counter
ref_gene = SeqIO.read('ref_genes/%s.fasta' %ref_gene_name,"fasta",IUPAC.unambiguous_dna)
ref_protein = ref_gene.seq.translate()
len_g = len(ref_gene.seq)
len_p = len_g/3
output_name_g = "blast_%s/combine_g_%s.txt" %(extension,ref_gene_name)
output_name_p = "blast_%s/combine_p_%s.txt" %(extension,ref_gene_name)
with open(output_name_g, "w") as g:
g.write("---- %s %s ----\n" %(species, ref_gene_name))
g.write(str(ref_gene.seq)+'\n')
with open(output_name_p, "w") as p:
p.write("---- %s %s ----\n" %(species, ref_gene_name))
p.write(str(ref_protein)+'\n')
counter = 0
to_handle = []
#try:
file_name = "no genome input"
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
genome_records = SeqIO.index('%s/%s' %(extension,file_name),"fasta",IUPAC.unambiguous_dna)
#print records["JLSA01000036"]
result_handle = open("blast_%s/%s/%s.xml" %(extension,ref_gene_name,genome))
[a,b,c,d] = combine_parse_single_blast(result_handle,ref_gene,genome_records,output_name_g)
if b == '' or d == '':
continue
elif b==len_g and d == len_p:
with open(output_name_g,"a") as g:
g.write(a+'\n')
with open(output_name_p,"a") as p:
p.write(c+'\n')
else:
to_handle += [[a,b,c,d]]
for item in to_handle:
with open(output_name_g,"a") as g:
g.write(item[1]+'\n')
n = 0
while n<len(item[1]):
if item[0][n] == itemp[1][n]:
g.write(' ')
else:
g.write('x')
n += 1
g.write('\n')
g.write(item[0]+'\n \n')
with open(output_name_p,"a") as p:
p.write(item[3]+'\n')
n = 0
while n<len(item[3]):
if item[2][n] == itemp[3][n]:
p.write(' ')
else:
p.write('x')
n += 1
p.write('\n')
p.write(item[2]+'\n \n')
def write_seq(file_list,ref_gene_name):
global counter
ref_gene = SeqIO.read('ref_genes/%s.fasta' %ref_gene_name,"fasta",IUPAC.unambiguous_dna)
counter = 0
makemydir('output_seq/%s/' %ref_gene_name)
file_name = "no genome input"
for file_name in file_list:
if "DS" in file_name:
continue
genome = file_name[:file_name.find('.'+extension)]
genome_records = SeqIO.index('%s/%s' %(extension,file_name),"fasta",IUPAC.unambiguous_dna)
result_handle = open("blast_%s/%s/%s.xml" %(extension,ref_gene_name,genome))
write_single_seq(result_handle,ref_gene,genome_records,'output_seq/%s/%s.fasta' %(ref_gene_name,file_name[:-len(extension)-1]))