【问题标题】:Changing the record id in a FASTA file using BioPython使用 BioPython 更改 FASTA 文件中的记录 ID
【发布时间】:2015-09-02 00:45:09
【问题描述】:

我有以下 FASTA 文件,original.fasta

>foo
GCTCACACATAGTTGATGCAGATGTTGAATTCACTATGAGGTGGGAGGATGTAGGGCCA

我需要把记录id从foo改成bar,所以我写了如下代码:

from Bio import SeqIO

original_file = r"path\to\original.fasta"
corrected_file = r"path\to\corrected.fasta"

with open(original_file) as original, open(corrected_file, 'w') as corrected:
    records = SeqIO.parse(original_file, 'fasta')
    for record in records:
        print record.id             # prints 'foo'
        if record.id == 'foo':
            record.id = 'bar'
        print record.id             # prints 'bar' as expected
        SeqIO.write(record, corrected, 'fasta')

我们打印了更改前后的记录id,得到了预期的结果。我们甚至可以通过 BioPython 再次读取更正后的文件并打印出记录 ID 来仔细检查:

with open(corrected_file) as corrected:
    for record in SeqIO.parse(corrected, 'fasta'):
        print record.id                  # prints 'bar', as expected

但是,如果我们在文本编辑器中打开更正后的文件,我们会看到记录 ID 不是 bar 而是 bar foo

>bar foo
GCTCACACATAGTTGATGCAGATGTTGAATTCACTATGAGGTGGGAGGATGTAGGGCCA

如果我们使用纯 Python 读取文件,我们可以确认这是写入文件的内容:

with open(corrected_file) as corrected:
    print corrected.readlines()[0][1:] # prints 'bar foo'

这是 BioPython 中的错误吗?如果没有,我做错了什么?如何使用 BioPython 更改 FASTA 文件中的记录 ID?

【问题讨论】:

  • fasta writer 打印 seq.id 和 seq.name 如果它们不同。如果您只想打印“bar”,则需要同时更改两者
  • record.description 包含foo ....
  • 我发现这个“如果你只更改描述,那么 fasta 文件包含旧 id 和新描述。如果旧 id 和新描述都更改相同,那么你得到你想要的效果。”在 (biostars.org/p/95095)
  • @heathobrien biopython print id and description ... name 从不打印
  • awk '/^>foo$/ { $1=">bar" }1' file.fasta >newfile.fasta

标签: bioinformatics biopython


【解决方案1】:

我找不到更好的解决方案(除了创建一个新的 SeqRecord),在我看来它看起来像一个错误

if record.id == 'foo':
  record.id, record.name = ('bar',)*2
  if record.description == 'foo':
    record.description = 'bar'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多