biopython的seqio.write()中的“invalid sequence”错误

时间:2017-07-11 16:30:05

标签: biopython

这个问题与生物信息学有关。我没有收到相应论坛的任何建议,所以我在这里写。

我需要删除fasta文件中的非ACTG核苷酸,并使用biopython中的seqio将输出写入新文件。

我的代码是

import re
import sys
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC


seq_list=[]
for seq_record in SeqIO.parse("test.fasta", "fasta",IUPAC.ambiguous_dna):
        sequence=seq_record.seq
        sequence=sequence.tomutable()
        seq_record.seq = re.sub('[^GATC]',"",str(sequence).upper())
        seq_list.append(seq_record)
SeqIO.write(seq_list,"test_out","fasta")

运行此代码会出错:

Traceback (most recent call last):
  File "remove.py", line 18, in <module>
    SeqIO.write(list,"test_out","fasta")
  File "/home/ghovhannisyan/Software/anaconda2/lib/python2.7/site-packages/Bio/SeqIO/__init__.py", line 481, in write
    count = writer_class(fp).write_file(sequences)
  File "/home/ghovhannisyan/Software/anaconda2/lib/python2.7/site-packages    /Bio/SeqIO/Interfaces.py", line 209, in write_file
    count = self.write_records(records)
  File "/home/ghovhannisyan/Software/anaconda2/lib/python2.7/site-packages/Bio/SeqIO/Interfaces.py", line 194, in write_records
    self.write_record(record)
  File "/home/ghovhannisyan/Software/anaconda2/lib/python2.7/site-packages/Bio/SeqIO/FastaIO.py", line 202, in write_record
    data = self._get_seq_string(record)  # Catches sequence being None
  File "/home/ghovhannisyan/Software/anaconda2/lib/python2.7/site-packages/Bio/SeqIO/Interfaces.py", line 100, in _get_seq_string
% record.id)
 TypeError: SeqRecord (id=CALB_TCONS_00001015) has an invalid sequence.

如果我更改此行

seq_record.seq = re.sub('[^GATC]',"",str(sequence).upper())

例如seq_record.seq = sequence + "A"一切正常。但是,re.sub('[^GATC]',"",str(sequence).upper())也应该在理论上起作用。

谢谢

1 个答案:

答案 0 :(得分:2)

Biopython的SeqIO期望SeqRecord对象的.seq是Seq对象(或类似对象),而不是普通字符串。尝试:

seq_record.seq = Seq(re.sub('[^GATC]',"",str(sequence).upper()))

对于FASTA输出,无需设置序列的字母表。