forked from laramaktub/TRUFA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
#lara_launcher.py#
executable file
·362 lines (284 loc) · 12.6 KB
/
#lara_launcher.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
#! /usr/bin/env python
# THINGS TO IMPROVE:
# THINGS TO FIX:
# if bowtie2 directly with cufflinks or express (directly from assembly) >>> folder created in .cache/tmp
# >>> this has been fixed but clumsy in bowtie2.cmd
# THINGS TO INSTALL:
#CDHITEST
# THINGS TO IMPROVE:
# in bowtie 2 command, check the output files names (the first linesof the cmd)
# remove env variables (not used the same way anymore) <<< not sure about that
# Make better the import of the parameters (without having to delete the other entries in sort_parameters)
# THINGS TO VERIFY (TESTING PARAMETERS NOT CORRECT FOR REAL USE)
# input files (reads)
#-------------------------------------------------------------------------------
import logging
import logging.handlers
import sys
import os
import getopt
import mimetypes
from lib import submiting_jobs as sj
from lib import soft_data
from lib import config
from lib import run_config
#from lib import common_fun
#-------------------------------------------------------------------------------
def configLog():
# get logger
logger = logging.getLogger()
# create file handler
ch = logging.handlers.RotatingFileHandler( config.LOGFILE, mode='a', maxBytes=config.LOGFILEBYTES, backupCount=5 )
ch.setLevel( logging.DEBUG )
# create formatter
formatter = logging.Formatter(
fmt='%(asctime)s -%(levelname)s- %(message)s',
datefmt='%Y%m%d %H:%M:%S' )
# add formatter to ch
ch.setFormatter( formatter )
# add ch to logger
logger.addHandler( ch )
logging.captureWarnings( True )
#-------------------------------------------------------------------------------
def checkIfCommandLine():
""" Check if command line or webserver version.
For command line: edit run_config in lib and launch ./pipe_launcher -c """
try:
opts, args = getopt.getopt(sys.argv[1:], "c")
except getopt.GetoptError as err:
print str(err)
sys.exit(2)
for o, a in opts:
if o == "-c":
return True
else:
return False
#-------------------------------------------------------------------------------
def setUidJid(COMMAND_MODE):
if COMMAND_MODE:
print "Launching in command line mode"
REALUSER = run_config.REALUSER
job_num = run_config.JOB_NUM
para_dict = run_config.para_dict
DEBUG = run_config.DEBUG
# common_fun.check_inputs_format(para_dict)
else: # if run by webserver
print "Launched by the webserver"
DEBUG = False
REALUSER, para_dict = sj.get_parameters(sys.argv[1],sys.argv[2])
job_num = sys.argv[-1]
return REALUSER, job_num, para_dict, DEBUG
#-------------------------------------------------------------------------------
def setPaths():
OUT_FOLDER = config.OUT_FOLDER + REALUSER + "/jobs/Job_" + job_num + "/"
DATA_FOLDER = config.OUT_FOLDER + REALUSER + "/data/"
folders_dict = dict(OUT_FOLDER = OUT_FOLDER,
DATA_FOLDER = DATA_FOLDER,
CACHE_PATH = OUT_FOLDER + ".cache/",
CLEANING_FOLDER = OUT_FOLDER + "CLEANING/",
STAT_FOLDER = OUT_FOLDER + "STAT/",
ASSEMBLY_MAPPING_FOLDER = OUT_FOLDER + "ASSEMBLY_MAPPING/",
IDENTIFICATION_FOLDER = OUT_FOLDER + "IDENTIFICATION/",
EXPRESSION_FOLDER = OUT_FOLDER + "EXPRESSION/",
PIPE_PATH = config.PIPE_PATH,
PY_BIOLIB_PATH = config.PY_BIOLIB_PATH,
COMMAND_PATH = config.COMMAND_PATH)
COMMAND_PATH = config.COMMAND_PATH
BLAST_DB_FOLDER = config.BLAST_DB_FOLDER
HMMER_PROFILE_FOLDER = config.HMMER_PROFILE_FOLDER
return folders_dict, COMMAND_PATH, BLAST_DB_FOLDER, HMMER_PROFILE_FOLDER
#-------------------------------------------------------------------------------
def makeOutputFolders(folders_dict):
try:
os.makedirs(folders_dict["OUT_FOLDER"])
os.makedirs(folders_dict["CACHE_PATH"])
os.mkdir(folders_dict["CACHE_PATH"] + "tmp")
os.mkdir(folders_dict["STAT_FOLDER"])
except Exception as e:
logging.error(e)
sys.exit()
#-------------------------------------------------------------------------------
def getInput(para_dict):
input_dict = sj.sort_parameters(para_dict)
print(input_dict)
READS_FILES = input_dict["reads_files"]
READS_FILES_COUNT = len(READS_FILES)
BLATN_CUSTOM_READS = input_dict["blatn_reads"]
BLATN_CUSTOM_ASS = input_dict["blatn_ass"]
BLATX_CUSTOM_ASS = input_dict["blatx_ass"]
HMMER_CUSTOM_DB = input_dict["hmm_custom_db"]
return input_dict, READS_FILES, READS_FILES_COUNT, BLATN_CUSTOM_READS,BLATN_CUSTOM_ASS, BLATX_CUSTOM_ASS, HMMER_CUSTOM_DB
#-------------------------------------------------------------------------------
def setBashEnv(input_dict, folders_dict):
env = sj.set_environment(input_dict, folders_dict)
env["MINIMAL_OUTPUT"]=config.MINIMAL_OUTPUT
# Add for MPIBLAST:
#env["BLASTMAT"]="/mnt/seq_dbs/matrices"
#env["MPIBLAST_SHARED"]="/mnt/seq_dbs/nr"
#env["MPIBLAST_LOCAL"]=folders_dict["IDENTIFICATION_FOLDER"] + ""
return env
#-------------------------------------------------------------------------------
def checkIfCompressedReads(folders_dict, READS_FILES):
new_READS_FILES = []
with open(folders_dict["OUT_FOLDER"] + ".tmp1.txt", "a") as f:
files_to_unzip = []
for rf in READS_FILES:
raw = rf
rf = rf.split( os.extsep, 2 )
fname = ".".join( rf[:2] )
ext = rf[-1]
# Check extension:
if ext in ["gz","tar.gz"]:
f.write("File: {0} is set for extraction\n".format(raw))
files_to_unzip.append(raw)
new_READS_FILES.append(fname)
else:
f.write("""File: {0} doesn't seem to be a compressed file,
or his extension is not recognized, so it will be considered
as a 'directly usable' file\n""".format(raw))
new_READS_FILES.append(raw)
return new_READS_FILES, files_to_unzip
#-------------------------------------------------------------------------------
def extract(files_to_unzip, OUT_FOLDER, COMMAND_PATH, dep):
env["FILES_TO_UNZIP"] = "'" + " ".join(files_to_unzip) + "'"
job_id = sj.make_and_submit_job(OUT_FOLDER,
COMMAND_PATH + "extract2.cmd",
"extract", 1,
1, "12:00:00","", env)
# slurm_ids.append(job_id)
dep.append(job_id)
return dep
#-------------------------------------------------------------------------------
class TrufaJob():
"""
To define better names
HERE:
jobfolder is for the whole Job, composed of TrufaJobs e.g; OUT_FOLDER
outfolder is the outfolder of the TrufaJob: STAT_FOLDER/fastqc_report
"""
def __init__(self, name, command, dep, jobfolder, ntasks, ncpus, tlim, env, outfolder=None):
self.name = name
self.command_path = config.COMMAND_PATH + command
self.dep = dep
self.jobfolder = jobfolder
self.outfolder = outfolder
self.ntasks = ntasks
self.ncpus = ncpus
self.tlim = tlim
self.env = env
def makeOutFolder(self):
if self.outfolder:
if not os.path.exists(self.outfolder):
os.makedirs(self.outfolder)
else:
print self.name
print self.outfolder + " already exists: not created (it's fine)."
def script(self):
sj.make_script(self.jobfolder,
self.command_path,
self.name,
self.ntasks,
self.ncpus,
self.tlim,
self.dep,
self.env,
DEBUG)
def submit(self):
job_id = sj.make_and_submit_job(self.jobfolder,
self.command_path,
self.name,
self.ntasks,
self.ncpus,
self.tlim,
self.dep,
self.env,
DEBUG)
return job_id
#-------------------------------------------------------------------------------
def prepareAndSubmit( jobname, cmd, dep, jobfolder, ntasks, cpus, tlim, env, outfolder=None):
"""
Prepare the outfolders and submit a TrufaJob
"""
try:
job = TrufaJob( jobname, cmd, dep, jobfolder, ntasks, cpus, tlim, env, outfolder)
job.makeOutFolder()
slurm_id = job.submit()
except Exception as e:
logging.error(e)
sys.exit()
return slurm_id
#------------------------------------------------------------------------------
def prepareScript( jobname, cmd, dep, jobfolder, ntasks, cpus, tlim, env, outfolder=None):
"""
Prepare the outfolders and submit a TrufaJob
"""
try:
print cmd
script = TrufaJob( jobname, cmd, dep, jobfolder, ntasks, cpus, tlim, env, outfolder)
script.script()
except Exception as e:
logging.error(e)
sys.exit()
return slurm_id
#-------------------------------------------------------------------------------
def setReadOut( names_in, suffix):
""" To setup the names of the output for the cleaning steps
"""
# Setup the reads files basenames for the cleaning outputs
names_in = names_in.replace("'", "").split(" ")
ext = [ os.path.splitext(x)[-1] for x in names_in ]
basenames = [ os.path.splitext(os.path.basename(x))[0]
for x in names_in ]
names_out = "'" + " ".join( [ x + suffix + y
for x, y in zip(basenames, ext) ] ) + "'"
return names_out
#-------------------------------------------------------------------------------
# MAIN
#-------------------------------------------------------------------------------
logging.getLogger().setLevel( logging.DEBUG )
configLog()
logging.info("Start server side")
# INIT VAR: # Not in the same line because of issues with values assignments
slurm_ids = []
dep = []
blat_dep = []
hmmer_dep = []
b2go_dep = []
expr_dep = []
COMMAND_MODE = checkIfCommandLine()
REALUSER, job_num, para_dict, DEBUG = setUidJid(COMMAND_MODE)
folders_dict, COMMAND_PATH, BLAST_DB_FOLDER, HMMER_PROFILE_FOLDER = setPaths()
makeOutputFolders(folders_dict)
input_dict, READS_FILES, READS_FILES_COUNT, BLATN_CUSTOM_READS,BLATN_CUSTOM_ASS, BLATX_CUSTOM_ASS, HMMER_CUSTOM_DB = getInput(para_dict)
steps = input_dict["progs"]
env = setBashEnv(input_dict, folders_dict)
# Make LOG for debugging
sj.make_log(REALUSER, input_dict, folders_dict["OUT_FOLDER"] + ".tmp1.txt")
# Check and file decompression
new_READS_FILES, files_to_unzip = checkIfCompressedReads(folders_dict, READS_FILES)
if files_to_unzip and not DEBUG:
dep = extract(files_to_unzip, folders_dict["OUT_FOLDER"], COMMAND_PATH, dep)
# Resetting READS_FILES variable to point to extracted files
env["READS_FILES"] = "'" + " ".join(new_READS_FILES) + "'"
#-------------------------------------------------------------------------------
if "FASTQC1" in steps:
#Generate the docker job
slurm_id = prepareAndSubmit("fastqc",
"dockers/fastqc.cmd",
dep,
folders_dict["OUT_FOLDER"],
READS_FILES_COUNT,2,"03:00:00",env,
folders_dict["STAT_FOLDER"] + "fastqc_report")
prepareScript("fastqc",
"cleaning/larafastqc.cmd",
dep,
folders_dict["OUT_FOLDER"],
READS_FILES_COUNT,2,"03:00:00",env,
folders_dict["STAT_FOLDER"] + "fastqc_report")
slurm_ids.append( slurm_id )
# Fastqc doesn't have to be incorporated in the dependency list
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# print list of SLURM IDS:
if not DEBUG:
print "slurmids: " + ",".join(slurm_ids)