subprocess woes

K

km

Hi all,

I have a strange question.
a program on shell looks as follows:
$cat test.fa |fasta34 -q @ s
where test.fa contains a protein sequence (alphabets); s is the
database to be searched and @
+indicates that the input is from stdin (ie., 'cat test.fa')
now instead of 'cat test.fa' i take that input into a varaible and
want to feed it to the
+program.
I have a sequence string in a variable named x, and use subprocess
module to wrap this:
######code start ######
import subprocess as sp
x = 'GSQIPSHYWKKNLWYYSHEIDGGCHNMW'
p0 = sp.Popen(["echo",x], stdout=sp.PIPE)
p1 = sp.Popen(["fasta34","-q","@",s],stdin=p0.stdout, stdout=sp.PIPE)
output = p1.communicate()[0]
print output
########code end#####

Output for this code doesnt give me the result as the program senses
the input as empty
let me know where i am wrong.

The idea is to pipe-input the string to the program as a variable as
mentioned above.

regards,
KM
 
D

Dennis Lee Bieber

######code start ######
import subprocess as sp
x = 'GSQIPSHYWKKNLWYYSHEIDGGCHNMW'
p0 = sp.Popen(["echo",x], stdout=sp.PIPE)

Why use this at all?
p1 = sp.Popen(["fasta34","-q","@",s],stdin=p0.stdout, stdout=sp.PIPE)
output = p1.communicate()[0]

Just feed "x" to this directly... (untested):

p1 = sp.Popen(["fasta34","-q","@",s],stdin=sp.PIPE, stdout=sp.PIPE)
output = p1.communicate(x)[0]
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Simon Forman

Dennis said:
######code start ######
import subprocess as sp
x = 'GSQIPSHYWKKNLWYYSHEIDGGCHNMW'
p0 = sp.Popen(["echo",x], stdout=sp.PIPE)

Why use this at all?
p1 = sp.Popen(["fasta34","-q","@",s],stdin=p0.stdout, stdout=sp.PIPE)
output = p1.communicate()[0]

Just feed "x" to this directly... (untested):

p1 = sp.Popen(["fasta34","-q","@",s],stdin=sp.PIPE, stdout=sp.PIPE)
output = p1.communicate(x)[0]
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

You can also pass data to a subprocess like this:

p1 = sp.Popen(["fasta34","-q","@",s],stdin=sp.PIPE, stdout=sp.PIPE)
p1.stdin.write(x)
p1.stdin.close()

But I think communicate() would be better for you in this case.

Peace,
~Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top