Question: Want to loop through list of BLAST objects in python
gravatar for westin.kosater
6 months ago by
westin.kosater10 wrote:

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


    for alignment in blast_record_list[0:len(blast_record_list)].alignments:
        for hsp in alignment.hsps:
            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?

blast biopython python • 290 views
ADD COMMENTlink modified 6 months ago by finswimmer12k • written 6 months ago by westin.kosater10

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 REPLYlink written 6 months ago by Joe14k
gravatar for finswimmer
6 months ago by
finswimmer12k wrote:

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 COMMENTlink written 6 months ago by finswimmer12k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 950 users visited in the last hour