This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcr_distance.py
297 lines (226 loc) · 13.2 KB
/
tcr_distance.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
# Checked for Python 3.7
import sys, re, os, time, subprocess, shlex, shutil
sys.path.append( os.environ.get('BBLAB_UTIL_PATH', 'fail') )
import format_utils
import mailer
# todo: fix this path -> turn it into an env var
tcr_dist_path = os.environ.get('BBLAB_UTIL_PATH', 'fail') + "../apps/tcr-dist"
this_file_path = os.path.dirname(os.path.realpath(__file__))
tmp_dirs_path = this_file_path + "/tmp_dirs"
g_TIMEOUT = 600 * 3 # 30 mins
def create_random_directory():
import random
random.seed()
os.chdir(tmp_dirs_path)
# find mostly unique dir_num
dir_num = str(random.randint(0, 100))
while os.path.exists( "tmp_{}".format(dir_num) ):
dir_num = dir_num + 1 #str(random.randint(0, 2**63 - 1))
# make tmp_n folder to store intermediary files in.
tmpdir = "tmp_{}".format(dir_num)
os.mkdir( tmpdir )
os.chmod( tmpdir, 0o777 )
return dir_num
# This function destroys a directory
def terminate(dir_num):
assert (shutil.rmtree.avoids_symlink_attacks == True), "version needs to protect against symlink attacks"
shutil.rmtree( "{}/tmp_{}".format(tmp_dirs_path, dir_num) )
# this function removes all files except for matricies.zip and status
def clear_dir(dir_num):
#file = open("{}/status".format(wd), "a")
#file.write("clearing directory! #{}".format(dir_num) + str("\n"))
#file.close()
assert (shutil.rmtree.avoids_symlink_attacks == True), "version needs to protect against symlink attacks"
for name in os.listdir( tmp_dirs_path + "/tmp_{}".format(dir_num) ):
if name != "matricies.zip" and name != "status" and name != "terminate":
file_path = "{}/tmp_{}/{}".format(tmp_dirs_path, dir_num, name)
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree( file_path )
def debug_log_append(val):
wd = tmp_dirs_path + "" # wd --> working directory
file = open("{}/debug.log".format(wd), "a")
file.write(str(val) + str("\n"))
file.close()
# This function checks for any directories which are either empty or directories which hit an error and removes them.
def remove_bad_dirs():
import datetime
for name in os.listdir( tmp_dirs_path ):
dir_path = tmp_dirs_path + "/" + name
duration = (datetime.datetime.now() - datetime.datetime.fromtimestamp(os.path.getmtime(dir_path)))
hours_age = duration.days * 24 + (duration.seconds / 3600)
if (os.path.isdir(dir_path) and len(os.listdir(dir_path)) == 0 and hours_age >= (1 / 60)) or \
(os.path.exists(dir_path + "/terminate")) or \
(hours_age > 48*2):
# remove directory given that it should be destroyed or is empty and more than 1 minute old or more than 4 days old.
assert (shutil.rmtree.avoids_symlink_attacks == True), "version needs to protect against symlink attacks"
shutil.rmtree(dir_path)
def run(input_kind, filtered_contig_annotations, consensus_annotations, clones_file, dir_num, organism, send_email, email_address, download_file, forward_to_visualizer):
##### Check if directory exists.
os.chdir(tmp_dirs_path)
tmpdir = "tmp_{}".format(dir_num)
if not os.path.exists( tmpdir ):
return "directory not assigned"
wd = tmp_dirs_path + "/" + tmpdir # wd --> working directory
##### Convert annotations files into clones file (using tcr dist)
def append_status_file(val):
file = open("{}/status".format(wd), "a")
file.write(str(val) + str("\n"))
file.close()
if input_kind == "10x":
append_status_file("starting 10x")
# write input files to tmpdir
file = open("{}/filtered_contig_annotations.tsv".format(wd), "w")
file.write(filtered_contig_annotations)
file.close()
file = open("{}/consensus_annotations.tsv".format(wd), "w")
file.write(consensus_annotations)
file.close()
append_status_file("10x files written")
command = "python2 {}/make_10x_clones_file.py -f {}/filtered_contig_annotations.tsv ".format(tcr_dist_path, wd) + \
"-c {}/consensus_annotations.tsv -o {}/clones_file --organism {}".format(wd, wd, organism)
append_status_file("starting 10x conversion")
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# This loop allows for the process to be terminated
process_done = False
time_waited = 0
while not process_done:
try:
process.wait(1) # give the process some time to complete.
process_done = True
except subprocess.TimeoutExpired as e:
time_waited += 1
if os.path.exists( tmpdir + "/terminate" ):
process.terminate()
append_status_file("took {} seconds".format(time_waited))
append_status_file( "10x conversion terminated" )
append_status_file( "done" )
remove_bad_dirs()
return
elif time_waited > g_TIMEOUT:
process.terminate()
append_status_file("took {} seconds".format(time_waited))
append_status_file( "10x conversion timed out" )
append_status_file( "done" )
remove_bad_dirs()
with open("{}/terminate".format(wd), "w") as f:
f.write("terminate")
return
append_status_file("took {} seconds".format(time_waited))
append_status_file("done 10x conversion")
process.terminate() # just in case
elif input_kind == "clones_file":
append_status_file("starting clones_file")
# write input files to tmpdir
file = open("{}/clones_file".format(wd), "w")
file.write(clones_file)
file.close()
append_status_file("clones_file written")
##### Convert clones file into matrix files (also using tcr dist)
command = "python2 {}/compute_distances.py --clones_file {}/clones_file --organism {}".format(tcr_dist_path, wd, organism)
append_status_file("starting distance computation");
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# This loop allows for the process to be terminated
process_done = False
time_waited = 0
while not process_done:
try:
process.wait(1) # give the process some time to complete.
process_done = True
except subprocess.TimeoutExpired as e:
time_waited += 1
if os.path.exists( tmpdir + "/terminate" ):
process.terminate()
append_status_file("took {} seconds".format(time_waited))
append_status_file( "distance computation terminated" )
append_status_file( "done" )
remove_bad_dirs()
return
elif time_waited > g_TIMEOUT:
process.terminate()
append_status_file("took {} seconds".format(time_waited))
append_status_file( "distance computation timed out" )
append_status_file( "done" )
remove_bad_dirs()
with open("{}/terminate".format(wd), "w") as f:
f.write("terminate")
return
# TODO: do the errors before appending the error message so error check only has to check last line.
#website.send("Error Message: {}".format( str(e) ))
#stdout, stderr = process.communicate()
#website.send("Other errors: {}".format( stderr ))
append_status_file("took {} seconds".format(time_waited))
append_status_file("done distance computation")
process.terminate() # just in case
##### Compress 3 matrix files
append_status_file("compressing files")
command = "cd {}; zip matricies.zip clones__A.dist clones__B.dist clones__AB.dist; cd -".format(wd)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# This loop allows for the process to be terminated
process_done = False
time_waited = 0
while not process_done:
try:
process.wait(1) # give the process some time to complete.
process_done = True
except subprocess.TimeoutExpired as e:
time_waited += 1
if os.path.exists( tmpdir + "/terminate" ):
process.terminate()
append_status_file("took {} seconds".format(time_waited))
append_status_file( "compressing terminated" )
append_status_file( "done" )
remove_bad_dirs()
return
elif time_waited > g_TIMEOUT:
process.terminate()
append_status_file("took {} seconds".format(time_waited))
append_status_file( "compressing timed out" )
append_status_file( "done" )
remove_bad_dirs()
with open("{}/terminate".format(wd), "w") as f:
f.write("terminate")
return
#website.send("Error Message: {}".format( str(e) ))
#stdout, stderr = process.communicate()
#website.send("Other errors: {}".format( stderr ))
append_status_file("took {} seconds".format(time_waited))
append_status_file("done compression")
process.terminate() # just in case
##### Very hacky check for any errors
# If the output file doesn't exist, then destroy the current directory
if not os.path.exists("{}/matricies.zip".format(wd)):
append_status_file("pipeline failed (output file was not generated). Make sure your input was correct.")
append_status_file("done")
time.sleep(5)
terminate(dir_num)
return
##### Read the compressed file and email it
email_size = os.path.getsize("{}/matricies.zip".format(wd))
if email_size * 1.37 < 25000000:
with open("{}/matricies.zip".format(wd), "rb") as file:
matricies_data = file.read()
##### Send an email with the xlsx file in it.
# 1.37 is the factor around which files tend to be expanded.
if send_email == 1 and email_size * 1.37 < 25000000:
# make the output files into mailable files.
mat_file = mailer.create_file( "matricies", 'zip', matricies_data )
# Add the body to the message and send it.
end_message = "This is an automatically generated email, please do not respond."
msg_body = "The included .zip file ({}.zip) contains the requested tcr_distance matrix data. \n\n{}".format("matricies", end_message)
if mailer.send_sfu_email("tcr_dist", email_address, "TCR Distance Results", msg_body, [mat_file]) == 0:
pass
if not download_file:
append_status_file("no download") # in the case that only the email is sent, the directory is still terminated.
# can we also destroy everything during this step?
elif not download_file: # default to downloading the file
append_status_file("request download")
#clear_dir(dir_num)
##### Download file if selected
if download_file:
append_status_file("request download")
#clear_dir(dir_num)
# This is only temporarily disabled
clear_dir(dir_num) # the idea here is that the directory is cleared when everything is done, but the zip is still there
return