-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCUTable.py
More file actions
112 lines (88 loc) · 3.18 KB
/
CUTable.py
File metadata and controls
112 lines (88 loc) · 3.18 KB
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
'''
Created on 08.02.2015
@author: david
'''
from Bio.Data.CodonTable import standard_dna_table
from Bio.Seq import Seq
class CUTable(object):
'''
classdocs
'''
def __init__(self, SPSUM_LABEL, spsum, code=standard_dna_table):
'''
Constructor
SPSUM_LABEL is a string of whitespace separated codons
spsusm is a string of whitespace separated numbers(frequencies)
as present in the .spsum files of Kazusa
Code is a Biopython codon-table object (TODO?: only standard at the moment)
'''
# codon >> aa map
self.codon2AA = dict()
# aa >> (codon, freq) list
self.aa2CodonAndFreq = dict()
labels = SPSUM_LABEL.split(" ")
freqs = spsum.split(" ")
for i in range(len(labels)):
## RNA labels to DNA labels
lt = Seq(labels[i]).back_transcribe()
labels[i] = str(lt)
## convert frequencies to ints
freqs[i] = int(freqs[i])
#print(code.forward_table)
for i in range(len(labels)):
if labels[i] in code.stop_codons:
self.codon2AA[labels[i]] = 'STOP'
else:
self.codon2AA[labels[i]] = code.forward_table[labels[i]]
tAA = self.codon2AA[labels[i]]
if not self.aa2CodonAndFreq.get(tAA):
self.aa2CodonAndFreq[tAA] = list()
self.aa2CodonAndFreq[tAA].append((labels[i], freqs[i]))
#print(self.codon2AA)
#print(self.aa2CodonAndFreq)
def print(self):
'''
print a (relatively) readable vesion of the CUTable to stdout
'''
for k, v in self.aa2CodonAndFreq.items():
print(k + ":")
for cd, fr in self.getCodonsForAA(k):
print("\t" + cd + ": " + str(fr))
def getAAForCodon(self, codon):
'''
get the AA corresponding to codon
'''
return self.codon2AA[codon]
def getCodonRelativeUsage(self, codon):
aa = self.codon2AA[codon]
for cd, fr in self.getCodonsForAARelative(aa):
if cd == codon:
return fr
def getCodonUsage(self, codon):
aa = self.codon2AA[codon]
for cd, fr in self.getCodonsForAA(aa):
if cd == codon:
return fr
def getCodonsForAA(self, aa):
'''
get a list of (codon, rel. usage) for all codons coding for aa
'''
res = list()
sumFr = 0
for cd, fr in self.aa2CodonAndFreq[aa]:
sumFr += fr
for cd, fr in self.aa2CodonAndFreq[aa]:
res.append((cd, fr/sumFr))
return res
def getCodonsForAARelative(self, aa):
'''
get a list of (codon, rel. usage %of max) for all codons coding for aa
'''
res = list()
maxFr = 0
for cd, fr in self.aa2CodonAndFreq[aa]:
if fr > maxFr:
maxFr = fr
for cd, fr in self.aa2CodonAndFreq[aa]:
res.append((cd, fr/maxFr))
return res