thanks DK, with some tweaking, that worked. here was the final command:
result_handle = os.popen("echo ""+seq_record.format("fasta")+"" | "+str(Blastx_Command)) record = NCBIXML.read(result_handle)
i am blasting sequences from a fasta file individually since the output will be handled differently depending on the quality of the hit.
currently i have this, which works fine:
blast_handle = Blastx_Command(stdin=seq_record.format("fasta"))
however, this involves writing the output to a temporary xml file and then reading it with NCBIXML.parse(file), which is quite slow.
NCBIXML.read() or parse() expects a file, so as far as i understand, it cannot directly take the standard output.
does anyone know a way to take the stdout from the Blastx_Command and somehow turn that into a BlastRecord?
thanks in advance
I am not sure how exactly you are forming your blast commands. But you can try using popen objects. For example
import os
result = os.popen('my blast command')
result is now a file handle that NCBIXML.parse should be able to read.
A simple example that'll list the current directory in linux:
import os
result = os.popen('ls')
for line in result:
print line
thanks DK, with some tweaking, that worked. here was the final command:
result_handle = os.popen("echo ""+seq_record.format("fasta")+"" | "+str(Blastx_Command)) record = NCBIXML.read(result_handle)
No, NCBIXML.read() or parse() expects a handle - not a file. This means if you run BLAST via subprocess (or one of the dreprecated alternatives like os.popen) you can get a handle for BLAST's stdout and parse that directly. There are examples of this in the Biopython Tutorial (for multiple sequence alignment tools at least).
to answer my own question, someone suggested cStringIO, which was implemented as:
import cStringIO
BlastX_Command = NcbiblastxCommandline (db="nt", query="-", outfmt=5)
blast_handle = cStringIO.StringIO(Blastx_Command(stdin=seq_record.format("fasta"))[0])
for record in NCBIXML.parse(blast_handle):
etc...
Log in to answer this question.