forked from EBIvariation/variant-remapping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
executable file
·303 lines (249 loc) · 9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env nextflow
// Enable syntax extension
// See https://www.nextflow.io/docs/latest/dsl2.html
nextflow.enable.dsl=2
def helpMessage() {
log.info"""
Process a VCF file containing SNV or short indels associated with the old genome to remap the variant's coordinates to the new genome.
Inputs:
--vcffile original vcf file containing variants to be remapped [required]
--oldgenome source genome file used to discover variants from the file provided with --vcffile [required]
--newgenome new genome which the original vcf file will be mapped against [required]
Outputs:
--outfile final vcf containing variants mapped to new genome [required]
"""
}
params.outfile = null
params.oldgenome = null
params.newgenome = null
params.vcffile = null
params.help = null
// Show help message
if (params.help) exit 0, helpMessage()
// Test input files
if (!params.vcffile || !params.oldgenome || !params.outfile || !params.newgenome) {
if (!params.vcffile) log.warn('Provide a input vcf file using --vcffile')
if (!params.oldgenome) log.warn('Provide a fasta file for the old genome --oldgenome')
if (!params.outfile) log.warn('Provide a path to the output vcf file using --outfile')
if (!params.newgenome) log.warn('Provide a fasta file for the new genome file using --newgenome')
exit 1, helpMessage()
}
// basename and basedir of the output file to know how to name the output files
outfile_basename = file(params.outfile).getName()
outfile_dir = file(params.outfile).getParent()
/*
* Uncompress VCF file
*/
process uncompressInputVCF {
input:
path "source.vcf"
output:
path "uncompressed.vcf", emit: vcf_file
script:
if ( file(params.vcffile).getExtension() == 'gz' )
"""
gunzip -c source.vcf > uncompressed.vcf
"""
else
"""
ln -nfs source.vcf uncompressed.vcf
"""
}
/*
* Store the original VCF header for later use
*/
process storeVCFHeader {
input:
path "source.vcf"
output:
path "vcf_header.txt", emit: vcf_header
"""
bcftools view --header-only source.vcf > vcf_header.txt
"""
}
include { prepare_old_genome; prepare_new_genome; prepare_new_genome_bowtie } from './prepare_genome.nf'
include { process_split_reads; process_split_reads_mid; process_split_reads_long; process_split_reads_with_bowtie } from './variant_to_realignment.nf'
/*
* Create the header for the output VCF
*/
process buildHeader {
input:
path "variants_remapped_sorted.vcf"
path "vcf_header.txt"
output:
path "final_header.txt", emit: final_header
"""
# Create list of contigs/chromosomes to be added to the header
cut -f 1 variants_remapped_sorted.vcf | sort -u > contig_names.txt
while read CHR; do echo "##contig=<ID=\${CHR}>"; done < contig_names.txt > contigs.txt
# Add the reference assembly
echo "##reference=${params.newgenome}" >> contigs.txt
# Copy everything that isn't #CHROM (the column titles), ##contig or ##reference from the old header to a temp
awk '(\$1 !~ /^##contig/ && \$1 !~ /^##reference/ && \$1 !~ /^#CHROM/) {print \$0}' vcf_header.txt > temp_header.txt
# Add variant remapping INFO definition to the header
echo -e '##INFO=<ID=st,Number=1,Type=String,Description="Strand change observed in the alignment.">' >> temp_header.txt
echo -e '##INFO=<ID=rac,Number=1,Type=String,Description="Reference allele change during the alignment.">' >> temp_header.txt
echo -e '##INFO=<ID=nra,Number=0,Type=Flag,Description="Novel reference allele that was not observed in the previous set of alternate">' >> temp_header.txt
echo -e '##INFO=<ID=zlr,Number=0,Type=Flag,Description="Zero length allele. Had to be expanded from the reference.">' >> temp_header.txt
# Add the two headers together and add the column names
cat temp_header.txt contigs.txt > final_header.txt
tail -n 1 vcf_header.txt >> final_header.txt
"""
}
/*
* Add header to output VCF and merge with reference allele file
*/
process mergeHeaderAndContent {
input:
path "final_header.txt"
path "variants_remapped_sorted.vcf"
output:
path "vcf_out_with_header.vcf", emit: final_vcf_with_header
"""
# Add header to the vcf file:
cat final_header.txt variants_remapped_sorted.vcf > vcf_out_with_header.vcf
"""
}
/*
* Sort VCF file
*/
process sortVCF {
input:
path "variants_remapped.vcf"
output:
path "variants_remapped_sorted.vcf.gz", emit: variants_remapped_sorted_gz
"""
bgzip variants_remapped.vcf
bcftools sort -o variants_remapped_sorted.vcf.gz -Oz variants_remapped.vcf.gz
"""
}
/*
* Run bcftools norm to swap the REF and ALT alleles if the REF doesn't match the new assembly
*/
process normalise {
publishDir outfile_dir,
overwrite: true,
mode: "copy"
input:
path "variants_remapped_sorted.vcf.gz"
path "genome.fa"
output:
path "${outfile_basename}", emit: final_output_vcf
"""
bcftools norm --check-ref e -f genome.fa variants_remapped_sorted.vcf.gz -o ${outfile_basename} -O v
"""
}
/*
* Create file containing remapping stats
*/
process outputStats {
publishDir outfile_dir,
overwrite: true,
mode: "copy"
input:
path "summary"
output:
path "${outfile_basename}.yml"
"""
ln -s summary "${outfile_basename}.yml"
"""
}
process combineVCF {
input:
path "variants1.vcf"
path "variants2.vcf"
path "variants3.vcf"
output:
path "merge.vcf", emit: merge_vcf
"""
cat variants1.vcf variants2.vcf variants3.vcf > merge.vcf
"""
}
process combineYaml {
input:
path "round1.yml"
path "round2.yml"
path "round3.yml"
output:
path "merge.yml", emit: merge_yml
"""
cat round1.yml round2.yml round3.yml > merge.yml
"""
}
// Take variants remapped to the new genome and merge them back with the original header and sort the output
workflow finalise {
take:
variants_remapped
vcf_header
genome
summary
main:
buildHeader(variants_remapped, vcf_header)
mergeHeaderAndContent(buildHeader.out.final_header, variants_remapped)
sortVCF(mergeHeaderAndContent.out.final_vcf_with_header)
normalise(sortVCF.out.variants_remapped_sorted_gz, genome)
outputStats(summary)
}
// process_with_minimap
// Workflow without a name is the default workflow that gets executed when the file is run through nextflow
workflow {
main:
prepare_old_genome(params.oldgenome)
prepare_new_genome(params.newgenome)
uncompressInputVCF(params.vcffile)
storeVCFHeader(uncompressInputVCF.out.vcf_file)
process_split_reads(
uncompressInputVCF.out.vcf_file,
params.oldgenome,
prepare_old_genome.out.genome_fai,
prepare_old_genome.out.genome_chrom_sizes,
params.newgenome,
prepare_new_genome.out.genome_fai
)
process_split_reads_mid(
process_split_reads.out.variants_unmapped,
params.oldgenome,
prepare_old_genome.out.genome_fai,
prepare_old_genome.out.genome_chrom_sizes,
params.newgenome,
prepare_new_genome.out.genome_fai
)
process_split_reads_long(
process_split_reads_mid.out.variants_unmapped,
params.oldgenome,
prepare_old_genome.out.genome_fai,
prepare_old_genome.out.genome_chrom_sizes,
params.newgenome,
prepare_new_genome.out.genome_fai
)
combineVCF(
process_split_reads.out.variants_remapped,
process_split_reads_mid.out.variants_remapped,
process_split_reads_long.out.variants_remapped
)
combineYaml(
process_split_reads.out.summary_yml,
process_split_reads_mid.out.summary_yml,
process_split_reads_long.out.summary_yml,
)
finalise(combineVCF.out.merge_vcf, storeVCFHeader.out.vcf_header, params.newgenome, combineYaml.out.merge_yml)
}
//process_with_bowtie
workflow process_with_bowtie {
main:
prepare_old_genome(params.oldgenome)
prepare_new_genome_bowtie(params.newgenome)
uncompressInputVCF(params.vcffile)
storeVCFHeader(uncompressInputVCF.out.vcf_file)
process_split_reads_with_bowtie(
uncompressInputVCF.out.vcf_file,
params.oldgenome,
prepare_old_genome.out.genome_fai,
prepare_old_genome.out.genome_chrom_sizes,
params.newgenome,
prepare_new_genome_bowtie.out.genome_fai,
prepare_new_genome_bowtie.out.bowtie_indexes
)
finalise(process_split_reads_with_bowtie.out.variants_remapped, storeVCFHeader.out.vcf_header,
params.newgenome, process_split_reads_with_bowtie.out.summary_yml)
}