This is a test version of Biostars. For the public version, visit https://www.biostars.org.
get match names from blast using biopython

Hi All, I am trying to make an overview of the most found organisms in a blast query by using biopython. How do i get the names of the organisms found in the search?

from Bio.Blast import NCBIXML
result_handle = open("../data/blastOutput/test_2.xml")
blast_records = list(NCBIXML.parse(result_handle))
for item in blast_records:
    print("\n", item.match,"\n")
blast biopython

2 answers

just for fun, a one-liner using xpath expressions ad wget...

xmllint --xpath '//Hit_accession/text()' input.blastn.xml  |\
sort | uniq |\
awk  'BEGIN {printf("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=nucleotide&id=");} {printf("%s%s",(NR==1?"":","),$1);}' |\
xargs wget -q -O - |\
xmllint --xpath '//Item[@Name="TaxId"]/text()' - |\
sort | uniq |\
awk  'BEGIN {printf("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=taxonomy&id=");} {printf("%s%s",(NR==1?"":","),$1);}' |\
xargs wget -q -O - |\
xmllint --xpath '//Item[@Name="ScientificName"]/text()' -

Acinetobacter baumannii AC12
Acinetobacter baumannii AC30
Amycolatopsis lurida NRRL 2430
Thermosensitive cloning vector pTN1
Sphingobacterium sp. PM2-P1-29
Desulfitobacterium hafniense
Escherichia coli

if you'd like to remain in Biopython, you could extract this information from the accession or title of each alignment:

from Bio.Blast import NCBIXML
import collections

result = open("blastoutput.xml")
records = NCBIXML.parse(result)
item = next(records) 
organisms = []

def get_organism(title):
    """Given an item title, return the organism as a string.
    """
    parts = title.split("|")
    words = parts[4].split(" ")
    return words[1]

for alignment in item.alignments:
    organisms.append(get_organism(alignment.title))

print(collections.Counter(organisms))

Log in to answer this question.