forked from nf-core/sarek
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuildReferences.nf
More file actions
442 lines (370 loc) · 12 KB
/
buildReferences.nf
File metadata and controls
442 lines (370 loc) · 12 KB
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
#!/usr/bin/env nextflow
/*
vim: syntax=groovy
-*- mode: groovy;-*-
kate: syntax groovy; space-indent on; indent-width 2;
================================================================================
= C A N C E R A N A L Y S I S W O R K F L O W =
================================================================================
New Cancer Analysis Workflow. Started March 2016.
--------------------------------------------------------------------------------
@Authors
Sebastian DiLorenzo <sebastian.dilorenzo@bils.se> [@Sebastian-D]
Jesper Eisfeldt <jesper.eisfeldt@scilifelab.se> [@J35P312]
Maxime Garcia <maxime.garcia@scilifelab.se> [@MaxUlysse]
Szilveszter Juhos <szilveszter.juhos@scilifelab.se> [@szilvajuhos]
Max Käller <max.kaller@scilifelab.se> [@gulfshores]
Malin Larsson <malin.larsson@scilifelab.se> [@malinlarsson]
Marcel Martin <marcel.martin@scilifelab.se> [@marcelm]
Björn Nystedt <bjorn.nystedt@scilifelab.se> [@bjornnystedt]
Pall Olason <pall.olason@scilifelab.se> [@pallolason]
Pelin Sahlén <pelin.akan@scilifelab.se> [@pelinakan]
--------------------------------------------------------------------------------
@Homepage
http://opensource.scilifelab.se/projects/caw/
--------------------------------------------------------------------------------
@Documentation
https://github.com/SciLifeLab/CAW/README.md
--------------------------------------------------------------------------------
Processes overview
- ProcessReference - Download all references if needed
- DecompressFile - Extract files if needed
- BuildBWAindexes - Build indexes for BWA
- BuildPicardIndex - Build index with Picard
- BuildSAMToolsIndex - Build index with SAMTools
- BuildVCFIndex - Build index for VCF files
================================================================================
= C O N F I G U R A T I O N =
================================================================================
*/
version = '1.2.4'
// Check that Nextflow version is up to date enough
// try / throw / catch works for NF versions < 0.25 when this was implemented
nf_required_version = '0.25.0'
try {
if( ! nextflow.version.matches(">= $nf_required_version") ){
throw GroovyException('Nextflow version too old')
}
} catch (all) {
log.error "====================================================\n" +
" Nextflow version $nf_required_version required! You are running v$workflow.nextflow.version.\n" +
" Pipeline execution will continue, but things may break.\n" +
" Please update Nextflow.\n" +
"============================================================"
}
if (params.help) exit 0, helpMessage()
if (params.version) exit 0, versionMessage()
if (!isAllowedParams(params)) exit 1, "params is unknown, see --help for more information"
if (!checkUppmaxProject()) exit 1, "No UPPMAX project ID found! Use --project <UPPMAX Project ID>"
// Default params:
// Such params are overridden by command line or configuration definitions
// No download
params.download = false
// refDir is empty
params.refDir = ''
verbose = params.verbose
download = params.download ? true : false
if (!download && params.refDir == "" ) exit 1, "No --refDir specified"
if (download && params.refDir != "" ) exit 1, "No need to specify --refDir"
if (params.genome == "smallGRCh37") {
referencesFiles =
[
'1000G_phase1.indels.b37.small.vcf.gz',
'1000G_phase3_20130502_SNP_maf0.3.small.loci',
'b37_cosmic_v74.noCHR.sort.4.1.small.vcf.gz',
'dbsnp_138.b37.small.vcf.gz',
'human_g1k_v37_decoy.small.fasta.gz',
'Mills_and_1000G_gold_standard.indels.b37.small.vcf.gz',
'small.intervals'
]
} else if (params.genome == "GRCh37") {
referencesFiles =
[
'1000G_phase1.indels.b37.vcf.gz',
'1000G_phase3_20130502_SNP_maf0.3.loci.tar.bz2',
'b37_cosmic_v74.noCHR.sort.4.1.vcf.tar.bz2',
'dbsnp_138.b37.vcf.gz',
'human_g1k_v37_decoy.fasta.gz',
'Mills_and_1000G_gold_standard.indels.b37.vcf.gz',
'wgs_calling_regions.grch37.list'
]
} else exit 1, "Can't build this reference genome"
if (download && params.genome != "smallGRCh37") exit 1, "Not possible to download $params.genome references files"
if (!download) referencesFiles.each{checkFile(params.refDir + "/" + it)}
/*
================================================================================
= P R O C E S S E S =
================================================================================
*/
startMessage()
process ProcessReference {
tag download ? {"Download: " + reference} : {"Link: " + reference}
input:
val(reference) from referencesFiles
output:
file(reference) into processedFiles
script:
if (download)
"""
wget https://github.com/szilvajuhos/smallRef/raw/master/$reference
"""
else
"""
ln -s $params.refDir/$reference .
"""
}
if (verbose) processedFiles = processedFiles.view {
"Files preprocessed : $it.fileName"
}
compressedfiles = Channel.create()
notCompressedfiles = Channel.create()
processedFiles
.choice(compressedfiles, notCompressedfiles) {it =~ ".(gz|tar.bz2)" ? 0 : 1}
process DecompressFile {
tag {reference}
input:
file(reference) from compressedfiles
output:
file("*.{vcf,fasta,loci}") into decompressedFiles
script:
realReference="readlink $reference"
if (reference =~ ".gz")
"""
gzip -d -c \$($realReference) > $reference.baseName
"""
else if (reference =~ ".tar.bz2")
"""
tar xvjf \$($realReference)
"""
}
if (verbose) decompressedFiles = decompressedFiles.view {
"Files decomprecessed: $it.fileName"
}
fastaFile = Channel.create()
otherFiles = Channel.create()
vcfFiles = Channel.create()
decompressedFiles
.choice(fastaFile, vcfFiles, otherFiles) {
it =~ ".fasta" ? 0 :
it =~ ".vcf" ? 1 : 2}
notCompressedfiles
.mix(otherFiles)
.collectFile(storeDir: "References/" + params.genome)
fastaForBWA = Channel.create()
fastaForPicard = Channel.create()
fastaForSAMTools = Channel.create()
fastaFile.into(fastaForBWA,fastaForPicard,fastaForSAMTools)
process BuildBWAindexes {
tag {reference}
publishDir "References/" + params.genome, mode: 'copy'
input:
file(reference) from fastaForBWA
output:
file(reference) into fastaFileToKeep
file("*.{amb,ann,bwt,pac,sa}") into bwaIndexes
script:
"""
bwa index $reference
"""
}
if (verbose) fastaFileToKeep.view {
"Fasta File : $it.fileName"
}
if (verbose) bwaIndexes.flatten().view {
"BWA index : $it.fileName"
}
process BuildPicardIndex {
tag {reference}
publishDir "References/" + params.genome, mode: 'copy'
input:
file(reference) from fastaForPicard
output:
file("*.dict") into picardIndex
script:
"""
java -Xmx${task.memory.toGiga()}g \
-jar \$PICARD_HOME/picard.jar \
CreateSequenceDictionary \
REFERENCE=$reference \
OUTPUT=${reference.baseName}.dict
"""
}
if (verbose) picardIndex.view {
"Picard index : $it.fileName"
}
process BuildSAMToolsIndex {
tag {reference}
publishDir "References/" + params.genome, mode: 'copy'
input:
file(reference) from fastaForSAMTools
output:
file("*.fai") into samtoolsIndex
script:
"""
samtools faidx $reference
"""
}
if (verbose) samtoolsIndex.view {
"SAMTools index : $it.fileName"
}
process BuildVCFIndex {
tag {reference}
publishDir "References/" + params.genome, mode: 'copy'
input:
file(reference) from vcfFiles
output:
file(reference) into vcfIndexed
file("*.idx") into vcfIndex
script:
"""
\$IGVTOOLS_HOME/igvtools index $reference
"""
}
if (verbose) vcfIndexed.view {
"VCF indexed : $it.fileName"
}
if (verbose) vcfIndex.view {
"VCF index : $it.fileName"
}
/*
================================================================================
= F U N C T I O N S =
================================================================================
*/
def cawMessage() {
// Display CAW message
log.info "CANCER ANALYSIS WORKFLOW ~ $version - " + this.grabRevision() + (workflow.commitId ? " [$workflow.commitId]" : "")
}
def checkFile(it) {
// Check file existence
final f = file(it)
if (!f.exists()) exit 1, "Missing file: $it, see --help for more information"
return true
}
def checkParams(it) {
// Check if params is in this given list
return it in [
'annotate-tools',
'annotate-VCF',
'annotateTools',
'annotateVCF',
'build',
'call-name',
'callName',
'contact-mail',
'contactMail',
'container-path',
'containerPath',
'containers',
'docker',
'download',
'genome',
'genomes',
'help',
'no-GVCF',
'no-reports',
'noGVCF',
'noReports',
'project',
'push',
'ref-dir',
'refDir',
'repository',
'run-time',
'runTime',
'sample-dir',
'sample',
'sampleDir',
'single-CPUMem',
'singleCPUMem',
'singularity',
'step',
'tag',
'test',
'tools',
'total-memory',
'totalMemory',
'vcflist',
'verbose',
'version']
}
def checkUppmaxProject() {
// check if UPPMAX project number is specified
return !(workflow.profile == 'slurm' && !params.project)
}
def grabRevision() {
// Return the same string executed from github or not
return workflow.revision ?: workflow.commitId ?: workflow.scriptId.substring(0,10)
}
def helpMessage() {
// Display help message
this.cawMessage()
log.info " Usage:"
log.info " nextflow run buildReferences.nf --refDir <pathToRefDir> --genome <genome>"
log.info " nextflow run buildReferences.nf --download --genome smallGRCh37"
log.info " nextflow run SciLifeLab/CAW --test [--step STEP] [--tools TOOL[,TOOL]] --genome <Genome>"
log.info " --download"
log.info " Download reference files. (only with --genome smallGRCh37)"
log.info " --refDir <Directoy>"
log.info " Specify a directory containing reference files."
log.info " --genome <Genome>"
log.info " Use a specific genome version."
log.info " Possible values are:"
log.info " GRCh37"
log.info " smallGRCh37 (Build a small reference (for tests))"
log.info " --help"
log.info " you're reading it"
log.info " --version"
log.info " displays version number"
}
def isAllowedParams(params) {
// Compare params to list of verified params
final test = true
params.each{
if (!checkParams(it.toString().split('=')[0])) {
println "params ${it.toString().split('=')[0]} is unknown"
test = false
}
}
return test
}
def minimalInformationMessage() {
// Minimal information message
log.info "Command Line: $workflow.commandLine"
log.info "Project Dir : $workflow.projectDir"
log.info "Launch Dir : $workflow.launchDir"
log.info "Work Dir : $workflow.workDir"
log.info "Genome : " + params.genome
}
def nextflowMessage() {
// Nextflow message (version + build)
log.info "N E X T F L O W ~ version $workflow.nextflow.version $workflow.nextflow.build"
}
def startMessage() {
// Display start message
this.cawMessage()
this.minimalInformationMessage()
}
def versionMessage() {
// Display version message
log.info "CANCER ANALYSIS WORKFLOW"
log.info " version : $version"
log.info workflow.commitId ? "Git info : $workflow.repository - $workflow.revision [$workflow.commitId]" : " revision : " + this.grabRevision()
}
workflow.onComplete {
// Display complete message
this.nextflowMessage()
this.cawMessage()
this.minimalInformationMessage()
log.info "Completed at: $workflow.complete"
log.info "Duration : $workflow.duration"
log.info "Success : $workflow.success"
log.info "Exit status : $workflow.exitStatus"
log.info "Error report: " + (workflow.errorReport ?: '-')
}
workflow.onError {
// Display error message
this.nextflowMessage()
this.cawMessage()
log.info "Workflow execution stopped with the following message: " + workflow.errorMessage
}