我想使用 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'
我错过了什么?
这是我的代码:
##!/usr/bin/env python
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/test"
files = os.listdir(os.curdir)
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 files in os.listdir(test):
for file in files:
fullpath = os.path.join(file)
print(process(fullpath))
最佳答案
这段代码应该可以解决我能看到的大多数问题。
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))
我改变了一些事情。首先,我订购了您的进口商品(个人元素)并确保进口 IUPAC
来自Bio.Alphabet
所以你实际上可以为你的序列分配正确的字母表。接下来,在您的process()
中函数中,我添加了一行将扩展名从文件名中分离出来,然后使用完整的文件名作为第一个参数,仅使用基本文件名(不带扩展名)来命名 Nexus 输出文件。说到这里,我假设您将使用 Nexus
后面的代码中的模块?如果没有,您应该将其从导入中删除。
我不确定最后一个片段的意义是什么,所以我没有包含它。不过,在其中,您似乎正在遍历文件树和 process()
再次读取每个文件,然后引用一些名为 count
的 undefined variable 。相反,只需运行 process()
一次,做任何事count
指的是该循环内。
您可能需要考虑向 for
添加一些逻辑循环测试 os.path.join()
返回的文件实际上是一个FASTA文件。否则,如果任何其他文件类型位于您搜索的目录之一中,则 process()
它,各种奇怪的事情都可能发生。
编辑
好的,根据您的新代码,我有一些建议。一、线路
files = os.listdir(os.curdir)
完全没有必要,如下process()
的定义函数,您正在重新定义 files
多变的。此外,上面的行将会失败,因为您没有调用 os.curdir()
,您只需将其引用传递给 os.listdir()
.
底部的代码应该是这样的:
for file in os.listdir(test):
print(process(file))
for file in files
是多余的,调用 os.path.join()
使用单个参数不会执行任何操作。
关于python - 使用 Biopython 将多个 FASTA 文件转换为 Nexus 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38547418/