python - 使用 Biopython 将多序列 fasta 蛋白质文件拆分为多个文件
问题描述
def batch_iterator(iterator, batch_size) :
entry = True
while entry :
batch = []
while len(batch) < batch_size :
try :
entry = iterator.__next__
except StopIteration :
entry = None
if entry is None :
#End of file
break
batch.append(entry)
if batch :
yield batch
from Bio import SeqIO
record_iter = SeqIO.parse(open("C:\\Users\\IDEAPAD\Desktop\\fypsplit\\protein.fasta"),"fasta")
for i, batch in enumerate(batch_iterator(record_iter, 1000)):
filename = "group_%i.fasta" % (i + 1)
with open(filename, "w") as handle:
count = SeqIO.write(batch, handle, "fasta")
print("Wrote %i records to %s" % (count, filename))
我正在尝试使用 Biopython 拆分 fasta 文件。在这个例子中,我想让它像 7 个文件一样。但我得到一个错误阅读AttributeError: 'function' object has no attribute 'id'
。
有人能帮我吗?先感谢您
解决方案
在这一行中抛出 AttributeError
count = SeqIO.write(batch, handle, "fasta")
因为SeqIO.write
需要一个可迭代的或类型的列表SeqRecord
。但是,您batch_iterator
会生成一个方法列表。
为什么是方法?好吧,您在这里缺少一个函数调用:
entry = iterator.__next__
应该
entry = iterator.__next__()
这使得代码无错误地运行。
对于一个由 11 个序列组成的测试文件,我得到了以下结果——在将批量大小从 1000 更改为 4 以进行测试之后:
Wrote 4 records to group_1.fasta
Wrote 4 records to group_2.fasta
Wrote 3 records to group_3.fasta
推荐阅读
- c# - 如何创建对话系统
- javascript - javascript中的语法错误可能会破坏代码
- javascript - 单击外部时隐藏列表
- c# - 过滤字典字符串键,列出对象包含属性值的对象
- php - Laravel 登录页面什么都不做
- sapui5 - 如何在 sap.m.UploadCollection 中获取文件的文件路径或二进制内容
- ruby-on-rails - nil:NilClass 的未定义方法“class_detail”
- excel - 如果 Second 值大于 Value,则删除行
- c# - (c#) WinForm和uwp如何实现端口通信
- vba - 检查工作簿中多个单元格字符串值的值与另一个工作簿中的行的匹配