-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.nf
More file actions
310 lines (286 loc) · 14.2 KB
/
Copy pathmain.nf
File metadata and controls
310 lines (286 loc) · 14.2 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
// include modules
include {
LaneMerge ;
Bwa ;
WriteBamLists ;
Index ;
Merge
} from './modules/bwa.nf'
include {
Bcf ;
Gridss ;
SomaticFilter ;
RCopyNum ;
FilterBcfVcf ;
FilterGridssSV ;
RPlotFull ;
RPlotROI ;
genesROI
} from './modules/variants.nf'
include {
FastQC ;
MosDepth ;
FlagStats ;
MultiQC
} from './modules/qc.nf'
include { validateParameters ; paramsSummaryLog ; samplesheetToList } from 'plugin/nf-schema'
def validateSampleIdContainsGroupId(List row) {
if (!row[1].toString().contains(row[0].toString())) {
error("Validation failed: sampleId '${row[1]}' does not contain groupId '${row[0]}'")
}
return row
}
workflow {
println("*****************************************************")
println("* Nextflow Malaria Variant analysis pipeline *")
println("* A Nextflow wrapper pipeline *")
println("* Written by WEHI Research Computing Platform *")
println("* research.computing@wehi.edu.au *")
println("* *")
println("*****************************************************")
println(" Required Pipeline parameters ")
println("-----------------------------------------------------")
println("Input Sequence Path: ${params.input_seq_path} ")
println("Sample Group file : ${params.input_file} ")
println("Output directory : ${params.outdir} ")
println("*****************************************************")
validateParameters()
// Create a new channel of metadata from a sample sheet passed to the pipeline through the --input parameter
Channel.fromList(samplesheetToList(params.input_file, "input_schema.json"))
.map { validateSampleIdContainsGroupId(it) }
//----------------Input Preparation-----------------------------------------
Channel.fromPath(params.input_file, checkIfExists: true)
.ifEmpty {
error(
"""
No groups could be found in group key file! Please check your directory path
is correct.
"""
)
}
.splitCsv(header: true, sep: '\t')
.map { row ->
def ref = ""
def refpath = ""
def bsref = ""
if (row.ref == "3D7") {
ref = params.ref3D7_path + "/PlasmoDB-52_Pfalciparum3D7_Genome"
refpath = params.ref3D7_path
bsref = "BSgenome.Pfalciparum3D7.PlasmoDB.52"
}
else if (row.ref == "Dd2") {
ref = params.refDd2_path + "/PlasmoDB-57_PfalciparumDd2_Genome"
refpath = params.refDd2_path
bsref = "BSgenome.PfalciparumDd2.PlasmoDB.57"
}
else if (row.ref == "Supp") {
ref = params.refSupp_path + "/PlasmoDB-52_Pfalciparum3D7_Genome_supplemented"
refpath = params.refSupp_path
bsref = "BSgenome.PfalciparumNF54iGP"
}
return tuple(row.groupId, row.sampleId, row.fastqbase, row.ref, row.parentId, ref, refpath, bsref)
}
.set { input_ch }
// Emits 0->groupId, 1->sampleId, 2->fastqbase,
// 3->ref_prefix, 4->parentId, 5->ref (path+name)
// 6->refpath , 7-> bsref
//----------------Alignment-----------------------------------------
bamlist_ch = WriteBamLists(Channel.fromPath(params.input_file, checkIfExists: true))
bamlist_ch = bamlist_ch
.flatten()
.map { row ->
tuple(row.baseName.split("_bams")[0], row)
}
// Emits groupID, bamslist.txt
if (params.merge_lanes) {
files_ch = input_ch
.map { row ->
def pattern = "${params.input_seq_path}/${row[2]}*_R1*"
def files = file("${pattern}")
// Handle case where no files match
if (files.isEmpty()) {
log.warn("No files found matching pattern: ${pattern}")
return []
}
return tuple(row[2], files) // fastqbase, R1 file paths
}
.map { row ->
def pattern = "${params.input_seq_path}/${row[0]}*_R2*"
def files = file("${pattern}")
// Handle case where no files match
if (files.isEmpty()) {
log.warn("No files found matching pattern: ${pattern}")
return []
}
return tuple(row[0], row[1], files)
}
// Emits fastqbase, R1 files, R2 files
LaneMerge(files_ch).set { mergedfastqfiles }
// Emits 0->fastqbase, 2->[fastqs]
input_ch
.map { row -> tuple(row[2], row[1], row[0], row[5], row[6]) }
.join(mergedfastqfiles, by: 0)
.set { bwa_input_ch }
// Emits tuple val(fastqbase), val(sampleId),val(groupId),val(ref),path(refpath),path(fastqs)
}
else {
Channel.fromFilePairs("${params.input_seq_path}/*_{,R}{1,2}*.{fq,fastq}{,.gz}", size: 2)
.ifEmpty {
error(
"""
No samples could be found! Please check whether your input directory
is correct, and that your samples match typical fastq paired end naming
convention(s).
"""
)
}
.map { row -> tuple(row[0].split(/_R[0-9]{1}/)[0], row[1]) }
.set { fastq_input_channel }
input_ch
.map { row -> tuple(row[2], row[1], row[0], row[5], row[6]) }
.join(fastq_input_channel, by: 0)
.set { bwa_input_ch }
// Emits tuple val(fastqbase), val(sampleId),val(groupId),val(ref),path(refpath),path(fastqs)
}
sam_ch = Bwa(bwa_input_ch)
bam_ch = Index(sam_ch)
//----------------Merge&List---------------------------------------
input_ch
.map { row -> row[4] }
.unique()
.combine(bam_ch.bamnodup, by: 0)
.map { row ->
tuple(row[0], row[1], row[1].baseName + ".bam")
}
.groupTuple()
.map { row ->
tuple(row[0], row[1], row[2].join(" "))
}
.ifEmpty {
error(
"""
No parent samples found.
"""
)
}
.set { parent_ch }
// Emits tuple val(parentId),path(bams), val(parentlist)
input_ch
.map { row -> row[4] }
.unique()
.combine(bam_ch.bai, by: 0)
.groupTuple()
.join(parent_ch)
.ifEmpty {
error(
"""
No parent samples found.
""")
}
.set{parent_all_ch} // Emits tuple val(parentId),path(bai),path(bams), val(parentlist)
merged_ch=Merge(parent_ch) // Emits tuple val(parentId),path(parentId.bam)
//----------------QC tools------------------------------------------
//MultiQC integrates files from fastqc and mosdepth, found in input dir, into single report
fastqc_ch=FastQC(bam_ch.bamnodup.map{row->row[1]}.unique({it.baseName}).collect())
// MosDepth Input Channel include val(sampleId), path(bam), path(bai)
mosdepth_ch=MosDepth(bam_ch.bysampleid) // join bams with their index based on groupId
// FlagStats Input Channel include val(sampleId), path(bam)
flagstat_ch=FlagStats(bam_ch.bysampleid)
MultiQC( fastqc_ch.zip.mix(mosdepth_ch, flagstat_ch).collect().ifEmpty([]),Channel.fromPath(params.multiqc_config) )
//----------------BCF tools----------------------------------------
//BCF Input Channel Emits parentId,groupId,ref,bamlist,bams,parentbams
input_ch.map{row -> tuple(row[0],row[4],row[5]+".fasta")}
.unique()
.join(bamlist_ch)
.combine(bam_ch.bamnodup,by:0)
.groupTuple(by:[0,1,2,3])
.combine(parent_ch.map{row->tuple(row[1],row[0])},by:1).set{bcf_input_ch}
// Emits val(parentId),val(groupId),path(ref), path(bamlist), path(bams),path(parentbams)
bcf_ch=Bcf(bcf_input_ch)
//----------------------Gridss-------------------------------------
input_ch.map{row -> tuple(row[0],row[4],row[5]+".fasta")}
.unique()
.join(bamlist_ch.map{gid,filepath ->
def fileLines = filepath.readLines()
def fileContents = fileLines.join(' ')
return tuple(gid, fileContents)
})
.combine(bam_ch.bamnodup,by:0)
.groupTuple(by:[0,1,2,3])
.combine(parent_ch.map{row->tuple(row[1],row[0])},by:1)
.ifEmpty {
error("""
Input to gridss is empty.
""")
}
.set{gridss_input_ch}// Emits val(parentId),val(groupId),path(ref), val(bamlistcontent), path(bams),path(parentbams)
gridss_ch=Gridss(gridss_input_ch.combine(Channel.fromPath(params.gridss_jar_path)))
input_ch.map{row -> tuple(row[4], row[0],row[7] )}
.unique()
.combine(parent_ch,by:0).map{row -> tuple(row[1],row[2],row[5])}
.join(bamlist_ch).join(gridss_ch.vcf)
.set{sv_input_ch} //Emit val(groupId),val(bsref),val(parentbamlist), path(bamlist), path(vcf)
sfilter_ch=SomaticFilter(sv_input_ch
.combine(Channel.fromPath("${projectDir}/Rtools/gridss_assets/gridss_somatic_filter.R")))
//----------------------CopyNum--------------------------------------
input_ch.map{row -> tuple(row[4], row[0],row[6],row[7] )}
.unique()
.combine(merged_ch,by:0)
.map{row -> if (row[0]) tuple(row[1],row[0],row[2],row[3],row[4])}
.combine(bam_ch.bamnodup,by:0)
.groupTuple(by:[0,1,2,3,4])
.ifEmpty {
error("""
Input to CopyNum Analysis is empty.
""")
}
.set{copynum_input_ch}//Emits val(groupId),val(parentId),path(refpath),val(bsref),path(mergedparent), path(bams)
copynum_ch=RCopyNum(copynum_input_ch
.combine(Channel.fromList( [params.bin_CNroi, params.bin_CNfull] ))
.combine(Channel.fromPath("${projectDir}/Rtools/malDrugR/copynumQDNAseq.R"))
)
//----------------------filter BCF-----------------------------------
input_ch.map{row -> tuple(row[4],row[0],row[6],row[3])}
.unique()
.combine(parent_all_ch,by:0)
.map{row-> tuple(row[1],row[2],row[3],row[4],row[5],row[6])}
.join(bcf_ch, by:0)
.join(bam_ch.bamnodup.groupTuple(),by:0)
.join(bam_ch.bai.groupTuple(),by:0)
.ifEmpty {
error("""
Input to filter is empty.
""")
}
.set{fbcf_ch} // Emits val(groupId),path(refpath),val(prefix),
// path(parentbai),path(parentbam), val(parentbamlist),
// path(vcf),
// path(bams), path(bai), val(dummy)
FilterBcfVcf(fbcf_ch
.combine(Channel.fromPath("${projectDir}/Rtools/malDrugR/filterBcfVcf.R"))
)
//----------------------GRIDSS filter-----------------------------------
input_ch.map{row -> tuple(row[0],row[4])}
.unique()
.combine(parent_ch.map{row->tuple(row[2],row[0])},by:1)
.map{row->tuple(row[1],row[2])}
.join(sfilter_ch.vcf)
.set{gf_ch} // Emits val(groupId),val(parentlist), path(vcf), val(dummy)
FilterGridssSV(gf_ch
.combine(Channel.fromPath("${projectDir}/Rtools/malDrugR/gridss_majorityfilt.R"))
.combine(Channel.fromPath("${projectDir}/Rtools/gridss_assets/"))
)
//----------------------Plot CopyNums-----------------------------------------
// Input Channel Emits val(groupId),val(parentId),path(rds)
input_ch.map{row -> tuple(row[0],row[4])}
.unique()
.join(copynum_ch.groupTuple().map{row-> tuple(row[0], row[1].flatten())},by:0).set{plot_ch}
RPlotFull(plot_ch.combine(Channel.fromPath("${projectDir}/Rtools/malDrugR/copynumPlotsFull.R")))
RPlotROI(plot_ch.combine(Channel.fromPath("${projectDir}/Rtools/malDrugR/copynumPlotsROI.R")))
//----------------------List genes in a region of interest, if specified------
// Input Channel emits path(refpath), val(ref_prefix)
input_ch.map{row -> tuple(row[6],row[3])}.unique().set{genesROI_input_ch}
if (params.genesRegion != "")
genesROI(genesROI_input_ch.combine(Channel.fromPath("${projectDir}/Rtools/malDrugR/genesROI.R")))
//-------------------------------------------------------------------
}