This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Want to loop through list of BLAST objects in python

I performed a BLAST search with Biopython from a FASTA file with 92 entries (goat.fasta)

from Bio.Blast import NCBIWWW
from Bio.Blast import NCBIXML
import pandas as pd

fasta_string = open("goat.fasta").read()

result_handle = NCBIWWW.qblast("blastx", sequence = fasta_string, database = "refseq_protein", entrez_query = 'txid9606[ORGN]')


blast_records = NCBIXML.parse(result_handle)

blast_record_list = list(blast_records)

The variable blast_record_list has a list of blast record objects that I would like to pull out the title of each alignment. Here are my two attempts at doing this, as well as the errors that I got

for entry in blast_record_list:
    for alignment in entry:
        for hsp in alignment.hsps:
            print("sequence:", alignment.title)

Traceback (most recent call last):

  File "<ipython-input-42-bb44df962013>", line 2, in <module>
    for alignment in entry:

TypeError: 'Blast' object is not iterable

And

    for alignment in blast_record_list[0:len(blast_record_list)].alignments:
        for hsp in alignment.hsps:
            print("****Alignment****")
            print("sequence:", alignment.title)





Traceback (most recent call last):

  File "<ipython-input-41-033f221fbc66>", line 1, in <module>
    for alignment in blast_record_list[0:len(blast_record_list)].alignments:

AttributeError: 'list' object has no attribute 'alignments'

Can someone tell me what I am doing wrong here?

biopython blast python

You may want to consider using the SearchIO module of BioPython instead of Pandas for this, since it already has a number of methods and data structures for handling blast data.

1 answer

Hello westin.kosater,

As far as I see, it must be for alignment in entry.alignments to iterate over the alignments of one blast object.

blast_record_list = list(blast_records)

Why are you doing this? NCBIXML.parse(result_handle) is a generator which already returns an iterator. I cannot see why you need the whole list at a time.

fin swimmer

Log in to answer this question.