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 0
/
Copy pathannotate.nf
executable file
·216 lines (190 loc) · 6.4 KB
/
annotate.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
#! /usr/bin/env nextflow
/*
* Annotates strings in fasta files.
*
* @author Dave Roe
* @todo handle gzipped input
* @todo standardize "KIR" in the output
* @todo remove the bar in the ft output
*/
params.home = baseDir
home = params.home + "/"
params.raw = home + '/raw/'
//params.output = "/Users/daver/git/kass/output"
params.output = home + "/output"
output = params.output + "/"
params.bowtie = "-a --end-to-end --rdg 3,3 --rfg 3,3"
params.threads = "28"
params.maxMem = "200G"
params.container = "droeatumn/kass:latest"
params.nocontainer = "null"
params.sbt = null
refAlleleDir = file("${home}/input/references/genes")
jointAlleleDir = file("${home}/input/references/joints")
featuresFile = file("${home}/input/features.txt") // markup features
markerFile = file("${home}/input/cap.fasta") // capture markers
alignProbesFile = file("${home}/src/alignment2ProbePairs.groovy")
annotateFile = file("${home}/src/annotateMarkup.groovy")
if(params.sbt == null) {
sbtFile = home + "/input/SubmissionTemplate.sbt"
} else {
sbtFile = file(params.raw + "/" + params.sbt)
}
raw = "${params.raw}/*{fasta,fa,fasta.gz,fa.gz}"
reads = Channel.fromPath(raw).ifEmpty { error "cannot find any reads matching ${raw}" }.map { path -> tuple(sample(path), path) }
annotateReads = Channel.create()
readTap = reads.tap(annotateReads).filter{ it[1] != params.sbt }
/*
* Use the capture probes to orient the input sequences.
*/
process orient {
if(params.nocontainer == "null") {
container = params.container
}
publishDir params.output, mode: 'copy', overwrite: true
errorStrategy 'ignore'
tag { s }
input:
set s, file(r) from readTap
path(markerFile)
output:
tuple s, file{"${s}*-orient.fasta"} into orientedFasta
script:
def rootName = r.name.replaceFirst(".gz", "")
def gzFlag = "" // tell bowtie it is a fasta or fastq
if(r.name.endsWith(".gz")) {
gzFlag = "1"
}
"""
if [ "$gzFlag" == "1" ]; then
gunzip -f ${r}
fi
orient.groovy -i ${rootName} -p ${markerFile} -o tmp.fasta
reformat.sh -Xmx${params.maxMem} in=tmp.fasta out=${s}-orient.fasta overwrite=true
"""
} // orient
/*
* Use the capture probes to annotate.
* Output is *_annotation.txt
*/
process structure {
if(params.nocontainer == "null") {
container = params.container
}
publishDir params.output, mode: 'copy', overwrite: true
errorStrategy 'ignore'
tag { s }
input:
set s, file(r) from orientedFasta
path(markerFile)
path(alignProbesFile)
path(annotateFile)
path(featuresFile)
output:
tuple s, file{"${s}*_markup.txt"} into markup
tuple s, path{"${s}*_annotation.txt"} into annotation
tuple s, path{r}, path{"${s}*_features.fasta"} into features mode flatten
script:
// todo: modularize this chunk
def bamName = r.name.replaceFirst(".fastq.gz", "").replaceFirst(".fasta.gz", "").replaceFirst(".fastq", "").replaceFirst(".fasta", "").replaceFirst("-orient", "")
def outName = bamName
def fastaFlag = "" // tell bowtie it is a fasta or fastq
if((r.name.endsWith("fasta") || r.name.endsWith("fa")) ||
r.name.endsWith("fasta.gz") || r.name.endsWith("fa.gz")) {
fastaFlag = "-f"
}
"""
# indexing
# todo: split this; only needs to happen once
echo "indexing ${r}..."
samtools faidx ${r}
#bwa index ${r}
mkdir -p bowtie_indexes
cd bowtie_indexes
bowtie2-build --threads ${params.threads} ../${r} ${s}_index
cd ..
# alignment
echo "aligning ${markerFile} to ${r}..."
bowtie2 ${params.bowtie} --threads ${params.threads} -x bowtie_indexes/${s}_index ${fastaFlag} ${markerFile} -S ${outName}.sam 2> ${outName}_err.txt
samtools view -b -S ${outName}.sam > ${outName}.bam
#samtools sort ${outName}.bam -o ${outName}_sorted.bam
#samtools index ${outName}_sorted.bam
samtools sort ${outName}.bam -O sam -o ${outName}_sorted.sam
rm ${outName}.[bs]am
# markup the alignment of the probe pairs
mkdir -p annotation
echo "marking ${bamName}..."
./${alignProbesFile} -d . -m 1 -o ${bamName}_markup.txt 2> ${bamName}_markup_err.txt
tail ${bamName}_markup_err.txt
echo "annotating ${s}..."
# annotate the markup with the genes
./${annotateFile} -i ${featuresFile} -f ${r} -m ${bamName}_markup.txt -o . 2> ${bamName}_annotation_err.txt
echo "done"
deep.pl replace '2DL4~3DL2' '2DL4~3DL1L2' '*features.fasta' || TRUE
deep.pl replace '2DL4~3DL2' '2DL4~3DL1S1' '*_annotation.txt' || TRUE
cut -f2 ${bamName}_annotation.txt | sort | uniq -c > ${bamName}_annotation_strings.txt
"""
} // structure
process interpret {
if(params.nocontainer == "null") {
container = params.container
}
//publishDir params.output, mode: 'copy', overwrite: true
tag { s }
input:
set s, file(inContig), file(r) from features
path(refAlleleDir)
output:
path{"${s}*.tbl.txt"} optional true into tables
script:
def nameNoExt = r.name.replaceAll("_features.fasta", "")
def intervening = nameNoExt.contains("3DP1-2DL4") // skip this one
def i = nameNoExt.lastIndexOf('_')
def locus = nameNoExt[i+1..-1]
"""
if [ "${locus}" != "3DP1-2DL4" ]; then
gfi.groovy -i ${refAlleleDir} -f ${r} -g KIR${locus} -j ${jointAlleleDir} -o . 2> gfi_${s}_${locus}_err.txt
fi
"""
} //
allFTs = tables.collectFile(name: 'tbl.txt', newLine: true)
/*
process publishGL {
if(params.nocontainer == "null") {
container = params.container
}
publishDir params.output, mode: 'copy', overwrite: true
input:
path(g) from gl
output:
path(g)
script:
"""
"""
} // publishGL
*/
/*
* Combine the per-feature feature tables into the per contig annotation.
* @todo linux64.table2asn_GFF -augustus-fix -f ${desc}.gff -i ${inContig} -outdir . -genbank -verbose -euk -V b -Z -t ${sbtF} -j "[organism=Homo sapiens]"
*/
process combine {
if(params.nocontainer == "null") {
container = params.container
}
publishDir params.output, mode: 'copy', overwrite: true
input:
path(ft) from allFTs
output:
path("*.ft.txt")
script:
"""
echo "${ft}"
combineFT.groovy -i tbl.txt 2> combineFT_err.txt
"""
} // combine
// combineGL (todo)
def sample(Path path) {
def name = path.getFileName().toString()
int start = Math.max(0, name.lastIndexOf('/'))
return name.substring(start, name.indexOf('.'))
}