forked from rbpisupati/nf-haplocaller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_gvcf.nf
247 lines (209 loc) · 6.24 KB
/
get_gvcf.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
/*
Provide directories as inputs with .gvcf files present
nextflow run ~/mygit/nf-haplocaller/get_gvcf.nf --input "0*" --input_ext "gvcf" --fasta 100.REF_SEQ/TAIR10_wholeGenome.fasta --outdir temp
*/
params.input = false
params.input_ext = "gz"
params.project = "the1001genomes"
build_index = false
params.outdir = '.'
params.fasta = false
params.cohort = "allsample"
params.name = false
chromosomes = ["Chr1", "Chr2", "Chr3", "Chr4", "Chr5"]
if ( params.fasta ){
genome = file(params.fasta)
reffol = genome.parent
refid = genome.baseName
if( !genome.exists() ) exit 1, "Reference fasta file not found: ${params.fasta}"
bwa_indices = Channel
.fromPath( "$reffol/${refid}.fasta.b*" )
.ifEmpty { build_index = true }
} else {
exit 1, "Provide reference fasta file. Ex., --fasta file_path"
}
/*
* 1. Create a channel for checking bwa index for genome ref
*/
if (build_index == true){
process makeBWAindex {
publishDir "${reffol}", mode: 'copy'
input:
file genome
output:
set file("${refid}.fasta"), file("${refid}.fasta.*"), file "${refid}.dict" into fasta_index
script:
"""
samtools faidx ${genome}
bwa index $genome
picard CreateSequenceDictionary -R $genome -O ${refid}.dict
"""
}
} else {
fasta_index = Channel
.from( [file("$reffol/${refid}.fasta"), file("$reffol/${refid}.fasta.*"), file("${refid}.dict") ])
}
/*
* 10.1 GenomicsDBImport for all the files
*/
chromosomes_ch = Channel.from( chromosomes )
raw_gvcf = Channel.fromPath( "${params.input}" )
raw_gvcf_index = Channel.fromPath( "${params.input}.*" )
process GenomicsDBImport {
tag "gendbi_${chr}"
label 'env_large'
publishDir "$params.outdir/raw_variants/genodbi_gatk", mode: 'copy'
input:
each chr from chromosomes_ch
file in_vcf from raw_gvcf.collect()
file(vcf_idx) from raw_gvcf_index.collect()
output:
set val(chr), file ("${params.cohort}.genomicsdbi.${chr}.dbi") into gendbi_in
script:
def try_vcfs = in_vcf.collect { "-V $it" }.join(' ')
"""
gatk GenomicsDBImport --java-options "-Xmx${task.memory.toGiga()}G -Xms${task.memory.toGiga()}G" \
--tmp-dir=${params.tmpdir} \
${try_vcfs} \
-L ${chr}\
--batch-size 50 \
--genomicsdb-workspace-path ${params.cohort}.genomicsdbi.${chr}.dbi
"""
}
/*
* 10.2 GenotypeGVCF for all the files
*/
process genotypeGVCFs {
tag "gvcf_${chr}"
label 'env_large'
// publishDir "$params.outdir/combinedGVCF", mode: 'copy'
input:
set val(chr), file(workspace) from gendbi_in
output:
set val(chr), file("${params.cohort}.${chr}.vcf.gz"), file("${params.cohort}.${chr}.vcf.gz.tbi") into combined_vcf
script:
"""
WORKSPACE=\$( basename ${workspace} )
gatk GenotypeGVCFs --java-options "-Xmx24g -Xms24g" \
--tmp-dir ${params.tmpdir}\
-R $reffol/${refid}.fasta \
-O ${params.cohort}.${chr}.vcf.gz \
-V gendb://\$WORKSPACE \
-L $chr \
--only-output-calls-starting-in-intervals \
--use-new-qual-calculator
"""
// -D ${dbsnp_resource_vcf} \
// -G StandardAnnotation \
}
/*
* 11 Join all the VCFs generated per chromosome
*/
try_vcf = chromosomes.collect{it -> "--INPUT ${params.cohort}.${it}.vcf.gz"}.join(' ')
process gatherVcfs {
tag "gatherVCF_${params.cohort}"
label 'env_gatk_medium'
publishDir "$params.outdir/raw_variants/combined_GVCF", mode: 'copy'
input:
file (vcf) from combined_vcf.collect()
output:
set file("${params.cohort}.vcf.gz"), file("${params.cohort}.vcf.gz.tbi") into (gvcf_sample_ch, gvcf_select_snp_ch)
script:
"""
gatk --java-options "-Xmx10g -Xms10g" \
GatherVcfs \
${try_vcf} \
--OUTPUT ${params.cohort}.vcf.gz
tabix ${params.cohort}.vcf.gz
"""
}
/*
* 12.1 Get sample names and filter the GVCF by SelectVariants
*/
process getSamples {
tag "samples_${params.cohort}"
label 'env_small'
input:
set file(gvcf), file(gvcf_index) from gvcf_sample_ch
output:
stdout sample_names
script:
"""
tabix -H $gvcf | grep -m 1 "^#CHROM" | awk '{for(i=10;i<=NF;i++) print \$i}'
"""
}
input_names = sample_names
.splitCsv()
.map {row -> "${row[0]}"}
process selectSNPs {
tag "$name"
publishDir "${params.outdir}/raw_variants/sample_biallelics", mode: 'copy'
label 'env_gatk_medium'
input:
val name from input_names
set file(gvcf), file(gvcf_index) from gvcf_select_snp_ch.collect()
output:
file("${name}.BIALLELIC.SNPs.vcf.gz*") into filter_vcf
script:
"""
gatk SelectVariants --java-options "-Xmx24g -Xms24g" \
--tmp-dir ${params.tmpdir}\
-R $reffol/${refid}.fasta \
-V $gvcf \
-select-type SNP --restrict-alleles-to BIALLELIC \
-se "${name}" \
-O ${name}.BIALLELIC.SNPs.vcf.gz
"""
}
// input_gvcfs = Channel
// .fromPath( "${params.input}", type: 'dir' )
// .map{ it -> [it.name, file("$it/*${params.input_ext}"), file("$it/*${params.input_ext}.*")] }
//
// process joinGVCFs {
// label 'env_large'
// tag "$fol_name"
// // publishDir "$params.outdir", mode: 'copy'
//
// input:
// set val(fol_name), file(in_vcf), file(in_vcf_idx) from input_gvcfs
// set ref, ref_idx, ref_dict from fasta_index.collect()
//
// output:
// set val("$fol_name"), file("${fol_name}.vcf.gz"), file("${fol_name}.vcf.gz.tbi") into combgVCF
//
// script:
// def try_vcfs = in_vcf.collect { "-V $it" }.join(' ')
// """
// java -Djava.io.tmpdir=${params.tmpdir} -jar \$EBROOTGATK/GenomeAnalysisTK.jar\
// -T GenotypeGVCFs -R $ref\
// -nt ${task.cpus} \
// $try_vcfs -o ${fol_name}.vcf.gz \
// -allSites
// """
// //gatk --java-options '-Djava.io.tmpdir=${params.tmpdir}' GenotypeGVCFs\
// //-R $reffol/${refid}.fasta\
// //$try_vcfs -O ${fol_name}.vcf.gz \
// //- -variant_index_type LINEAR -variant_index_parameter 128000
// // }
//
// process selectSNPs {
// tag "$fol_name"
// label 'env_medium'
// publishDir "$params.outdir", mode: 'copy'
//
// input:
// set val(fol_name), file(gvcf), file(gvcf_index) from combgVCF
//
// output:
// set file("${fol_name}.filter.vcf"), file("${fol_name}.filter.vcf.idx") into filter_vcf
//
// script:
// """
// java -Djava.io.tmpdir=${params.tmpdir} -jar \$EBROOTGATK/GenomeAnalysisTK.jar\
// -T SelectVariants -R $reffol/${refid}.fasta\
// -nt ${task.cpus} \
// -V $gvcf \
// -o ${fol_name}.filter.vcf \
// -selectType SNP -restrictAllelesTo BIALLELIC
// """
// }