Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Biopython将多个FASTA文件转换为Nexus时出错_Python_Biopython_Fasta - Fatal编程技术网

使用Biopython将多个FASTA文件转换为Nexus时出错

使用Biopython将多个FASTA文件转换为Nexus时出错,python,biopython,fasta,Python,Biopython,Fasta,我想使用BIO.SeqIO模块将多个FASTA格式文件(DNA序列)转换为NEXUS格式,但出现以下错误: Traceback (most recent call last): File "fasta2nexus.py", line 28, in <module> print(process(fullpath)) File "fasta2nexus.py", line 23, in process alphabet=IUPAC.ambiguous_dna)

我想使用BIO.SeqIO模块将多个FASTA格式文件(DNA序列)转换为NEXUS格式,但出现以下错误:

Traceback (most recent call last):
  File "fasta2nexus.py", line 28, in <module>
    print(process(fullpath))
  File "fasta2nexus.py", line 23, in process
    alphabet=IUPAC.ambiguous_dna)
  File "/Library/Python/2.7/site-packages/Bio/SeqIO/__init__.py", line 1003, in convert
    with as_handle(in_file, in_mode) as in_handle:
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Library/Python/2.7/site-packages/Bio/File.py", line 88, in as_handle
    with open(handleish, mode, **kwargs) as fp:
IOError: [Errno 2] No such file or directory: 'c'
  • 名称错误
  • 您导入了SeqIO,但正在调用SeqIO.convert()。Python区分大小写。该行应为:

    return SeqIO.convert(filename + '.fa', "fasta", filename + '.nex', "nexus", alphabet=IUPAC.ambiguous_dna)
    
  • IOError:
    用于os.WACK(测试)中的文件:
  • 无法打开文件时引发IOError。这通常是因为提供的文件名和/或文件路径不存在

    os.walk(test)
    遍历路径
    test
    中的所有子目录。在每次迭代期间,
    文件
    将是3个元素的列表。第一个元素是目录的路径,第二个元素是该路径中的子目录列表,第三个元素是该路径中的文件列表。您应该将文件名传递给
    process()
    ,但您正在
    过程(文件)
    中传递列表

    对于os.walk(test)中的root、dirs和文件,您已经在这个块中正确地实现了它。。我建议您在下面的
    for
    循环中类似地实现它

  • 您正在将
    .fa
    添加到
    文件名中。不要添加
    .fa

  • 这段代码应该可以解决我所看到的大多数问题

    from __future__ import print_function # or just use Python 3!
    
    import fileinput
    import os
    import re
    import sys
    
    from Bio import SeqIO, Nexus
    from Bio.Alphabet import IUPAC
    
    test = "/Users/teton/Desktop"
    
    def process(filename):
        # retuns ("basename", "extension"), so [0] picks "basename"
        base = os.path.splitext(filename)[0] 
        return SeqIO.convert(filename, "fasta", 
                             base + ".nex", "nexus", 
                             alphabet=IUPAC.ambiguous_dna)
        
    for root, dirs, files in os.walk(test):
        for file in files:
            fullpath = os.path.join(root, file)
            print(process(fullpath))
    
    我改变了一些事情。首先,我订购了您的导入(个人物品),并确保从
    Bio.Alphabet
    导入
    IUPAC
    ,这样您就可以为序列分配正确的字母表。接下来,在您的
    process()
    函数中,我添加了一行,将扩展名从文件名中分离出来,然后使用完整的文件名作为第一个参数,仅使用基本文件名(不带扩展名)命名Nexus输出文件。说到这里,我假设您将在后面的代码中使用
    Nexus
    模块?如果没有,则应将其从导入中删除

    我不确定最后一个片段的要点是什么,所以我没有包括它。不过,在其中,您似乎正在遍历文件树,并再次对每个文件执行
    process()
    操作,然后引用一些名为
    count
    的未定义变量。相反,只需运行一次
    process()
    ,然后在该循环中执行
    count
    所指的任何操作

    您可能想考虑为Road添加一些代码,以测试<代码> OS.PATION.COUNTY()//COD>返回的文件实际上是一个FASTA文件。否则,如果任何其他文件类型在您搜索的目录中,并且您

    process()
    it,则可能会发生各种奇怪的事情

    编辑 好的,根据你的新代码,我有一些建议。首先是线路

    files = os.listdir(os.curdir)
    
    是完全没有必要的,正如下面定义的
    process()
    函数一样,您正在重新定义
    files
    变量。此外,上面的行将失败,因为您没有调用
    os.curdir()
    ,而是将其引用传递给
    os.listdir()

    底部的代码应该是这样的:

    for file in os.listdir(test):
        print(process(file))
    

    对于files-in-files
    是多余的,使用单个参数调用
    os.path.join()
    没有任何作用。

    谢谢。现在我犯了这个错误!返回SeqIO.convert(filename+'.fa',“fasta”,filename+'.nex',“nexus”,alphabet=IUPAC.dummessive_-dna)文件“/Library/Python/2.7/site packages/Bio/SeqIO/_u-init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.py”,第1003行,并使用as-handle(在_u文件中,在_u模式下)作为_uu-handle:File“/System/Library/Frameworks/framework,在输入return self.gen.next()File“/Library/Python/2.7/site packages/Bio/File.py”的第88行中,以fp:IOError:[Errno 2]的形式打开(handleish,mode,**kwargs)的as_句柄,我没有编辑过这样的文件或目录来回答您的评论。我建议你编辑你的问题,将你在评论中提供的信息包括在内,这样我的回答对以后遇到这个问题的人来说是有意义的。谢谢。我会的。事实上,我不需要子目录,只需要当前目录就可以了。现在我看到这样一个错误:没有这样的文件或目录:'/Users/teton/Desktop/bucky/cluster1.fa.fa'为什么它会假设一个额外的.fa?存在cluster1.fa。如果不需要在子目录中递归。看看这个。您之所以会出现此错误,是因为您执行了
    filename+'.fa'
    。不要添加
    .fa
    @Ramon,因为您的参数包括
    filename+“.fa”
    。请改为传递
    cluster1
    ,或者调整命名约定。请包含整个回溯,以便我们可以看到错误出现在哪一行。@Ramon请不要删除原始代码和问题,否则答案与问题不匹配。如果您更改了代码并出现新错误,只需在底部进行编辑。好的,很抱歉。谢谢!您可以只更改当前目录的代码吗。我得到了这个错误:文件“/Library/Python/2.7/site packages/Bio/AlignIO/uuu init_uuuuuuuuuuu.py”,第214行,在write count=writer_class(fp)中。write_File(alignments)File“/Library/Python/2.7/site packages/Bio/AlignIO/NexusIO.py”,第98行,在write_File self.write_alignment(first_“/Library/Python/2.7/site packages/Bio/AlignIO/NexusIO.py”,第105行,在write_alignment raise ValueError中(“必须至少有一个序列”)ValueError:必须至少有一个sequence@Ramon你是什么意思,只有当前目录?只要把你想扫描的目录放到
    测试中就行了。我做了,但它一直说:ValueError:必须至少有一个序列。我修改了代码:test=“/Users/teton/Desktop/bucky“bucky文件夹中有数百个文件。请查看您的
    .fa
    文件。它们是否至少有一个序列?我刚刚在一个FASTA文件上运行了
    SeqIO.convert()
    代码,我碰巧在其中有一个DNA序列,它返回了一个
    .nex
    文件,打印
    1
    (我猜序列的数量?)。请获取一些.fa
    for file in os.listdir(test):
        print(process(file))