[Biopython] example request for using stdin and stdout with 'needle' in EMBOSS

Brad Chapman chapmanb at 50mail.com
Fri Feb 19 13:58:40 UTC 2010


Li;

> Wonder if anyone can provide an example for using needle but take stdin as
> input and stdout as output within biopython.
> I did like this, but it doesn't work.
> 
> cline = NeedleCoomandline( gapopen=10, gapextend=.5, outfile='stdout',
> asequence='stdin', bsequence='stdout')
> child = subprocess.Popen( cline, shell=True, stdout=PIPE, stdin = PIPE,
> stderr = PIPE )
> SeqIO.write( a, child.stdin, 'fasta')
> SeqIO.write( b, child.stdin, 'fasta')
> child.stdin.close()
> print child.returncode

For Emboss commandline options that take two different inputs, like
needle, I don't know of a way to pass them in via standard input.
My approach would be to write to a temporary file for the input
sequences. A fully worked example is here:

http://gist.github.com/308708

and pasted below. 

For your own debugging purposes,you should avoid redirecting stderr to
the subprocess PIPE. Emboss will write out error messages about what
is wrong with the commandline, and they get ignored silently.

Hope this helps,
Brad


import os
import subprocess
import tempfile

from Bio import SeqIO
from Bio.Emboss.Applications import NeedleCommandline

# read in file from somewhere
in_file = os.path.join("Tests", "NeuralNetwork", "enolase.fasta")
in_handle = open(in_file)
gen = SeqIO.parse(in_handle, "fasta")
a = gen.next()
a.id = "1"
b = gen.next()
b.id = "2"

# create temporary file
(_, tmp_file) = tempfile.mkstemp()
tmp_handle = open(tmp_file, "w")
SeqIO.write([a, b], tmp_handle, 'fasta')
tmp_handle.close()

# run needle
cline = NeedleCommandline( gapopen=10, gapextend=.5, outfile='stdout',
        asequence='%s:%s' % (tmp_file, a.id), 
        bsequence='%s:%s' % (tmp_file, b.id))
child = subprocess.Popen(str(cline), shell=True, stdout=subprocess.PIPE,)
child.wait()
os.remove(tmp_file)
print child.returncode

print child.stdout.read()



More information about the Biopython mailing list