forked from Schlicklab/RAG-IF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runRNAinverse-gaif.py
295 lines (201 loc) · 7.56 KB
/
runRNAinverse-gaif.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
# coding: utf-8
# modified on 08/21/2018
# This script is designed to run GAIF program to carry out the mutation generation
# and it is also designed for verify the mutated sequence candidates.
import os
import tempfile
import sys
from checkBPSEQ import * # modified on 2018-07-16
#RNAinverseDir="/Users/yt34/NYU_Drive_Google/Work/RNA-projects/GAIF/"#"/opt/viennaRNA2.4.8/bin/"
#NuPACKDir="/opt/nupack3.2.2/bin/"
#TGpath="/Users/yt34/NYU_Drive_Google/Work/RNA-projects/modified-treeGraph/"
#fd2seqDir="/Users/yt34/NYU_Drive_Google/Work/RNA-projects/myOwnScripts/dotfa2bp/"
RNAinverseDir="/Users/sj78/Documents/labwork/MutationsForDesign/RAG-IF_Code/"#"/opt/viennaRNA2.4.8/bin/"
NuPACKDir="/opt/nupack3.2.2/bin/"
TGpath="/Users/sj78/Documents/labwork/MutationsForDesign/RAG-IF_Code/modified-treeGraph/"
fd2seqDir="/Users/sj78/Documents/labwork/MutationsForDesign/RAG-IF_Code/"
#inpf = "Top49.mfe.rnaInverseInp"
#tmpf = "Top49.seq"
inpf = sys.argv[1]
tmpf = sys.argv[2] # newly added for template file
prefix = inpf.split(".")[0]
# Determine the number of sequences to be generated:
# if < 200, 4^N
# else 200
Rvalue = 500
with open(inpf,"r") as myfile:
data = myfile.read().replace('\n','')
freq = data.count('N')
#print freq
if 4**freq < 500:
Rvalue = 4**freq
# Run Commands !!!!!!!!!!
#os.system(RNAinverseDir+"RNAinverse -Fm −−noGU −−noClosingGU -f 0.5 -R"+str(Rvalue)+" -d2 <"+inpf+" >"+prefix+".mut_seq1" )
os.system("python "+RNAinverseDir+"gaif.py "+ inpf +" " + tmpf)
# output file is "heaven.txt"
# Remove duplicate generated sequences
seq1 = []
with open("heaven.txt") as pointer:
for line in pointer:
#
seq1.append( line.split()[0].upper() )
#print len(seq1)
seq2 = list(set(seq1))
print "Number of candidate sequences for this case:",len(seq2) # list(set(seq1)))
#seq2 = list(set(seq1))
#print seq2
# Verify these generated sequences with NuPACK and RNAfold
# one by one stored in seq2
## NuPACK
### NuPACK input - [prefix].in
final_seq = []
final_label = []
# # # # # # # # # # # # # # # # # #
# Loop over all sequnces in seq2 #
# # # # # # # # # # # # # # # # #
#ex_seq = seq2[0]
process_bar = ShowProcess(len(seq2), 'OK')
for i in range(len(seq2)):
ex_seq = seq2[i]
good_seq = 'y'
# Get the targeted topology
with open(prefix+".tg_log") as pointer:
for line in pointer:
#
if "Graph ID" in line:
#print line
target_top = line.split()[-1]
# Verify with RNAfold
f1 = open("tmpRNAfold.in","w")
f1.write(ex_seq)
f1.close()
os.system("/opt/viennaRNA2.3.5/bin/RNAfold -p -d2 --noLP < tmpRNAfold.in > tmpRNAfold.out" )
os.system("rm -rf tmpRNAfold.in rna.ps dot.ps")
f1 = open("tmpRNAfold-mfe.fa","w")
f1.write(">test\n")
f1.write(ex_seq)
f1.close()
os.system("cp tmpRNAfold-mfe.fa tmpRNAfold-centroid.fa")
# get dot-bracket and then convert to bpseq
# 1. mfe - 2nd line
# 2. centroid - 4th line
counter = 0
with open("tmpRNAfold.out") as pointer:
for line in pointer:
counter = counter + 1
if counter == 2:
tmpRNA_mfe_ss = line.split()[0]
if counter == 4:
tmpRNA_centroid_ss = line.split()[0]
os.system("rm -rf tmpRNAfold.out")
f1 = open("tmpRNAfold-mfe.dotbracket","w")
f1.write(tmpRNA_mfe_ss)
f1.close()
os.system("rm -rf tmpRNAfold-mfe.bpseq")
os.system("python "+fd2seqDir+"dotfa2bpseq.py tmpRNAfold-mfe.fa tmpRNAfold-mfe.dotbracket"+">/dev/null ")
os.system("rm -rf tmpRNAfold-mfe.fa tmpRNAfold-mfe.dotbracket")
f1 = open("tmpRNAfold-centroid.dotbracket","w")
f1.write(tmpRNA_centroid_ss)
f1.close()
os.system("rm -rf tmpRNAfold-centroid.bpseq")
os.system("python "+fd2seqDir+"dotfa2bpseq.py tmpRNAfold-centroid.fa tmpRNAfold-centroid.dotbracket"+">/dev/null ")
os.system("rm -rf tmpRNAfold-centroid.fa tmpRNAfold-centroid.dotbracket ")
# mfe - treegraph
os.system("tail -n +2 tmpRNAfold-mfe.bpseq > tmp1")
os.system("mv tmp1 tmpRNAfold-mfe.bpseq")
os.system("python "+TGpath+"treeGraphs.py tmpRNAfold-mfe.bpseq > tmpRNAfold-mfe.tg_log")
with open("tmpRNAfold-mfe.tg_log") as pointer:
for line in pointer:
if "Graph ID" in line:
rnafold_mfe_top = line.split()[-1]
os.system("rm -rf tmpRNAfold-mfe.bpseq tmpRNAfold-mfe.tg_log")
###print rnafold_mfe_top
# centroid - treegraph
os.system("tail -n +2 tmpRNAfold-centroid.bpseq > tmp1")
os.system("mv tmp1 tmpRNAfold-centroid.bpseq")
#os.system("python "+TGpath+"treeGraphs.py tmpRNAfold-centroid.bpseq > tmpRNAfold-centroid.tg_log")
# modified on 2018-07-18
ifSkip = chkBPSEQ("tmpRNAfold-centroid.bpseq") # modified on 2018-07-16
if ifSkip == 'n':
os.system("python "+TGpath+"treeGraphs.py tmpRNAfold-centroid.bpseq > tmpRNAfold-centroid.tg_log")
with open("tmpRNAfold-centroid.tg_log") as pointer:
for line in pointer:
if "Graph ID" in line:
rnafold_centroid_top = line.split()[-1]
os.system("rm -rf tmpRNAfold-centroid.bpseq tmpRNAfold-centroid.tg_log")
###print rnafold_centroid_top
else:
rnafold_centroid_top = 'invalid'
if (rnafold_centroid_top != target_top ) and (rnafold_mfe_top != target_top):
good_seq = 'n'
# Verify with NuPack
f1 = open("tmpNupack.in","w")
f1.write(ex_seq)
f1.close()
os.system("/opt/nupack3.2.2/bin/mfe -material rna tmpNupack") # 2
os.system("rm tmpNupack.in")
f1 = open("tmpNupack.fa","w")
f1.write(">test\n")
f1.write(ex_seq)
f1.close()
# Get dot-bracket from "tmpNupack.mfe"
with open("tmpNupack.mfe") as pointer:
for line in pointer:
#
if "((" in line:
target = line
if ".." in line:
target = line
if "))" in line:
target = line
f1 = open("tmpNupack.dotbracket","w")
f1.write(target)
f1.close()
os.system("rm -rf tmpNupack.bpseq")
os.system("python "+fd2seqDir+"dotfa2bpseq.py tmpNupack.fa tmpNupack.dotbracket"+">/dev/null ")
os.system("tail -n +2 tmpNupack.bpseq > tmp1")
os.system("mv tmp1 tmpNupack.bpseq")
# double check the NuPACK verification result
ifSkipB = 'n'
ifSkipB = chkTermi("tmpNupack.bpseq") # 07-18
os.system("python "+TGpath+"treeGraphs.py tmpNupack.bpseq > tmpNupack.tg_log"+" 2>/dev/null") #modified 2018-07-18
# clean up
os.system("rm -rf tmpNupack.mfe tmpNupack.fa tmpNupack.dotbracket tmpNupack.bpseq")
# get its top. graph id
unpack_top = "not_assigned"
with open("tmpNupack.tg_log") as pointer:
for line in pointer:
if "Graph ID" in line:
nupack_top = line.split()[-1]
os.system("rm -rf tmpNupack.tg_log ")
if ifSkipB == "y": # 07-18
nupack_top = 'verified_failed'
if target_top != nupack_top:
good_seq = "n"
#print good_seq
if good_seq == 'y':
final_seq.append( ex_seq )
# print out status
stat_1 = (target_top == nupack_top)
stat_2 = (target_top == rnafold_mfe_top )
stat_3 = (target_top == rnafold_centroid_top)
#print stat_1, stat_2, stat_3 #,"->",(stat_1 and stat_2 and stat_3)
if stat_2 and stat_3:
final_label.append("B")
if stat_2 and (not stat_3):
final_label.append("M")
if (not stat_2) and stat_3:
final_label.append("C")
#
process_bar.show_process()
print "Number of successful sequences for this case:",len(final_seq)
# MFE only -> M
# CENTROID ONLY -> C
# Both -> B
# export successful sequences
f1 = open(prefix+".survivors","w")
for i in range(len(final_seq)):
f1.write( final_seq[i].strip() )
f1.write(" "+final_label[i] )
f1.write("\n")
f1.close()