-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrna.py
286 lines (232 loc) · 7.71 KB
/
rna.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import webbrowser
import numpy as np
import matplotlib.pyplot as plt
import math
class Molecule:
"""RNA molecule class."""
def __init__(self, seq, dot=None):
"""Make RNA molecule.
Args:
seq: String that contains RNA sequence.
dot: Bracket notation of the secondary structure.
"""
if not set(seq) <= set('AGCU'):
raise Exception("Wrong RNA sequence - only A,G,C,U nucleobases are allowed.")
if dot:
if dot.count('(') != dot.count(')'):
raise Exception("Wrong bracket notation.")
self.__seq = seq
self.__dot = None
self.matrix = None
if dot:
self.__dot = dot
def __repr__(self):
return "\n{}\n{}\n".format(self.seq, self.dot)
def __str__(self):
return "{}\n{}".format(self.seq, self.dot)
def __eq__(self, other):
if isinstance(other, Molecule):
return self.seq == other.seq and self.dot == other.dot
else:
return False
def __ne__(self, other):
return (not self.__eq__(other))
def __hash__(self):
return hash(self.__repr__())
def show(self):
"""Show molecule's secondary structure in the browser."""
if self.__dot:
webbrowser.open(
"http://nibiru.tbi.univie.ac.at/forna/forna.html?id=url/name&sequence={}&structure={}".format(
self.__seq,
self.__dot))
else:
raise Exception('Structure notation does not exist.')
@property
def seq(self):
"""String containing RNA sequence."""
return self.__seq
@seq.setter
def seq(self, seq):
self.__seq = seq
@property
def dot(self):
"""Bracket notation of the secondary structure."""
return self.__dot
@dot.setter
def dot(self, dot):
self.__dot = dot
def get_substrings(self, length):
"""Get all substrings having correct structure of given length.
Args:
length: Length of returned substrings.
Returns:
valid: List of valid substrings.
"""
if self.dot is None:
raise Exception("There is no structure given for this molecule.")
else:
valid = []
for i in range(len(self.seq) - length + 1):
substring = self.seq[i:i + length]
subdot = self.dot[i:i + length]
ctr = 0
for j in range(length):
if subdot[j] == '(':
ctr += 1
if subdot[j] == ')':
ctr -= 1
if ctr < 0:
break
if ctr == 0:
valid.append(Molecule(substring, subdot))
return valid
def repair(self):
"""Repair the secondary structure by removing invalid pairs and sharp loops.
Returns:
self: Molecule object (repaired in place).
"""
self.dot = self.dot.replace('()', '..').replace('(.)', '...').replace('(..)', '....').replace('(...)', '.....')
self.matrix = pair_matrix(self)
length = len(self.seq)
for x in range(length):
for y in range(x, length):
if self.matrix[x, y] == 1:
if not is_pair_allowed(self.seq[x], self.seq[y]):
self.dot = self.dot[:x] + '.' + self.dot[x + 1:y] + '.' + self.dot[y + 1:]
return self
def evaluate(self):
"""Evaluate the energy of Molecule.
Returns:
score: Score based on pairings and hairpin loops.
"""
self.matrix = pair_matrix(self)
score = 0
for x in range(len(self.seq)):
for y in range(x, len(self.seq)):
if self.matrix[x, y] == 1:
if abs(x - y) < 5:
score -= 7
if self.seq[x] == complementary(self.seq[y]):
score += 2
elif self.seq[x] == 'U' and self.seq[y] == 'G' or self.seq[x] == 'G' and self.seq[y] == 'U':
score += 1
else:
score -= 5
return score
def complementary(a):
"""Get complementary nucleobase.
Args:
a: One of four nucleobases ('A', 'G', 'C', 'U').
Returns:
b: Complementary base.
"""
a = a.upper()
if a == 'A':
return 'U'
if a == 'U':
return 'A'
if a == 'C':
return 'G'
if a == 'G':
return 'C'
raise Exception('The given letter is not a valid RNA base.')
def is_pair_allowed(a, b):
"""Check if the nucleobase pair is allowed.
Args:
a: First base.
b: Second base.
Returns:
allowed (bool): Information whether the pair is allowed or not.
"""
if a == complementary(b):
return True
if a == 'G' and b == 'U' or a == 'U' and b == 'G':
return True
return False
def encode_rna(x):
"""Encode RNA sequence as a list of integers.
Args:
x: RNA sequence.
Returns:
e: List containing encoded sequence.
"""
return [0 if y == 'A' else 1 if y == 'U' else 2 if y == 'G' else 3 for y in x]
def match_parentheses(dot, position):
"""Find matching parenthesis in bracket notation.
Args:
dot: Bracket notation.
position: Position where there is an opening parenthesis to match.
Returns:
i: Index of matching parenthesis (-1 if nothing was found).
"""
stack = 0
for i in range(position + 1, len(dot)):
if dot[i] == '(':
stack += 1
elif dot[i] == ')':
if stack == 0:
return i
else:
stack -= 1
return -1
def dot_reverse(dot):
"""Reverse bracket notation.
Args:
dot: Bracket notation.
Return:
reversed (string): Reversed bracket notation.
"""
return dot[::-1].replace('(', '/').replace(')', '(').replace('/', ')')
def pair_matrix(m, show=False):
"""Produce pair matrix for the given molecule.
Args:
m: Molecule object.
show (bool): Make a matrix plot of the result.
Returns:
p: Pair matrix.
"""
l = len(m.seq)
p = np.zeros((l, l))
dot = m.dot
for begin in range(l):
if dot[begin] == '(':
end = match_parentheses(dot, begin)
p[begin, end] = p[end, begin] = 1
if show:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.matshow(p, cmap=plt.cm.gray, interpolation='nearest')
ax.set_xticks(np.arange(l))
ax.set_yticks(np.arange(l))
ax.set_xticklabels([i for i in m.seq])
ax.set_yticklabels([i for i in m.seq])
plt.show()
return p
def complementarity_matrix(m, show=False):
"""Produce complementarity matrix for the given molecule.
Complementary bases (according to Watson-Crick) are assigned 2, and G-U pair are assigned 1.
Args:
m: Molecule object.
show (bool): Make a matrix plot of the result.
Returns:
p: Complementarity matrix.
"""
l = len(m.seq)
p = np.zeros((l, l))
for i in range(l):
for j in range(l):
if m.seq[i] == complementary(m.seq[j]):
p[i, j] = 2
if m.seq[i] == 'G' and m.seq[j] == 'U' or m.seq[i] == 'U' and m.seq[j] == 'G':
p[i, j] = 1
if show:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.matshow(p, interpolation='nearest')
ax.set_xticks(np.arange(l))
ax.set_yticks(np.arange(l))
ax.set_xticklabels([i for i in m.seq])
ax.set_yticklabels([i for i in m.seq])
plt.show()
return p