annotate translate.py @ 0:a55eb751d2d8 draft

Uploaded
author jasper
date Tue, 09 May 2017 13:09:07 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
1 import sys
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
2 import warnings
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
3 import argparse
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
4 from Bio import SeqIO, SeqRecord, BiopythonWarning
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
5 from Bio.Seq import Seq
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
6 from Bio.Alphabet import IUPAC
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
7
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
8
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
9 warnings.simplefilter("ignore", BiopythonWarning)
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
10 parser = argparse.ArgumentParser(description="Translation")
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
11 parser.add_argument('-r', action='store', dest='read_frame', type=int)
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
12 parser.add_argument('-i', action='store', dest='input_file')
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
13 parser.add_argument('-o', action='store', dest='output_file')
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
14
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
15 inputs = parser.parse_args()
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
16 with open(inputs.output_file, 'w') as f:
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
17 for seq_record in SeqIO.parse(inputs.input_file, 'fasta'):
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
18 seqid = seq_record.id
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
19 name=seq_record.name
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
20 description = seq_record.description
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
21 if inputs.read_frame > 0:
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
22 seq_record = seq_record[(inputs.read_frame - 1):]
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
23 elif inputs.read_frame == -1:
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
24 seq_record = seq_record.reverse_complement()
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
25 elif inputs.read_frame == -2:
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
26 seq_record = seq_record.reverse_complement()
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
27 seq_record = seq_record[2:]
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
28 elif inputs.read_frame == -3:
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
29 seq_record = seq_record.reverse_complement()
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
30 seq_record = seq_record[1:]
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
31 dna = Seq(str(seq_record.seq), IUPAC.ambiguous_dna)
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
32 protein = SeqRecord.SeqRecord(seq=dna.translate(), id=seqid, name=name, description=description)
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
33 SeqIO.write(protein, f, 'fasta')
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
34 f.write('\n')
a55eb751d2d8 Uploaded
jasper
parents:
diff changeset
35 f.close()