-
Notifications
You must be signed in to change notification settings - Fork 2
/
scalepopgen.nf
298 lines (201 loc) · 9.3 KB
/
scalepopgen.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env nextflow
nextflow.enable.dsl=2
/*
========================================================================================
IMPORT LOCAL MODULES/SUBWORKFLOWS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
//
// MODULES
//
include { FILTER_SNPS_FROM_BED } from "${baseDir}/modules/plink/filter_snps_from_bed"
include { PLOT_GEO_MAP } from "${baseDir}/modules/plot_geo_map"
include { PRINT_GENERAL_OPTIONS } from "${baseDir}/modules/help/print_general_options"
include { PRINT_FILTERING_OPTIONS } from "${baseDir}/modules/help/print_filtering_options"
include { PRINT_GENSTRUCT_OPTIONS } from "${baseDir}/modules/help/print_genstruct_options"
include { PRINT_TREEMIX_OPTIONS } from "${baseDir}/modules/help/print_treemix_options"
include { PRINT_SIG_SEL_OPTIONS } from "${baseDir}/modules/help/print_sig_sel_options"
include { EXTRACT_UNRELATED_SAMPLE_LIST } from "${baseDir}/modules/plink/extract_unrelated_sample_list"
include { KEEP_INDI } from "${baseDir}/modules/vcftools/keep_indi"
include { REMOVE_INDI } from "${baseDir}/modules/vcftools/remove_indi"
include { FILTER_SITES } from "${baseDir}/modules/vcftools/filter_sites"
include { PREPARE_NEW_MAP } from "${baseDir}/modules/prepare_new_map"
include { CONCAT_VCF } from "${baseDir}/modules/vcftools/concat_vcf"
include { GENERATE_POP_COLOR_MAP } from "${baseDir}/modules/generate_pop_color_map.nf"
//
// SUBWORKFLOW: Consisting of a mix of local modules
//
include { CHECK_INPUT } from "${baseDir}/subworkflows/check_input"
include { PREPARE_INDIV_REPORT } from "${baseDir}/subworkflows/prepare_indiv_report"
include { EXPLORE_GENETIC_STRUCTURE } from "${baseDir}/subworkflows/explore_genetic_structure"
include { CONVERT_VCF_TO_PLINK as CONVERT_FILTERED_VCF_TO_PLINK } from "${baseDir}/subworkflows/convert_vcf_to_plink"
include { RUN_TREEMIX } from "${baseDir}/subworkflows/run_treemix"
include { CONVERT_BED_TO_SPLITTED_VCF } from "${baseDir}/subworkflows/convert_bed_to_splitted_vcf"
include { RUN_SEL_VCFTOOLS } from "${baseDir}/subworkflows/run_sel_vcftools"
include { PREPARE_ANC_FILES } from "${baseDir}/subworkflows/prepare_anc_files"
include { RUN_SEL_SWEEPFINDER2 } from "${baseDir}/subworkflows/run_sel_sweepfinder2"
include { RUN_SIG_SEL_PHASED_DATA } from "${baseDir}/subworkflows/run_sig_sel_phased_data"
workflow{
if( params.help ){
if ( params.indi_snp_filters ){
PRINT_FILTERING_OPTIONS()
exit 0
}
if ( params.gen_struct ){
PRINT_GENSTRUCT_OPTIONS()
exit 0
}
if ( params.phylogeny ){
PRINT_TREEMIX_OPTIONS()
exit 0
}
if ( params.signature_selection ){
PRINT_SIG_SEL_OPTIONS()
exit 0
}
else{
PRINT_GENERAL_OPTIONS()
exit 0
}
}
else{
// first check if the input parameter contains ".csv"
// yes --> input is vcf and sample map files is required
// no --> input is assumed to be plink bed file
if( params.input.endsWith(".csv") ){
// check input vcfsheet i.e. if vcf file exits //
CHECK_INPUT(
params.input
)
samplesheet = Channel.fromPath( params.sample_map )
map_file = samplesheet.map{ samplesheet -> if(!file(samplesheet).exists() ){ exit 1, "ERROR: file does not exit or sample map does not end with .map -> ${samplesheet}" }else{samplesheet} }
// combine channel for vcf and sample map file //
chrom_vcf_idx_map = CHECK_INPUT.out.chrom_vcf_idx.combine(map_file)
is_vcf = true
}
else{
prefix_bed = Channel.fromFilePairs(params.input, size:3)
is_vcf = false
}
GENERATE_POP_COLOR_MAP(
is_vcf ? chrom_vcf_idx_map.map{chrom, vcf, idx, map_f -> map_f}.unique() : prefix_bed.map{prefix,bed -> bed[2]}
)
if ( is_vcf ){
if( params.apply_indi_filters ){
o_map = chrom_vcf_idx_map.map{chrom, vcf, idx, map_f -> map_f}.unique()
/* --> king_cutoff and missingness filter should be based on the entire genome therefore vcf file should be concatenated first and then
supply to plink. From plink module, the list of individuals to be kept is piped out and supply to keep indi module. This module will
then extract these sets of individuals from each chromosome file separately. Note that if custom individuals to be removed are also
supplied then this will be considered in extract_unrelated_sample_list module as well.
*/
if( params.king_cutoff > 0 || params.mind > 0 ){
vcflist = chrom_vcf_idx_map.map{chrom, vcf, idx, map_f -> vcf}.collect()
CONCAT_VCF(vcflist)
EXTRACT_UNRELATED_SAMPLE_LIST( CONCAT_VCF.out.concatenatedvcf )
KEEP_INDI( chrom_vcf_idx_map.combine( EXTRACT_UNRELATED_SAMPLE_LIST.out.keep_indi_list ))
PREPARE_NEW_MAP(
o_map,
EXTRACT_UNRELATED_SAMPLE_LIST.out.keep_indi_list
)
n0_chrom_vcf_idx_map = KEEP_INDI.out.f_chrom_vcf_idx.combine(PREPARE_NEW_MAP.out.n_map)
}
/*
if only the individuals to be removed are supplied then there is no need to concat the file.
*/
else{
rmindilist = Channel.fromPath( params.rem_indi )
ril = rmindilist.map{ rmindilist -> if(!file(rmindilist).exists() ){ exit 1,"ERROR: file does not exit -> ${rmindilist}" }else{rmindilist} }
chrom_vcf = chrom_vcf_idx_map.map{chrom, vcf, idx, map -> tuple(chrom,vcf)}
REMOVE_INDI( chrom_vcf )
PREPARE_NEW_MAP(
o_map,
ril
)
n0_chrom_vcf_idx_map = REMOVE_INDI.out.f_chrom_vcf_idx.combine(PREPARE_NEW_MAP.out.n_map)
}
}
else{
n0_chrom_vcf_idx_map = chrom_vcf_idx_map
}
if ( params.apply_snp_filters ){
n_map = n0_chrom_vcf_idx_map.map{chrom, vcf, idx, map_f -> map_f}.unique()
chrom_vcf = n0_chrom_vcf_idx_map.map{chrom, vcf, idx, map_f -> tuple(chrom, vcf)}
FILTER_SITES(chrom_vcf)
n1_chrom_vcf_idx_map = FILTER_SITES.out.s_chrom_vcf_tbi.combine(n_map)
}
else{
n1_chrom_vcf_idx_map = n0_chrom_vcf_idx_map
}
if( params.indiv_summary ){
PREPARE_INDIV_REPORT( n1_chrom_vcf_idx_map )
}
}
// else input is bed:
// indi filtering and sites filtering --> use plink
else{
if( params.apply_indi_filters ){
//f_bed = prefix_bed.map{ prefix, bed -> bed }
EXTRACT_UNRELATED_SAMPLE_LIST( prefix_bed )
n2_bed = EXTRACT_UNRELATED_SAMPLE_LIST.out.indi_filt_bed
}
else{
n2_bed = prefix_bed.map{ prefix, bed -> bed }
}
if( params.apply_snp_filters ){
FILTER_SNPS_FROM_BED( n2_bed )
n3_bed = FILTER_SNPS_FROM_BED.out.filt_sites_bed
}
else{
n3_bed = n2_bed
}
if (params.treemix || params.sig_sel) {
CONVERT_BED_TO_SPLITTED_VCF( n3_bed )
n1_chrom_vcf_idx_map = CONVERT_BED_TO_SPLITTED_VCF.out.p2_chrom_vcf_idx_map
}
}
//plot samples on world map
if( params.geo_plot_yml != "none" && params.tile_yml != "none" && params.f_pop_cord != "none" ){
geo_yml = Channel.fromPath(params.geo_plot_yml)
tile_yml = Channel.fromPath(params.tile_yml)
PLOT_GEO_MAP(
geo_yml,
tile_yml,
GENERATE_POP_COLOR_MAP.out.m_pop_sc_colr
)
}
// in case of pca and admixture, convert filtered vcf to bed (if input is vcf)
// the main rationale is that all plink dependent analysis should be covered in this "if" block
if ( params.genetic_structure ) {
if( is_vcf ){
CONVERT_FILTERED_VCF_TO_PLINK(
n1_chrom_vcf_idx_map
)
n4_bed = CONVERT_FILTERED_VCF_TO_PLINK.out.bed
}
else{
n4_bed = n3_bed
}
EXPLORE_GENETIC_STRUCTURE(
n4_bed,
GENERATE_POP_COLOR_MAP.out.m_pop_sc_colr
)
}
if( params.treemix ){
RUN_TREEMIX( n1_chrom_vcf_idx_map )
}
if( params.sig_sel ){
if( params.tajimas_d || params.pi || params.pairwise_fst || params.single_vs_all_fst ){
RUN_SEL_VCFTOOLS( n1_chrom_vcf_idx_map )
}
if( params.clr || params.ihs || params.xpehh ){
if( params.clr ){
PREPARE_ANC_FILES( n1_chrom_vcf_idx_map )
RUN_SEL_SWEEPFINDER2( PREPARE_ANC_FILES.out.n2_chrom_vcf_idx_map_anc )
}
if ( params.ihs || params.xpehh ) {
RUN_SIG_SEL_PHASED_DATA( n1_chrom_vcf_idx_map )
}
}
}
}
}