-
Notifications
You must be signed in to change notification settings - Fork 38
/
demultiplex.nf
436 lines (375 loc) · 17.5 KB
/
demultiplex.nf
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
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
VALIDATE INPUTS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
def valid_params = [
demultiplexers: ["bases2fastq", "bcl2fastq", "bclconvert", "fqtk", "sgdemux"]
]
def summary_params = NfcoreSchema.paramsSummaryMap(workflow, params)
// Validate input parameters
WorkflowDemultiplex.initialise(params, log, valid_params)
// Check input path parameters to see if they exist
def checkPathParamList = [
params.input,
params.multiqc_config
]
for (param in checkPathParamList) { if (param) { file(param, checkIfExists: true) } }
// Check mandatory parameters
if (params.input) { ch_input = file(params.input) } else { Nextflow.error 'Input samplesheet not specified!' }
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CONFIG FILES
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
ch_multiqc_config = Channel.fromPath("$projectDir/assets/multiqc_config.yml", checkIfExists: true)
ch_multiqc_custom_config = params.multiqc_config ? Channel.fromPath( params.multiqc_config, checkIfExists: true ) : Channel.empty()
ch_multiqc_logo = params.multiqc_logo ? Channel.fromPath( params.multiqc_logo, checkIfExists: true ) : Channel.empty()
ch_multiqc_custom_methods_description = params.multiqc_methods_description ? file(params.multiqc_methods_description, checkIfExists: true) : file("$projectDir/assets/methods_description_template.yml", checkIfExists: true)
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
IMPORT LOCAL MODULES/SUBWORKFLOWS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
//
// SUBWORKFLOW: Consisting of a mix of local and nf-core/modules
//
include { BCL_DEMULTIPLEX } from '../subworkflows/nf-core/bcl_demultiplex/main'
include { BASES_DEMULTIPLEX } from '../subworkflows/local/bases_demultiplex/main'
include { FQTK_DEMULTIPLEX } from '../subworkflows/local/fqtk_demultiplex/main'
include { SINGULAR_DEMULTIPLEX } from '../subworkflows/local/singular_demultiplex/main'
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
IMPORT NF-CORE MODULES/SUBWORKFLOWS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
//
// MODULE: Installed directly from nf-core/modules
//
include { CUSTOM_DUMPSOFTWAREVERSIONS } from '../modules/nf-core/custom/dumpsoftwareversions/main'
include { FASTP } from '../modules/nf-core/fastp/main'
include { FALCO } from '../modules/nf-core/falco/main'
include { MULTIQC } from '../modules/nf-core/multiqc/main'
include { UNTAR } from '../modules/nf-core/untar/main'
include { MD5SUM } from '../modules/nf-core/md5sum/main'
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RUN MAIN WORKFLOW
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// Info required for completion email and summary
def multiqc_report = []
workflow DEMULTIPLEX {
// Value inputs
demultiplexer = params.demultiplexer // string: bases2fastq, bcl2fastq, bclconvert, fqtk, sgdemux
trim_fastq = params.trim_fastq // boolean: true, false
skip_tools = params.skip_tools ? params.skip_tools.split(',') : [] // list: [falco, fastp, multiqc]
// Channel inputs
ch_versions = Channel.empty()
ch_multiqc_files = Channel.empty()
// Sanitize inputs and separate input types
// FQTK's input contains an extra column 'per_flowcell_manifest' so it is handled seperately
// For reference:
// https://raw.githubusercontent.com/nf-core/test-datasets/demultiplex/samplesheet/1.3.0/fqtk-samplesheet.csv VS
// https://raw.githubusercontent.com/nf-core/test-datasets/demultiplex/samplesheet/1.3.0/sgdemux-samplesheet.csv
if (demultiplexer == 'fqtk'){
ch_inputs = extract_csv_fqtk(ch_input)
ch_inputs.dump(tag: 'DEMULTIPLEX::inputs',{FormattingService.prettyFormat(it)})
// Split flowcells into separate channels containg run as tar and run as path
// https://nextflow.slack.com/archives/C02T98A23U7/p1650963988498929
ch_flowcells = ch_inputs
.branch { meta, samplesheet, run, manifest ->
tar: run.toString().endsWith('.tar.gz')
dir: true
}
ch_flowcells_tar = ch_flowcells.tar
.multiMap { meta, samplesheet, run, manifest ->
samplesheets: [ meta, samplesheet, manifest ]
run_dirs: [ meta, run ]
}
} else {
ch_inputs = extract_csv(ch_input)
ch_inputs.dump(tag: 'DEMULTIPLEX::inputs',{FormattingService.prettyFormat(it)})
// Split flowcells into separate channels containg run as tar and run as path
// https://nextflow.slack.com/archives/C02T98A23U7/p1650963988498929
ch_flowcells = ch_inputs
.branch { meta, samplesheet, run ->
tar: run.toString().endsWith('.tar.gz')
dir: true
}
ch_flowcells_tar = ch_flowcells.tar
.multiMap { meta, samplesheet, run ->
samplesheets: [ meta, samplesheet ]
run_dirs: [ meta, run ]
}
}
// MODULE: untar
// Runs when run_dir is a tar archive
// Except for bclconvert and bcl2fastq for wich we untar in the process
// Re-join the metadata and the untarred run directory with the samplesheet
if (demultiplexer in ['bclconvert', 'bcl2fastq']) ch_flowcells_tar_merged = ch_flowcells_tar.samplesheets.join(ch_flowcells_tar.run_dirs, failOnMismatch:true, failOnDuplicate:true)
else {
ch_flowcells_tar_merged = ch_flowcells_tar.samplesheets.join( UNTAR ( ch_flowcells_tar.run_dirs ).untar, failOnMismatch:true, failOnDuplicate:true )
ch_versions = ch_versions.mix(UNTAR.out.versions)
}
// Merge the two channels back together
ch_flowcells = ch_flowcells.dir.mix(ch_flowcells_tar_merged)
// RUN demultiplexing
//
ch_raw_fastq = Channel.empty()
switch (demultiplexer) {
case 'bases2fastq':
// MODULE: bases2fastq
// Runs when "demultiplexer" is set to "bases2fastq"
BASES_DEMULTIPLEX ( ch_flowcells )
ch_raw_fastq = ch_raw_fastq.mix(BASES_DEMULTIPLEX.out.fastq)
// TODO: verify that this is the correct output
ch_multiqc_files = ch_multiqc_files.mix(BASES_DEMULTIPLEX.out.metrics.map { meta, metrics -> return metrics} )
ch_versions = ch_versions.mix(BASES_DEMULTIPLEX.out.versions)
break
case ['bcl2fastq', 'bclconvert']:
// SUBWORKFLOW: illumina
// Runs when "demultiplexer" is set to "bclconvert" or "bcl2fastq"
BCL_DEMULTIPLEX( ch_flowcells, demultiplexer )
ch_raw_fastq = ch_raw_fastq.mix( BCL_DEMULTIPLEX.out.fastq )
ch_multiqc_files = ch_multiqc_files.mix( BCL_DEMULTIPLEX.out.reports.map { meta, report -> return report} )
ch_multiqc_files = ch_multiqc_files.mix( BCL_DEMULTIPLEX.out.stats.map { meta, stats -> return stats } )
ch_versions = ch_versions.mix(BCL_DEMULTIPLEX.out.versions)
break
case 'fqtk':
// MODULE: fqtk
// Runs when "demultiplexer" is set to "fqtk"
// Collect fastqs and read structures from field 2 of ch_flowcells
fastq_read_structure = ch_flowcells.map{it[2]}
.splitCsv(header:true)
.map{[it.fastq, it.read_structure]}
// Combine the directory containing the fastq with the fastq name and read structure
// [example_R1.fastq.gz, 150T, ./work/98/30bc..78y/fastqs/]
fastqs_with_paths = fastq_read_structure.combine(UNTAR.out.untar.collect{it[1]}).toList()
// Format ch_input like so:
// [[meta:id], <path to sample names and barcodes in tsv: path>, [<fastq name: string>, <read structure: string>, <path to fastqs: path>]]]
ch_input = ch_flowcells.merge( fastqs_with_paths ) { a,b -> tuple(a[0], a[1], b)}
FQTK_DEMULTIPLEX ( ch_input )
ch_raw_fastq = ch_raw_fastq.mix(FQTK_DEMULTIPLEX.out.fastq)
ch_multiqc_files = ch_multiqc_files.mix(FQTK_DEMULTIPLEX.out.metrics.map { meta, metrics -> return metrics} )
ch_versions = ch_versions.mix(FQTK_DEMULTIPLEX.out.versions)
break
case 'sgdemux':
// MODULE: sgdemux
// Runs when "demultiplexer" is set to "sgdemux"
SINGULAR_DEMULTIPLEX ( ch_flowcells )
ch_raw_fastq = ch_raw_fastq.mix(SINGULAR_DEMULTIPLEX.out.fastq)
ch_multiqc_files = ch_multiqc_files.mix(SINGULAR_DEMULTIPLEX.out.metrics.map { meta, metrics -> return metrics} )
ch_versions = ch_versions.mix(SINGULAR_DEMULTIPLEX.out.versions)
break
default:
Nextflow.error "Unknown demultiplexer: ${demultiplexer}"
}
ch_raw_fastq.dump(tag: "DEMULTIPLEX::Demultiplexed Fastq",{FormattingService.prettyFormat(it)})
//
// RUN QC and TRIMMING
//
ch_fastq_to_qc = ch_raw_fastq
// MODULE: fastp
if (!("fastp" in skip_tools)){
FASTP(ch_raw_fastq, [], [], [])
ch_multiqc_files = ch_multiqc_files.mix( FASTP.out.json.map { meta, json -> return json} )
ch_versions = ch_versions.mix(FASTP.out.versions)
if (trim_fastq) {
ch_fastq_to_qc = FASTP.out.reads
}
}
// MODULE: falco, drop in replacement for fastqc
if (!("falco" in skip_tools)){
FALCO(ch_fastq_to_qc)
ch_multiqc_files = ch_multiqc_files.mix( FALCO.out.txt.map { meta, txt -> return txt} )
ch_versions = ch_versions.mix(FALCO.out.versions)
}
// MODULE: md5sum
// Split file list into separate channels entries and generate a checksum for each
MD5SUM(ch_fastq_to_qc.transpose())
// DUMP SOFTWARE VERSIONS
CUSTOM_DUMPSOFTWAREVERSIONS (
ch_versions.unique().collectFile(name: 'collated_versions.yml')
)
// MODULE: MultiQC
workflow_summary = WorkflowDemultiplex.paramsSummaryMultiqc(workflow, summary_params)
ch_workflow_summary = Channel.value(workflow_summary)
methods_description = WorkflowDemultiplex.methodsDescriptionText(workflow, ch_multiqc_custom_methods_description)
ch_methods_description = Channel.value(methods_description)
ch_multiqc_files = ch_multiqc_files.mix(ch_workflow_summary.collectFile(name: 'workflow_summary_mqc.yaml'))
ch_multiqc_files = ch_multiqc_files.mix(ch_methods_description.collectFile(name: 'methods_description_mqc.yaml'))
ch_multiqc_files = ch_multiqc_files.mix(CUSTOM_DUMPSOFTWAREVERSIONS.out.mqc_yml.collect())
ch_multiqc_files.collect().dump(tag: "DEMULTIPLEX::MultiQC files",{FormattingService.prettyFormat(it)})
MULTIQC (
ch_multiqc_files.collect(),
ch_multiqc_config.toList(),
ch_multiqc_custom_config.toList(),
ch_multiqc_logo.toList()
)
multiqc_report = MULTIQC.out.report.toList()
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
COMPLETION EMAIL AND SUMMARY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
workflow.onComplete {
if (params.email || params.email_on_fail) {
NfcoreTemplate.email(workflow, params, summary_params, projectDir, log, multiqc_report)
}
NfcoreTemplate.summary(workflow, params, log)
if (params.hook_url) {
NfcoreTemplate.IM_notification(workflow, params, summary_params, projectDir, log)
}
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// Extract information (meta data + file(s)) from csv file(s)
def extract_csv(input_csv, input_schema=null) {
// Flowcell Sheet schema
// Possible values for the "content" column: [meta, path, number, string, bool]
if(input_schema == null){
def default_input_schema = [
'columns': [
'id': [
'content': 'meta',
'meta_name': 'id',
'pattern': '',
],
'samplesheet': [
'content': 'path',
'pattern': '^.*.csv$',
],
'lane': [
'content': 'meta',
'meta_name': 'lane',
'pattern': '',
],
'flowcell': [
'content': 'path',
'pattern': '',
],
],
required: ['id','flowcell', 'samplesheet'],
]
input_schema = default_input_schema
}
// Don't change these variables
def row_count = 1
def all_columns = input_schema.columns.keySet().collect()
def mandatory_columns = input_schema.required
// Header checks
Channel.value(input_csv).splitCsv(strip:true).first().map({ row ->
if(row != all_columns) {
def commons = all_columns.intersect(row)
def diffs = all_columns.plus(row)
diffs.removeAll(commons)
if(diffs.size() > 0){
def missing_columns = []
def wrong_columns = []
for(diff : diffs){
diff in all_columns ? missing_columns.add(diff) : wrong_columns.add(diff)
}
if(missing_columns.size() > 0){
Nextflow.error "[Samplesheet Error] The column(s) $missing_columns is/are not present. The header should look like: $all_columns"
}
else {
Nextflow.error "[Samplesheet Error] The column(s) $wrong_columns should not be in the header. The header should look like: $all_columns"
}
}
else {
Nextflow.error "[Samplesheet Error] The columns $row are not in the right order. The header should look like: $all_columns"
}
}
})
// Field checks + returning the channels
Channel.value(input_csv).splitCsv(header:true, strip:true).map({ row ->
row_count++
// Check the mandatory columns
def missing_mandatory_columns = []
for(column : mandatory_columns) {
row[column] ?: missing_mandatory_columns.add(column)
}
if(missing_mandatory_columns.size > 0){
Nextflow.error "[Samplesheet Error] The mandatory column(s) $missing_mandatory_columns is/are empty on line $row_count"
}
def output = []
def meta = [:]
for(col : input_schema.columns) {
key = col.key
content = row[key]
if(!(content ==~ col.value['pattern']) && col.value['pattern'] != '' && content != '') {
Nextflow.error "[Samplesheet Error] The content of column '$key' on line $row_count does not match the pattern '${col.value['pattern']}'"
}
if(col.value['content'] == 'path'){
output.add(content ? file(content, checkIfExists:true) : col.value['default'] ?: [])
}
else if(col.value['content'] == 'meta'){
for(meta_name : col.value['meta_name'].split(",")){
meta[meta_name] = content != '' ? content.replace(' ', '_') : col.value['default'] ?: null
}
}
}
output.add(0, meta)
return output
})
}
// Parse flowcell input map
def parse_flowcell_csv(row) {
def meta = [:]
meta.id = row.id.toString()
meta.lane = null
if (row.containsKey("lane") && row.lane ) {
meta.lane = row.lane.toInteger()
}
def flowcell = file(row.flowcell, checkIfExists: true)
def samplesheet = file(row.samplesheet, checkIfExists: true)
return [meta, samplesheet, flowcell]
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FUNCTIONS FOR FQTK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// Extract information (meta data + file(s)) from csv file(s)
def extract_csv_fqtk(input_csv) {
// Flowcell Sheet schema
// Possible values for the "content" column: [meta, path, number, string, bool]
def input_schema = [
'columns': [
'id': [
'content': 'meta',
'meta_name': 'id',
'pattern': '',
],
'samplesheet': [
'content': 'path',
'pattern': '^.*.csv$',
],
'lane': [
'content': 'meta',
'meta_name': 'lane',
'pattern': '',
],
'flowcell': [
'content': 'path',
'pattern': '',
],
'per_flowcell_manifest': [
'content': 'path',
'pattern': '',
]
],
required: ['id','flowcell', 'samplesheet', 'per_flowcell_manifest'],
]
return extract_csv(input_csv, input_schema)
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THE END
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/