【问题标题】:A biopython script to split a large fasta file into multiple ones一个 biopython 脚本,用于将一个大的 fasta 文件拆分为多个文件
【发布时间】: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 但我没有那样做。

标签: python biopython


【解决方案1】:

在阅读示例中的代码后,迭代器似乎没有根据 gene id 分隔文件,而只是将序列划分为 batch_size 组,因此在您的情况下为 93 个序列每个文件。

【讨论】:

  • 我就是这么想的。但作为这方面的新手,我认为由于每个基因都有一个序列,它会相应地将它们分开。该文件的排序方式是前 93 个序列对应于gene_1,接下来的 93 个序列对应于另一个gene.id,依此类推。无论如何我可以按照我想要的方式对它们进行分离吗?谢谢
  • @Ana 你可以,有几种方法可以做到这一点,不仅仅是一种。这取决于您是否已经知道所有基因的名称,或者您是否需要先获取它们。然后你可以根据你拥有的不同基因的数量多次解析文件(缓慢的过程),或者你可以想一个更聪明的方法。我建议您尝试这样做,如果您被阻止,请在此处或 biostars 中发布问题,并附上您尝试过的代码以及您遇到的错误或阻塞。
  • 我知道基因的名称,因为我之前修改过它们。我想有很多方法可以做到这一点,但我对使用 python 处理文件的知识并不多。
【解决方案2】:

以防将来有人对此脚本感兴趣。该脚本按原样完美运行。问题是我试图分割的文件有更多的序列。所以我删除了坏文件,并生成了一个与上述脚本完美分割的新文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多