forked from h3abionet/h3agwas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopbottom.nf
227 lines (174 loc) · 5.19 KB
/
topbottom.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
/* (C) H3ABionet
GPL
Scott Hazelhust, 2017-2018
*/
params.chipdescription = "/external/diskA/novartis/metadata/HumanOmni5-4v1_B_Physical-and-Genetic-Coordinates.txt"
inpat = "${params.input_dir}/${params.input_pat}"
params.output = "chip"
params.strandreport = false
params.manifest = false
params.queue = 'batch'
plink_src = Channel.create()
null_values = [0,"0",false,""]
if (! null_values.contains(params.samplesheet))
samplesheet = Channel.fromPath(params.samplesheet)
def condChannel = { parm, descr ->
filename = "/tmp/emptyZ0${descr}.txt";
new File(filename).createNewFile()
if ((parm==0) || (parm=="0") || (parm == false) || (parm=="") || (parm=="false")) {
return Channel.fromPath(filename)
} else {
return Channel.fromPath(parm);
}
}
ref_ch = condChannel(params.reference,"ref")
manifest_src_ch = condChannel(params.manifest,"man")
strand_src_ch = condChannel(params.strandreport,"strn")
manifest_ch = Channel.create()
manifest1_ch = Channel.create()
manifest_src_ch.separate(manifest_ch,manifest1_ch)
strand_ch = Channel.create()
strand1_ch = Channel.create()
strand_src_ch.separate(strand_ch,strand1_ch) {a -> [a,a]}
ref1_ch = Channel.create()
ref2_ch = Channel.create()
ref_ch.separate(ref1_ch, ref2_ch) { a -> [a, a] }
def gChrom= { x ->
println x
m = x =~ /.*-(\w+).*/;
return m[0][1]
}
output = params.output
// array may be the manifest or pref a file with both genetic
// and physical coordinates
array = Channel.fromPath(params.chipdescription)
report = Channel.fromPath(inpat)
output_align = params.output_align
process illumina2lgen {
maxForks params.max_forks
input:
set file(report), file(array) from report.combine(array)
output:
set file("${output}.ped"), file("${output}.map") into ped_ch
script:
idpat = params.idpat
samplesize = params.samplesize
idpat = params.idpat
output = report.baseName
template "topbot2plink.py"
}
process bedify {
input:
set file(ped), file(map) from ped_ch
output:
file("${base}.bed") into bed_ch
file ("${base}.bim") into bim_ch
file("${base}.fam") into fam_ch
script:
base = ped.baseName
"""
echo $base
plink --file $base --make-bed --out $base
"""
}
process combineBed {
input:
file(bed) from bed_ch.toList()
file(bim) from bim_ch.toList()
file(fam) from fam_ch.toList()
output:
set file("raw.bed"), file("raw.fam"), file("raw.log") into plink_src
set file("rawraw.bim") into fill_in_bim_ch
file ('raw.fam') into fix_fam_ch
script:
"""
ls *.bed | sort > beds
ls *.bim | sort > bims
ls *.fam | sort > fams
paste beds bims fams > mergelist
plink --merge-list mergelist --make-bed --out raw
mv raw.bim rawraw.bim
"""
}
process fillInBim { // Deals with monomorphic or non-called SNPs
input:
file(inbim) from fill_in_bim_ch
file(strand) from strand1_ch
file(manifest) from manifest1_ch
output:
file("raw.bim") into filled_bim_ch
script:
"fill_in_bim.py $strand $manifest $inbim raw.bim"
}
process getFlips {
input:
file(strandreport) from strand_ch
file(manifest) from manifest_ch
output:
file(flips) into flip_ch
script:
flips = "flips.lst"
template "strandmismatch.py"
}
process checkReferenceFormat {
input:
file(ref) from ref1_ch
output:
stdout into ref_parm_ch
script:
refBase = ref.baseName
newref = "${refBase}.new"
"checkRef.py $ref $newref"
}
process alignStrand {
input:
set file(bed), file(fam), file(logfile) from plink_src
file(bim) from filled_bim_ch
file(ref) from ref2_ch
val(ref_parm) from ref_parm_ch
file(flips) from flip_ch
publishDir params.output_dir, pattern: "*.{bed,bim,log,badsnps}", \
overwrite:true, mode:'copy'
output:
set file("*.{bed,bim,log,badsnps}") into aligned_ch
script:
base = bed.baseName
refBase = ref.baseName
ref_parm = ref_parm.trim()
opt = "--a2-allele $ref $ref_parm"
if (refBase=="empty") opt="--keep-allele-order"
"""
plink --bfile $base $opt --flip $flips --make-bed --out $output
grep Impossible ${output}.log | tr -d . | sed 's/.*variant//' > ${output}.badsnps
"""
}
if (null_values.contains(params.samplesheet)) {
process fixFam{
input:
file(fam) from fix_fam_ch
publishDir params.output_dir, pattern: "${output}.fam", \
overwrite:true, mode:'copy'
output:
set file("${output}.fam") into fixedfam_ch
script:
"cp $fam ${output}.fam"
}
} else {
process fixFam {
input:
file(samplesheet)
file(fam) from fix_fam_ch
publishDir params.output_dir, pattern: "${output}.fam", \
overwrite:true, mode:'copy'
output:
set file("${output}.fam") into fixedfam_ch
script:
idpat = params.idpat
batch_col = params.batch_col
template "sheet2fam.py"
}
}
fixedfam_ch.combine(aligned_ch).subscribe {
files = it.collect { fn -> fn.getName().replace(".*/","") }
println "The output can be found in ${params.output_dir}: files are ${files}"
}