forked from epi2me-labs/wf-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
221 lines (199 loc) · 7.15 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
#!/usr/bin/env nextflow
// Developer notes
//
// This template workflow provides a basic structure to copy in order
// to create a new workflow. Current recommended practices are:
// i) create a simple command-line interface.
// ii) include an abstract workflow scope named "pipeline" to be used
// in a module fashion
// iii) a second concrete, but anonymous, workflow scope to be used
// as an entry point when using this workflow in isolation.
import groovy.json.JsonBuilder
nextflow.enable.dsl = 2
include { fastq_ingress; xam_ingress } from './lib/ingress'
include {
getParams;
} from './lib/common'
OPTIONAL_FILE = file("$projectDir/data/OPTIONAL_FILE")
process getVersions {
label "wftemplate"
cpus 1
output:
path "versions.txt"
script:
"""
python -c "import pysam; print(f'pysam,{pysam.__version__}')" >> versions.txt
fastcat --version | sed 's/^/fastcat,/' >> versions.txt
"""
}
process makeReport {
label "wftemplate"
input:
val metadata
tuple path(per_read_stats, stageAs: "stats/stats*.tsv.gz"), val(no_stats)
path client_fields
path "versions/*"
path "params.json"
output:
path "wf-template-*.html"
script:
String report_name = "wf-template-report.html"
String metadata = new JsonBuilder(metadata).toPrettyString()
String stats_args = no_stats ? "" : "--stats stats"
String client_fields_args = client_fields.name == OPTIONAL_FILE.name ? "" : "--client_fields $client_fields"
"""
echo '${metadata}' > metadata.json
workflow-glue report $report_name \
--versions versions \
$stats_args \
$client_fields_args \
--params params.json \
--metadata metadata.json
"""
}
// See https://github.com/nextflow-io/nextflow/issues/1636. This is the only way to
// publish files from a workflow whilst decoupling the publish from the process steps.
// The process takes a tuple containing the filename and the name of a sub-directory to
// put the file into. If the latter is `null`, puts it into the top-level directory.
process output {
// publish inputs to output directory
label "wftemplate"
publishDir (
params.out_dir,
mode: "copy",
saveAs: { dirname ? "$dirname/$fname" : fname }
)
input:
tuple path(fname), val(dirname)
output:
path fname
"""
"""
}
// Creates a new directory named after the sample alias and moves the ingress results
// into it.
process collectIngressResultsInDir {
label "wftemplate"
input:
// both inputs might be `OPTIONAL_FILE` --> stage in different sub-directories
// to avoid name collisions
tuple val(meta),
path(reads, stageAs: "reads/*"),
path(index, stageAs: "index/*"),
path(stats, stageAs: "stats/*")
output:
// use sub-dir to avoid name clashes (in the unlikely event of a sample alias
// being `reads` or `stats`)
path "out/*"
script:
String outdir = "out/${meta["alias"]}"
String metaJson = new JsonBuilder(meta).toPrettyString()
String reads = reads.fileName.name == OPTIONAL_FILE.name ? "" : reads
String index = index.fileName.name == OPTIONAL_FILE.name ? "" : index
String stats = stats.fileName.name == OPTIONAL_FILE.name ? "" : stats
"""
mkdir -p $outdir
echo '$metaJson' > metamap.json
mv metamap.json $reads $stats $index $outdir
"""
}
// workflow module
workflow pipeline {
take:
reads
main:
// fastq_ingress doesn't have the index; add one extra null for compatibility.
// We do not use variable name as assigning variable name with a tuple
// not matching (e.g. meta, bam, bai, stats <- [meta, bam, stats]) causes
// the workflow to crash.
reads = reads
.map{
it.size() == 4 ? it : [it[0], it[1], null, it[2]]
}
// Resolve and extract stats files.
per_read_stats = reads
.flatMap {
meta, path, index, stats ->
stats ? file(stats.resolve('*read*.tsv.gz')) : null
}
| ifEmpty(OPTIONAL_FILE)
client_fields = params.client_fields && file(params.client_fields).exists() ? file(params.client_fields) : OPTIONAL_FILE
software_versions = getVersions()
workflow_params = getParams()
metadata = reads.map { it[0] }.toList()
report = makeReport(
metadata,
// having a list of files with the same name or an `OPTIONAL_FILE` is quite
// annoying as we need to avoid naming collisions but this will also
// overwrite the name of the `OPTIONAL_FILE`. We therefore add an extra
// boolean designating whether there were stats or not.
per_read_stats.collect() | map { file_list ->
[file_list, file_list[0] == OPTIONAL_FILE]
},
// if the client fields file doesn't exist then set a boolean as above.
client_fields,
software_versions,
workflow_params
)
reads
// replace `null` with path to optional file
| map {
meta, path, index, stats ->
[ meta, path ?: OPTIONAL_FILE, index ?: OPTIONAL_FILE, stats ?: OPTIONAL_FILE ]
}
| collectIngressResultsInDir
emit:
ingress_results = collectIngressResultsInDir.out
report
workflow_params
// TODO: use something more useful as telemetry
telemetry = workflow_params
}
// entrypoint workflow
WorkflowMain.initialise(workflow, params, log)
workflow {
Pinguscript.ping_start(nextflow, workflow, params)
// demo mutateParam
if (params.containsKey("mutate_fastq")) {
CWUtil.mutateParam(params, "fastq", params.mutate_fastq)
}
if (params.fastq) {
samples = fastq_ingress([
"input":params.fastq,
"sample":params.sample,
"sample_sheet":params.sample_sheet,
"analyse_unclassified":params.analyse_unclassified,
"stats": params.wf.fastcat_stats,
"fastcat_extra_args": "",
"required_sample_types": [],
"watch_path": params.wf.watch_path,
])
} else {
// if we didn't get a `--fastq`, there must have been a `--bam` (as is codified
// by the schema)
samples = xam_ingress([
"input":params.bam,
"sample":params.sample,
"sample_sheet":params.sample_sheet,
"analyse_unclassified":params.analyse_unclassified,
"keep_unaligned": params.wf.keep_unaligned,
"stats": params.wf.bamstats,
"watch_path": params.wf.watch_path,
"return_fastq": params.wf.return_fastq,
])
}
pipeline(samples)
pipeline.out.ingress_results
| map { [it, "${params.fastq ? "fastq" : "xam"}_ingress_results"] }
| concat (
pipeline.out.report.concat(pipeline.out.workflow_params)
| map { [it, null] }
)
| output
}
workflow.onComplete {
Pinguscript.ping_complete(nextflow, workflow, params)
}
workflow.onError {
Pinguscript.ping_error(nextflow, workflow, params)
}