Want to loop through list of BLAST objects in python
1
1
Entering edit mode
5.0 years ago

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 • 2.3k views
ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
5.0 years ago

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

ADD COMMENT

Login before adding your answer.

Traffic: 2971 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6