By Abrar


2020-05-22 23:49:58 8 Comments

I am writing the PDB protein sequence fragment to fasta format as below.

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'])

It gives the following error

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'

I search around and checked this, this, and this but could not resolve the issue. The complete code is here where the issue is in last cell.

Edit: I am using

conda version : 4.8.3
conda-build version : 3.18.11
python version : 3.7.6.final.0
biopython version : 1.77.dev0 

0 comments

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Importing files from different folder

17 Answered Questions

[SOLVED] Extract file name from path, no matter what the os/path format

  • 2011-12-05 11:39:49
  • BuZz
  • 1123906 View
  • 792 Score
  • 17 Answer
  • Tags:   python

12 Answered Questions

1 Answered Questions

[SOLVED] PDB BioPython- extracting the coordinates

  • 2016-10-17 09:23:18
  • User
  • 842 View
  • 0 Score
  • 1 Answer
  • Tags:   python biopython

1 Answered Questions

[SOLVED] BioPython iterating through sequences from fasta file

  • 2018-04-06 00:41:10
  • Jimmy Perrott
  • 542 View
  • 0 Score
  • 1 Answer
  • Tags:   python biopython

1 Answered Questions

[SOLVED] Estimate Alphabet in Biopython from fasta file

2 Answered Questions

[SOLVED] Biopython: How to get the compound name of the pdb file of a protein?

  • 2016-06-05 14:38:30
  • hobo
  • 229 View
  • 0 Score
  • 2 Answer
  • Tags:   biopython

1 Answered Questions

[SOLVED] Biopython PDB: get resseq

  • 2014-10-31 13:49:11
  • Andrea_C
  • 678 View
  • 0 Score
  • 1 Answer
  • Tags:   python biopython

3 Answered Questions

[SOLVED] renumber residues in a protein structure file (pdb)

  • 2011-05-12 19:54:58
  • Stylize
  • 4378 View
  • 5 Score
  • 3 Answer
  • Tags:   python biopython

Sponsored Content