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

passing python variable (string) to bash command through echo pipe

我无法将 python(python 变量)中的字符串作为输入传递给命令行上的序列比对程序(muscle)(bash ). 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 > out.file 以获得下游处理所需的 fasta 比对。但是到达那里,我必须传递 'muscle' FASTA 序列字符串,我认为最好通过 echo 完成,如上 bash。

因此,该脚本采用两个 multiFASTA 文件,并根据每个文件的 ID 列表对每个 FASTA 序列进行配对 - 这是有效的(尽管我意识到这可能不是最有效的方法 - 我我是新 python 用户)。然后我需要在计算 distances/difference 之前对齐 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 字符串,但我似乎无法 "pipe" 通过标准输入到肌肉...我得到的主要错误是:AttributeError: 'str' object has no attribute '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:之前的 post 是相关的,但我的问题是关于链接两个 bash 命令,这些命令以 python 变量开头,该变量是一个字符串...然后,理想情况下,将第二个命令的标准输出捕获回 python 变量...我不太明白如何将 post 的答案转化为我的特定问题的解决方案...我想我不;不完全理解 poster 试图做什么。

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

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