-
Notifications
You must be signed in to change notification settings - Fork 4
/
tasks.py
executable file
·544 lines (423 loc) · 17.2 KB
/
tasks.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import luigi
import os
from subprocess import call
from shutil import copyfile
from base64 import b64encode
import uuid
from gemmaclient import GemmaClientAPI
#from meta import ExcavateFQ
#from meta import LogAssembly
class BaseTask(luigi.Task):
here = os.path.dirname(os.path.realpath(__file__))
commit_dir = os.path.dirname(os.path.realpath(__file__)) + "/commit"
wd = here + "/../Pipelines/rsem/"
MODES = os.getenv("MODES")
SCRIPTS = os.getenv("SCRIPTS")
# Para meters
gse = luigi.Parameter()
nsamples = luigi.Parameter(default=0)
scope = luigi.Parameter(default="genes")
ignorecommit = luigi.Parameter(default=0)
class QcGSE(BaseTask):
wd = BaseTask.SCRIPTS + "/"
method = "./qc_download.sh"
# Todo: Check taxon/assembly relation.
def requires(self):
return DownloadGSE(self.gse, self.nsamples)
def output(self):
return luigi.LocalTarget(self.commit_dir + "/qc_%s.tsv" % self.gse)
def run(self):
try:
print "Running from " + self.wd
os.chdir(self.wd)
except Exception as e:
print "=======> QcGSE <=========="
print e.message
print "Error changing to", self.wd, "when in", os.getcwd()
raise e
# Call job
try:
job = [ self.method, self.gse, self.nsamples ]
ret = None
ret = call(job)
except Exception as e:
print "EXCEPTION:", e, "with", " ".join([str(x) for x in job])
print "Message:", e.message
raise e
if ret != 0:
print "# Job '{}' executed, but failed with exit code {}.".format( " ".join([str(x) for x in job]), ret)
exit(-1)
# Commit output
with self.output().open('w') as out_file:
out_file.write(self.gse+"\n")
class CountGSE(BaseTask):
method = "./rsem_count.sh" # TODO: Generalize
#wd = "."
def init(self):
"""
Set paths and whatnot.
"""
if self.scope is None:
self.scope = "genes"
quantDir = os.environ['QUANTDIR']
countDir = os.environ['COUNTDIR']
if 'SCOPE' in os.environ.keys():
self.scope = os.environ['SCOPE']
try:
os.mkdir(countDir)
except:
pass # Dir must exists
print "INFO: QUANTDIR => ", quantDir
print "INFO: COUNTDIR => ", countDir
self.path_to_inputs = quantDir + "/" +str(self.gse)+ "/"
self.count_source = self.path_to_inputs + "countMatrix."+self.scope
self.count_destination = countDir +str(self.gse)+ "_counts."+self.scope
self.fpkm_source = self.path_to_inputs + "fpkmMatrix."+self.scope
self.fpkm_destination = countDir +str(self.gse)+ "_fpkm."+self.scope
self.tpm_source = self.path_to_inputs + "tpmMatrix."+self.scope
self.tpm_destination = countDir +str(self.gse)+ "_tpm."+self.scope
def requires(self):
self.init()
return ProcessGSE(self.gse, self.nsamples)
def output(self):
return luigi.LocalTarget(self.commit_dir + "/count_%s.tsv" % self.gse)
def run(self):
try:
os.chdir(self.wd)
except Exception as e:
print "=======> CountGSE <=========="
print e.message
print "Error changing to", self.wd, "when in", os.getcwd()
raise e
# Call job
try:
job = [ self.method, self.path_to_inputs, self.scope ]
ret = call(job)
print "Copying files from (e.g. ", self.count_source, ") to destination (e.g. ", self.count_destination, ")."
copyfile( self.count_source, self.count_destination)
copyfile( self.fpkm_source, self.fpkm_destination)
copyfile( self.tpm_source, self.tpm_destination)
# Might as well do isoforms while we're here.
job = [ self.method, self.path_to_inputs, "isoforms" ]
ret = call(job)
except Exception as e:
print "EXCEPTION:", e, "with", " ".join(job)
print e.message
ret = -1
if ret:
exit("Job '{}' failed with exit code {}.".format( " ".join(job), ret))
# Commit output
with self.output().open('w') as out_file:
out_file.write(self.gse+"\n")
class CheckGemmaGSE(BaseTask):
"""
Check that the GSE is ready to be loaded in Gemma.
"""
method = None
method_args = None
def init(self):
"""
Set CLI method.
"""
try:
self.method = os.getenv("GEMMACMD").split(" ")
#self.method_args = ["addGEOData", "-u", os.getenv("GEMMAUSERNAME"), "-p", os.getenv("GEMMAPASSWORD"), "-e", self.gse, "--allowsuper"]
self.method_args = ["addGEOData", "-u", os.getenv("GEMMAUSERNAME"), "-p", os.getenv("GEMMAPASSWORD"), "-e", self.gse]
except Exception as e:
print "$GEMMACMD/GEMMAUSERNAME/GEMMPASSWORD appear to not all be set. Please set environment variables."
raise e
print "INFO: Method => " + str(self.method_args)
def requires(self):
self.init()
return CountGSE(self.gse, self.nsamples)
def output(self):
return luigi.LocalTarget(self.commit_dir + "/checkgemma_%s.tsv" % self.gse)
def run(self):
prejob = self.method + []
job = self.method + self.method_args
# Call job
try:
g = GemmaClientAPI(dataset=self.gse)
username, password = [os.getenv(x) for x in ["GEMMAUSERNAME", "GEMMAPASSWORD"] ]
g.setCredentials(username, password)
ret = 0
if g.isEmpty():
print self.gse + " is not created in Gemma."
try:
print "Attempting to create experiment for " + self.gse
ret = call(job)
print "Done."
print "Checking that the experiment exists."
g.clear()
if g.isEmpty():
print "Experiment doesn't appear to have been added."
ret = -1
else:
print "Experiment added."
except Exception as e:
print "EXCEPTION: Could not create Gemma experiment with '" + " ".join(job) + "' ."
ret = -1
else:
print self.gse + " exists and should be ready for upload."
ret = 0
except Exception as e:
print "EXCEPTION:", e, "with checkGemma for", self.gse
print e.message
ret = -1
if ret:
print "Error code", ret, "."
print ""
exit( "CheckGemmaGSE failed for request '{}' with exit code {}.".format( g.getUrl(), ret) )
# Commit output
with self.output().open('w') as out_file:
out_file.write(g.toString()+"\n")
class LoadGemmaGSE(BaseTask):
"""
Load into Gemma.
"""
method = None
method_args = None
def init(self):
"""
Set CLI method.
"""
try:
# Check if GEMMACMD is set, otherwise problems will happen later.
foo = os.getenv("GEMMACMD").split(" ")
foo = os.getenv("GEMMAUSERNAME")
foo = os.getenv("GEMMAPASSWORD")
except Exception as e:
print "$GEMMACMD/GEMMAUSERNAME/GEMMPASSWORD appear to not all be set. Please set environment variables."
raise e
try:
PATH = os.getenv("COUNTDIR")
TAXONS = ['human', 'mouse', 'rat']
MODES = os.getenv("MODES").split(",")
SCRIPTS = os.getenv("SCRIPTS")
taxon = None
for taxon_ in MODES :
if taxon_ in TAXONS:
taxon = taxon_
break
print "Using scripts from", SCRIPTS
print "Loading matrices from", PATH
print "Taxon:", taxon
self.method = ["python", SCRIPTS+"/load_rnaseq_to_gemma.py"]
self.method_args = ["--shortname", self.gse, "--taxon", taxon, "--path", PATH]
except Exception as e:
print "Error accessing environment variables."
raise e
print "INFO: Method => " + str(self.method_args)
def requires(self):
self.init()
return CheckGemmaGSE(self.gse, self.nsamples)
def output(self):
return luigi.LocalTarget(self.commit_dir + "/loadgemma_%s.tsv" % self.gse)
def run(self):
job = self.method + self.method_args
# Call job
try:
print "Attempting to load experiment for " + self.gse
ret = call(job)
print "Done."
except Exception as e:
print "EXCEPTION: Could not load Gemma experiment with '" + " ".join(job) + "' ."
ret = -1
if ret:
print "Error code", ret, "."
print ""
exit( "LoadGemmaGSE failed for request '{}' with exit code {}.".format( " ".join(job), ret) )
# Commit output
with self.output().open('w') as out_file:
out_file.write(" ".join(job) + "\n")
class GatherMetadataGSE(BaseTask):
method = ["bash", "-c"]
method_args = None
metadataDir = None
def init(self):
"""
Set paths and whatnot.
"""
SCRIPTS = os.getenv("SCRIPTS")
self.base_method = SCRIPTS + "/" + "pipeline_metadata.sh"
self.alignment_method = SCRIPTS + "/" + "alignment_metadata.sh"
self.qc_method = SCRIPTS + "/" + "qc_report.sh"
self.method_args = [self.gse]
self.metadataDir = os.environ['METADATA'] + "/" + self.gse
print "INFO: METADATA DIRECTORY => ", os.environ['METADATA']
def requires(self):
self.init()
return CountGSE(self.gse, self.nsamples)
def output(self):
return luigi.LocalTarget(self.commit_dir + "/metadata_%s.tsv" % self.gse)
def run(self):
# Change working directory
try:
os.chdir(os.environ['SCRIPTS'])
print "Entered:", os.environ['SCRIPTS']
except Exception as e:
print "=======> GatherMetadataGSE <=========="
print e.message
print "Error changing to", self.wd, "when in", os.getcwd()
raise e
# Create metadata directory for GSE
try:
print "Creating",self.metadataDir
job = [ "mkdir", "-p", self.metadataDir ]
ret = call(job)
job = None
except Exception as e:
print "EXCEPTION: Could not create directory at " + self.metadataDir
raise e
# Call base metadata gathering job
try:
base_metadata_file = self.metadataDir + "/" + self.gse + ".base.metadata"
print "Generating base metadata", base_metadata_file
f = open(base_metadata_file,"wb")
ferr = open("/dev/null", 'wb')
job = [ self.base_method ] + self.method_args
print "Call:", " ".join(job)
ret = call(job, stdout=f, stderr=ferr)
job = None
f.close()
#os.system( self.base_method + " " + self.gse + " '\t' " " > " + base_metadata_file )
except Exception as e:
print "EXCEPTION: Gathering base metadata", e, "with", " ".join(job)
print e.message
ret = -1
raise e
# Call alignment metadata job
try:
print "Gathering STAR metadata to", self.metadataDir
job = [ self.alignment_method ] + self.method_args
print "Call:", " ".join(job)
ret = call(job)
job = None
print "Done."
except Exception as e:
print "EXCEPTION: Gathering STAR metadata", e, "with", " ".join(job)
print e.message
ret = -1
raise e
# Call alignment metadata job
try:
print "Gathering FastQC reports to", self.metadataDir
job = [ self.qc_method ] + self.method_args
print "Call:", " ".join(job)
ret = call(job)
job = None
print "Done."
except Exception as e:
print "EXCEPTION: Gathering FastQC metadata", e, "with", " ".join(job)
print e.message
ret = -1
raise e
if ret:
print "Error code", ret,"."
print ""
exit("Job '{}' failed with exit code {}.".format( " ".join(job), ret))
# Commit output
with self.output().open('w') as out_file:
out_file.write(self.gse+"\n")
class PurgeGSE(BaseTask):
method = "rm"
method_args = "-rf"
def init(self):
"""
Set paths and whatnot.
"""
dataDir = os.environ['DATA']
# TODO: Possibly, also get rid of quant dir?
print "INFO: DATADIR => ", dataDir
self.purgeDir = dataDir + "/" + str(self.gse)+ "/"
def requires(self):
self.init()
return GatherMetadataGSE(self.gse, self.nsamples)
def output(self):
return luigi.LocalTarget(self.commit_dir + "/purge_%s.tsv" % self.gse)
def run(self):
try:
os.chdir(self.wd)
except Exception as e:
print "=======> CountGSE <=========="
print e.message
print "Error changing to", self.wd, "when in", os.getcwd()
raise e
# Call job
try:
## Workaround for missing DIR
job = [ "mkdir", "-p", self.purgeDir ]
ret = call(job)
## Ok, not delete directory
print "Deleting files from", self.purgeDir, "."
job = [ self.method, self.method_args, self.purgeDir ]
ret = call(job)
print "Done."
except Exception as e:
print "EXCEPTION:", e, "with", " ".join(job)
print e.message
ret = -1
if ret:
print "Error code", ret,"."
print ""
exit("Job '{}' failed with exit code {}.".format( " ".join(job), ret))
# Commit output
with self.output().open('w') as out_file:
out_file.write(self.gse+"\n")
class ProcessGSE(BaseTask):
method = "./multiple_rsem.sh" # TODO: Generalize
def requires(self):
return QcGSE(self.gse, self.nsamples)
def output(self):
uniqueID=""
if int(self.ignorecommit) == 1:
print "INFO: Ignoring previous commits."
uniqueID = "_" + str(uuid.uuid1()) # Skipping commit logic.
return luigi.LocalTarget(self.commit_dir + "/process" +uniqueID+ "_%s.tsv" % self.gse)
def run(self):
try:
os.chdir(self.wd)
except Exception as e:
print "Error changing to", self.wd, "when in", os.getcwd()
raise e
MODES = "MODES='"+BaseTask.MODES+"'"
# Call job
try:
job = [self.method, self.gse, self.gse]
ret = call(job, env=os.environ)
except Exception as e:
print "=======> ProcessGSE <=========="
print e.message
print "EXCEPTION:", e, "with", " ".join(job)
ret = -1
if ret:
exit("Job '{}' failed with exit code {}.".format( " ".join(job), ret))
# Commit output
with self.output().open('w') as out_file:
out_file.write(self.gse+"\n")
class DownloadGSE(BaseTask):
#method_geo = "../scripts/geo_to_sra.R" # TODO: Generalize
#method_geo = "/space/grp/Pipelines/rnaseq-pipeline/scripts/geo_to_sra.R" # TODO: Generalize
#method_arrayexpres = "/space/grp/Pipelines/rnaseq-pipeline/scripts/arrayexpress_to_fastq.R"
method_arrayexpress = "/space/grp/Pipelines/rnaseq-pipeline/scripts/arrayexpress_to_fastq.sh"
method_gse = "/space/grp/Pipelines/rnaseq-pipeline/scripts/GSE_to_fastq.sh"
method = None
GEO_TOKENS = ["GSE"]
def output(self):
return luigi.LocalTarget(self.commit_dir + "/download_%s.tsv" % self.gse)
def run(self):
# TODO: Figure wheter to call the GEO or ArrayExpress script
if any([ TOKEN in self.gse for TOKEN in self.GEO_TOKENS ]):
self.method = self.method_gse
else:
self.method = self.method_arrayexpress
# Call job
#job = ["Rscript", self.method, self.gse]
#job = ['ssh', 'chalmers', 'cd', '/space/grp/Pipelines/rnaseq-pipeline/scripts/', '&&', "Rscript", self.method, self.gse]
job = [self.method, self.gse]
ret = call(job)
if ret:
exit("Job '{}' failed with exit code {}.".format( " ".join(job), ret))
# Commit output
with self.output().open('w') as out_file:
out_file.write(self.gse+"\n")