python - 如何加入两个子进程的标准输出并通过管道连接到python中新子进程的标准输入

标签 python subprocess samtools

假设我从 shell 运行了以下命令

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

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

我的问题是如何使用子进程在 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 - 如何加入两个子进程的标准输出并通过管道连接到python中新子进程的标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28774716/

相关文章:

python - 绘制 Pandas DataFrame 中出现的次数

Python:如何在类中调用def

python - 尽管进程已死,为什么我会收到子进程资源警告?

python - subprocess.Popen 参数的编码

java - 使用 jython 或 groovy 中的 htsjdk 定义的类

python - NLTK MLE 模型澄清三元组及更多

Python--提取http头

python - Popen.communicate 被卡住,直到子进程产生的进程终止

Makefile - samtools 安装失败