【发布时间】:2017-09-05 09:09:57
【问题描述】:
我正在处理一个大型 fasta 文件,我想根据基因 ID 将其拆分为多个文件。我正在尝试使用 biopython 教程中的上述脚本:
def batch_iterator(iterator, batch_size):
"""Returns lists of length batch_size.
This can be used on any iterator, for example to batch up
SeqRecord objects from Bio.SeqIO.parse(...), or to batch
Alignment objects from Bio.AlignIO.parse(...), or simply
lines from a file handle.
This is a generator function, and it returns lists of the
entries from the supplied iterator. Each list will have
batch_size entries, although the final list may be shorter.
"""
entry = True # Make sure we loop once
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
record_iter=SeqIO.parse(open('/path/sorted_sequences.fa'), 'fasta')
for i, batch in enumerate (batch_iterator(record_iter, 93)):
filename='gene_%i.fasta' % (i + 1)
with open('/path/files/' + filename, 'w') as ouput_handle:
count=SeqIO.write(batch, ouput_handle, 'fasta')
print ('Wrote %i records to %s' % (count, filename))
它确实拆分了包含 93 个序列的文件,但它为每组 93 个文件提供了 2 个文件。我看不到错误,但我猜有一个。 还有另一种方法可以以不同的方式拆分大型 fasta 文件? 谢谢
【问题讨论】:
-
“它每组 93 个文件提供 2 个文件”是什么意思?
-
脚本生成重复文件,即 2 个文件,每个文件包含 93 个基因,每个文件都带有gene_1。我知道每个都有 93 个。所以在生成第一个 93 序列文件后,应该移动到下一个 93 但我没有那样做。