user1882385
user1882385

Reputation: 169

Biopython translation script not working?

I'm new to Biopython and I simply want to translate a DNA Fasta file and write the output to a new file. I thought it would be easy but I can't get the script to work.
Here is my attempt:

#!/usr/bin/env python
import sys
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

in = open(sys.argv[1],'r')
out = open(sys.argv[2],'w')

messenger_rna = Seq(in, IUPAC.unambiguous_rna)
out = messenger_rna.translate()

out.close()
in.close()

Anyone know what I'm doing wrong?

Upvotes: 0

Views: 464

Answers (1)

glglgl
glglgl

Reputation: 91119

While I don't know what all this etc. is, I am quite sure that you treat the files wrong.

Especially

out = open(sys.argv[2],'w')
out = messenger_rna.translate()
out.close()

seems wrong to me.

Replace the middle line with

out.write(messenger_rna.translate())

if that is a string what .translate() returns.

Further optimized version:

#!/usr/bin/env python

import sys
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

with open(sys.argv[1],'r') as infile: # for auto-closing
    messenger_rna = Seq(infile, IUPAC.unambiguous_rna)
    # if Seq() takes a string rather than a file, do infile.read() instead.

with open(sys.argv[2],'w') as outfile:
    outfile.write(messenger_rna.translate())

Upvotes: 3

Related Questions