forked from alfredkam/corRna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.py
executable file
·417 lines (406 loc) · 12.7 KB
/
wrapper.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python
import sys
import re
import commands
import shutil
import getopt
import os, glob, sys
import os.path
import random, math
from multiprocessing import Process, Manager, Array, Value
from decimal import Decimal
from time import clock, time
from math import *
import Pearson
import smail
import BootStrap
DEBUG = False
MUTATION_DEPTH = 2
NSIZE = 10
SEQUENCE = ''
LOOP_SIZE = 10
BUCKETS = []
NODE = ''
Question_Marks = ''
AVG = float(0)
nAVG = float(0)
totalCORRELATION = 0
totalSEQUENCE = 0
totalTIME = 0
SIGNIFICANT = []
OUTPUT_DATA = ""
EMAILMSG = ""
def usage():
print "Displaying help:"
print "-s [sequence]"
print "-h display help file"
print "-m [x] mutation depth"
print "-n [x] number of samples"
print "-l [x] loop size"
print "-t [x] correlation threshold"
print "-u [url location]"
print "-d Debugging Mode"
print "-c CSV *note*, requires debugging mode to be enabled"
print "-a EMAIL"
print "sample usage:\n ./wrapper.py -m 2 -n 10 -l 10 -t 0.4 -s AGCGGGGGAGACAUAUAUCAUAGCCUGUCUCGUGCCCGACCCCGC"
def SCOUT(seq):
#!/bin/bash
if(DEBUG) :
if(re.match('AGCGGGGGAGACAUCUAUCACAGCCUGUCUCGUGCCCGACCCCGC', seq)):
print "A68C"
if re.match('AGCGGGGGAGACAUAUACCACAGCCUGUCUCGUGCCCGACCCCGC', seq):
print "U71C"
if re.match('AGCGGGGGAGACAUAUAUCAUAGCCUGUCUCGUGCCCGACCCCGC', seq):
print "C74U"
if re.match('AGCGGGGGAGACAUAUAUCACUCUCUGUCUCGUGCCCGACCCCGC', seq):
print "A75U_G76C_C77U"
if re.match('AGCGGGGGAGACAUAUAUCACAGUCUGUCUCGUGCCCGACCCCGC', seq):
print "C77U"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUAGGGCCCGACCCCGC', seq):
print "C84A_U86G"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUAGAGCCCGACCCCGC', seq):
print "C84A_U86A"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGGGCCCGACCCCGC', seq):
print "U86G"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCAGGCCCCGC', seq):
print "C90A_A92G"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCCGGCCCCGC', seq):
print "C92G"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCAGACCCCGC', seq):
print "C90A"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCCGUCCCCGC', seq):
print "A92U"
if re.match('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCCGCCCCCGC', seq):
print "A92C"
def getAvg():
"""
x = float(0.0)
x += P.getCorr('AGCGGGGGAGACAUCUAUCACAGCCUGUCUCGUGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUACCACAGCCUGUCUCGUGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCAUAGCCUGUCUCGUGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACUCUCUGUCUCGUGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGUCUGUCUCGUGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUAGGGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUAGAGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGGGCCCGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCAGGCCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCCGGCCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCAGACCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCCGUCCCCGC');
x += P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUCGUGCCCGCCCCCGC');
y = float(x)/float(13)
"""
global AVG
AVG = 0.814673348838
global nAVG
#nAVG = P.getCorr('AGCGGGGGAGACAUAUAUCACAGCCUGUCUAGGGCCCGACCCCGC');
#modified to use threshold from command line
if EMAIL:
global EMAILMSG
EMAILMSG = "<p>Under Avg: "+str(AVG)+" Threshold : "
if(nAVG < 0):
EMAILMSG+="none"
else:
EMAILMSG+=str(nAVG)
EMAILMSG+=" </p>"
if(DEBUG):
if not CSV:
print "<Under Avg> :: under "+str(AVG)+" across the 13 given confirmed sequence"
print "<Under Threshold> :: under "+str(nAVG)+" on A84_U86G or user input threshold"
else:
global OUTPUT_DATA
OUTPUT_DATA+='[{"method":"mutation","setAvg":"'+str(AVG)+'","setThres":"'+str(nAVG)+'","wildtype":"'+SEQUENCE+'","data":['
def getRNAfoldOptStruct(seq):
rant = random.randint(1,100)
rant_t = random.randint(1,100)
fname = str(rant)+'-'+str(rant_t)
f = open(fname+'.tmp','w')
f.write('> '+fname+'\n')
f.write(seq)
f.close()
out = commands.getoutput('cat '+fname+'.tmp @- | RNAfold -d0')
commands.getoutput('rm '+fname+'.tmp')
commands.getoutput('rm '+fname+'*.ps')
for line in out.splitlines():
if re.match("^[.|?]",line):
line = re.sub("\s+.*$", "", line)
return line
def initial():
node = ''
getAvg()
global Question_Marks
global NODE
global totalTIME
global totalCORRELATION
global totalSEQUENCE
global OUTPUT_DATA
sequenceCOUNTER = 0
sequenceCORRELATION = 0
for i in range(len(SEQUENCE)):
BUCKETS.append(int(0))
Question_Marks+='?'
node+="o"
#Question_Marks = '?((((((????????????????????????????????))))))'
NODE = node
start = time()
out = commands.getoutput('./RNAmutants -l lib -m '+str(MUTATION_DEPTH)+' -s '+ SEQUENCE +'-n '+str(NSIZE))
end = time() - start
totalTIME+=end
OUTPUT_DATA+='{"seq":['
bool_first = 0
underAvg = 0
underThres = 0
if EMAIL:
global EMAILMSG
EMAILMSG+="<table border = 0><thead><td>Sequence</td><td>Structure</td><td>Correlation</td><td>MFE</td><td>Under Threshold</td></thead><tbody>"
for line in out.splitlines():
if re.match('\w+', line):
if re.match("C|G|c|G|A|a|U|u", line):
mfe = line
mfe = re.sub('^\w+\s+\(',"",mfe)
mfe = re.sub('\).*$',"",mfe)
sequenceCOUNTER+=1
line = re.sub('\s+.*$',"",line)
struct = getRNAfoldOptStruct(line)
x = P.getCorr(line)
sequenceCORRELATION+=x
note = ''
if(AVG > x):
underAvg = 1
note = " <Under Avg>"
if(nAVG > x):
underThres = 1
SIGNIFICANT.append(line)
note+=" <Under Threshold>"
#print line +" "+str(x) + note
if EMAIL:
EMAILMSG+="<tr><td>"+str(line)+"</td><td>"+str(struct)+"</td><td>"+str(x)+"</td><td>"+str(mfe)+"</td><td>"
#if(AVG > x):
# EMAILMSG+="Y"
#EMAILMSG+="</td><td>"
if(nAVG > x):
EMAILMSG+="Y"
EMAILMSG+="</td></tr>"
if(DEBUG):
if(CSV):
print str(line) +","+struct+","+str(x)+","+str(mfe)
else:
print str(line) +","+struct+","+str(x)+","+str(mfe)+","+str(note)
##print '{0}, {0}, {1:.4f}, {1:.4f}, {2}'.format(line,struct,x,mfe,note)
SCOUT(line)
else:
if(bool_first == 0):
bool_first = 1
elif bool_first == 1:
OUTPUT_DATA+=','
OUTPUT_DATA+='{"seq":"'+str(line)+'", "struct":"'+str(struct)+'", "corr":"'+str(x)+'","mfe":"'+str(mfe)+'","avg":"'
if(underAvg == 1):
OUTPUT_DATA+='t'
underAvg = 0
else:
OUTPUT_DATA+='f'
OUTPUT_DATA+='","navg":"'
if underThres == 1:
OUTPUT_DATA+='t'
underThres = 0
else:
OUTPUT_DATA+='f'
OUTPUT_DATA+='"}'
node = locate(line, node)
totalCORRELATION+=sequenceCORRELATION
totalSEQUENCE+=sequenceCOUNTER
if EMAIL:
EMAILMSG+="</tbody></table><p>Mutations: "+str(node)+" Avg: "+str(sequenceCORRELATION/sequenceCOUNTER)+" Time: "+str(end)+"</p>"
if(DEBUG):
if not CSV:
print line
print "avg correlation for this round "+str(sequenceCORRELATION/sequenceCOUNTER)
print 'time took: '+str(end)
else:
OUTPUT_DATA+='],"node":"'+str(node)+'","avg":"'+str(sequenceCORRELATION/sequenceCOUNTER)+'","time":"'+str(end)+'"}'
loop(node)
def loop(node):
#print LOOP_SIZE
global totalTIME
global totalSEQUENCE
global totalCORRELATION
global OUTPUT_DATA
if EMAIL:
global EMAILMSG
#lock file name for this session
rant = random.randint(1, 100);
wrapper = str(rant)+'-wrapper.in'
while(os.path.isfile(wrapper)):
rant = random.randint(1, 100);
wrapper = str(rant)+'-wrapper.in'
for i in range(int(LOOP_SIZE)):
sequenceCOUNTER =0
sequenceCORRELATION = 0
f = open(wrapper, 'w')
f.write(SEQUENCE+'\n')
f.write(node+'\n')
f.write(Question_Marks+'\n')
f.close()
if(DEBUG):
if not CSV:
print node
node = NODE
start = time()
out = commands.getoutput('./RNAmutants -l lib -m '+str(MUTATION_DEPTH) +' -n '+str(NSIZE) +' -f '+ wrapper +' -C')
end = time() - start
totalTIME+=end
underAvg = 0
underThres = 0
bool_first = 0
if EMAIL:
EMAILMSG+="<table border = 0><thead><td>Sequence</td><td>Structure</td><td>Correlation</td><td>MFE</td><td>Under Threshold</td></thead><tbody>"
OUTPUT_DATA += ',{"seq":['
# node = lockS(node)
for line in out.splitlines():
if re.match('\w+', line):
if re.match("C|G|c|G|A|a|U|u", line):
mfe = line
mfe = re.sub('^\w+\s+\(',"",mfe)
mfe = re.sub('\).*$',"",mfe)
sequenceCOUNTER+=1
line = re.sub('\s+.*$',"",line)
struct = getRNAfoldOptStruct(line)
x = P.getCorr(line)
sequenceCORRELATION+=x
note = ''
if(AVG > x):
note = " <Under Avg>"
underAvg = 1
if(nAVG > x):
underThres = 1
SIGNIFICANT.append(line)
note+=" <Under Threshold>"
#print line +" "+str(x) + note
if EMAIL:
EMAILMSG+="<tr><td>"+str(line)+"</td><td>"+str(struct)+"</td><td>"+str(x)+"</td><td>"+str(mfe)+"</td><td>"
#if(AVG > x):
# EMAILMSG+="Y"
#EMAILMSG+="</td><td>"
if(nAVG > x):
EMAILMSG+="Y"
EMAILMSG+="</td></tr>"
if(DEBUG):
if(CSV):
print str(line) +","+str(struct)+","+str(x)+","+str(mfe)
else:
print str(line) +","+str(struct)+","+str(x)+","+str(mfe)+","+str(note)
##print '{0}, {0}, {1:.4f}, {1:.4f}, {2}'.format(line,struct,x,mfe,note)
SCOUT(line)
else:
if(bool_first == 0):
bool_first = 1
elif bool_first == 1:
OUTPUT_DATA+=','
OUTPUT_DATA+='{"seq":"'+str(line)+'", "struct":"'+str(struct)+'","corr":"'+str(x)+'", "mfe":"'+str(mfe)+'","avg":"'
if(underAvg == 1):
OUTPUT_DATA+='t'
underAvg = 0
else:
OUTPUT_DATA+='f'
OUTPUT_DATA+='","navg":"'
if underThres == 1:
OUTPUT_DATA+='t'
underThres = 0
else:
OUTPUT_DATA+='f'
OUTPUT_DATA+='"}'
node = locate(line, node)
if EMAIL:
EMAILMSG+="</tbody></table><p>Mutations: "+str(node)+" Avg: "+str(sequenceCORRELATION/sequenceCOUNTER)+" Time: "+str(end)+"</p>"
if(DEBUG):
if not CSV:
print "avg correlation for this round "+str(sequenceCORRELATION/sequenceCOUNTER)
print 'time took: '+str(end)
else:
OUTPUT_DATA+='],"node":"'+str(node)+'","avg":"'+str(sequenceCORRELATION/sequenceCOUNTER)+'","time":"'+str(end)+'"}'
out = commands.getoutput('rm '+wrapper)
if EMAIL:
EMAILMSG += "<p> Avg Correlation: "+str(totalCORRELATION/totalSEQUENCE)+" Total Sequence: "+str(totalSEQUENCE)+"<br>Total Time: "+str(totalTIME)+" Total Avg Time: "+str(totalTIME/(LOOP_SIZE+1))+"</p>"
EMAILMSG += "Total Sequence Under Threshold: "+str(len(SIGNIFICANT))+"<br>"
EMAILMSG += "<table border = 0><thead><td>Sequence</td><td>Correlation</td></thead><tbody>"
for i in SIGNIFICANT:
EMAILMSG +="<tr><td>"+str(i)+"</td><td>"+str(P.getCorr(i))+"</td></tr>"
EMAILMSG+="</tbody></table>"
global email
email.sent(EMAILMSG)
if(DEBUG):
print "total avg correlation "+str(totalCORRELATION/totalSEQUENCE)
print "total TIME took:: "+str(totalTIME) + " avg time took:: "+str(totalTIME/(LOOP_SIZE+1))
print BUCKETS
print 'No of SIGS Under Threshold threshold {0} = {1}'.format(nAVG,len(SIGNIFICANT))
print "# [sequence] [correlation]"
#print SIGNIFICANT
for i, seq in enumerate(SIGNIFICANT):
print i, seq, "%.4f" % P.getCorr(seq)
else:
OUTPUT_DATA+='], "tAvgCorr":"'+str(totalCORRELATION/totalSEQUENCE)+'", "tSeq":"'+str(totalSEQUENCE)+'", "tTime":"'+str(totalTIME)+'", "tAvgTime":"'+str(totalTIME/(LOOP_SIZE+1))+'","tThres":"'+str(len(SIGNIFICANT))+'"}]'
print OUTPUT_DATA
print "Time(s): "+str(totalTIME)
def lockS(node):
for i in range(len(BUCKETS)):
if(BUCKETS[i] != 0 and BUCKETS[i] % 10 == 0):
node = node[:i]+"X"+node[i+1:]
BUCKETS[i] = 0
return node
def locate(seq, node):
i=0
for x in seq:
if re.match("c|g|a|u", x):
if not re.match("X", node[i]):
node = node[:i]+"X"+node[i+1:]
BUCKETS[i]+=1;
i+=1;
return node
if __name__ == "__main__":
EMAIL = False
ADDRESS = ""
CSV = False
try:
optlist, args = getopt.getopt(sys.argv[1:], 'cn:l:s:m:t:hda:u:', ["help"])
except getopt.GetoptError, err:
print str(err)
print usage()
sys.exit(2)
for opt, query in optlist:
if opt == "-s":
SEQUENCE = query
P = Pearson.Correlation(SEQUENCE)
elif opt in ("-h", "--help"):
usage()
sys.exit(2)
elif opt == "-m":
MUTATION_DEPTH = int(query)
elif opt == "-n":
NSIZE = int(query)
elif opt == "-l":
LOOP_SIZE = int(query)
elif opt =="-d":
DEBUG = True
elif opt == "-t":
if(DEBUG):
print 'Setting threshold to {0}'.format(query)
nAVG = float(query)
elif opt == "-u":
location = query
elif opt == "-a":
EMAIL = True
ADDRESS=query
elif opt == "-c":
CSV = True
else:
print "ERROR: incorrect usage"
usage()
sys.exit
if EMAIL:
email = smail.mail(ADDRESS, SEQUENCE,location)
if(re.match('\w+',SEQUENCE)):
sampling = BootStrap.bootstrap(seq)
initial()
else:
print "ERROR: Buggy or Null Sequence"
sys.exit