【问题标题】:How do I pass Biopython SeqIO.convert() over multiple files in a directory?如何将 Biopython SeqIO.convert() 传递给目录中的多个文件?
【发布时间】:2014-02-13 01:23:29
【问题描述】:

我正在编写一个 python 脚本(2.7 版),它将指定目录中的每个输入文件(.nexus 格式)更改为 .fasta 格式。 Biopython 模块 SeqIO.convert 完美地处理了单独指定文件的转换,但是当我尝试使用 os.walk 在目录上自动执行该过程时,我无法正确地将每个输入文件的路径名传递给 SeqIO.convert。我哪里错了?我是否需要使用 os.path 模块中的 join() 并将完整路径名传递给 SeqIO.convert?

    #Import modules
    import sys
    import re
    import os
    import fileinput

    from Bio import SeqIO

    #Specify directory of interest
    PSGDirectory = "/Users/InputDirectory”
    #Create a class that will run the SeqIO.convert function repeatedly
    def process(filename):
      count = SeqIO.convert("files", "nexus", "files.fa", "fasta", alphabet= IUPAC.ambiguous_dna)
    #Make sure os.walk works correctly
    for path, dirs, files in os.walk(PSGDirectory):
       print path
       print dirs
       print files

    #Now recursively do the count command on each file inside PSGDirectory
    for files in os.walk(PSGDirectory):
       print("Converted %i records" % count)
       process(files)      

当我运行脚本时,我收到以下错误消息: Traceback (most recent call last): File "nexus_to_fasta.psg", line 45, in <module> print("Converted %i records" % count) NameError: name 'count' is not defined This conversation 很有帮助,但我不知道在哪里插入 join() 函数语句。 Here is an example of one of my nexus files 感谢您的帮助!

【问题讨论】:

    标签: python biopython os.walk


    【解决方案1】:

    发生了一些事情。

    首先,您的流程函数没有返回“计数”。你可能想要:

    def process(filename):
       return seqIO.convert("files", "nexus", "files.fa", "fasta", alphabet=IUPAC.ambiguous_dna) 
       # assuming seqIO.convert actually returns the number you want
    

    此外,当您编写 for files in os.walk(PSGDirectory) 时,您正在操作 os.walk 返回的 3 元组,而不是单个文件。你想做这样的事情(注意使用 os.path.join):

    for root, dirs, files in os.walk(PSGDirectory):
        for filename in files:
                fullpath = os.path.join(root, filename)
                print process(fullpath)
    

    更新:

    所以我查看了 seqIO.convert 的文档,它期望被调用:

    • in_file - 输入句柄或文件名
    • in_format - 输入文件格式,小写字符串
    • out_file - 输出句柄或文件名
    • out_format - 输出文件格式,小写字符串
    • 字母表 - 假设的可选字母表

    in_file 是要转换的文件的名称,最初您只是使用“files”调用 seqIO.convert。

    所以你的流程功能应该是这样的:

    def process(filename):
        return seqIO.convert(filename, "nexus", filename + '.fa', "fasta", alphabet=IUPAC.ambiguous_dna)
    

    【讨论】:

    • 顺便说一下,如果 seqIO 没有返回任何东西(它只是转换一个文件,也许),你可以写 process(fullpath) 同时知道 len(files) 会告诉你有多少您处理的文件。
    • 帮助很大!但我仍然收到此错误:Traceback (most recent call last): File "nexus_to_fasta.psg", line 41, in &lt;module&gt; print process (fullpath) File "nexus_to_fasta.psg", line 35, in process return SeqIO.convert("files", "nexus", "files.fa", "fasta", alphabet= IUPAC.ambiguous_dna) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/SeqIO/__init__.py", line 899, in convert in_handle = open(in_file, "rU") IOError: [Errno 2] No such file or directory: 'files'
    • 那个错误来自你调用转换的方式;在查看 seqIO.convert 的文档后,我更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    相关资源
    最近更新 更多