python - 通过 echo 管道将 python 变量(字符串)传递给 bash 命令

标签 python bash subprocess stdin biopython

<分区>

我在将 python 中的字符串(python 变量)作为输入传递给命令行 (bash) 上的序列比对程序 (muscle) 时遇到问题。 muscle 可以从命令行获取标准输入,例如;

~# echo -e ">1\nATTTCTCT\n>2\nATTTCTCC" | muscle

MUSCLE v3.8.31 by Robert C. Edgar

http://www.drive5.com/muscle
This software is donated to the public domain.
Please cite: Edgar, R.C. Nucleic Acids Res 32(5), 1792-97.

- 2 seqs, max length 8, avg  length 8
00:00:00     22 MB(2%)  Iter   1  100.00%  K-mer dist pass 1
00:00:00     22 MB(2%)  Iter   1  100.00%  K-mer dist pass 2
00:00:00     23 MB(2%)  Iter   1  100.00%  Align node       
00:00:00     23 MB(2%)  Iter   1  100.00%  Root alignment
>1
ATTTCTCT
>2
ATTTCTCC

我所追求的就是这个 fasta 对齐(最后 4 行)——你可以重定向 muscle (echo -e ">1\nATTTCTCT\n>2\nATTTCTCC"| muscle > 输出。 file 以获得下游处理所需的 fasta 比对。但是要到达那里,我必须将“肌肉”传递给 FASTA 序列字符串,我认为最好通过 echo 在 bash 中如上。

因此,该脚本采用两个 multiFASTA 文件,并根据每个文件的 ID 列表对每个 FASTA 序列进行配对 - 这是有效的(尽管我意识到这可能不是最有效的方法 - 我是新手 python 用户)。然后我需要在计算距离/差异之前对齐 muscle 中的每个集合。

这是我目前所拥有的:

#! /env/python
from pairwise_distances import K2Pdistance
import pairwise_distances
import subprocess
from subprocess import call
import os     

fasta1=['']
for line in open('test1.fasta'):
    if not line.startswith('>'):
         fasta1.append(line.strip())

fasta2=['']
for line in open('test2.fasta'):
    if not line.startswith('>'):
         fasta2.append(line.strip())

for l1, l2 in zip(open('test1.list'), open ('test2.list')):
 try:
   a=fasta1[int(l1)]
 except IndexError,e:  
   a="GGG"

 try:
   b=fasta2[int(l2)]
 except (IndexError):
   b="CCC"

 temp_align=str(">1"+'\n'+a+'\n'+">2"+'\n'+b) 

 first=subprocess.check_output(['echo','-e',temp_align])
 print first
 subprocess.call(['bash','muscle'], stdin=first.stdout)
 print second

 #new=K2Pdistance(outfast1,outfast2) 
 #subprocess.Popen(['bash','muscle'], stdin=subprocess.check_output(['echo','-e',temp_align], stdout=subprocess.PIPE).std.out)`

“temp_align”变量是我想传递给 muscle 的——它是将每个 multiFASTA 文件中适当的 fasta 序列组合在一起的结果,对于 ids/list 上的每个循环,其格式类似于 FASTA 文件。

这个问题是我可以 echo FASTA 字符串,但我似乎无法通过标准输入将其“传送”到肌肉...我得到的主要错误是:AttributeError: ' str' 对象没有属性 'stdout'

~#python Beta3.py 
>1
ATCGACTACT
>2
ATCGCGCTACT

Traceback (most recent call last):
  File "Beta3.py", line 38, in <module>
    subprocess.call(['bash','muscle'], stdin=first.stdout)
AttributeError: 'str' object has no attribute 'stdout'

子进程或其他命令行模块可以将字符串作为标准输入吗?如果没有,我不确定我如何回应然后进入肌肉...... 任何其他想法将不胜感激。我尝试了这个线程中的几个选项 unix.stackexchange

但还没有运气。

编辑:以下是一些示例文件:

~# cat test1.fasta 
>1
ATCGACTACT
>2
ACTCAGTCA
>3
TTCACAGGG
~# cat test2.fasta 
>1
ATCGCGCTACT
>2
GGCGTCAGTCA
>3
TTCAACCCCAGGG
~# cat test1.list 
1
2
3
~# cat test2.list 
1
3
2

编辑 2:上一篇文章是相关的,但我的问题是关于链接两个以 python 变量开头的 bash 命令,该变量是一个字符串...然后,理想情况下,将第二个命令的标准输出捕获回 python 变量...我不太明白如何将那个帖子的答案转化为我的特定问题的解决方案...我想我不完全理解张贴者试图做什么。

最佳答案

看来你要和muscle进程通信,那你需要一个PIPE,用这个

(out, err) = subprocess.Popen(['muscle'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate(temp_align)
print out

关于python - 通过 echo 管道将 python 变量(字符串)传递给 bash 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42452033/

相关文章:

python - 尝试在打印语句中用 % 替换普通 var

bash - 比较不区分大小写的字符串,然后计算重复项

linux - 在 Linux 上以另一个用户身份运行脚本

python : How to make a script enter and exit minicom terminal?

python - 在 Python 中通过 ssh 运行 popen 时,终端窗口不会保持打开状态

python - subprocess.Popen 在不同的控制台中

python - Python 中 nosetests/unittest 中测试之间的依赖关系

python - 接收 bool 结果以查看 crontab 是否存在

python - C++ 和 Python 之间的轮廓差异

bash - 在 Bash : [ -n "$var" ] or [ "$var" ] 中测试非零长度字符串