Very large input and piping using subprocess.Popen

前端 未结 5 711
北恋
北恋 2020-12-25 15:40

I have pretty simple problem. I have a large file that goes through three steps, a decoding step using an external program, some processing in python, and then recoding usi

5条回答
  •  情话喂你
    2020-12-25 16:10

    I was using the .read() method on the stdout stream. Instead, I simply needed to read directly from the stream in the for loop above. The corrected code does what I expected.

    #!/usr/bin/env python
    import os
    import sys
    import subprocess
    
    def main(infile,reflist):
        print infile,reflist
        samtoolsin = subprocess.Popen(["samtools","view",infile],
                                      stdout=subprocess.PIPE,bufsize=1)
        samtoolsout = subprocess.Popen(["samtools","import",reflist,"-",
                                        infile+".tmp"],stdin=subprocess.PIPE,bufsize=1)
        for line in samtoolsin.stdout:
            if(line.startswith("@")):
                samtoolsout.stdin.write(line)
            else:
                linesplit = line.split("\t")
                if(linesplit[10]=="*"):
                    linesplit[9]="*"
                samtoolsout.stdin.write("\t".join(linesplit))
    

提交回复
热议问题