This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Attribute Error: 'Tuple' Object Has No Attribute 'Id' In Biopython

Hello,

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)]:
                prots.append(prot)
    return prots

def translateSeq(record):
    prot1 = SeqRecord(record.seq.translate(table="Yeast Mitochondrial"),record.id)#normal RF
    seq2=Seq(str(record.seq)[1:])
    prot2= SeqRecord(seq2.translate(table="Yeast Mitochondrial"),record.id+'RF2')#RF+1
    seq3=Seq(str(record.seq)[2:])
    prot3 = SeqRecord(seq3.translate(table="Yeast Mitochondrial"),record.id+'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'])
    writeOutput(prots)


main()

and here is the error message:

Traceback (most recent call last):
  File "C:\pathtoscript", line 31, in <module>
    main()
  File "C:\pathtoscript", line 28, in main
    writeOutput(prots)
  File "pathtoscript", line 23, in writeOutput
    SeqIO.write(prots,filename, "fasta")
  File "C:\Python32\lib\site-packages\Bio\SeqIO\__init__.py", line 426, in write
    count = writer_class(fp).write_file(sequences)
  File "C:\Python32\lib\site-packages\Bio\SeqIO\Interfaces.py", line 254, in write_file
    count = self.write_records(records)
  File "C:\Python32\lib\site-packages\Bio\SeqIO\Interfaces.py", line 239, in write_records
    self.write_record(record)
  File "C:\Python32\lib\site-packages\Bio\SeqIO\FastaIO.py", line 174, in write_record
    id = self.cleanrecord.id)
AttributeError: 'tuple' object has no attribute 'id'
biopython

1 answer

change this

for prot in [translateSeq(seq_record)]:
                prots.append(prot)

to

for prot in translateSeq(seq_record):
                prots.append(prot)

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

prots.extend(translateSeq(seq_record))

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

Log in to answer this question.