forked from hsalis/Ribosome-Binding-Site-Calculator-v1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViennaRNA.py
executable file
·343 lines (250 loc) · 10.7 KB
/
ViennaRNA.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#Python wrapper for the Vienna RNA Package by Andreas R. Gruber, Ronny Lorenz, Stephan H. Bernhart, Richard Neuböck, and Ivo L. Hofacker (NAR, 2008).
#This file is part of the Ribosome Binding Site Calculator.
#The Ribosome Binding Site Calculator is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#The Ribosome Binding Site Calculator is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with Ribosome Binding Site Calculator. If not, see <http://www.gnu.org/licenses/>.
#This Python wrapper is written by Howard Salis. Copyright 2008-2009 is owned by the University of California Regents. All rights reserved. :)
#Use at your own risk.
import os.path
import os, popen2, time
current_dir = os.path.dirname(os.path.abspath(__file__)) + "/tmp"
if not os.path.exists(current_dir): os.mkdir(current_dir)
debug=0
#Class that encapsulates all of the functions from NuPACK 2.0
class ViennaRNA(dict):
debug_mode = 0
RT = 0.61597 #gas constant times 310 Kelvin (in units of kcal/mol)
def __init__(self,Sequence_List,material = "rna37"):
self.ran = 0
import re
import random
import string
exp = re.compile('[ATGCU]',re.IGNORECASE)
for seq in Sequence_List:
if exp.match(seq) == None:
error_string = "Invalid letters found in inputted sequences. Only ATGCU allowed. \n Sequence is \"" + str(seq) + "\"."
raise ValueError(error_string)
self["sequences"] = Sequence_List
self["material"] = material
random.seed(time.time())
long_id = "".join([random.choice(string.letters + string.digits) for x in range(10)])
self.prefix = current_dir + "/temp_" + long_id
def mfe(self, strands,Temp = 37.0, dangles = "all",outputPS = False):
self["mfe_composition"] = strands
if Temp <= 0: raise ValueError("The specified temperature must be greater than zero.")
seq_string = "&".join(self["sequences"])
input_string = seq_string + "\n"
handle = open(self.prefix,"w")
handle.write(input_string)
handle.close()
#Set arguments
material = self["material"]
if dangles is "none":
dangles = " -d0 "
elif dangles is "some":
dangles = " -d1 "
elif dangles is "all":
dangles = " -d2 "
if outputPS:
outputPS_str = " "
else:
outputPS_str = " -noPS "
#Call ViennaRNA C programs
cmd = "RNAcofold"
args = outputPS_str + dangles + " < " + self.prefix
output = popen2.Popen3(cmd + args)
#output.tochild.write(input_string)
while output.poll() < 0:
try:
output.wait()
time.sleep(0.001)
except:
break
if debug == 1: print output.fromchild.read()
#Skip the unnecessary output lines
line = output.fromchild.readline()
line = output.fromchild.readline()
words = line.split(" ")
bracket_string = words[0]
(strands,bp_x, bp_y) = self.convert_bracket_to_numbered_pairs(bracket_string)
energy = float(words[len(words)-1].replace(")","").replace("(","").replace("\n",""))
self._cleanup()
self["program"] = "mfe"
self["mfe_basepairing_x"] = [bp_x]
self["mfe_basepairing_y"] = [bp_y]
self["mfe_energy"] = [energy]
self["totalnt"]=strands
#print "Minimum free energy secondary structure has been calculated."
def subopt(self, strands,energy_gap,Temp = 37.0, dangles = "all", outputPS = False):
self["subopt_composition"] = strands
if Temp <= 0: raise ValueError("The specified temperature must be greater than zero.")
seq_string = "&".join(self["sequences"])
input_string = seq_string + "\n"
handle = open(self.prefix,"w")
handle.write(input_string)
handle.close()
#Set arguments
material = self["material"]
if dangles is "none":
dangles = " -d0 "
elif dangles is "some":
dangles = " -d1 "
elif dangles is "all":
dangles = " -d2 "
if outputPS:
outputPS_str = " "
else:
outputPS_str = " -noPS "
#Call ViennaRNA C programs
cmd = "RNAsubopt"
args = " -e " + str(energy_gap) + outputPS_str + dangles + " < " + self.prefix
output = popen2.Popen3(cmd + args)
while output.poll() < 0:
try:
output.wait()
time.sleep(0.001)
except:
break
#print output.fromchild.read()
if debug == 1: print output.fromchild.read()
#Skip unnecessary line
line = output.fromchild.readline()
self["subopt_basepairing_x"] = []
self["subopt_basepairing_y"] = []
self["subopt_energy"] = []
self["totalnt"]=[]
counter=0
while len(line)>0:
line = output.fromchild.readline()
if len(line) > 0:
counter+=1
words = line.split(" ")
bracket_string = words[0]
energy = float(words[len(words)-1].replace("\n",""))
(strands,bp_x, bp_y) = self.convert_bracket_to_numbered_pairs(bracket_string)
self["subopt_energy"].append(energy)
self["subopt_basepairing_x"].append(bp_x)
self["subopt_basepairing_y"].append(bp_y)
self["subopt_NumStructs"] = counter
self._cleanup()
self["program"] = "subopt"
#print "Minimum free energy and suboptimal secondary structures have been calculated."
def energy(self, strands, base_pairing_x, base_pairing_y, Temp = 37.0, dangles = "all"):
self["energy_composition"] = strands
if Temp <= 0: raise ValueError("The specified temperature must be greater than zero.")
seq_string = "&".join(self["sequences"])
strands = [len(seq) for seq in self["sequences"]]
bracket_string = self.convert_numbered_pairs_to_bracket(strands,base_pairing_x,base_pairing_y)
input_string = seq_string + "\n" + bracket_string + "\n"
handle = open(self.prefix,"w")
handle.write(input_string)
handle.close()
#Set arguments
material = self["material"]
if dangles is "none":
dangles = " -d0 "
elif dangles is "some":
dangles = " -d1 "
elif dangles is "all":
dangles = " -d2 "
#Call ViennaRNA C programs
cmd = "RNAeval"
args = dangles + " < " + self.prefix
output = popen2.Popen3(cmd + args)
while output.poll() < 0:
try:
output.wait()
time.sleep(0.001)
except:
break
#if debug == 1: print output.fromchild.read()
self["energy_energy"] = []
#Skip the unnecessary output lines
line = output.fromchild.readline()
line = output.fromchild.readline()
words = line.split(" ")
energy = float(words[len(words)-1].replace("(","").replace(")","").replace("\n",""))
self["program"] = "energy"
self["energy_energy"].append(energy)
self["energy_basepairing_x"] = [base_pairing_x]
self["energy_basepairing_y"] = [base_pairing_y]
self._cleanup()
return energy
def convert_bracket_to_numbered_pairs(self,bracket_string):
all_seq_len = len(bracket_string)
bp_x = []
bp_y = []
strands = []
for y in range(bracket_string.count(")")):
bp_y.append([])
last_nt_x_list = []
counter=0
num_strands=0
for (pos,letter) in enumerate(bracket_string[:]):
if letter is ".":
counter += 1
elif letter is "(":
bp_x.append(pos-num_strands)
last_nt_x_list.append(pos-num_strands)
counter += 1
elif letter is ")":
nt_x = last_nt_x_list.pop()
nt_x_pos = bp_x.index(nt_x)
bp_y[nt_x_pos] = pos-num_strands
counter += 1
elif letter is "&":
strands.append(counter)
counter=0
num_strands+=1
else:
print "Error! Invalid character in bracket notation."
if len(last_nt_x_list) > 0:
print "Error! Leftover unpaired nucleotides when converting from bracket notation to numbered base pairs."
strands.append(counter)
bp_x = [pos+1 for pos in bp_x[:]] #Shift so that 1st position is 1
bp_y = [pos+1 for pos in bp_y[:]] #Shift so that 1st position is 1
return (strands,bp_x, bp_y)
def convert_numbered_pairs_to_bracket(self,strands,bp_x,bp_y):
bp_x = [pos-1 for pos in bp_x[:]] #Shift so that 1st position is 0
bp_y = [pos-1 for pos in bp_y[:]] #Shift so that 1st position is 0
bracket_notation = []
counter=0
for (strand_number,seq_len) in enumerate(strands):
if strand_number > 0: bracket_notation.append("&")
for pos in range(counter,seq_len+counter):
if pos in bp_x:
bracket_notation.append("(")
elif pos in bp_y:
bracket_notation.append(")")
else:
bracket_notation.append(".")
counter+=seq_len
return "".join(bracket_notation)
def _cleanup(self):
if os.path.exists(self.prefix): os.remove(self.prefix)
return
if __name__ == "__main__":
sequences = ["AGGGGGGATCTCCCCCCAAAAAATAAGAGGTACACATGACTAAAACTTTCAAAGGCTCAGTATTCCCACT"] #,"acctcctta"]
test = ViennaRNA(sequences,material = "rna37")
test.mfe([1],Temp = 37.0, dangles = "all")
bp_x = test["mfe_basepairing_x"][0]
bp_y = test["mfe_basepairing_y"][0]
strands = test["totalnt"]
bracket_string = test.convert_numbered_pairs_to_bracket(strands,bp_x,bp_y)
print bracket_string
(strands,bp_x, bp_y) = test.convert_bracket_to_numbered_pairs(bracket_string)
print "Strands = ", strands
print "bp_x = ", bp_x
print "bp_y = ", bp_y
print test.energy(strands, bp_x, bp_y, dangles = "all")
test.subopt(strands,3.5,dangles = "all")
print test
# print bracket_string
# print test.convert_numbered_pairs_to_bracket(strands,bp_x,bp_y)