【问题标题】:Bioinformatics script using Python/Biopython/Clustalw using stdout to iterate over a directory of proteins使用 Python/Biopython/Clustalw 的生物信息学脚本,使用标准输出迭代蛋白质目录
【发布时间】: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


【解决方案1】:

您看到的具体错误是什么?您不应该将 sys.sterr 和 sys.stdout 设置为字符串值(clustalw_cline() 函数将 clustal stderr 和 stdout 作为字符串返回),因为您将无法从 python 向 stdout 写入任何内容。

我尝试清理并更正您的以下代码。

#!/usr/bin/env python

import Bio
import os
from glob import glob
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 = glob(os.path.join(folder, '*.fasta'))
    for infile in listing:
        print infile
        with open(os.path.splitext(infile) + '.pir') as f_out:
            with open(infile) as f_in:
                for record in SeqIO.parse(infile,"fasta"):
                        id = record.id
                        seq = record.seq
                        print("Name: %s, size: %s"%(id,len(seq)))
                assert os.path.isfile(clustal_loc), "Clustal W not found"
                clustalw_cline= ClustalwCommandline(clustal_loc,infile=f_in,
                                                    outfile=f_out, align=True, 
                                                    outorder="ALIGNED",convert=True, output="pir")
                stdout, stderr = clustalw_cline()
except Exception:
    print("There was a problem aligning. Check ClustalW path and .fasta folder format/location")

【讨论】:

  • 好吧,我正在尝试将 clustalw 生成的对齐方式写入 .pir 文件。第一个脚本似乎可以很好地处理单个文件。你是说我应该放弃标准输出和标准错误吗?当我这样做时,处理单个文件的原始脚本无法正常工作。感谢您的清理和帮助。
  • 在您的第一个脚本中,stdout 和 stderr 变量将 clustal 的输出捕获为字符串。在您的第二个脚本中,您将类似 python 文件的对象 sys.stdout 分配给 clustalw_cline() 的输出,它是 (stdout, stderr) 形式的元组,这几乎肯定不是您想要做的。如果要将 clustalw 输出写入 sys.stdout,则应使用 print 函数,或使用 sys.stdout.write(output)
  • 我只想将 clustalw_cline() 输出写入我打开的文件,即 f_out... 但这次我似乎做不到。
猜你喜欢
  • 2013-11-27
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多