如何在python中将两个子进程的stdout和管道连接到新子进程的stdin

假设我从shell运行以下命令

{ 
samtools view -HS header.sam;           # command1
samtools view input.bam 1:1-50000000;   # command2
} | samtools view -bS - > output.bam    # command3

对于那些不熟悉samtools视图的人(因为这是stackoverflow).这实际上是在创建一个具有新标头的新bam文件. bam文件通常是大型压缩文件,因此即使在某些情况下通过文件也可能非常耗时.一种替代方法是进行command2,然后使用samtools reheader来切换标头.这会两次通过大文件.上面的命令一次性通过bam,这对于较大的bam文件是有用的(即使在压缩时它们也会大于20GB – WGS).

我的问题是如何使用subprocess在python中实现这种类型的命令.

我有以下内容:

fh_bam = open('output.bam', 'w')
params_0 = [ "samtools", "view", "-HS", "header.sam" ]
params_1 = [ "samtools", "view", "input.bam", "1:1-50000000"]
params_2 = [ "samtools", "view", "-bS", "-" ]
sub_0 = subprocess.Popen(params_0, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
sub_1 = subprocess.Popen(params_1, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
### SOMEHOW APPEND sub_1.stdout to sub_0.stdout
sub_2 = subprocess.Popen(params_2, stdin=appended.stdout, stdout=fh_bam)

任何帮助是极大的赞赏.谢谢.

最佳答案
如果你已经在字符串中有shell命令,那么你可以按原样运行它:

#!/usr/bin/env python
from subprocess import check_call

check_call(r"""
{ 
samtools view -HS header.sam;           # command1
samtools view input.bam 1:1-50000000;   # command2
} | samtools view -bS - > output.bam    # command3
""", shell=True)

要在Python中模拟管道:

#!/usr/bin/env python
from subprocess import Popen, PIPE

# start command3 to get stdin pipe, redirect output to the file
with open('output.bam', 'wb', 0) as output_file:
    command3 = Popen("samtools view -bS -".split(), 
                     stdin=PIPE, stdout=output_file)
# start command1 with its stdout redirected to command3 stdin
command1 = Popen('samtools view -HS header.sam'.split(),
                 stdout=command3.stdin)
rc_command1 = command1.wait() #NOTE: command3.stdin is not closed, no SIGPIPE or a write error if command3 dies
# start command2 after command1 finishes
command2 = Popen('samtools view input.bam 1:1-50000000'.split(),
                 stdout=command3.stdin)
command3.stdin.close() # inform command2 if command3 dies (SIGPIPE or a write error)
rc_command2 = command2.wait()
rc_command3 = command3.wait()

转载注明原文:如何在python中将两个子进程的stdout和管道连接到新子进程的stdin - 代码日志