-
Notifications
You must be signed in to change notification settings - Fork 4
/
Translation.py
executable file
·191 lines (178 loc) · 5.81 KB
/
Translation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#====================================================================
# Copyright (C)2016 William H. Majoros ([email protected]).
# This is OPEN SOURxCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
#====================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
from NgramIterator import NgramIterator
######################################################################
# Attributes:
# codon (class attribute) hash: codon -> amino acid
# complementMap (class attribute): hash: nuc -> revcomp(nuc)
# Class Methods:
# aaSeq=Translation.translate(nucSeq)
# revSeq=Translation.reverseComplement(nucSeq)
# hash=Translation.getFourfoldDegenerateCodons()
# Private methods:
# initCodonMap()
# initComplementMap()
# nucleotide=complement($nucleotide)
######################################################################
class Translation:
"""Translation provides routines for translating or reverse-
complementing nucleotide sequences
"""
codon={}
complementMap={}
@classmethod
def translate(cls,transcript):
translation=""
L=len(transcript)
for i in range(0,L-3,3):
three=transcript[i:i+3]
residue=cls.codon[three]
if(not residue): residue="X"
translation+=residue
return translation
@classmethod
def reverseComplement(cls,seq):
L=len(seq)
buffer=""
for i in range(0,L):
buffer+=cls.complement(seq[L-i-1:L-i])
return buffer
@classmethod
def complement(cls,c):
if(cls.complementMap.get(c,None) is None): cls.complementMap[c]="N";
return cls.complementMap[c]
@classmethod
def initComplementMap(cls):
cls.complementMap['A']='T'
cls.complementMap['T']='A'
cls.complementMap['G']='C'
cls.complementMap['C']='G'
cls.complementMap['R']='Y'
cls.complementMap['Y']='R'
cls.complementMap['-']='-' # for gaps in alignments
cls.complementMap['N']='N' ### <------DEBUGGING!
@classmethod
def getFourfoldDegenerateCodons(cls):
codonMap=cls.codon
alphabet="ACGT"
degenerate=set()
ngramIterator=NgramIterator(alphabet,2)
while(True):
pair=ngramIterator.nextString()
if(pair is None): break
acids=set(); codons=set()
for third in alphabet:
codon=pair+third
acid=codonMap[codon]
acids.add(acid)
codons.add(codon)
if(len(acids)>1): continue
for codon in codons: degenerate.add(codon)
return degenerate
@classmethod
def initCodonMap(cls):
cls.codon["GTT"]='V'
cls.codon["GTC"]='V'
cls.codon["GTA"]='V'
cls.codon["GTG"]='V'
cls.codon["GTR"]='V'
cls.codon["GTY"]='V'
cls.codon["GTN"]='V'
cls.codon["GCT"]='A'
cls.codon["GCC"]='A'
cls.codon["GCA"]='A'
cls.codon["GCG"]='A'
cls.codon["GCR"]='A'
cls.codon["GCY"]='A'
cls.codon["GCN"]='A'
cls.codon["GAT"]='D'
cls.codon["GAC"]='D'
cls.codon["GAY"]='D'
cls.codon["GAA"]='E'
cls.codon["GAG"]='E'
cls.codon["GAR"]='E'
cls.codon["GGT"]='G'
cls.codon["GGC"]='G'
cls.codon["GGA"]='G'
cls.codon["GGG"]='G'
cls.codon["GGR"]='G'
cls.codon["GGY"]='G'
cls.codon["GGN"]='G'
cls.codon["TTT"]='F'
cls.codon["TTC"]='F'
cls.codon["TTY"]='F'
cls.codon["TTA"]='L'
cls.codon["TTG"]='L'
cls.codon["TTR"]='L'
cls.codon["CTT"]='L'
cls.codon["CTC"]='L'
cls.codon["CTA"]='L'
cls.codon["CTG"]='L'
cls.codon["CTN"]='L'
cls.codon["YTR"]='L'
cls.codon["TCT"]='S'
cls.codon["TCC"]='S'
cls.codon["TCA"]='S'
cls.codon["TCG"]='S'
cls.codon["TCN"]='S'
cls.codon["AGT"]='S'
cls.codon["AGC"]='S'
cls.codon["AGY"]='S'
cls.codon["TAT"]='Y'
cls.codon["TAC"]='Y'
cls.codon["TAY"]='Y'
cls.codon["TAA"]='*'
cls.codon["TAG"]='*'
cls.codon["TAR"]='*'
cls.codon["TAG"]='*'
cls.codon["TGT"]='C'
cls.codon["TGC"]='C'
cls.codon["TGY"]='C'
cls.codon["TGA"]='*'
cls.codon["TGG"]='W'
cls.codon["CCT"]='P'
cls.codon["CCC"]='P'
cls.codon["CCA"]='P'
cls.codon["CCG"]='P'
cls.codon["CCR"]='P'
cls.codon["CCY"]='P'
cls.codon["CCN"]='P'
cls.codon["CAT"]='H'
cls.codon["CAC"]='H'
cls.codon["CAY"]='H'
cls.codon["CAA"]='Q'
cls.codon["CAG"]='Q'
cls.codon["CAR"]='Q'
cls.codon["CGT"]='R'
cls.codon["CGC"]='R'
cls.codon["CGA"]='R'
cls.codon["CGG"]='R'
cls.codon["CGN"]='R'
cls.codon["ATT"]='I'
cls.codon["ATC"]='I'
cls.codon["ATA"]='I'
cls.codon["ATH"]='I'
cls.codon["ATG"]='M'
cls.codon["ACT"]='T'
cls.codon["ACC"]='T'
cls.codon["ACA"]='T'
cls.codon["ACG"]='T'
cls.codon["ACN"]='T'
cls.codon["AAT"]='N'
cls.codon["AAC"]='N'
cls.codon["AAY"]='N'
cls.codon["AAA"]='K'
cls.codon["AAG"]='K'
cls.codon["AAR"]='K'
cls.codon["AGG"]='R'
cls.codon["AGA"]='R'
cls.codon["AGY"]='R'
Translation.initCodonMap()
Translation.initComplementMap();