-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.nf
executable file
·193 lines (175 loc) · 4.86 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
#!/usr/bin/env nextflow
/*
* kpi
*
* KIR structural interpretation for raw reads.
*
* Predict a (possibly-amiguous) pair of haplotypes given the
* presence/absence (PA) genotype from one individual and a collection
* of PA reference haplotypes.
*
* The input is a directory of 2-column tab-separated unix-formatted text
* files. Each file maps IDs to one or more files. The first column of each
* file is the ID of a set of reads (e.g., representing
* an individual). The second column is non-space-containing path to a
* optionally-gzipped fastq or fasta file.
*
* The output contains genotype and haplotype-pair predictions.
*
* One individual with 60G of gzippped fastq takes about todo minutes
* on an 8 CPU computer with 20G memory.
*
* @author Dave Roe
*/
inputSuffix = "txt"
nfKMCForks = 1 // run this many input text files in parallel
params.input = '/opt/kpi/raw/'
params.output = '/opt/kpi/output'
params.id = 'defaultID'
geneProbes = '/opt/kpi/input/markers_v3'
nfForks = 4 // run this many input text files in parallel
// input: kmc probe txt files
kmcNameSuffix = '_hits.txt' // extension on the file name
bin1Suffix = 'bin1'
probeFile = '/opt/kpi/input/markers_v3.fasta'
params.haps = '/opt/kpi/input/all_haps_v6.txt'
params.p = null
params.m = null
workflow.onComplete { file('work').deleteDir() }
// things that probably won't change per run
resultDir = params.output
haps = params.haps
if(!resultDir.trim().endsWith("/")) {
resultDir += "/"
}
probeCmd = ""
mapDir = ""
if(params.p != null) {
probeCmd = "probeFastqsKMC.groovy -d ${params.id} -p ${params.p} -o . -w ."
mapDir - params.p
} else if(params.m != null) {
probeCmd = "probeFastqsKMC.groovy -m ${params.m} -o . -w ."
mapDir - params.m
}
if(!mapDir.trim().endsWith("/")) {
mapDir += "/"
}
fqsIn = Channel.fromPath(mapDir).ifEmpty { error "cannot find anything in $mapDir" }
/*
* @todo handle both input options
* @todo m option only publishes everything at the end
*/
process probeFastqs {
//container = "droeatnmdp/kpi:latest"
//publishDir resultDir, mode: 'copy', overwrite: true
maxForks 1
input: file(f) from fqsIn
output:
file('*.kmc_*') into kmcdb
script:
"""
${probeCmd}
"""
} // probeFastqs
process probeDB {
//publishDir resultDir, mode: 'copy', overwrite: true
input: file(kmc) from kmcdb
output:
file{ "*_hits.txt"} into filterdb
script:
"""
filterMarkersKMC2.groovy -d . -p ${geneProbes} -o . -w .
"""
} // probeFastqs
/*
* db2Locus
*
* Given a kmc output file, bin the hit reads into separate files based on locus.
*
* e.g., ./kmc2Locus2.groovy -j 100a.txt -p kmers.txt -e bin1 -o output
*
* Input files: e.g., 100a.fasta
* Output files have an extension of 'bin1'.
* @todo improve the memory usage here
*/
process db2Locus {
//publishDir resultDir, mode: 'copy', overwrite: true
maxForks nfForks
input:
file(hits) from filterdb.flatMap()
output:
file{"*.bin1"} into bin1Fastqs
val(id) into idc
script:
// e.g., gonl-100a.fasta
// todo: document this
String dataset
String idn
id = hits.name.replaceFirst(kmcNameSuffix, "")
"""
kmc2LocusAvg2.groovy -j ${hits} -p ${probeFile} -e ${bin1Suffix} -i ${id} -o .
if ls *.bin1 1> /dev/null 2>&1; then
: # noop
else
echo "combined: uninterpretable" > "${id}_prediction.txt"
touch "${id}_uninterpretable.bin1"
fi
"""
} // db2Locus
/*
* hapInterp
*
* 1) Makes haplotype predictions from PA probes.
*
*
* @todo document
*/
process hapInterp {
publishDir resultDir, mode: 'copy', overwrite: true
input:
file(b1List) from bin1Fastqs
val(idIn) from idc
output:
file{"*_prediction.txt"} into predictionChannel
script:
"""
FILES="*.bin1"
fileList=""
id=""
ext="*bin1*"
for bFile in \$FILES; do
if [ -s \$bFile ]; then
if [[ \$bFile == \$ext ]]; then
id=\$(basename "\$bFile")
# '%' Means start to remove after the next character;
# todo: change this to kmcNameSuffix
id="\${id%%_*}"
if [ "\$id" == "" ]; then
id=\$(basename "\$bFile")
fi
#echo \$bFile
if [ "\$fileList" == "" ]; then
:
else
fileList+=","
fi
fileList+=\$bFile
fi
fi
done
outFile=${idIn}
outFile+="_prediction.txt"
pa2Haps.groovy -h ${haps} -q "\$fileList" -o "\$outFile"
"""
} // hapInterp
// get the per-sample name
def sample(Path path) {
def name = path.getFileName().toString()
int start = Math.max(0, name.lastIndexOf('/'))
int end = name.indexOf(inputSuffix)
if ( end <= 0 ) {
throw new Exception( "Expected file " + name + " to end in '" + inputSuffix + "'" );
}
end = end -1 // Remove the trailing '.'
return name.substring(start, end)
} // sample