-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.nf
161 lines (128 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
#!/usr/bin/env nextflow
/*
flow Consensus assembly, variant calling, and allele interpretation workflow.
Copyright (c) 2014-2015 National Marrow Donor Program (NMDP)
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> http://www.gnu.org/licenses/lgpl.html
*/
params.experiment = "${baseDir}/tutorial"
params.reference = "${baseDir}/tutorial/ref/chr6-ex.fa"
refName = file(params.reference).name
refIndices = Channel.fromPath("${params.reference}*").toList()
rawDir = file("${params.experiment}/raw")
finalDir = file("${params.experiment}/final")
raw1 = "${rawDir}/**_R1*{fastq,fq,fastq.gz,fq.gz}"
raw2 = "${rawDir}/**_R2*{fastq,fq,fastq.gz,fq.gz}"
reads1 = Channel.fromPath(raw1).ifEmpty { error "cannot find any reads matching ${raw1}" }.map { path -> tuple(sample(path), path) }
reads2 = Channel.fromPath(raw2).ifEmpty { error "cannot find any reads matching ${raw2}" }.map { path -> tuple(sample(path), path) }
alignmentReadPairs = Channel.create()
readPairs = reads1.phase(reads2).map{ read1, read2 -> [ read1[0], read1[1], read2[1] ] }.tap(alignmentReadPairs)
process fastqToSsake {
tag { s }
input:
set s, file(r1), file(r2) from readPairs
output:
set s, file {"${s}.ssake.fa.gz"} into ssakeFasta
"""
ngs-fastq-to-ssake -1 ${r1} -2 ${r2} -o ${s}.ssake.fa.gz --insert-size 500
"""
}
process reformat {
tag { s }
input:
set s, file(f) from ssakeFasta
output:
set s, file {"${s}.ssake.reformatted.fa.gz"} into reformatted
"""
zcat $f | sed -e 's/a/A/g' -e 's/c/C/g' -e 's/g/G/g' -e 's/t/T/g' -e 's/n/N/g' -e 's/^>.*/>reformatted:500/' | gzip -c > ${s}.ssake.reformatted.fa.gz
"""
}
process ssake {
tag { s }
input:
set s, file(f) from reformatted
output:
set s, file {"${s}.ssake.d/${s}.contigs"} into contigs
"""
gunzip -c ${f} > ${f}.tmp
mkdir ${s}.ssake.d
cat >.timeout.sh <<'EOF'
timeout \$*
status=\$?
if [ \$status -eq 124 ] ; then
echo process timed out
exit 0
elif [ \$status -ne 0 ] ; then
echo process returned error: \$status
fi
exit \$status
EOF
chmod ugo+x .timeout.sh
./.timeout.sh 4h SSAKE -f ${f}.tmp -b ${s}.ssake.d/${s} -w 1 -h 1 -p 1 -m 50 -o 30 -c 1 -e 0.90 -k 4 -a 0.1 -x 20 > /dev/null
rm ${f}.tmp
"""
}
process alignContigs {
tag { s }
input:
file '*' from refIndices
set s, file(f) from contigs
output:
set s, file {"${f}"} into contigsFasta
set s, file {"${s}.contigs.bwa.sorted.bam"} into contigsBam
set s, file {"${s}.contigs.bwa.sorted.vcf.gz"} into contigsVcf
"""
bwa bwasw -b 1 ${refName} ${f} | samtools view -hub - | samtools sort -l 0 -T ${s}.contigs.bwa.tmp -o ${s}.contigs.bwa.sorted.bam
samtools index ${s}.contigs.bwa.sorted.bam
samtools mpileup -RB -C 0 -Q 0 -f ${refName} ${s}.contigs.bwa.sorted.bam -v -u | gzip -c > ${s}.contigs.bwa.sorted.vcf.gz
"""
}
process interleave {
tag { s }
input:
set s, file(r1), file(r2) from alignmentReadPairs
output:
set s, file {"${s}.paired.fq.gz"} into interleavedReads
"""
ngs-interleave-fastq -1 ${r1} -2 ${r2} -p ${s}.paired.fq.gz -u ${s}.unpaired.fq.gz
"""
}
process alignReads {
tag { s }
input:
file '*' from refIndices
set s, file(r) from interleavedReads
output:
set s, file {"${s}.reads.bwa.sorted.bam"} into readsBam
set s, file {"${s}.reads.bwa.sorted.vcf.gz"} into readsVcf
"""
bwa mem ${refName} -a -p ${r} | samtools view -hub - | samtools sort -l 0 -T ${s}.reads.bwa.tmp -o ${s}.reads.bwa.sorted.bam
samtools index ${s}.reads.bwa.sorted.bam
samtools mpileup -f ${refName} ${s}.reads.bwa.sorted.bam -v -u | gzip -c > ${s}.reads.bwa.sorted.vcf.gz
"""
}
finalDir.mkdirs()
readsBam.subscribe { s, file -> copy('readsBam', s, file) }
readsVcf.subscribe { s, file -> copy('readsVcf', s, file) }
contigsVcf.subscribe { s, file -> copy('configVcf', s, file) }
contigsBam.subscribe { s, file -> copy('contigsBam', s, file) }
contigsFasta.subscribe { s, file -> copy('contigsFasta', s, file) }
def copy (type, s, file) {
log.info "Copying ${file.name} ($type) into: $finalDir"
file.copyTo(finalDir)
}
def sample(Path path) {
def name = path.getFileName().toString()
int start = Math.max(0, name.lastIndexOf('/'))
return name.substring(start, name.indexOf("_R"))
}