This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to write all the possible translation into a new fasta file?

Dear all, This is my first time working with Biopython. I tried to translate unambigous rna sequences from a multi-fasta format file (containing 5 different random sequences) with 3 frame shifts (= meaning getting all possible translation of the RNA sequence). I got this part with the help of Biopython Tutorial and Cook book pdf . “onlinehttp://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc35”
Then I tried to write all the possible translation into a new file. And here I am, stuck. What I already tried: 1) Using Bio.SeqRecord I get only 5 translations of the 5 different RNA sequences. So I don’t get the other 2 frame shifted translation sequences under the original one.

2) And I even tried it with records but then I get 3 sequences only translated. But these are from the last sequence (= means 5th sequence from fasta file) of the random RNA with all 3 possible translation of RNA.

I did not find any other method that could write my translated sequences into a new fasta file. Please help I have tried everything. That means that I have even tried to write all the sequences with hand (see site: writing sequence files) even this attempt was not successful because every translated frame was written in one single line. So I got 5 lines, with 1 line containing all possible translations. What I am trying to achieve is to put all the translated sequences into a fasta file. This means there should be in total 15 sequences.

all possible translation in frame bio.seqrecord

It'd be helpful to post some code snippets to illustrate what you'd tried. Without checking into biopython's ability to translate 3 frames directly from the interface, one approach can be rec.seq.translate(), rec.seq[1:].translate(), and rec.seq[2:].translate().

I already tried that but then also I would only get 5 translated sequences that are in the frame. The other 2 translations are not there.

from Bio.SeqRecord import SeqRecord
def make_protein_record(nuc_record):
    """Returns a new SeqRecord with the translated sequence (default table)."""
    return SeqRecord(seq = nuc_record.seq.translate(cds=False), \
                     id = "trans_" + nuc_record.id, \
                     description = "translation of proteins, unambiguous RNA seq")

# so the function return "SeqRecord(seq = nuc_record.seq.translate(cds=False),\" giives in new fasta file 
#only the first 5 translated proteins. 
#But when I add a new code like this :r
        return SeqRecord(seq = seq_record.seq.translate(), \
                      seq1 = seq_record.seq[1:].translate(), \
                       seq2 = seq_record.seq[2:].translate(), \
                       id = "rc_" + record.id, \
                       description = "translation of protein, unambiguous RNA seq")

This code will give: TypeError: __init__() got an unexpected keyword argument 'seq1'

Please use the formatting bar (especially the code option) to present your post better. I've done it for you this time.
code_formatting

Thank you!

2 answers

Assuming

[/scratch/tmp/biostars/biopython_translate_allframes]$ cat test.fa 
>a
agctagctagc
>b
gctagctgctag
>c
gatcgatcgatcga
>d
gctgctagctagct
>e
gctagctagctag

Something along this should work fine.

with open('test.translated.fa', 'w') as fout:
  for rec in SeqIO.parse('test.fa', 'fasta'):
    for frame in range(0,3):
      SeqIO.write(SeqRecord(seq=rec[frame:].seq.translate(), id=rec.id, description=str(frame)), fout, 'fasta')
from Bio import SeqIO
for record in SeqIO.parse("rna_1.fasta", "fasta"):
    printrecord.id)
    print(record.seq.translate())
    print(record.seq[1:].translate())
    print(record.seq[2:].translate())

BioPython’s SeqUtils offers a six_frame_translations method, I’d advise using this. Eric’s solution, while neat, probably isn’t really the ‘pythonic’ way - you’d be getting your hands unnecessarily dirty ;)

http://biopython.org/DIST/docs/api/Bio.SeqUtils-module.html

Nice! I knew there must be a way to translate 6-way within the module.

Log in to answer this question.