-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossalign.py
executable file
·326 lines (262 loc) · 10.8 KB
/
crossalign.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
#!/usr/bin/env python
import argparse
import IPython
import yaml
import os
import subprocess
import shutil, errno
import sys
# 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:
shutil.copytree(src, dst)
except OSError as exc: # python >2.5
if exc.errno == errno.ENOTDIR:
shutil.copy(src, dst)
else: raise
# read the task definition yaml file
if sys.argv[1] == "-text=Yes":
# read the task definition yaml file
with open(os.path.join(SCRIPT_PATH, "crossalign.yaml"), "r") as task_f:
task_definition = yaml.load(task_f)
input_mode = "text"
else:
with open(os.path.join(SCRIPT_PATH, "crossalign_file.yaml"), "r") as task_f:
task_definition = yaml.load(task_f)
input_mode = "file"
parser = argparse.ArgumentParser(
description='Launches crossalign algorithm with properly parset parameters')
parser.add_argument(
'-text', type=str, nargs="?", help='Just to enable text mode')
if input_mode == "file":
parser.add_argument('-fileA', type=str, default=["none"], nargs=1, help='Fasta sequence')
parser.add_argument('-fileB', type=str, default=["none"], nargs=1, help='Fasta sequence')
else:
parser.add_argument('-fileA', type=str, default=["none"], nargs=1, help='Fasta sequence')
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 "?"
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])
random_number = (""" "{}" """.format(args.output_dir[0])).split("/")[3]
TMP_PATH = SCRIPT_PATH+"/tmp/"+random_number
#print "tmp path: ", TMP_PATH
#print "src path: ", SCRIPT_PATH+"/template"
if os.path.exists(TMP_PATH) == True:
shutil.rmtree(TMP_PATH)
copyfolder(SCRIPT_PATH+"/template", TMP_PATH)
#import IPython
#IPython.embed()
import re
import StringIO
from Bio import SeqIO
if input_mode == "text":
Rpat = re.compile('>.*?\n[GATCU]+', re.IGNORECASE)
if Rpat.match(args.FORMsequence_one[0]) == None:
args.FORMsequence_one[0] = ">input_rna\n"+args.FORMsequence_one[0]
rnaSeq = []
for record in SeqIO.parse(StringIO.StringIO(args.FORMsequence_one[0]), "fasta"):
rnaSeq.append(record)
rnaFile = os.path.join(OUTPUT_PATH.replace("outputs/", ""),"rna.fasta")
output_handle1 = open(rnaFile, "w")
len1=len(record.seq)
if args.FORMfeature[0]!="dataset" and args.FORMfeature[0]!="custom_dataset":
Rpat = re.compile('>.*?\n[GATCU]+', re.IGNORECASE)
if Rpat.match(args.FORMsequence_two) == None:
#print args.FORM
args.FORMsequence_two = ">input_rna2\n"+args.FORMsequence_two
rnaSeq2 = []
for record in SeqIO.parse(StringIO.StringIO(args.FORMsequence_two), "fasta"):
rnaSeq2.append(record)
rnaFile2 = os.path.join(OUTPUT_PATH.replace("outputs/", ""),"rna2.fasta")
output_handle2 = open(rnaFile2, "w")
len2=len(record.seq)
if args.FORMfeature[0]!="fragment":
if len1>len2:
SeqIO.write(rnaSeq, output_handle2, "fasta")
SeqIO.write(rnaSeq2, output_handle1, "fasta")
output_handle2.close()
output_handle1.close()
else:
SeqIO.write(rnaSeq, output_handle1, "fasta")
SeqIO.write(rnaSeq2, output_handle2, "fasta")
output_handle2.close()
output_handle1.close()
elif args.FORMfeature[0]=="fragment":
SeqIO.write(rnaSeq, output_handle1, "fasta")
SeqIO.write(rnaSeq2, output_handle2, "fasta")
output_handle2.close()
output_handle1.close()
else:
SeqIO.write(rnaSeq, output_handle1, "fasta")
output_handle1.close()
if args.FORMfeature[0]=="custom_dataset":
customrnaSeq = os.path.abspath(args.fileA[0])
print "custom dataset, fileA: ",args.fileA[0]
shutil.copyfile(customrnaSeq, os.path.join(os.path.dirname(customrnaSeq),"multi.rna.fasta" ))
#os.rename(customrnaSeq, os.path.join(os.path.dirname(customrnaSeq),"multi.rna.fasta" ))
customrnaSeq = os.path.join(os.path.dirname(customrnaSeq),"multi.rna.fasta" )
else:
rnaSeq = []
input_handle = open(args.fileA[0], "rU")
for record in SeqIO.parse(input_handle, "fasta"):
rnaSeq.append(record)
rnaFile = os.path.join(OUTPUT_PATH.replace("outputs/", ""),"rna.fasta")
output_handle1 = open(rnaFile, "w")
len1=len(record.seq)
#import IPython
#IPython.embed()
if args.FORMfeature[0]!="dataset" and args.FORMfeature[0]!="custom_dataset":
rnaSeq2 = []
rnaFile2 = os.path.join(OUTPUT_PATH.replace("outputs/", ""),"rna2.fasta")
input_handle = open(args.fileB[0], "rU")
for record in SeqIO.parse(input_handle, "fasta"):
rnaSeq2.append(record)
rnaFile2 = os.path.join(OUTPUT_PATH.replace("outputs/", ""),"rna2.fasta")
output_handle2 = open(rnaFile2, "w")
len2=len(record.seq)
if args.FORMfeature[0]!="fragment":
if len1>len2:
SeqIO.write(rnaSeq, output_handle2, "fasta")
SeqIO.write(rnaSeq2, output_handle1, "fasta")
output_handle2.close()
output_handle1.close()
else:
SeqIO.write(rnaSeq, output_handle1, "fasta")
SeqIO.write(rnaSeq2, output_handle2, "fasta")
output_handle2.close()
output_handle1.close()
elif args.FORMfeature[0]=="fragment":
SeqIO.write(rnaSeq, output_handle1, "fasta")
SeqIO.write(rnaSeq2, output_handle2, "fasta")
output_handle2.close()
output_handle1.close()
else:
SeqIO.write(rnaSeq, output_handle1, "fasta")
output_handle1.close()
if args.FORMfeature[0]=="custom_dataset":
customrnaSeq = os.path.abspath(args.fileB[0])
shutil.copyfile(customrnaSeq, os.path.join(os.path.dirname(customrnaSeq),"multi.rna.fasta" ))
#os.rename(customrnaSeq, os.path.join(os.path.dirname(customrnaSeq),"multi.rna.fasta" ))
customrnaSeq = os.path.join(os.path.dirname(customrnaSeq),"multi.rna.fasta" )
os.chdir(SCRIPT_PATH)
args.FORMtitle = "".join([t.replace(' ', '_') for t in args.FORMtitle])
if args.FORMfeature[0]!="dataset" and args.FORMfeature[0]!="custom_dataset":
command = """ bash crossalign.sh "{}" "{}" "{}" "{}" """.format(rnaFile,rnaFile2,args.FORMfeature[0],random_number,args.FORMemail[0])
elif args.FORMfeature[0]=="custom_dataset":
command = """ bash crossalign.sh "{}" "{}" "{}" "{}" """.format(rnaFile,customrnaSeq,args.FORMfeature[0],random_number,args.FORMemail[0])
elif args.FORMfeature[0]=="dataset":
print args.FORMorganism[0:],args.FORMfeature[0]
command = """ bash crossalign.sh "{}" "{}" "{}" "{}" """.format(rnaFile,args.FORMorganism[0:],args.FORMfeature[0],random_number,args.FORMemail[0])
print command
p = subprocess.Popen(command, cwd=SCRIPT_PATH, shell=True)
p.communicate()
#os.system("php "+SCRIPT_PATH+"/index.cross.html > "+SCRIPT_PATH+"/index.cross2.html")
if p.returncode == 0:
TMP_PATH = SCRIPT_PATH+ "/tmp/"+ random_number+"/outputs/"
dirList=os.listdir(TMP_PATH)
for file in dirList:
shutil.copyfile(TMP_PATH+file, OUTPUT_PATH+file)
from django.template import Template
from django.template import Context
from django.conf import settings
from django.template import Template
settings.configure(TEMPLATE_DIRS=(os.path.join(SCRIPT_PATH,'./')), DEBUG=True, TEMPLATE_DEBUG=True)
# read the template file into a variable
if args.FORMfeature[0]!="dataset" and args.FORMfeature[0]!="custom_dataset":
i=0
myfile=open(TMP_PATH+"score.txt","r").readlines()
for line in myfile:
distance=line[:-1]
#P-VALUE
if args.FORMfeature[0]!="fragment" and args.FORMfeature[0]!="dataset" and args.FORMfeature[0]!="custom_dataset":
myfile2=open(TMP_PATH+"pval.txt","r").readlines()
for line in myfile2:
pval=line[:-1]
summary_line=''
#HTML INDEX DECISION
if args.FORMfeature[0]=="normal":
with open(os.path.join(SCRIPT_PATH, "index.crossalign.html"), "r") as template_file:
template_string = "".join(template_file.readlines())
if args.FORMfeature[0]=="obe":
with open(os.path.join(SCRIPT_PATH, "index.crossalign_obe.html"), "r") as template_file:
template_string = "".join(template_file.readlines())
myfile2=open(TMP_PATH+"start.txt","r").readlines()
for line in myfile2:
begin=line[:-1]
myfile3=open(TMP_PATH+"end.txt","r").readlines()
for line2 in myfile3:
finish=line2[:-1]
if args.FORMfeature[0]=="fragment":
with open(os.path.join(SCRIPT_PATH, "index.crossalign_fragments.html"), "r") as template_file:
template_string = "".join(template_file.readlines())
if args.FORMfeature[0]=="dataset" or args.FORMfeature[0]=="custom_dataset":
with open(os.path.join(SCRIPT_PATH, "index.dataset.html"), "r") as template_file:
template_string = "".join(template_file.readlines())
import datetime
# create template from the string
t = Template(template_string)
# context contains variables to be replaced
if args.FORMfeature[0]=="normal":
c = Context(
{
"title": args.FORMtitle,
"randoms" : random_number,
"feature" : args.FORMfeature[0],
"value" : distance,
"pvalue" : pval,
"generated" : str(datetime.datetime.now()),
"summary" : summary_line
}
)
if args.FORMfeature[0]=="obe":
c = Context(
{
"title": args.FORMtitle,
"randoms" : random_number,
"feature" : args.FORMfeature[0],
"value" : distance,
"pvalue" : pval,
"start" : begin,
"end" : finish,
"generated" : str(datetime.datetime.now()),
"summary" : summary_line
}
)
if args.FORMfeature[0]=="fragment":
c = Context(
{
"title": args.FORMtitle,
"randoms" : random_number,
"feature" : args.FORMfeature[0],
"generated" : str(datetime.datetime.now()),
"summary" : summary_line
}
)
if args.FORMfeature[0]=="dataset" or args.FORMfeature[0]=="custom_dataset":
c = Context(
{
"title": args.FORMtitle,
"randoms" : random_number,
"feature" : args.FORMfeature[0],
"generated" : str(datetime.datetime.now()),
"summary" : summary_line
}
)
# and this bit outputs it all into index.html
#print "crosspy OUTPUT_path", OUTPUT_PATH
with open(os.path.join(OUTPUT_PATH, "index.html"), "w") as output:
output.write(t.render(c))
print "done"
else:
sys.exit("The execution of the C code failed.")