This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.nf
executable file
·205 lines (191 loc) · 6 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
#!/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.
*
* @author Dave Roe
* @todo test that required inputs are submitted
* @todo combine probeDB and db2Locus so the kmc files can be deleted
* @todo coverage of 255 or higher is considered off kir
*/
params.home = baseDir
home = params.home + '/'
params.raw = home + '/raw/'
raw = params.raw + "/"
params.output = home + '/output/'
params.id = "defaultID"
markerDBPrefix = 'markers'
markerDBSuf = file("${home}/input/markers.kmc_suf")
markerDBPre = file("${home}/input/markers.kmc_pre")
// input: kmc probe txt files
kmcNameSuffix = '_hits.txt' // extension on the file name
bin1Suffix = 'bin1'
markerFile = file("${home}/input/markers.fasta")
params.haps = file("${home}/input/haps.txt")
params.map = null
params.l = null // logging level (1=most to 5=least)
params.container = "droeatumn/kpi:latest"
makeKmerDBFile = file("${home}/src/probeFastqsKMC.groovy")
queryDBFile = file("${home}/src/filterMarkersKMC2.groovy")
db2LocusFile = file("${home}/src/kmc2LocusAvg2.groovy")
pa2HapsFile = file("${home}/src/pa2Haps.groovy")
srcDir = pa2HapsFile.parent
params.nocontainer = "null"
// input file type for kmc
// -f<a/q/m/bam/kmc> - input in FASTA format (-fa), FASTQ format (-fq), multi FASTA (-fm) or BAM (-fbam) or KMC(-fkmc); default: FASTQ
params.filetype = "fq" // default to fq
inFileType = params.filetype
// things that probably won't change per run
fileSeparator = "/"
resultDir = params.output
haps = params.haps
if(!resultDir.endsWith("/")) {
resultDir += "/"
}
logIn = ""
if(params.l != null) {
logIn = "-l ${params.l}"
}
inputSuffix = "*.txt" // for --m
inOption = "" // input type for probeFastqsKMC.groovy
dOption = "" // d option probeFastqsKMC.groovy
kpiIn = null
mapDir = home
if(params.map != null) {
inOption = "-m"
mapFile = params.map
mapDir = file(params.map).parent
kpiIn = Channel.fromPath(mapFile).ifEmpty { error "cannot find file $mapFile" }
} else if(raw != null) {
inOption = "-p"
dOption = "-d " + params.id
kpiIn = Channel.fromPath(raw).ifEmpty { error "cannot find fastq/fastq/bam in $mapDir" }
// fqsIn = Channel.fromPath(mapDir).map{ file -> tuple(file.baseName, file) }.ifEmpty { error "cannot find fastq/fastq in $mapDir" }
// fqsIn = Channel.fromPath(["${mapDir}*.fq", "${mapDir}*.fastq","${mapDir}*.fq.gz", "${mapDir}*.fastq.gz", "${mapDir}*.fa", "${mapDir}*.fasta","${mapDir}*.fa.gz", "${mapDir}*.fasta.gz"] ).ifEmpty { error "cannot find fastq/fastq in $mapDir" }
}
/*
* @todo handle both input options
* @todo m option only publishes everything at the end
*/
process makeKmerDB {
if(params.nocontainer == "null") {
container = params.container
}
// publishDir resultDir, pattern: '*.kmc_*', mode: 'copy', overwrite: true
// publishDir resultDir, pattern: '*.log', mode: 'copy', overwrite: true
// errorStrategy 'ignore'
// validExitStatus 0,1
input:
path(f) from kpiIn
path(makeKmerDBFile)
path(markerDBSuf)
path(markerDBPre)
path(mapDir)
path(queryDBFile)
val(markerDBPrefix)
// env(JAVA_OPTS) from ("-Xmx200g")
output:
file{ "*_hits.txt"} into filterdb
// file('*.log') optional true into kmcdbLog
script:
"""
export JAVA_OPTS='-Xmx200g'
./${makeKmerDBFile} ${dOption} ${inOption} ${f} -t ${inFileType} ${logIn} -o . -w . 2> probeFastqsKMC.log
./${queryDBFile} -d . -p ${markerDBPrefix} -o . -w . 2> filterMarkersKMC2.log
find . -type f -size 0 | xargs rm # remove 0 length files, especially hits.txt files
"""
} // makeKmerDB
/*
* db2Locus
*
* Given a kmc output file, bin the hit reads into separate files based on locus.
*
* Input files: e.g., 100a.fasta
* Output files have an extension of 'bin1'.
* @todo improve the memory usage here
*/
process db2Locus {
if(params.nocontainer == "null") {
container = params.container
}
//publishDir resultDir, mode: 'copy', overwrite: true
input:
file(hits) from filterdb.flatMap()
file(markerFile)
file(db2LocusFile)
env(JAVA_OPTS) from ('-Xms2G -Xmx50G')
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, "")
"""
./${db2LocusFile} -j ${hits} -p ${markerFile} -e ${bin1Suffix} -i ${id} -o . 2> kmc2LocusAvg2.log
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 {
if(params.nocontainer == "null") {
container = params.container
}
publishDir resultDir, mode: 'copy', overwrite: true
input:
file(b1List) from bin1Fastqs
val(idIn) from idc
file(haps)
file(pa2HapsFile)
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"
./${pa2HapsFile} -h ${haps} -q "\$fileList" -o "\$outFile" 2> pa2Haps.log
"""
} // hapInterp