Skip to content

Commit

Permalink
test parsing blast results
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosp420 committed Jan 6, 2015
1 parent b00cd0e commit a7a918e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 8 additions & 0 deletions voseq/blast_local/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,11 @@ def test_do_blast(self):
self.blast.save_query_to_file()
result = self.blast.do_blast()
self.assertTrue(os.path.isfile(result))

def test_parse_blast_output(self):
self.blast.save_seqs_to_file()
self.blast.create_blast_db()
self.blast.save_query_to_file()
self.blast.do_blast()
result = self.blast.parse_blast_output()
self.assertTrue(1057 in [i['length'] for i in result])
21 changes: 18 additions & 3 deletions voseq/blast_local/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import subprocess
import uuid

from Bio import SeqIO
from Bio.Blast.Applications import NcbiblastnCommandline
from Bio.Blast import NCBIXML
from Bio.Seq import Seq
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
import pytz

Expand All @@ -32,6 +33,7 @@ def __init__(self, blast_type, voucher_code, gene_code, mask=None, test=None):
:param voucher_code:
:param gene_code:
"""
self.e_value = 0.001
self.blast_type = blast_type
self.voucher_code = voucher_code
self.gene_code = gene_code
Expand Down Expand Up @@ -184,7 +186,7 @@ def save_query_to_file(self):

def do_blast(self):
blastn_cline = NcbiblastnCommandline(query=self.query_file, db=self.db,
evalue=0.001, outfmt=5, out=self.output_file)
evalue=self.e_value, outfmt=5, out=self.output_file)
blastn_cline()
return self.output_file

Expand All @@ -194,7 +196,20 @@ def parse_blast_output(self):
match_description, max_score, total_score, query_cover, e_value, % ident, accession number
"""
pass
handle = open(self.output_file, 'r')
blast_record = NCBIXML.read(handle)
hits = []
append = hits.append

for alignment in blast_record.alignments:
for hsp in alignment.hsps:
if hsp.expect < self.e_value:
obj = {}
obj['sequence'] = alignment.title
obj['length'] = alignment.length
obj['e_value'] = hsp.expect
append(obj)
return hits

def strip_question_marks(self, seq):
seq = re.sub('^\?+', '', seq)
Expand Down

0 comments on commit a7a918e

Please sign in to comment.