【发布时间】:2012-08-28 06:29:29
【问题描述】:
所以我在 python 中做一些生物信息学工作,利用 Biopython 和 Clustalw2 来对齐蛋白质序列。我对此相当陌生(只有几个月的经验),并且在使用 stdout 并遍历整个目录时遇到了问题。任何帮助将不胜感激。
所以我写了这个,它一次处理一个文件并产生所需的结果......
#!/usr/bin/python
import Bio
import os
from Bio.Align.Applications import ClustalwCommandline
from Bio import Seq
from Bio import SeqIO
from Bio import AlignIO
from Bio.SeqRecord import SeqRecord
clustal_loc=r"/Users/Wes/Desktop/eggNOG_files/clustalw-2.1-macosx/clustalw2"
try:
f_in=raw_input("Enter the filepath of the FASTA to align: ")
f_out= raw_input("Enter the output filename: ")
fh= open(f_in)
fo=open(f_out,'w')
for record in SeqIO.parse(fh,"fasta"):
id = record.id
seq = record.seq
print("Name: %s, size: %s"%(id,len(seq)))
try:
cl = ClustalwCommandline(clustal_loc,infile=f_in, outfile=f_out, align=True, outorder="ALIGNED", convert=True, output="pir")
assert os.path.isfile(clustal_loc), "Clustal W not found"
stdout, stderr = cl()
print cl
except:
print("There was a problem aligning. Check ClustalW path and .fasta input.")
fh.close()
fo.close()
except:
print("Could not parse. Check to make sure filepath is correct and that file is in FASTA format")
...这似乎工作得很好。当我尝试在整个目录上迭代它时问题就来了(比如需要对齐的 1000 多个蛋白质序列文件。我知道问题出在标准输出上,但此时我有点太业余了,不知道如何解决它. 下面是损坏的代码——
/usr/bin/python
import Bio
import os
from Bio.Align.Applications import ClustalwCommandline
from Bio import Seq
from Bio import SeqIO
from Bio import AlignIO
from Bio.SeqRecord import SeqRecord
import subprocess
from subprocess import Popen
clustal_loc=r"/Users/Wes/Desktop/eggNOG_files/clustalw-2.1-macosx/clustalw2"
try:
folder= raw_input("Enter the folder of .fasta files to iterate over and align: ")
listing = os.listdir(folder)
for infile in listing:
print folder+'/'+infile
f_in = open(folder+'/'+infile,'r')
f_out=open(folder+'/'+infile+".pir",'w')
for record in SeqIO.parse(f_in,"fasta"):
id = record.id
seq = record.seq
print("Name: %s, size: %s"%(id,len(seq)))
clustalw_cline= ClustalwCommandline(clustal_loc,infile=f_in, outfile=f_out, align=True, outorder="ALIGNED", convert=True, output="pir")
assert os.path.isfile(clustal_loc), "Clustal W not found"
saveout = sys.stdout
sys.stdout = clustalw_cline()
sys.stdout = saveout
f_in.close()
f_out.close()
except:
print("There was a problem aligning. Check ClustalW path and .fasta folder format/location")
如你所见,我一直把这个搞砸了。感谢您提供的任何帮助。
【问题讨论】:
-
你也可以问biostar:biostars.org
-
在您构造 ClustalwCommandline 对象的地方,infile 和 outfile 参数应该是字符串形式的文件名,而不是文件对象。我会在这里写更多,但是 cmets 不允许我进行代码格式化...
标签: python stdout bioinformatics biopython