-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
142 lines (119 loc) · 2.85 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
process indexVCF {
container 'quay.io/biocontainers/bcftools:1.18--h8b25389_0'
publishDir params.outdir+'/raw', mode: 'copy'
input:
path(vcfFile)
output:
path("${vcfFile}"), emit: vcf
path("${vcfFile}.csi"), emit: index
script:
"""
bcftools index ${vcfFile}
"""
}
process listVariants {
container 'quay.io/biocontainers/bcftools:1.18--h8b25389_0'
input:
path(vcfFile)
path(vcfFileIndex)
output:
path("variants.txt")
script:
"""
bcftools query -f '%CHROM\t%POS\n' ${vcfFile} > variants.txt
"""
}
process writeWindows {
container 'quay.io/biocontainers/pandas:1.5.2'
publishDir params.outdir, mode: 'copy'
input:
path(variantFile)
val(windowSize)
val(overlap)
output:
path("windows.txt")
script:
"""
#!/usr/bin/env python3
import pandas as pd
from math import ceil
windowSize=$windowSize
overlap=$overlap
df=pd.read_csv('${variantFile}', header=None, sep='\t')
grouped_df = df.groupby(df[0])
with open('windows.txt', 'w') as f:
for group_name, group_data in grouped_df:
tmp = group_data.reset_index(drop=True)
end = 0
for i in range(0, ceil(len(tmp)/windowSize)+1):
start = max(end-overlap, 0)
end = min(start+windowSize, len(tmp))
f.write(str(group_name)+':'+str(tmp[1][start])+'-'+str(tmp[1][end-1])+'\\n')
"""
}
process splitVCFByWindow {
container 'quay.io/biocontainers/bcftools:1.18--h8b25389_0'
publishDir params.outdir+'/windows', mode: 'copy'
input:
val(window)
path(vcfFile)
path(vcfFileIndex)
output:
path("${window}_${vcfFile}")
script:
"""
bcftools view -r ${window} ${vcfFile} -Oz -o ${window}_${vcfFile}
"""
}
process imputeWindows {
container 'quay.io/biocontainers/beagle:5.4_22Jul22.46e--hdfd78af_0'
publishDir params.outdir+'/imputed/windows', mode: 'copy'
input:
path(vcfFile)
output:
path("imputed_${vcfFile}")
script:
"""
beagle gt=${vcfFile} out=imputed_${vcfFile.name.replaceAll('.vcf.gz', '')}
"""
}
process indexImputedWindow {
container 'quay.io/biocontainers/bcftools:1.18--h8b25389_0'
input:
path(vcfFile)
output:
path("${vcfFile}.csi"), emit: index
script:
"""
bcftools index ${vcfFile}
"""
}
process mergeImputedWindows {
container 'quay.io/biocontainers/bcftools:1.18--h8b25389_0'
publishDir params.outdir+'/imputed/', mode: 'copy'
input:
path(vcfFiles)
path(vcfIndices)
output:
path("merged_imputed.vcf.gz")
script:
"""
bcftools concat imputed*.vcf.gz -Oz -o merged_imputed.vcf.gz
"""
}
workflow {
indexVCF(params.vcf)
| listVariants
writeWindows(listVariants.out, params.windowSize, params.overlap)
| splitText
| map { it.trim()}
| set { ch_windows}
splitVCFByWindow(ch_windows, indexVCF.out.vcf, indexVCF.out.index)
| imputeWindows
| collect
| set { ch_imputedWindows}
indexImputedWindow(imputeWindows.out)
| collect
| set { ch_windowIndices}
mergeImputedWindows(ch_imputedWindows, ch_windowIndices)
}