2017-02-25 80 views
2

我无法在命令行(bash)中将python(python变量)中的字符串作为输入传递给序列比对程序(muscle)。 muscle可以从命令行获取stdin,例如:通过回声管道传递python变量(字符串)到bash命令

~# 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行),我以后 - 你可以重定向肌肉的输出(echo -e ">1\nATTTCTCT\n>2\nATTTCTCC" | muscle > out.file得到公正的FASTA比,这是我需要的下游加工,但到那里。 ,我必须通过'肌肉'的FASTA序列字符串,我认为最好通过echo完成上面的bash。

因此,脚本需要两个multiFASTA文件,并且根据ID列表对每个FASTA序列进行配对对于每个文件 - 这是行得通的(尽管我意识到它可能不是最有效的方法 - 我是新的Python用户)然后我需要对齐每个集合在muscle之前计算di姿态/区别。

这是我到目前为止有:

#! /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”变量是我想要传递给肌肉是什么 - 它是合适的fasta序列从每个multiFASTA文件相结合,为每个结果遍历ids/list,并格式化为FASTA文件。

这个问题是,我可以FASTA字符串,但我似乎不能“管”,通过stdin肌肉...我得到的主要错误是: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:那以前的帖子是相关的,但我的问题是关于连接两个庆典命令开始一个python的变量,它是一个字符串...和那么,理想情况下,将第二个命令的stdout捕获回python变量......我不太清楚如何将该帖子上的答案转化为针对我的具体问题的解决方案......我想我完全不明白这张海报正试图去做。

+0

'first'是一个字符串,将其发送到命令看到:HTTP://计算器.com/a/165662/7311767 –

+0

为什么'subprocess.call(['bash','muscle'],stdin = first.stdout)'而不仅仅是'subprocess.call(['muscle'],stdin = first。标准输出)'? –

+0

它没有什么区别(字符串错误仍然存​​在),但我猜'bash'在那里是不需要的。 –

回答

2

似乎要与muscle进程通信,那么你需要一个管道,使用此

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

啊。我现在明白了。非常感谢你的帮助。 –