-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomixcore.py
executable file
·289 lines (219 loc) · 9.16 KB
/
omixcore.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
#!/usr/bin/env python
import argparse
import yaml
import os
import subprocess
import shutil, errno
import sys
import json
from django.template import Template
from django.template import Context
from django.conf import settings
from django.template import Template
import datetime
import IPython
# we want to be agnostic to where the script is ran
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
WORKER_PATH = os.path.realpath(os.curdir)
# Function to copy the output directory content
def copyfolder(src, dst):
try:
if os.path.exists(dst):
shutil.rmtree(dst)
shutil.copytree(src, dst)
except OSError as exc: # python >2.5
if exc.errno == errno.ENOTDIR:
shutil.copy(src, dst)
else: raise
def error_page(OUTPUT_PATH,SCRIPT_PATH,title,random_number,error_message):
settings.configure(TEMPLATE_DIRS=(os.path.join(SCRIPT_PATH,'./')), DEBUG=True, TEMPLATE_DEBUG=True)
with open(os.path.join(SCRIPT_PATH, "index.error.html"), "r") as template_file:
template_string = "".join(template_file.readlines())
# create template from the string
t = Template(template_string)
c = Context(
{"title": title,
"randoms" : random_number,
"generated" : str(datetime.datetime.now()),
"error_message" : error_message,
})
with open(os.path.join(OUTPUT_PATH, "index.html"), "w") as output:
output.write(t.render(c))
# read the task definition yaml file
with open(os.path.join(SCRIPT_PATH, "omixcore.yaml"), "r") as task_f:
task_definition = yaml.load(task_f)
parser = argparse.ArgumentParser(
description='Launches')
# parser.add_argument(
# '-fileA', type=str, default=["none"], nargs=1, help='Dataset A')
#
# parser.add_argument(
# '-fileB', type=str, default=["none"], nargs=1, help='Dataset B')
parser.add_argument(
'-output_dir', type=str, nargs=1,
help='Directory where the output is going to be stored')
# accept form fields
for item in task_definition['form_fields']:
nargs = 1 if item['required'] else "?"
'''if item['name']=="rna_seq":
nargs='*'
'''
parser.add_argument(
'-FORM%s'%item['name'], type=str, default="none", nargs=nargs,
help='Form argument: %s' % item)
# this parse_args stops if any unexpected arguments is passed
args = parser.parse_args()
OUTPUT_PATH = os.path.join(WORKER_PATH, args.output_dir[0])
import re
import StringIO
from Bio import SeqIO
Ppat = re.compile('>.*?\n[ARNDCQEGHILKMFPSTWYV]+', re.IGNORECASE)
if Ppat.match(args.FORMprotein_seq[0]) == None:
args.FORMprotein_seq[0] = ">input_protein\n"+args.FORMprotein_seq[0]
protSeq = []
for record in SeqIO.parse(StringIO.StringIO(args.FORMprotein_seq[0]), "fasta"):
protSeq.append(record)
break
args.FORMrna_seq=[args.FORMrna_seq]
Rpat = re.compile('>.*?\n[GATCU]+', re.IGNORECASE)
rnaSeq = []
records = SeqIO.parse(StringIO.StringIO(args.FORMrna_seq[0]), "fasta")
SeqIO.write(records, os.path.join(OUTPUT_PATH,"Submission_rna.fasta") , "fasta")
for record2 in SeqIO.parse(StringIO.StringIO(args.FORMrna_seq[0]), "fasta"):
if record2.id == "":
record2.id="input_rna"
check=">"+record2.id+"\n"+str(record2.seq)
if Rpat.match(check) != None:
rnaSeq.append(record2)
rnafolder=os.path.join(OUTPUT_PATH.replace("output/", ""),"rnas")
if not os.path.exists(rnafolder):
os.makedirs(rnafolder)
valid_entries=0
rnaAllFile=os.path.join(OUTPUT_PATH,"rna.fasta")
output_all_handle = open(rnaAllFile, "w")
for entry in rnaSeq:
if len(entry.seq)>=500:
valid_entries+=1
entry.id=re.sub('[^0-9a-zA-Z]+', '_', entry.id)
entry.description=""
rnaFile = os.path.join(rnafolder,entry.id)
output_handle = open(rnaFile, "w")
SeqIO.write(entry, output_handle, "fasta")
SeqIO.write(entry, output_all_handle, "fasta")
output_handle.close()
protFile = os.path.join(OUTPUT_PATH.replace("output/", ""),"protein.fasta")
output_handle = open(protFile, "w")
for protein_record in SeqIO.parse(StringIO.StringIO(args.FORMprotein_seq[0]), "fasta"):
output_handle.write(str(">input_protein\n"+protein_record.seq))
break
# SeqIO.write(protSeq, output_handle, "fasta")
output_handle.close()
os.chdir(SCRIPT_PATH)
# print(WORKER_PATH)
random_number = (""" "{}" """.format(args.output_dir[0])).split("/")[3]
args.FORMtitle = "".join([t.replace(' ', '_') for t in args.FORMtitle])
# os.rename(os.path.join(WORKER_PATH,args.fileA[0]), os.path.join(WORKER_PATH,args.fileA[0].replace(' ', '-')))
# os.rename(os.path.join(WORKER_PATH,args.fileB[0]), os.path.join(WORKER_PATH,args.fileB[0].replace(' ', '-')))
if type(args.FORMtitle)==list:
title = "none"
else:
title = args.FORMtitle.replace(" ", "_")
logfile = open(os.path.join(OUTPUT_PATH,"pylog."+str(random_number)+".txt"),"w")
modeFile=open(os.path.join(OUTPUT_PATH,"mode"),'w')
modeFile.writelines(args.FORMmode[0])
modeFile.close()
if len(rnaSeq) == 0:
print "Please make sure that the RNA sequence is in the correct FASTA format, containing only [GATCU] characters and resubmit."
error_message= "Please make sure that the RNA sequence is in the correct FASTA format, containing only [GATCU] characters and resubmit."
error_page(OUTPUT_PATH,SCRIPT_PATH,title,random_number,error_message)
logfile.write("Please make sure that the RNA sequence is in the correct FASTA format, containing only [GATCU] characters and resubmit.\n")
sys.exit()
if args.FORMmode[0]=="custom" and valid_entries==0:
error_message="Custom option selected, with 0 valid entries. Please check the lengths of the custom transcript sequences to be at least 500nt and re-submit!"
error_page(OUTPUT_PATH,SCRIPT_PATH,title,random_number,error_message)
logfile.write("custom option and valid entries. Created error index.html\n")
sys.exit()
#sys.exit("Wrong Submission. Custom option selected, with 0 valid entries. The execution of the bash script failed.")
if len(protein_record.seq)<=150:
error_message="Protein sequence too short! Please check the length of the protein sequence to be at least 150aa and re-submit!"
error_page(OUTPUT_PATH,SCRIPT_PATH,title,random_number,error_message)
logfile.write("Protein sequence too short. Created error index.html\n")
sys.exit()
#sys.exit("Wrong Submission. Protein sequence too short! The execution of the bash script failed.")
cmd = """bash omixcore.sh "{}" "{}" "{}" "{}" "{}" "{}" "{}" """.format(random_number, args.FORMemail[0], title, "150", protFile, args.FORMmode[0],rnafolder)
p = subprocess.Popen(cmd, cwd=SCRIPT_PATH, shell=True)
logfile.write(str(p.returncode)+"\n")
p.wait()
logfile.write(str(p.returncode)+"\n")
if p.returncode == 0:
TMP_PATH = "./tmp/{}/outputs/".format(random_number)
logfile.write(TMP_PATH+"\n")
dirList=os.listdir(TMP_PATH)
shutil.copyfile(os.path.join(OUTPUT_PATH.replace("output/", ""),"protein.fasta"), OUTPUT_PATH+"protein.fasta")
logfile.write("copied fastas\n")
for file in dirList:
if os.path.isfile(TMP_PATH+file): #ignore directories
shutil.copyfile(TMP_PATH+file, OUTPUT_PATH+file)
logfile.write("copied all files\n")
if os.path.exists(TMP_PATH+"rna_libs") == True :
copyfolder(TMP_PATH+"rna_libs", OUTPUT_PATH+"rna_libs")
logfile.write("copied rna_libs folder\n")
if os.path.exists(OUTPUT_PATH+"images") == False :
copyfolder(SCRIPT_PATH+"/images", OUTPUT_PATH+"images")
logfile.write("copied images folder\n")
settings.configure(TEMPLATE_DIRS=(os.path.join(SCRIPT_PATH,'./')), DEBUG=True, TEMPLATE_DEBUG=True)
with open(os.path.join(OUTPUT_PATH,"Signature_prediction.txt"), "r") as sign_result:
PredictionScore=float(sign_result.readlines()[0])
if PredictionScore<0.5:
with open(os.path.join(SCRIPT_PATH, "index.nrbp.html"), "r") as template_file:
template_string = "".join(template_file.readlines())
# create template from the string
t = Template(template_string)
c = Context(
{"title": title,
"PredictionScore" : PredictionScore,
"randoms" : random_number,
"generated" : str(datetime.datetime.now()), })
with open(os.path.join(OUTPUT_PATH, "index.html"), "w") as output:
output.write(t.render(c))
logfile.write("created nrbp index.html\n")
else:
with open(os.path.join(SCRIPT_PATH, "index.mat.html"), "r") as template_file:
template_string = "".join(template_file.readlines())
logfile.write("copied index.mat.tmp.html\n")
# create template from the string
t = Template(template_string)
# context contains variables to be replaced
c = Context(
{
"title": title,
"rnaFrag" : "150",
"randoms" : random_number,
"fileA" : protFile,
# "fileB" : rnaFile,
"generated" : str(datetime.datetime.now()),
}
)
# and this bit outputs it all into index.html
rnas=[]
values=[]
with open(os.path.join(OUTPUT_PATH,"filter.processed.txt"), "r") as fltr:
for line in fltr:
if len(line.split())==2:
rnas.append(line.split()[0].strip())
values.append(line.split()[1].strip())
fltr.close()
with open(os.path.join(OUTPUT_PATH,"transcript.rows"),'w') as fobj:
json.dump(zip(rnas,values),fobj)
fobj.close()
with open(os.path.join(OUTPUT_PATH, "index.html"), "w") as output:
output.write(t.render(c))
logfile.write("created index.html\n")
else:
error_message="Webserver failed to run prediction. Please try again!"
error_page(OUTPUT_PATH,SCRIPT_PATH,title,random_number, error_message)
logfile.write("bash script failed\n")
sys.exit()
logfile.write("that's it!\n")
logfile.close()
#os.remove("pylog."+str(random_number)+".txt")