BioPython: extracting sequence IDs from a Blast output file
Question: I have a BLAST output file in XML format. It is 22 query sequences with 50
hits reported from each sequence. And I want to extract all the 50x22 hits.
This is the code I currently have, but it only extracts the 50 hits from the
first query.

    
    
    from Bio.Blast import NCBIXM
    blast_records = NCBIXML.parse(result_handle)
    blast_record = blast_records.next()
    
    save_file = open("/Users/jonbra/Desktop/my_fasta_seq.fasta", 'w')
    
    for alignment in blast_record.alignments:
        for hsp in alignment.hsps:
                save_file.write('>%s\n' % (alignment.title,))
    save_file.close()
    

Somebody have any suggestions as to extract all the hits? I guess I have to
use something else than alignments. Hope this was clear. Thanks!

Jon


Answer: This should get all records. The novelty compared with the original is the  

    
    
    for blast_record in blast_records
    

which is a python idiom to iterate through items in a "list-like" object, such
as the blast_records (checking the [CBIXML module
documentation](http://www.biopython.org/DIST/docs/api/Bio.Blast.NCBIXML-
module.html) showed that parse() indeed returns an iterator)

    
    
    from Bio.Blast import NCBIXM
    blast_records = NCBIXML.parse(result_handle)
    
    save_file = open("/Users/jonbra/Desktop/my_fasta_seq.fasta", 'w')
    
    for blast_record in blast_records:
      for alignment in blast_record.alignments:
          for hsp in alignment.hsps:
                save_file.write('>%s\n' % (alignment.title,))
      #here possibly to output something to file, between each blast_record
    save_file.close()
    

