-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimpute.nf
219 lines (148 loc) · 6.56 KB
/
impute.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
params.pedFile = 'TODO'
params.mapFile = 'TODO'
params.genticMapDir = 'TODO'
params.chromosomeSizesFile = 'b37.chrom.sizes'
params.referenceHapsFilePattern = "ALL.chr%s.integrated_phase1_v3.20101123.snps_indels_svs.genotypes.nomono.haplotypes.gz"
params.referenceLegendFilePattern = "ALL.chr%s.integrated_phase1_v3.20101123.snps_indels_svs.genotypes.nomono.legend.gz"
params.referenceGeneticMapPattern = "genetic_map_chr%s_combined_b37.txt"
params.referenceSample = "ALL.integrated_phase1_v3.20101123.snps_indels_svs.genotypes.sample"
params.publishDirPath = "$HOME/imputation-results"
mapFileChan = Channel.fromPath(params.mapFile)
pedFileChan = Channel.fromPath(params.pedFile)
chromosomesList = 1..22
db_path = file(params.genticMapDir)
process plink {
container 'insilicodb/docker-impute2'
maxForks 6
input:
file mapFile from mapFileChan
file pedFile from pedFileChan
each chromosome from chromosomesList
output:
set val(chromosome), file("chr${chromosome}.bed"), file("chr${chromosome}.fam"), file("chr${chromosome}.bim") into plinkOutChan
"""
plink --noweb --file ${pedFile.baseName} --chr $chromosome --make-bed --out chr${chromosome}
plink -bfile chr${chromosome} --list-duplicate-vars ids-only suppress-first
[[ -e "plink.dupvar" ]] && plink --bfile chr${chromosome} --exclude plink.dupvar --make-bed --out chr${chromosome}
"""
}
//plinkOutChan.subscribe{ println "${it.name}"}
process shapeitCheck {
validExitStatus 0,1,2
errorStrategy 'ignore'
maxForks 6
container 'insilicodb/docker-impute2'
input:
set val(chromosome), file("chr${chromosome}.bed"), file("chr${chromosome}.fam"), file("chr${chromosome}.bim") from plinkOutChan
file db_path
output:
set val(chromosome), file("chr${chromosome}.alignments.log"), file("chr${chromosome}.alignments.snp.strand.exclude"), file("chr${chromosome}.bed"), file("chr${chromosome}.fam"), file("chr${chromosome}.bim") into shapitCheckChan
script:
hapFile = file( db_path.name + "/" + sprintf(params.referenceHapsFilePattern, chromosome) )
legendFile = file( db_path.name + "/" + sprintf(params.referenceLegendFilePattern, chromosome) )
sampleFile = file( db_path.name + "/" + params.referenceSample )
"""
shapeit -check --input-bed chr${chromosome}.bed chr${chromosome}.bim chr${chromosome}.fam --input-ref $hapFile $legendFile $sampleFile --output-log chr${chromosome}.alignments
"""
}
//shapitCheckChan.subscribe{ println "${it.name}"}
process shapeit {
maxForks 4
container 'insilicodb/docker-impute2'
input:
set val(chromosome), file("chr${chromosome}.alignments.log"), file("chr${chromosome}.alignments.snp.strand.exclude"), file("chr${chromosome}.bed"), file("chr${chromosome}.fam"), file("chr${chromosome}.bim") from shapitCheckChan
file db_path
output:
set val(chromosome), file("chr${chromosome}.phased.haps"), file("chr${chromosome}.phased.sample") into shapeitChan
script:
hapFile = file( db_path.name + "/" + sprintf(params.referenceHapsFilePattern, chromosome) )
legendFile = file( db_path.name + "/" + sprintf(params.referenceLegendFilePattern, chromosome) )
sampleFile = file( db_path.name + "/" + params.referenceSample )
excludeFile = "chr${chromosome}.alignments.snp.strand.exclude"
mapFile = file( db_path.name + "/" + sprintf(params.referenceGeneticMapPattern, chromosome) )
"""
shapeit --input-bed chr${chromosome}.bed chr${chromosome}.bim chr${chromosome}.fam --input-ref $hapFile $legendFile $sampleFile --exclude-snp $excludeFile --input-map $mapFile -O chr${chromosome}.phased
"""
}
imputeChromChunckChannel = shapeitChan.flatMap { chromosome, hapsFile, sampleFile ->
def results = []
def chunks = getChromosomeChunkPairs(getChromosomeSize(file(params.chromosomeSizesFile), chromosome))
chunks.each { chunkStart, chunkEnd ->
results.push( [ chromosome, hapsFile, sampleFile, chunkStart, chunkEnd] )
}
return results
}
process impute2 {
errorStrategy 'ignore'
container 'insilicodb/docker-impute2'
maxForks 2
input:
set val(chromosome), file("chr${chromosome}.phased.haps"), file("chr${chromosome}.phased.sample"), val(chunkStart), val(chunkEnd) from imputeChromChunckChannel
file db_path
output:
set val(chromosome), file("chr${chromosome}-${chunkStart}-${chunkEnd}.imputed") into impute2Chan
script:
hapFile = file( db_path.name + "/" + sprintf(params.referenceHapsFilePattern, chromosome) )
legendFile = file( db_path.name + "/" + sprintf(params.referenceLegendFilePattern, chromosome) )
sampleFile = file( db_path.name + "/" + params.referenceSample )
mapFile = file( db_path.name + "/" + sprintf(params.referenceGeneticMapPattern, chromosome) )
"""
impute2 -use_prephased_g -known_haps_g chr${chromosome}.phased.haps -h $hapFile -l $legendFile -m $mapFile -int $chunkStart $chunkEnd -Ne 20000 -o chr${chromosome}-${chunkStart}-${chunkEnd}.imputed
#sometimes there are no SNP's in a region
if [ ! -f "chr${chromosome}-${chunkStart}-${chunkEnd}.imputed" ]; then
touch "chr${chromosome}-${chunkStart}-${chunkEnd}.imputed";
fi
"""
}
impute2List = impute2Chan.toSortedList() //gives a dataFlow instance, nee to get the val property of it
impute2List = impute2List.val
impute2Map = [:]
impute2List.each { chrom, file ->
if ( !impute2Map.containsKey(chrom) ) {
impute2Map.put(chrom, [])
}
impute2Map.get(chrom).add(file)
}
impute2MapChannel = Channel.create()
impute2Map.each { chrom, fileList ->
impute2MapChannel.bind([chrom, fileList])
}
impute2MapChannel.close()
process impute2Concat {
publishDir params.publishDirPath
input:
set val(chromosome), file(imputedFiles) from impute2MapChannel
output:
set val(chromosome), file("chr${chromosome}.imputed") into impute2ConcatChan
"""
cat $imputedFiles > chr${chromosome}.imputed
"""
}
// ----==== utility methods ====----
def getChromosomeSize( chromosomeSizesFile, chromosome ) {
def result = 0
chromosomeSizesFile.splitEachLine("\t") {fields ->
def genomeId
def path
if ( fields[0].trim() == "${chromosome}" ) {
//println "in if"
result = fields[1].trim().toInteger()
return
}
}
result
}
def getChromosomeChunkPairs ( chromosomeSize, chunkSize=5000000 ) {
def result = []
def numberOfChunks = chromosomeSize / chunkSize
def remainder = chromosomeSize % chunkSize
1.upto(numberOfChunks) {
endPosition = it * chunkSize
startPosition = (endPosition - chunkSize) + 1
result = result + [[startPosition, endPosition ]]
}
if ( remainder > 0 ) {
result = result + [[endPosition + 1 , endPosition + remainder ]]
}
result
}