I have opened a file with blast results and printed out the hits in fasta format to the screen.
The code looks like this:
result_handle = open(\"/Use
you can use with statement
to ensure that file will be closed
from __future__ import with_statement
with open('/Users/jonbra/Desktop/my_blast.xml', 'w') as outfile:
from Bio.Blast import NCBIXML
blast_records = NCBIXML.parse(result_handle)
blast_record = blast_records.next()
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
outfile.write('>%s\n%s\n' % (alignment.title, hsp.sbjct))
or use try ... finally
outfile = open('/Users/jonbra/Desktop/my_blast.xml', 'w')
try:
from Bio.Blast import NCBIXML
blast_records = NCBIXML.parse(result_handle)
blast_record = blast_records.next()
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
outfile.write('>%s\n%s\n' % (alignment.title, hsp.sbjct))
finally:
outfile.close()