回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在将PDB蛋白质序列片段写入fasta格式,如下所示</p>
<pre><code>from Bio.SeqIO import PdbIO, FastaIO
def get_fasta(pdb_file, fasta_file, transfer_ids=None):
fasta_writer = FastaIO.FastaWriter(fasta_file)
fasta_writer.write_header()
for rec in PdbIO.PdbSeqresIterator(pdb_file):
if len(rec.seq) == 0:
continue
if transfer_ids is not None and rec.id not in transfer_ids:
continue
print(rec.id, rec.seq, len(rec.seq))
fasta_writer.write_record(rec)
get_fasta(open('pdb1tup.ent'), open('1tup.fasta', 'w'), transfer_ids=['1TUP:B'])
get_fasta(open('pdb1olg.ent'), open('1olg.fasta', 'w'), transfer_ids=['1OLG:B'])
get_fasta(open('pdb1ycq.ent'), open('1ycq.fasta', 'w'), transfer_ids=['1YCQ:B'])
</code></pre>
<p>它给出了以下错误</p>
<pre><code>AttributeError Traceback (most recent call last)
<ipython-input-9-8ecf92753ac9> in <module>
12 fasta_writer.write_record(rec)
13
---> 14 get_fasta(open('pdb1tup.ent'), open('1tup.fasta', 'w'), transfer_ids=['1TUP:B'])
15 get_fasta(open('pdb1olg.ent'), open('1olg.fasta', 'w'), transfer_ids=['1OLG:B'])
16 get_fasta(open('pdb1ycq.ent'), open('1ycq.fasta', 'w'), transfer_ids=['1YCQ:B'])
<ipython-input-9-8ecf92753ac9> in get_fasta(pdb_file, fasta_file, transfer_ids)
10 continue
11 print(rec.id, rec.seq, len(rec.seq))
---> 12 fasta_writer.write_record(rec)
13
14 get_fasta(open('pdb1tup.ent'), open('1tup.fasta', 'w'), transfer_ids=['1TUP:B'])
~/anaconda3/envs/bioinformatics/lib/python3.7/site-packages/Bio/SeqIO/FastaIO.py in write_record(self, record)
303 def write_record(self, record):
304 """Write a single Fasta record to the file."""
--> 305 assert self._header_written
306 assert not self._footer_written
307 self._record_written = True
AttributeError: 'FastaWriter' object has no attribute '_header_written'
</code></pre>
<p>我四处搜索并选中了<a href="https://stackoverflow.com/questions/60144261/attributeerror-list-object-has-no-attribute-seqrecord-while-trying-to-sli">this</a>、<a href="https://stackoverflow.com/questions/51488228/attributeerror-str-object-has-no-attribute-id-using-biopython-parsing-fast">this</a>和<a href="https://stackoverflow.com/questions/26646925/error-while-writing-fasta-file-using-biopython">this</a>,但无法解决问题。
完整的代码是<a href="https://github.com/PacktPublishing/Bioinformatics-with-Python-Cookbook-Second-Edition/blob/master/Chapter07/PDB.ipynb" rel="nofollow noreferrer">here</a>,问题在最后一个单元格中</p>
<p><strong>编辑:</strong>我正在使用</p>
<pre><code>conda version : 4.8.3
conda-build version : 3.18.11
python version : 3.7.6.final.0
biopython version : 1.77.dev0
</code></pre>