-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreMatrix.py
67 lines (63 loc) · 1.82 KB
/
scoreMatrix.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
import amino
aminoListChr = amino.getOrderedArray()
class ScoreMatrix:
def __init__(self):
self.mat = []
for i in xrange(22):
r = []
for i in xrange(22):
r.append('u') #initialize with 'u' for uninitialized
self.mat.append(r)
self.indexLookup = {}
for i in xrange(len(aminoListChr)):
self.indexLookup[aminoListChr[i]] = i
def printMatrix(self):
for i in self.mat:
print i
def get(self, v1, v2):
#make sure v1p is always lower or equal to v2p
v1p = v1 if ord(v1)<ord(v2) else v2
v2p = v1 if ord(v1)>=ord(v2) else v2
try:
x = self.indexLookup[v1p]
y = self.indexLookup[v2p]
except KeyError:
raise Exception('RuntimeException','Amino acid not defined '+ v1 + ' or ' + v2)
if(self.mat[x][y] == 'u'):
raise Exception('RuntimeException','Uninitialized amino acid pair')
if(self.mat[x][y] == 'i'):
raise Exception('RuntimeException','Amino acid invalidated')
else:
return self.mat[x][y]
def set(self,v1,v2, score):
v1p = v1 if ord(v1)<ord(v2) else v2
v2p = v1 if ord(v1)>=ord(v2) else v2
try:
x = self.indexLookup[v1p]
y = self.indexLookup[v2p]
except KeyError:
raise Exception('RuntimeException','Amino acid not defined')
if(self.mat[x][y] == 'i'):
raise Exception('RuntimeException','Amino acid invalidated')
self.mat[x][y] = score
def setInvalid(self,v):
try:
x = self.indexLookup[v]
except KeyError:
raise Excpetion('RuntimeException', 'Amino acid not defined')
for i in xrange(len(aminoListChr)):
self.mat[i][x] = 'i'
for i in xrange(len(aminoListChr)):
self.mat[x][i] = 'i'
def fwrite(self, fname):
f = open(fname,'w')
for i in self.mat:
line = ""
for j in i:
line += str(j) + ","
f.write(line[0:-1] + '\n')
def fread(self, fname):
f = open(fname,'r')
self.mat = []
for i in f:
self.mat.append(i.strip().split(','))