Question: Want to loop through list of BLAST objects in python
0
gravatar for westin.kosater
11 months ago by
westin.kosater20 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

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?

blast biopython python • 491 views
ADD COMMENTlink modified 11 months ago by finswimmer13k • written 11 months ago by westin.kosater20

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 11 months ago by Joe16k
0
gravatar for finswimmer
11 months ago by
finswimmer13k
Germany
finswimmer13k 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 11 months ago by finswimmer13k
Please log in to add an answer.

Help
Access

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