forked from clara-parabricks-workflows/parabricks-nextflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.nf
More file actions
225 lines (184 loc) · 7.73 KB
/
main.nf
File metadata and controls
225 lines (184 loc) · 7.73 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
#!/usr/bin/env nextflow
nextflow.enable.dsl = 2
// Pull in igenomes
params.fasta = WorkflowMain.getGenomeAttribute(params, 'fasta')
params.fasta_fai = "${params.fasta}.fai"
params.bwa_index = WorkflowMain.getGenomeAttribute(params, 'bwa')
log.info """\
======================================================
P A R A B R I C K S - N E X T F L O W
======================================================
samplesheet: ${params.input}
outdir: ${params.outdir}
assay: ${params.assay}
known_sites: ${params.known_sites}
target_region_bed: ${params.target_region_bed}
proposed_variants: ${params.proposed_variants}
model_file: ${params.model_file}
ignore_samples: ${params.ignore_samples}
fasta: ${params.fasta}
fasta_fai: ${params.fasta_fai}
bwa_index: ${params.bwa_index}
"""
// local modules
include { PARABRICKS_FQ2BAM } from './modules/local/parabricks/fq2bam/main'
include { PARABRICKS_DEEPVARIANT } from './modules/local/parabricks/deepvariant/main'
// nf-core modules
include { TABIX_BGZIPTABIX } from './modules/nf-core/tabix/bgziptabix/main'
include { MULTIQC } from './modules/nf-core/multiqc/main'
include { CUSTOM_DUMPSOFTWAREVERSIONS } from './modules/nf-core/custom/dumpsoftwareversions/main'
def known_sites = params.known_sites ? params.known_sites.collect { file(it, checkIfExists: true) } : []
def model_file = params.model_file ? file(params.model_file, checkIfExists: true) : []
def proposed_variants = params.proposed_variants ? file(params.proposed_variants, checkIfExists: true) : []
// Check input path parameters to see if they exist
if (params.input) { ch_input = file(params.input) } else { exit 1, 'Input samplesheet not specified!' }
workflow {
ch_versions = Channel.empty()
ch_genome = [params.fasta, params.fasta_fai]
Channel.value(ch_input)
.splitCsv ( header:true, sep:',' )
.set { sheet }
ch_fastq = sheet
.filter { row -> !params.ignore_samples.contains(row.sample) } // Skip matching samples
.map { row -> [[row.sample], row] }
.groupTuple()
.map { meta, rows ->
[rows, rows.size()]
}
.transpose()
.map { row, numLanes ->
create_fastq_channel(row + [num_lanes:numLanes])
}
ch_fastq
.map { meta, r1_fastq, r2_fastq ->
grouped_id = meta.sample
grouped_prefix = meta.id
grouped_num_lanes = meta.num_lanes
grouped_single_end = meta.single_end
grouped_meta = [id: grouped_id, prefix: grouped_prefix, read_group: grouped_id, num_lanes: grouped_num_lanes, single_end: grouped_single_end]
return [grouped_meta, meta, r1_fastq, r2_fastq]
}
.groupTuple()
.map { grouped_meta, meta, r1_fastq, r2_fastq ->
def target_region_bed = params.target_region_bed ? file(params.target_region_bed, checkIfExists: true) : []
return [grouped_meta, meta, r1_fastq, r2_fastq, target_region_bed]
}
.set { ch_grouped_fastq }
ch_grouped_fastq
.map { grouped_meta, metas, r1, r2, bed ->
def all_se = metas.every { it.single_end }
def all_pe = metas.every { !it.single_end }
if (!all_se && !all_pe) {
error "ERROR: Inconsistent 'single_end' flags in grouped sample: ${grouped_meta.id} → ${metas*.single_end}"
}
def fixed_r2 = all_se ? [] : r2
grouped_meta.single_end = metas.single_end
tuple(grouped_meta, metas, r1, fixed_r2, bed)
}
.set { ch_grouped_fastq_normalized }
// fastq -> bam (fq2bam)
PARABRICKS_FQ2BAM (
ch_grouped_fastq_normalized,
ch_genome,
params.bwa_index,
known_sites
)
ch_versions = ch_versions.mix(PARABRICKS_FQ2BAM.out.versions.first().ifEmpty(null))
// construct bam_bai ch, add target_region_bed to ch
ch_bam_bai = PARABRICKS_FQ2BAM.out.bam_bai
.map {meta, bam, bai ->
def target_region_bed = params.target_region_bed ? file(params.target_region_bed, checkIfExists: true) : []
return [meta, bam, bai, target_region_bed]
}
.set { ch_bam_bai_interval }
// bam -> vcf (deepvariant)
PARABRICKS_DEEPVARIANT (
ch_bam_bai_interval,
ch_genome,
model_file,
proposed_variants
)
ch_versions = ch_versions.mix(PARABRICKS_DEEPVARIANT.out.versions.first().ifEmpty(null))
deepvariant_vcf = PARABRICKS_DEEPVARIANT.out.vcf
// bgzip and index vcf
TABIX_BGZIPTABIX (
deepvariant_vcf
)
ch_versions = ch_versions.mix(TABIX_BGZIPTABIX.out.versions.first().ifEmpty(null))
//
// MODULE: MultiQC
//
//CUSTOM_DUMPSOFTWAREVERSIONS (
// ch_versions.unique().collectFile(name: 'collated_versions.yml')
//)
// get multiqc conf 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.fromPath("$projectDir/assets/Element_Biosciences_Logo_Black_RGB.png", checkIfExists: true)
ch_multiqc_files = Channel.empty()
ch_multiqc_files = ch_multiqc_files.mix(PARABRICKS_FQ2BAM.out.qc_metrics.collect{it[1]}.ifEmpty([]))
ch_multiqc_files = ch_multiqc_files.mix(PARABRICKS_FQ2BAM.out.bqsr_table.collect{it[1]}.ifEmpty([]))
ch_multiqc_files = ch_multiqc_files.mix(PARABRICKS_FQ2BAM.out.duplicate_metrics.collect{it[1]}.ifEmpty([]))
MULTIQC (
ch_multiqc_files.collect(),
ch_multiqc_config.toList(),
ch_multiqc_custom_config.toList(),
ch_multiqc_logo.toList()
)
multiqc_report = MULTIQC.out.report.toList()
}
def create_fastq_channel(LinkedHashMap row) {
def meta = [
id: row.sample,
sample: row.sample,
prefix: row.sample + "__" + row.read_group,
read_group: row.read_group,
platform: row.platform,
gender: row.gender,
num_lanes: row.num_lanes,
single_end: false // Default to paired-end
]
def fields = [
'r1_fastq': ['meta': [:], 'read_num': 'R1'],
'r2_fastq': ['meta': [:], 'read_num': 'R2'],
'fastq_1': ['meta': [:], 'read_num': 'R1'],
'fastq_2': ['meta': [:], 'read_num': 'R2']
]
// Add paths of the fastq files to the meta map
def fastq_files = []
fields.each { key, value ->
if (row[key]) {
def file_path = file(row[key])
if (!file_path.exists()) {
error("ERROR: Please check input samplesheet -> ${value.read_num} FastQ file does not exist!\n${row[key]}")
}
}
}
// Set r1_fastq and r2_fastq explicitly
def r1_fastq = null
def r2_fastq = []
// Validate R1 fastq file
if (row.r1_fastq || row.fastq_1) {
r1_fastq = file(row.r1_fastq ? row.r1_fastq : row.fastq_1)
if (!r1_fastq.exists()) {
error("ERROR: Please check input samplesheet -> R1 FastQ file does not exist!\n${r1_fastq}")
}
} else {
error("ERROR: R1 FastQ file is required but not found in the samplesheet for sample ${row.sample}")
}
// Validate R2 fastq file (OPTIONAL)
if (row.r2_fastq || row.fastq_2) {
r2_fastq = file(row.r2_fastq ? row.r2_fastq : row.fastq_2)
if (!r2_fastq.exists()) {
log.warn "WARNING: R2 FastQ file does not exist for sample ${row.sample}. Proceeding as single-end."
r2_fastq = []
}
}
// Determine if the read is single-ended
if (!row.fastq_2) {
meta.single_end = true
row.fastq_2 = []
}
// Return the meta and the explicit r1 and r2 fastq files
return [meta, r1_fastq, r2_fastq]
}