-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.nf
executable file
·275 lines (213 loc) · 7.27 KB
/
main.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
#!/usr/bin/env nextflow
import Helper
// A pipeline to run mash screen for pATLAS.
if (params.help) {
Help.print_help(params)
exit 0
}
// check if noWinner is provided or not
winnerVar = (params.noWinner == false) ? "-w" : ""
// start all optional channels
readInputs = Channel.empty()
readInputs2 = Channel.empty()
fastaInputs = Channel.empty()
/**
* The combination of these four channels (two forked) allows to double check for
* the run mode. If mash_screen is provided, then it will search for the two
* channel types. However if mapping is provided but mash_screen don't, it will
* search for the read inputs ignoring mash sketch.
*/
if (params.mash_screen || params.mapping) {
Channel.fromFilePairs(params.reads, size: params.singleEnd ? 1 : 2, type: 'file')
.ifEmpty { exit 0, "reads (fastq) files were not provided" }
.into { readInputs; readInputs2 }
// this empties one of the channels in the case one is not required to run
if (params.mash_screen == false) {
readInputs = Channel.empty()
}
else if (params.mapping == false) {
readInputs2 = Channel.empty()
}
}
if (params.mash_screen || params.assembly) {
// creates two channels for each approach
Channel
.value("/ngstools/data/plasmid_db_reference.msh")
.into { refSketchChannel; refSketchChannel2 }
}
else {
// creates an empty channel for both processes
Channel.empty()
.into { refSketchChannel; refSketchChannel2 }
}
/**
* This channel enables assembly approach, allowing users to import a fasta to
* provide to mash dist
*/
if (params.assembly) {
// channel for fasta inputs
fastaInputs = Channel
.fromPath(params.fasta, type: 'file')
.ifEmpty {exit 0, "no fasta file was provided"}
}
//todo implement checks on file type
if (params.mapping) {
//fetch indexes for mapping approach, available in Docker
// bowtie2Index = "/ngstools/data/indexes/bowtie2idx/bowtie2.idx" // idx_file
// samtoolsIndex = "/ngstools/data/indexes/fasta/samtools.fasta.fai" // maindb_path
// lengthJson = "/ngstools/data/reads_sample_result_length.json"
bowtieStuffChannel = Channel
.value("/ngstools/data/indexes/patlas_bowtie2_index")
samtoolsStuffChannel = Channel
.value("/ngstools/data/indexes/master_fasta_plasmid_db.fas.fai")
lengthJsonChannel = Channel
.value("/ngstools/data/length_plasmid_db.json")
}
else {
bowtieStuffChannel = Channel.empty()
samtoolsStuffChannel = Channel.empty()
lengthJsonChannel = Channel.empty()
}
/******************************/
/*** MASH SCREEN processes ****/
/******************************/
// process to run mashScreen and sort the output into
// sortedMashScreenResults_{sampleId}.txt
process mashScreen {
tag { "running mash screen for sample: " + sample }
input:
set sample, file(reads) from readInputs
val refSketch from refSketchChannel
output:
set sample, file("sortedMashScreenResults_${sample}.txt") into mashScreenResults
"""
mash screen -i ${params.identity} -v ${params.pValue} -p \
${task.cpus} ${winnerVar} ${refSketch} ${reads} > mashScreenResults_${sample}.txt
sort -gr mashScreenResults_${sample}.txt > sortedMashScreenResults_${sample}.txt
"""
}
// process to parse the output to json format
process mashOutputJson {
tag { "dumping json file from: " + mashtxt }
publishDir 'results/mashscreen/'
input:
set sample, file(mashtxt) from mashScreenResults
output:
set sample, file("sortedMashScreenResults_${sample}.json") optional true into mashScreenOutput
script:
template "mashscreen2json.py"
}
/******************************/
/**** MASH DIST processes *****/
/******************************/
// runs mash dist
process runMashDist {
tag { "running mash dist for fasta file: " + fasta }
input:
file fasta from fastaInputs
val refSketch from refSketchChannel2
output:
set file(fasta), file("${fasta}_mashdist.txt") into mashDistResults
"""
mash dist -i -p ${task.cpus} -v ${params.pValue} \
-d ${params.mash_distance} ${refSketch} ${fasta} > ${fasta}_mashdist.txt
"""
}
// parses mash dist output to a json file that can be imported into pATLAS
process mashDistOutputJson {
tag { "dumping json file from: " + mashtxt }
publishDir 'results/mashdist/'
input:
set fasta, file(mashtxt) from mashDistResults
output:
file "*.json" optional true into mashDistOutput
script:
template "mashdist2json.py"
}
/******************************/
/********** MAPPING ***********/
/******************************/
// process that runs bowtie2
process mappingBowtie {
tag { "mapping sample: " + sample}
input:
set sample, file(reads) from readInputs2
val bowtie2Index from bowtieStuffChannel
output:
set sample, file("mappingBowtie_${sample}.sam") into bowtieResults
script:
if (params.singleEnd == true) {
readsString = "-U ${reads}"
}
else {
readsString = "-1 ${reads[0]} -2 ${reads[1]}"
}
"""
bowtie2 -x ${bowtie2Index} ${readsString} -p ${task.cpus} -a \
-5 ${params.trim5} -S mappingBowtie_${sample}.sam
"""
}
/**
* samtools faidx is escaped because index file is already provided in docker
* image.
*/
process samtoolsView {
tag { "samtools commands: " + sample }
input:
set sample, file(samtoolsFile) from bowtieResults
val samtoolsIdx from samtoolsStuffChannel
output:
set sample, file("samtoolsDepthOutput_${sample}.txt") into samtoolsResults
"""
samtools view -b -t ${samtoolsIdx} -@ ${task.cpus} ${samtoolsFile} | \
samtools sort -@ ${task.cpus} -o samtoolsSorted_${sample}.bam
samtools index samtoolsSorted_${sample}.bam
samtools depth samtoolsSorted_${sample}.bam > samtoolsDepthOutput_${sample}.txt
"""
}
/**
* These dumping process parses the depth file for each sample and filters it
* depending on the cutoff set by the user.
*/
process jsonDumpingMapping {
tag { "Dumping json: " + sample }
publishDir 'results/mapping/'
input:
set sample, file(depthFile) from samtoolsResults
val lengthJson from lengthJsonChannel
output:
set sample, file("samtoolsDepthOutput_${sample}.txt_mapping.json") optional true into mappingOutput
script:
template "mapping2json.py"
}
/**
* After generating all output jsons check again the params specified.
* If mapping and mash_screen are executed the consensus process will generate
* a consensus from these two json outputs. If assembly is also provided, the
* consensus will have the consensus between the three approaches. Otherwise,
* no consensus will be generated.
*/
if (params.mapping && params.mash_screen) {
// if (params.assembly) {
// test = mappingOutput.merge(mashDistOutput, mashScreenOutput)
// }
// else {
consensusChannel = mappingOutput.join(mashScreenOutput)
// }
}
else {
consensusChannel = Channel.empty()
}
/**
* A process that creates a consensus from all the outputted json files
*/
process fullConsensus {
tag { "Creating consensus json file for: " + sample}
publishDir 'results/consensus/'
input:
set sample, file(infile_list) from consensusChannel.map{ ot -> [ ot[0], ot[1..-1] ] }
output:
file "consensus_${sample}.json"
script:
template "pATLAS_consensus_json.py"
}