-
Notifications
You must be signed in to change notification settings - Fork 1
/
telomeric_ends.nf
412 lines (329 loc) · 11.7 KB
/
telomeric_ends.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
nextflow.enable.dsl=2
date = new Date().format( 'yyyyMMdd' )
params.outdir = "teloAssem-${date}"
params.telomere = 'TTAGGC'
params.min_occurr = 15
params.reads = "mini_DF5120.ccs.fasta.gz"
params.memeMotif = "DF5120_diminution_400.meme.txt"
params.teloRepeatWindowSize = 1000
params.dimiWidth = 200
reads = Channel.fromPath(params.reads, checkIfExists: true)
.map { file -> tuple(file.Name - ~/(\.ccs)?(\.fa)?(\.fasta)?(\.gz)?$/, file) }
memeMotif = Channel.fromPath(params.memeMotif, checkIfExists: true).collect()
process get_telomeric_reads {
tag "${strain}"
label 'btk'
publishDir "$params.outdir/teloReads", mode: 'copy'
input:
tuple val(strain), path(reads)
output:
tuple val(strain), path("${strain}.telo.fasta.gz")
script:
"""
zcat $reads | filter_telomeric_reads.py --motif ${params.telomere} \
--times ${params.min_occurr} --out ${strain}.telo.fasta.gz
"""
}
process hifiasm {
tag "${strain}"
publishDir "$params.outdir/dimiAssem", mode: 'copy'
label 'btk'
input:
tuple val(strain), path(reads)
output:
tuple val(strain), path("${strain}.hifiasm.fasta.gz")
script:
"""
/software/team301/hifiasm/hifiasm $reads -o $strain -t ${task.cpus}
awk '/^S/{print ">"\$2"\\n"\$3}' ${strain}.p_ctg.gfa | fold | bgzip -c > ${strain}.hifiasm.fasta.gz
"""
}
process flye {
tag "${strain}"
publishDir "$params.outdir/dimiAssem", mode: 'copy'
label 'btk'
input:
tuple val(strain), path(reads)
output:
tuple val(strain), path("${strain}.flye.fasta.gz")
script:
"""
/software/team301/Flye-2.8.2/Flye/bin/flye --threads ${task.cpus} \
--pacbio-hifi $reads --meta -o flyemeta
cat flyemeta/assembly.fasta | bgzip -c > ${strain}.flye.fasta.gz
"""
}
process direct_by_telomere {
tag "${strain}"
publishDir "$params.outdir/assemblies", mode: 'copy'
input:
tuple val(strain), path(assembly)
output:
tuple val(strain), path("${strain}.hifiasm.telopointed.fasta.gz")
script:
"""
seqkit locate --bed -M -G -p ${params.telomere} $assembly > tmp
bioawk -t '{tlen[\$1]+=\$2; nr[\$1]+=1}END { for (i in tlen) print i, tlen[i]/nr[i]}' tmp | sort -k1,1 > avgTeloOccur.tsv
zcat $assembly | awk '/^>/{if (l!="") print l; printf "%s\\t", \$1; l=0; next}{l+=length(\$0)}END{print l}' | sed "s/>//" | sort -k1,1 > seqlen.tsv
join seqlen.tsv avgTeloOccur.tsv > both.tsv
awk '\$3>\$2/2{print \$1}' both.tsv > idsToRev.txt
awk '\$3<=\$2/2{print \$1}' both.tsv > straightIds.txt
seqtk subseq $assembly idsToRev.txt | seqkit seq -r | gzip -c > reversed.fasta.gz
seqtk subseq -l 60 $assembly straightIds.txt | gzip -c > straight.fasta.gz
cat reversed.fasta.gz straight.fasta.gz > ${strain}.hifiasm.telopointed.fasta.gz
"""
}
process map_reads {
tag "${strain}"
label 'btk'
input:
tuple val(strain), path(reads), path(assembly)
output:
tuple val(strain), path("${strain}.bam")
script:
"""
minimap2 -a -k 19 -w 10 -I 10G -g 5000 -r 2000 -N 100 \
--lj-min-ratio 0.5 -A 2 -B 5 -O 5,56 -E 4,1 -z 400,50 \
--sam-hit-only -t ${task.cpus} ${assembly} \
$reads | \
samtools sort -@ ${task.cpus} -o ${strain}.bam
"""
}
process get_read_coordinates {
tag "${strain}"
label 'btk'
input:
tuple val(strain), path(bam)
output:
tuple val(strain), path("${strain}.teloMapped.coords.tsv")
script:
"""
samtools view ${bam} | teloCoord > ${strain}.teloMapped.coords.tsv
"""
}
process group_read_coordinates {
tag "${strain}"
label 'r'
publishDir "$params.outdir/telomere_read_coords", mode: 'copy'
input:
tuple val(strain), path(coords)
output:
tuple val(strain), path("${strain}.teloPositions.tsv")
script:
"""
grCoords.R ${coords} ${strain}.teloPositions.tsv
"""
}
process get_diminuted_regions {
tag "${strain}"
label 'btk'
input:
tuple val(strain), path(teloPos), path(fasta)
output:
tuple val(strain), path("${strain}.diminuted.fasta")
script:
"""
awk 'BEGIN{FS="\\t"}(NR>1){startCoord=\$2-${params.dimiWidth}; if(0 > startCoord){startCoord=0}; print \$1":"startCoord"-"\$2+${params.dimiWidth}}' $teloPos | \
xargs samtools faidx $fasta > ${strain}.diminuted.fasta
"""
}
process map_telomeric_reads {
tag "$strain"
publishDir "$params.outdir/teloMaps", mode: 'copy'
label 'btk'
input:
tuple val(strain), path(reads), path(assembly)
output:
tuple val("$strain"), path("${strain}.teloMapped.paf.gz")
script:
"""
minimap2 $assembly $reads | \
gzip -c > ${strain}.teloMapped.paf.gz
"""
}
process bam2fasta {
tag "$strain"
label 'btk'
input:
tuple val(strain), path(bam)
output:
tuple val("$strain"), path("${strain}.mappedReads.fasta.gz")
script:
"""
samtools fasta $bam | bgzip -c > ${strain}.mappedReads.fasta.gz
"""
}
process bam2coords {
tag "$strain"
input:
tuple val(strain), path(bam)
output:
tuple val("$strain"), path("${strain}.mappedCoords.tsv.gz")
script:
"""
samtools view $bam | teloCoord | bgzip -c > ${strain}.mappedCoords.tsv.gz
"""
}
process remove_end_reads {
tag "$strain"
label 'btk'
input:
tuple val(strain), path(reads), path(end_reads)
output:
tuple val("$strain"), path("${strain}.dimiReads.fasta.gz")
script:
"""
seqkit seq $end_reads -n > end_reads.ids.txt
seqkit seq $reads -n | sort > some_reads.ids.txt
cat end_reads.ids.txt some_reads.ids.txt | sort | \
uniq --unique > unique_reads.txt
# We need to join becuase there can be end_reads that did not map
# to the assembly and hence will appear only once in the above list
join some_reads.ids.txt unique_reads.txt > non_end_reads.txt
cat non_end_reads.txt | xargs samtools faidx $reads | \
bgzip -c > ${strain}.dimiReads.fasta.gz
"""
}
process fimo {
tag "$strain"
publishDir "$params.outdir/fimo", mode: 'copy'
input:
tuple val(strain), path(assembly)
path(motif)
output:
tuple val("$strain"), path("${strain}.fimo.tsv")
script:
"""
if [ -f *.gz ]; then
gunzip -c $assembly > assembly.fasta
else
ln -s $assembly assembly.fasta
fi
fimo --max-strand $motif assembly.fasta
mv fimo_out/fimo.tsv ${strain}.fimo.tsv
"""
}
process meme {
tag "$strain"
publishDir "$params.outdir/meme", mode: 'copy'
input:
tuple val(strain), path(diminuted_regions)
output:
path "${strain}.meme.{txt,html}"
script:
"""
meme -dna $diminuted_regions
mv meme_out/meme.txt ${strain}.meme.txt
mv meme_out/meme.html ${strain}.meme.html
"""
}
process count_telomeric_repeat {
tag "${strain}"
publishDir "$params.outdir/teloRepeatCounts", mode: 'copy'
label 'btk'
input:
tuple val(strain), path(assembly)
output:
tuple val(strain), path( "${strain}_teloRepeatCounts.tsv.gz")
script:
"""
zcat $assembly | \
awk '/^>/{if (l!="") print l; printf "%s\\t", \$1; l=0; next}{l+=length(\$0)}END{print l}' | \
sed "s/>//" > ${strain}.seqlen.tsv
seqkit locate --bed -M -G -p ${params.telomere} $assembly | \
cut -f 1,2,3 | \
sort -k1,1 -k2,2n > ${strain}.teloRepeats.tsv
bedtools makewindows -g ${strain}.seqlen.tsv -w ${params.teloRepeatWindowSize} | \
bedtools intersect -a stdin -b ${strain}.teloRepeats.tsv -wa -wb | \
bedtools groupby -i stdin -g 1,2,3 -c 1 -o count | \
awk -F '\\t' 'BEGIN{OFS=FS}{print \$0, "telomeres"}' | \
gzip -c > ${strain}_teloRepeatCounts.tsv.gz
rm ${strain}.seqlen.tsv ${strain}.teloRepeats.tsv
"""
}
process crossCheckMotif {
tag "${strain}"
publishDir "$params.outdir/crossCheckMotif", mode: 'copy'
label 'btk'
input:
tuple val(strain), path(mappedReads), path(motifCoords)
output:
path "${assembler}.{pdf,buscoString.txt,teloMappedBlocks.tsv}"
path "${assembler}.chromQC.tsv" , emit: busco_full
script:
"""
nemaChromQC.R --assemblyName $assembler \
--nigon $busco2nigons --busco $buscoTable \
--teloMappedPaf $teloMappedReads \
--teloRepeats $teloRepeats \
--minimumGenesPerSequence $params.minimumGenesPerSequence \
--minimumNigonFrac $params.minimumNigonFrac \
--minimumFracAlignedTeloReads $params.minimumFracAlignedTeloReads \
--windowSize $params.windowSizeQC
"""
}
workflow get_diminuted_reads {
take:
reads
main:
get_telomeric_reads(reads) | hifiasm //| direct_by_telomere
map_reads(reads.join(hifiasm.out)) | bam2fasta
remove_end_reads(bam2fasta.out.join(get_telomeric_reads.out))
emit:
diminuted_reads = remove_end_reads.out
end_reads = get_telomeric_reads.out
end_related_reads = bam2fasta.out
}
workflow locate_diminution {
take:
diminuted_reads
end_reads
memeMotif
end_related_reads
main:
flye(end_related_reads) | count_telomeric_repeat
fimo(flye.out, memeMotif)
map_reads(end_reads.join(flye.out)) | get_read_coordinates | group_read_coordinates
get_diminuted_regions(group_read_coordinates.out.join(flye.out)) | meme
emit:
meme.out
}
workflow {
get_diminuted_reads(reads)
locate_diminution(get_diminuted_reads.out.diminuted_reads, \
get_diminuted_reads.out.end_reads, \
memeMotif, get_diminuted_reads.out.end_related_reads)
}
//
/*
cut -f 1 telomeres_flye_CEW1.fasta.fai > ids
printf "" > allout
while read p; do
samtools faidx telomeres_flye_CEW1.fasta $p > query
grep -v $p ids | xargs samtools faidx telomeres_flye_CEW1.fasta > target
minimap2 target query >> allout
echo $p
done < ids
bioawk -t '{print $1, $3, $4, $6, $8, $9}' allout > data/selfmap.txt
bioawk -t '{print "chr", "-", $1, $1, 0, $2, "lblue"}' telomeres_flye_CEW1.fasta.fai > data/otitelo_karyotype.txt
## gscope parsing #
printf "%s\tkcov\t" sample > miniBtk-20210123/gscope.tsv
head -n 1 miniBtk-20210123/genomescope/PS2068_k31_gscope/summary.tsv | tr '\n' '\t' >> miniBtk-20210123/gscope.tsv # tr is to remove end of line from and keep writing to the header on the next line
printf "plot\tlog plot\n" ${sampName} ${sampName} >> miniBtk-20210123/gscope.tsv
for fdir in miniBtk-20210123/genomescope/*; do
sampName=$(basename $fdir)
printf "%s\t" ${sampName%_k31_gscope} >> miniBtk-20210123/gscope.tsv
grep "^kmercov" ${fdir}/model.txt | tr -s " " | cut -f 2 -d ' ' | xargs printf "%.1f\t" >> miniBtk-20210123/gscope.tsv
tail -n 1 ${fdir}/summary.tsv | tr '\n' '\t' >> miniBtk-20210123/gscope.tsv
printf "assets/images/kmers/%s/plot.png\tassets/images/kmers/%s/plot.log.png\n" ${sampName} ${sampName} >> miniBtk-20210123/gscope.tsv
done
## Btk weighted average coverage #
printf "sample\tweighted_average_coverage\n" > bothBtks_cov.tsv
for dataset in ~/testNext/bothBtks/*filtered; do
sampName=$(basename $dataset)
~/sw/blobtoolkit/blobtools2/blobtools filter --table exe.tsv $dataset
printf "%s\t" $sampName >> bothBtks_cov.tsv
bioawk -t 'BEGIN{tsum=0; tlen=0}NR>1{tsum+=$5*$4;tlen+=$4}END{printf "%.1f\n", tsum/tlen}' exe.tsv >> bothBtks_cov.tsv
done
nextflow -C /lustre/scratch123/tol/teams/blaxter/projects/tol-nemotodes/sw/nxf_pipelines/telomeric_ends.conf run /lustre/scratch123/tol/teams/blaxter/projects/tol-nemotodes/sw/nxf_pipelines/telomeric_ends.nf --reads /lustre/scratch123/tol/teams/blaxter/projects/tol-nemotodes/bothBatch/analyses/qualitymetrics/miniBtk-20210123/filteredData/DF5120_filtered.ccs.fasta.gz --outdir testDimiId -profile farm -resume
*/