-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreMatrix.py
158 lines (126 loc) · 4.7 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
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
import numpy as np
class ScoreMatrix(object):
"""
ROLE: Create the score matrix of the 2 sequences
"""
def __init__(
self,
seq1="No",
seq2="Sequence",
pointsMatch = 2,
pointsMissmatchIntra = 1,
pointsMissmatchExtra = -1,
pointsOpeningGap = -10,
pointsExtensiveGap = -1
):
"""
ROLE: Constructor
PARAMETERS: seq1, string
seq2, string
pointsMatch, float
pointsMissmatchIntra, float
pointsMissmatchExtra, float
pointsOpeningGap, float
pointsExtensiveGap, float
OUTPUTS: seqLen1, int
seqLen2, int
"""
self.seq1 = seq1
self.seq2 = seq2
self.seqLen1 = len(seq1)
self.seqLen2 = len(seq2)
self.pointsMatch = pointsMatch
self.pointsMissmatchIntra = pointsMissmatchIntra
self.pointsMissmatchExtra = pointsMissmatchExtra
self.pointsOpeningGap = pointsOpeningGap
self.pointsExtensiveGap = pointsExtensiveGap
def init_matrix(self):
"""
ROLE: Build a numpy array of 1 + sequences lenght dimensions.
Init the first column and row with defaults integers values
Fill the rest with None
OUTPUTS: matrix, np.array
"""
self.matrix=np.array([[None]*(self.seqLen2+1)]*(self.seqLen1+1))
for i in range(self.seqLen2 + 1):
self.matrix[0][i] = -i - 10
for i in range(self.seqLen1 + 1):
self.matrix[i][0] = -i - 10
self.matrix[0][0] = 0
def __getMatch(self, i, j):
"""
ROLE: Get the score for a match
PARAMETERS: i, int
j, int
"""
score = 0
if self.seq1[i].lower() == self.seq2[j].lower():
score = self.pointsMatch + self.matrix[i][j]
elif (self.seq2[j].lower() == 'a' and self.seq1[i].lower() == 'g') \
or (self.seq2[j].lower() == 'g' and self.seq1[i].lower() == 'a') \
or (self.seq2[j].lower() == 'c' and self.seq1[i].lower() == 't') \
or (self.seq2[j].lower() == 't' and self.seq1[i].lower() == 'c') \
or (self.seq2[j].lower() == 'u' and self.seq1[i].lower() == 'c') \
or (self.seq2[j].lower() == 'c' and self.seq1[i].lower() == 'u'):
score = self.pointsMissmatchIntra + self.matrix[i][j]
else:
score = self.pointsMissmatchExtra + self.matrix[i][j]
self.diagonalScore = score
def __getGapUp(self, mtb, i, j):
"""
ROLE: Get the score for a gap in the sequence 2
PARAMETERS: mtb, TracebackMatrix
i, int
j, int
"""
score = 0
if i == 0:
score = self.matrix[1 + i - 1][1 + j] + self.pointsOpeningGap
else:
if mtb[1 + i - 1][1 + j] == '|':
score = self.matrix[1 + i - 1][1 + j] + self.pointsExtensiveGap
else:
score = self.matrix[1 + i - 1][1 + j] + self.pointsOpeningGap
self.upScore = score
def __getGapLeft(self, mtb, i, j):
"""
ROLE: Get the score for a gap in the sequence 1
PARAMETERS: mtb, TracebackMatrix
i, int
j, int
"""
score = 0
if j == 0:
score = self.matrix[1 + i][j] + self.pointsOpeningGap
else:
if mtb[1 + i][j] == '-': #symbole du gap (si gap a gauche -> extensif)
score = self.matrix[1 + i][j] + self.pointsExtensiveGap
else:
score = self.matrix[1 + i][j] + self.pointsOpeningGap
self.leftScore = score
def bestScore(self, mtb, i, j):
"""
ROLE: Get the best score between match, gap seq 2 and gap seq 1.
Set the orgin of the best score
PARAMETERS: mtb, TracebackMatrix
i, int
j, int
"""
self.__getMatch(i, j)
self.__getGapUp(mtb, i , j)
self.__getGapLeft(mtb, i , j)
if self.diagonalScore > self.upScore and self.diagonalScore > self.leftScore:
self.maxScoreOrigin = "*"
elif self.leftScore > self.diagonalScore and self.leftScore > self.upScore:
self.maxScoreOrigin = "-"
elif self.upScore > self.diagonalScore and self.upScore > self.leftScore:
self.maxScoreOrigin = "|"
elif self.diagonalScore == self.leftScore and self.diagonalScore > self.upScore:
self.maxScoreOrigin = "DG"
elif self.diagonalScore == self.upScore and self.diagonalScore > self.leftScore:
self.maxScoreOrigin = "DH"
elif self.leftScore == self.upScore and self.leftScore > self.diagonalScore:
self.maxScoreOrigin = "GH"
elif self.diagonalScore == self.leftScore and self.diagonalScore == self.upScore:
self.maxScoreOrigin = "3"
self.maxScore = max(self.diagonalScore, self.upScore, self.leftScore)