Blast without creating any file using biopython

247 views Asked by At

I have a working code, which is doing BLAST on ncbi server, then returns sequences in xml format. It is working but I want to avoid making new files and print BLAST results directly on terminal. It is there a good solution to do this? Im pasting below my code, which is working, but it is creating a new file.

result_handle = NCBIWWW.qblast(
"blastx",
"nr",
sequences,
entrez_query = organism)

save_file = open("BLAST.xml", "w")
save_file.write(result_handle.read())
save_file.close()

result_handle.close()

result = open("BLAST.xml", "r")
records = NCBIXML.parse(result)

for i, record in enumerate(records):
    if record.alignments == []:
        print ("There is no BLAST result")
    else:
        for align in record.alignments:
            print (align.hit_id)
            break

I wanted to do something like this:

result = result_handle.read()
record = NCBIXML.parse(result)
for i, record in enumerate(record):

But it is not working.

Thank You in advance!

1

There are 1 answers

0
cdlane On

My belief is the solution should be even simpler than you're hoping:

result_handle = NCBIWWW.qblast("blastx", "nr", sequences, entrez_query=organism)

records = NCBIXML.parse(result_handle)

for i, record in enumerate(records):
    if record.alignments:
        for align in record.alignments:
            print(align.hit_id)
    else:
        print("There is no BLAST result for", i)

The result from NCBIWWW.qblast() is a handle onto a StringIO input stream which you can directly hand to NCBIXML.parse().