shell - using Python subprocess to redirect stdout to stdin? -


i'm making call program shell using subprocess module outputs binary file stdout.

i use popen() call program , want pass stream function in python package (called "pysam") unfortunately cannot python file objects, can read stdin. i'd have output of shell command go stdout stdin.

how can done within popen/subprocess module? way i'm calling shell program:

p = subprocess.popen(my_cmd, stdout=subprocess.pipe, shell=true).stdout 

this read "my_cmd"'s stdout output , stream in p. since python module cannot read "p" directly, trying redirect stdout of "my_cmd" stdin using:

p = subprocess.popen(my_cmd, stdout=subprocess.pipe, stdin=subprocess.pipe, shell=true).stdout 

i call module, uses "-" placeholder stdin:

s = pysam.samfile("-", "rb") 

the above call means read stdin (denoted "-") , read binary file ("rb").

when try this, binary output sent screen, , doesn't samfile() function can read it. occurs if remove call samfile, think it's call popen problem , not downstream steps.

edit: in response answers, tried:

sys.stdin = subprocess.popen(tagbam_cmd, stdout=subprocess.pipe, shell=true).stdout print "opening sam.."                                                                                             s = pysam.samfile("-","rb") print "done?" sys.stdin = sys.__stdin__     

this seems hang. output:

opening sam.. 

but never gets past samfile("-", "rb") line. idea why?

any idea how can fixed?

edit 2: adding link pysam documentation in case helps, cannot figure out. documentation page is:

http://wwwfgu.anat.ox.ac.uk/~andreas/documentation/samtools/usage.html

and specific note streams here:

http://wwwfgu.anat.ox.ac.uk/~andreas/documentation/samtools/usage.html#using-streams

in particular:

""" pysam not support reading , writing true python file objects, support reading , writing stdin , stdout. following example reads stdin , writes stdout:

infile = pysam.samfile( "-", "r" ) outfile = pysam.samfile( "-", "w", template = infile ) s in infile: outfile.write(s) 

it work bam files. following script converts bam formatted file on stdin sam formatted file on stdout:

infile = pysam.samfile( "-", "rb" ) outfile = pysam.samfile( "-", "w", template = infile ) s in infile: outfile.write(s) 

note, file open mode needs changed r rb. """

so want take stream coming popen, reads stdout, , redirect stdin, can use samfile("-", "rb") above section states possible.

thanks.

i'm little confused see binary on stdout if using stdout=subprocess.pipe, however, overall problem need work sys.stdin if want trick pysam using it.

for instance:

sys.stdin = subprocess.popen(my_cmd, stdout=subprocess.pipe, shell=true).stdout s = pysam.samfile("-", "rb") sys.stdin = sys.__stdin__ # restore original stdin 

update: assumed pysam running in context of python interpreter , means python interpreter's stdin when "-" specified. unfortunately, doesn't; when "-" specified reads directly file descriptor 0.

in other words, not using python's concept of stdin (sys.stdin) replacing has no effect on pysam.samfile(). not possible take output popen call , somehow "push" on file descriptor 0; it's readonly , other end of connected terminal.

the real way output onto file descriptor 0 move additional script , connect 2 first. ensures output popen in first script end on file descriptor 0 of second one.

so, in case, best option split 2 scripts. first 1 invoke my_cmd , take output of , use input second popen of python script invokes pysam.samfile("-", "rb").


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -