Question: Attribute Error: 'Tuple' Object Has No Attribute 'Id' In Biopython
gravatar for jwolter1
7.9 years ago by
jwolter10 wrote:


I'm trying to write a simple script using Biopython (1.61 and Python 3.2 in windows)to go through a fasta file containing DNA sequences, translate the sequences in the three forward reading frames, and write the protein sequences to a new fasta file with the id's slightly modified.

I am getting a strange error where it appears a record object is somehow being treated as a tuple, which is breaking SeqIO.write() I thought I must somehow be doing the conversion accidentally, but don't see where.

Any help would be greatly appreciated.

Here is my code:

from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import generic_protein
def readSeqs(filenames):
    prots =[]
    for file in filenames:
        for seq_record in SeqIO.parse(file, "fasta"):
            for prot in [translateSeq(seq_record)]:
    return prots

def translateSeq(record):
    prot1 = SeqRecord(record.seq.translate(table="Yeast Mitochondrial"), RF
    prot2= SeqRecord(seq2.translate(table="Yeast Mitochondrial"),'RF2')#RF+1
    prot3 = SeqRecord(seq3.translate(table="Yeast Mitochondrial"),'RF3')#RF+2
    return prot1,prot2,prot3

def writeOutput(prots):
    filename = "ContigProteins.fasta" #change accordingly
    SeqIO.write(prots,filename, "fasta")
    print('file created')

def main():
    prots = readSeqs(['ContigCDS.fasta'])


and here is the error message:

Traceback (most recent call last):
  File "C:\pathtoscript", line 31, in <module>
  File "C:\pathtoscript", line 28, in main
  File "pathtoscript", line 23, in writeOutput
    SeqIO.write(prots,filename, "fasta")
  File "C:\Python32\lib\site-packages\Bio\SeqIO\", line 426, in write
    count = writer_class(fp).write_file(sequences)
  File "C:\Python32\lib\site-packages\Bio\SeqIO\", line 254, in write_file
    count = self.write_records(records)
  File "C:\Python32\lib\site-packages\Bio\SeqIO\", line 239, in write_records
  File "C:\Python32\lib\site-packages\Bio\SeqIO\", line 174, in write_record
    id =
AttributeError: 'tuple' object has no attribute 'id'
biopython • 7.3k views
ADD COMMENTlink modified 4.2 years ago by Biostar ♦♦ 20 • written 7.9 years ago by jwolter10
gravatar for Istvan Albert
7.9 years ago by
Istvan Albert ♦♦ 86k
University Park, USA
Istvan Albert ♦♦ 86k wrote:

change this

for prot in [translateSeq(seq_record)]:


for prot in translateSeq(seq_record):

in the former you are iterating on a list that has a single element: a tuple (triplet), whereas in the latter you are iterating on the tuple itself

better yet just bypass the loop and use

ADD COMMENTlink modified 7.9 years ago • written 7.9 years ago by Istvan Albert ♦♦ 86k

Thanks! That fixed it right away. I guess I should have tested that one line more thoroughly.

ADD REPLYlink written 7.9 years ago by jwolter10
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1618 users visited in the last hour