-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.nf
202 lines (158 loc) · 6.89 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
#!/usr/bin/env nextflow
/*
========================================================================================
genepi/nf-imputationserver
========================================================================================
Github : https://github.com/genepi/nf-imputationserver
Author: Lukas Forer & Sebastian Schönherr
---------------------------
*/
if (params.refpanel_yaml){
params.refpanel = RefPanelUtil.loadFromFile(params.refpanel_yaml)
}
requiredParams = [
'project', 'files', 'output', 'refpanel'
]
for (param in requiredParams) {
if (params[param] == null) {
exit 1, "Parameter ${param} is required."
}
}
//TODO create json validation file
def phasing_engine = params.phasing.engine
if (phasing_engine != 'eagle' && phasing_engine != 'beagle' && phasing_engine != 'no_phasing' ) {
println "::error:: For phasing, only options 'eagle', 'beagle' or 'no_phasing' are allowed."
exit 1
}
// create random password when not set by user
if (params.password == null) {
params.encryption_password = PasswordCreator.createPassword()
} else {
params.encryption_password = params.password
}
//set default population to "off" when allele_frequency_population is null
params.population = params.allele_frequency_population ?: "off"
Channel
.fromPath(params.files)
.set {files}
files.ifEmpty {
println "::error:: No vcf.gz input files detected."
exit 1
}
// Find site files from full pattern and make site file pattern relative
params.refpanel.sites_pattern = "${params.refpanel.sites}"
params.refpanel.sites = "./${file(params.refpanel.sites).fileName}"
site_files_ch = Channel.of(1..22, 'X', 'MT')
.map {
it ->
def sites_file = file(PatternUtil.parse(params.refpanel.sites_pattern, [chr: it]))
def sites_file_index = file(PatternUtil.parse(params.refpanel.sites_pattern+ ".tbi", [chr: it]))
if(!sites_file.exists()){
return null;
}
if(sites_file.exists() && !sites_file_index.exists()){
error "Missing tabix index for " + sites_file
}
return tuple(sites_file, sites_file_index);
}
include { INPUT_VALIDATION } from './workflows/input_validation'
include { QUALITY_CONTROL } from './workflows/quality_control'
include { PHASING } from './workflows/phasing'
include { IMPUTATION } from './workflows/imputation'
include { ENCRYPTION } from './workflows/encryption'
include { ANCESTRY_ESTIMATION } from './workflows/ancestry_estimation'
include { PGS_CALCULATION } from './workflows/pgs_calculation'
workflow {
println "Welcome to ${params.service.name} (${workflow.manifest.version})"
if (params.imputation.enabled){
INPUT_VALIDATION()
QUALITY_CONTROL(
INPUT_VALIDATION.out.validated_files,
INPUT_VALIDATION.out.validation_report,
site_files_ch.collect()
)
// check if QC chunks exist in case QC failed
QUALITY_CONTROL.out.qc_metafiles.ifEmpty {
error "QC step failed"
}
if (params.mode == 'imputation') {
phased_ch = QUALITY_CONTROL.out.qc_metafiles
if (phasing_engine != 'no_phasing') {
PHASING(
QUALITY_CONTROL.out.qc_metafiles
)
phased_ch = PHASING.out.phased_ch
}
IMPUTATION(
phased_ch
)
if (params.merge_results === true) {
ENCRYPTION(
IMPUTATION.out.groupTuple()
)
}
}
}
// handles empty objects (e.g. cloudgene)
ancestry_enabled = params.ancestry != null && params.ancestry != "" && params.ancestry.enabled
if (ancestry_enabled) {
ANCESTRY_ESTIMATION()
}
if (params.pgs.enabled) {
PGS_CALCULATION(
IMPUTATION.out,
ancestry_enabled ? ANCESTRY_ESTIMATION.out : Channel.empty()
)
}
}
workflow.onComplete {
//TODO: use templates
//TODO: move in EmailHelper class
if (!workflow.success) {
def statusMessage = workflow.exitStatus != null || workflow.errorReport == "QC step failed" ? "failed" : "canceled"
if (params.send_mail && params.user.email != null){
sendMail{
to "${params.user.email}"
subject "[${params.service.name}] Job ${params.project} ${statusMessage}"
body "Dear ${params.user.name}, \n Your job has been ${statusMessage}.\n\n More details can be found at the following link: ${params.service.url}/index.html#!jobs/${params.project}"
}
}
println "::error:: Imputation job ${statusMessage}."
return
}
//submit counters for successful imputation jobs
if (params.mode == 'imputation') {
println "::submit-counter name=samples::"
println "::submit-counter name=genotypes::"
println "::submit-counter name=chromosomes::"
println "::submit-counter name=runs::"
println "::set-value-and-submit name=reference_panel::${params.refpanel.id}"
println "::set-value-and-submit name=phasing_engine::${phasing_engine}"
println "::set-value-and-submit name=genome_build::${params.build}"
}
// imputation job
if (params.merge_results === true && params.encryption.enabled === true) {
if (params.send_mail && params.user.email != null) {
sendMail{
to "${params.user.email}"
subject "[${params.service.name}] Job ${params.project} is complete"
body "Dear ${params.user.name}, \n Your imputation job has finished succesfully. The password for the imputation results is: ${params.encryption_password}\n\n You can download the results from the following link: ${params.service.url}/index.html#!jobs/${params.project}"
}
println "::message:: Data have been exported successfully. We have sent a notification email to <b>${params.user.email}</b>"
} else {
println "::message:: Data have been exported successfully. We encrypted the results with the following password: <b>${params.encryption_password}</b>"
}
return
}
//PGS job
if (params.send_mail && params.user.email != null) {
sendMail{
to "${params.user.email}"
subject "[${params.service.name}] Job ${params.project} is complete"
body "Dear ${params.user.name}, \n Your PGS job has finished successfully. \n\n You can download the results from the following link: ${params.service.url}/index.html#!jobs/${params.project}"
}
println "::message:: Data have been exported successfully. We have sent a notification email to <b>${params.user.email}</b>"
} else {
println "::message:: Data have been exported successfully."
}
}