-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_reads_telomere_length_distribution.split.sh
561 lines (501 loc) · 18.6 KB
/
05_reads_telomere_length_distribution.split.sh
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/bin/bash
set -e
echo "loading and validating env"
export E2EAssembler=$(dirname "$0")
if [ -z ${1+x} ]; then
echo "Please provide a configuration file. See ${E2EAssembler}/my.example.config for an example."
exit 1
fi
# load and valdiate env
source $1
${E2EAssembler}/00_check_environment.sh
if [[ $NANOPORE_FASTQ == *.fastq ]]; then
export NANOPORE_BASE=${NANOPORE_FASTQ%.fastq}
elif [[ $NANOPORE_FASTQ == *.fastq.gz ]]; then
export NANOPORE_BASE=${NANOPORE_FASTQ%.fastq.gz}
else
export NANOPORE_BASE=${NANOPORE_FASTQ}
fi
export REF_ASSEMBLY_NAME=$(basename ${NANOPORE_BASE})
export TELOTAG_RC=$(perl -e '
my $seq = reverse("'$TELOTAG'");
$seq =~ tr/ACGTUacgtu/TGCAAtgcaa/;
print $seq;
')
export TELOMOTIF_RC=$(perl -e '
my $seq = reverse("'$TELOMOTIF'");
$seq =~ tr/ACGTUacgtu/TGCAAtgcaa/;
print $seq;
')
# echo "sqlitedb setup to import and analyse telomeric reads"
# sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
# DROP TABLE IF EXISTS genome_info;
# create table genome_info (
# ref_chr text,
# len integer
# );
# "
# sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import $GENOME.chromsizes genome_info"
echo "removing previous run results"
rm -fr $PWD/telomotif/${REF_ASSEMBLY_NAME}* 2>/dev/null
mkdir -p $PWD/telomotif/$REF_ASSEMBLY_NAME
echo "extracting reads with telotag on $REF_ASSEMBLY_NAME corrected reads"
for f in $CANU_OUTPATH/${REF_ASSEMBLY_NAME}.*
do
ASSEMBLY_NAME=$(basename $f)
FA_IN=$f/${ASSEMBLY_NAME}.correctedReads.fasta.gz
echo "changing orientation to be same as reference genome"
echo "unzipping fasta $FA_IN"
pigz -dc -p $LOCAL_THREAD $FA_IN > $f/${ASSEMBLY_NAME}.correctedReads.fasta
echo "sqlitedb setup to import selected reads"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
DROP TABLE IF EXISTS tmp_telo_reads;
create table tmp_telo_reads (
id text,
sequence text,
sequence_rc text
);
DROP TABLE IF EXISTS tmp_reads_mapping;
create table tmp_reads_mapping (
id text,
flag integer,
ref_chr text,
align_pos integer,
mapq integer
);
"
echo "fasta to tsv"
perl -ne '
chomp($_);
if($_ =~ /^\>/){
my($id) = $_ =~ /^\>(.*) id=/;
print "\n".$id."\t";
}
else{
print $_;
}
' $f/${ASSEMBLY_NAME}.correctedReads.fasta > $f/${ASSEMBLY_NAME}.correctedReads.fasta.tsv
sed -i '1d' $f/${ASSEMBLY_NAME}.correctedReads.fasta.tsv
echo "adding reverse complement to reads tsv"
perl -ne '
chomp($_);
my @t = split("\t",$_);
my $rc_seq = reverse($t[1]);
$rc_seq =~ tr/ACGTUacgtu/TGCAAtgcaa/;
print $t[0] . "\t" . $t[1] . "\t" . $rc_seq . "\n";
' $f/${ASSEMBLY_NAME}.correctedReads.fasta.tsv > $f/${ASSEMBLY_NAME}.correctedReads.fasta.rc.tsv
echo "importing reads sequence + rc"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import $f/${ASSEMBLY_NAME}.correctedReads.fasta.rc.tsv tmp_telo_reads"
### align using minimap
echo "aligning reads on reference genome to determine orientation"
$MINIMAP2 -t $LOCAL_THREAD -ax map-ont $GENOME $f/${ASSEMBLY_NAME}.correctedReads.fasta \
| $SAMTOOLS view --threads $LOCAL_THREAD -Sh -q 20 -F 2048 -F 256 \
| $SAMTOOLS sort --threads $LOCAL_THREAD -o $f/${ASSEMBLY_NAME}.correctedReads.fasta.sam
echo "import SAM alignment information to sqlitedb"
perl -ne '
chomp($_);
if($_ !~ /^\@/){
# not header line
my @f = split("\t",$_);
print
$f[0] . "\t"
. $f[1] . "\t"
. $f[2] . "\t"
. $f[3] . "\t"
. $f[4] . "\n";
}
' $f/${ASSEMBLY_NAME}.correctedReads.fasta.sam > $f/${ASSEMBLY_NAME}.correctedReads.fasta.sam.tsv
echo "importing reads mapping on ref genome with original sequence"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import $f/${ASSEMBLY_NAME}.correctedReads.fasta.sam.tsv tmp_reads_mapping"
echo "regen read fasta with correct read sequence orientation"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' "
SELECT
am.id,
CASE
WHEN am.flag = 0 THEN ac.sequence
WHEN am.flag = 16 THEN ac.sequence_rc
END
FROM
tmp_reads_mapping am
join tmp_telo_reads ac on ac.id=am.id;
" > $PWD/telomotif/$REF_ASSEMBLY_NAME/${ASSEMBLY_NAME}.correctedReads.ok.tsv
echo "fasta to tsv"
perl -ne '
chomp($_);
my @t = split("\t",$_);
print ">" . $t[0] . "\n";
print $t[1] . "\n";
' $PWD/telomotif/${REF_ASSEMBLY_NAME}/${ASSEMBLY_NAME}.correctedReads.ok.tsv > $PWD/telomotif/$REF_ASSEMBLY_NAME/${ASSEMBLY_NAME}.correctedReads.ok.fasta
# chr start
# TACTTCGCTAAGGTTAACACAAAGACACCGACAACTTTCTTCAGCACCTCAGTCTACACATATTCTCTGTTTTTTTTTTTTTTTTTTTTTTTCCACACCCACACCACACCCACACACCAC
# CAGTCTACACATATTCTCTGT
echo "select reads with 5' telotag sequences for $ASSEMBLY_NAME"
${SEQKIT} grep --threads ${LOCAL_THREAD} -s -P -m $MAX_TELOTAG_MISSMATCH -R 1:200 \
-p ${TELOTAG_RC} $PWD/telomotif/$REF_ASSEMBLY_NAME/${ASSEMBLY_NAME}.correctedReads.ok.fasta > $PWD/telomotif/$REF_ASSEMBLY_NAME/${ASSEMBLY_NAME}.${TELOTAG_RC}.fasta
# chr end
# TGGTGTGGGTGTGGTGTGTGGGTGTGGAAAAAAAAAAAAAACAGAGAATATGTGTAGACTGAGGTGCTGAAGAAAGTTGTCGGTGTCTTTGTGTTAACCTTAGCAATACGTAACTGAACG
# ACAGAGAATATGTGTAGACTG
# fetch reads with telotag motif
echo "select reads with 3' telotag sequences for $ASSEMBLY_NAME"
${SEQKIT} grep --threads ${LOCAL_THREAD} -s -P -m $MAX_TELOTAG_MISSMATCH -R -200:-1 \
-p ${TELOTAG} $PWD/telomotif/$REF_ASSEMBLY_NAME/${ASSEMBLY_NAME}.correctedReads.ok.fasta > $PWD/telomotif/$REF_ASSEMBLY_NAME/${ASSEMBLY_NAME}.${TELOTAG}.fasta
done
echo "initialising sqlite db"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
DROP TABLE IF EXISTS telo_reads;
create table telo_reads (
extr integer,
id text,
seq text,
telo_trimmed_seq text
);
CREATE INDEX extr_id_telo_reads_idx on telo_reads(extr,id);
DROP TABLE IF EXISTS telo_stretch;
create table telo_stretch (
extr integer,
telo_read_id text,
start integer,
end nteger,
len integer
);
CREATE INDEX extr_id_telo_stretch_idx on telo_stretch(extr,telo_read_id);
"
for extr in 5 3
do
if [ "$extr" = "3" ]; then
telo=${TELOTAG}
motif=${TELOMOTIF}
else
telo=${TELOTAG_RC}
motif=${TELOMOTIF_RC}
fi
###### Combine the reads that have 5' telotag ######
echo "combining all ${DATASET_SPLIT_COVERAGE}X assembly corrected reads with ${extr}' telotag motif"
cat $PWD/telomotif/${REF_ASSEMBLY_NAME}/*.${telo}.fasta > $PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.fasta
echo "convert fasta to tsv with extr info"
perl -ne '
chomp($_);
if($_ =~ /^\>/){
my($id) = $_ =~ /^\>(.*)$/;
print "\n'$extr'\t" . $id . "\t";
}
else{
print $_;
}
' $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.fasta > $PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.tsv
tail -n +2 "$PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.tsv" > "$PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.tsv.tmp"
mv "$PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.tsv.tmp" "$PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.tsv"
echo "import tsv to sqlite"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
DROP TABLE IF EXISTS tmp_raw_telo_reads;
create table tmp_raw_telo_reads (
extr integer,
id text,
seq text
);
CREATE INDEX id_tmp_raw_telo_reads_idx on tmp_raw_telo_reads(id);
"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import $PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.tsv tmp_raw_telo_reads"
echo "locate telotag in reads sequences"
${SEQKIT} locate --threads ${LOCAL_THREAD} -P -m $MAX_TELOTAG_MISSMATCH \
-p ${telo} \
$PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.fasta > $PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.locate.tsv
echo "import locate results to sqlite"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
DROP TABLE IF EXISTS tmp_telo_locate;
create table tmp_telo_locate (
id text,
pattern_name text,
pattern text,
strand text,
start integer,
end integer,
matched text
);
CREATE INDEX id_tmp_telo_locate_idx on tmp_telo_locate(id);
"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import $PWD/telomotif/$REF_ASSEMBLY_NAME.${telo}.locate.tsv tmp_telo_locate"
echo "insert ${extr}' results to telo_reads table"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
INSERT INTO telo_reads
SELECT
r.extr,
r.id,
r.seq,
CASE
WHEN r.extr='5' THEN substr(r.seq,l.start)
WHEN r.extr='3' THEN substr(r.seq,1,l.start)
ELSE ''
END
FROM tmp_raw_telo_reads r
join tmp_telo_locate l ON l.id=r.id;
"
echo "using $SEED_LEN nt as seed lenght to generate ${extr}' telomeric repeat db"
export TELOMERIC_REGEX=$(perl -e '
my $telomotif = "'$motif'";
my $telomotif_str = $telomotif x 50;
my @seeds = $telomotif_str =~ /.{'$SEED_LEN'}.?/g;
my %h = map { $_ => 1 } @seeds;
$teloregex .= join(" -p ",keys(%h));
print $teloregex . "\n";
')
echo "generate telomeric reads fasta from sqlitedb for seqkit"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' "
select
id || '_' || length(telo_trimmed_seq),
telo_trimmed_seq
from telo_reads
where
extr = "${extr}";
" > $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.tsv
perl -e '
open(my $FH,"<'$PWD'/telomotif/'$REF_ASSEMBLY_NAME'.'${telo}'.telotag_trimmed.reformat.tsv");
while( my $l = <$FH>) {
chomp($l);
my($h,$s) = split("\t",$l);
print ">$h\n$s\n";
}
' > $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.fasta
echo "running seqkit locate to identify ${extr}' telomeric seeds"
cat $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.fasta | \
${SEQKIT} locate --threads ${LOCAL_THREAD} -r --bed \
-p $TELOMERIC_REGEX -V 0 -m 0 -P > $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.bed
echo "extracting telomeric stretch from reads"
${E2EAssembler}/extract_telomeric_stretch.pl $extr $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.bed $SEED_LEN > $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.filtered.tsv
echo "importing telomeric stretch to sqlite db"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.filtered.tsv telo_stretch"
# map reads to reference to determine chr
echo "generate telomeric reads fasta from sqlitedb for minimap2"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' "
select
id,
telo_trimmed_seq
from telo_reads
where
extr = "${extr}";
" > $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.tsv
perl -e '
open(my $FH,"<'$PWD'/telomotif/'$REF_ASSEMBLY_NAME'.'${telo}'.telotag_trimmed.reformat.tsv");
while( my $l = <$FH>) {
chomp($l);
my($h,$s) = split("\t",$l);
print ">$h\n$s\n";
}
' > $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.fasta
echo "aligning reads on reference genome using minimap2"
$MINIMAP2 -t $LOCAL_THREAD -ax map-ont ${PWD}/merged_assembly/${REF_ASSEMBLY_NAME}.fasta $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.fasta \
| $SAMTOOLS sort --threads $LOCAL_THREAD -o $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.sam
echo "import SAM alignment information to sqlitedb"
perl -ne '
chomp($_);
if($_ !~ /^\@/){
# not header line
my @f = split("\t",$_);
print
"'${extr}'\t"
. $f[0] . "\t"
. $f[1] . "\t"
. $f[2] . "\t"
. $f[3] . "\t"
. $f[4] . "\n";
}
' $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.sam > $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.tsv
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
DROP TABLE IF EXISTS telo_reads_mapping_"$extr";
create table telo_reads_mapping_"$extr" (
extr integer,
telo_read_id text,
flag integer,
chr_map text,
align_pos integer,
mapq integer
);
"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import $PWD/telomotif/${REF_ASSEMBLY_NAME}.${telo}.telotag_trimmed.reformat.tsv telo_reads_mapping_"$extr""
done
${SAMTOOLS} faidx ${PWD}/merged_assembly/${REF_ASSEMBLY_NAME}.fasta
cut -f1,2 ${PWD}/merged_assembly/${REF_ASSEMBLY_NAME}.fasta.fai > ${PWD}/merged_assembly/${REF_ASSEMBLY_NAME}.fasta.chromsizes
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
DROP TABLE IF EXISTS assembly_info;
create table assembly_info (
ass_chr text,
len integer
);
"
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' ".import ${PWD}/merged_assembly/${REF_ASSEMBLY_NAME}.fasta.chromsizes assembly_info"
# sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
# DROP TABLE IF EXISTS telo_reads_mapping;
#
# CREATE TABLE telo_reads_mapping as
# WITH q5 AS (
# select
# telo_read_id,
# count(*) c
# FROM telo_reads_mapping_5
# GROUP BY 1
# HAVING c >= 2
# ),
# q3 as (
# select
# telo_read_id,
# count(*) c
# FROM telo_reads_mapping_3
# GROUP BY 1
# HAVING c >= 2
# )
# SELECT
# *
# from telo_reads_mapping_5
# where
# telo_read_id not in (select telo_read_id from q5)
# UNION
# SELECT
# *
# from telo_reads_mapping_3
# where
# telo_read_id not in (select telo_read_id from q3);
#
# CREATE INDEX extr_id_telo_reads_mapping_idx on telo_reads_mapping(extr,telo_read_id);
# "
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite "
DROP TABLE IF EXISTS telo_reads_mapping;
CREATE TABLE telo_reads_mapping as
SELECT
*
from telo_reads_mapping_5
where
flag = 0
UNION
SELECT
*
from telo_reads_mapping_3
where
flag = 0;
CREATE INDEX extr_id_telo_reads_mapping_idx on telo_reads_mapping(extr,telo_read_id);
"
# generate report graph using sqlitedb data
echo "generate telomere length report (reads filtered for minimal telomeric stretch of $TELO_MINLEN and aligning within $READ_TELOTAG_EXTR_CUTOFF nt of chromosome extremity)"
mkdir -p $PWD/report
## tsv report by chr: CHR, extr, median_all, avg_all, stdev_all, median_chr, avg_chr, stdev_chr
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' "
SELECT
case trm.extr
WHEN '5' THEN trm.chr_map || 'L'
WHEN '3' THEN trm.chr_map || 'R'
ELSE trm.chr_map || '?'
END telomere,
group_concat(ts.len)
from
telo_reads_mapping trm
join telo_stretch ts on ts.telo_read_id=trm.telo_read_id and ts.extr=trm.extr
join assembly_info g on trm.chr_map=g.ass_chr
join telo_reads tr on tr.id=trm.telo_read_id and tr.extr=trm.extr
WHERE
ts.len >= "$TELO_MINLEN"
AND (
trm.align_pos <= "$READ_TELOTAG_EXTR_CUTOFF"
or (trm.align_pos + length(tr.telo_trimmed_seq)) >= (g.len - "$READ_TELOTAG_EXTR_CUTOFF")
)
GROUP BY trm.chr_map,trm.extr
ORDER BY trm.chr_map ASC, trm.extr DESC
" > $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.data.tsv
ALL_MEDIAN=$(sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' "
SELECT
ts.len
FROM
telo_reads_mapping trm
join telo_stretch ts on ts.telo_read_id=trm.telo_read_id and ts.extr=trm.extr
join assembly_info g on trm.chr_map=g.ass_chr
join telo_reads tr on tr.id=trm.telo_read_id and tr.extr=trm.extr
where
ts.len >= "$TELO_MINLEN"
AND (
trm.align_pos <= "$READ_TELOTAG_EXTR_CUTOFF"
or (trm.align_pos + length(tr.telo_trimmed_seq)) >= (g.len - "$READ_TELOTAG_EXTR_CUTOFF")
)
ORDER BY ts.len
LIMIT 1
OFFSET (
SELECT COUNT(*)
FROM
telo_reads_mapping trm
join telo_stretch ts on ts.telo_read_id=trm.telo_read_id and ts.extr=trm.extr
join assembly_info g on trm.chr_map=g.ass_chr
join telo_reads tr on tr.id=trm.telo_read_id and tr.extr=trm.extr
where
ts.len >= "$TELO_MINLEN"
AND (
trm.align_pos <= "$READ_TELOTAG_EXTR_CUTOFF"
or (trm.align_pos + length(tr.telo_trimmed_seq)) >= (g.len - "$READ_TELOTAG_EXTR_CUTOFF")
)
) / 2;
")
echo -e "telomere\tall_length_median\tlength_median\tlength_average\tlength_stdev\tdatanbr\tlength_data" > $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.tsv
perl -ne '
use Statistics::Basic qw(:all nofill);
chomp($_);
my($chr_lbl,$data_str) = split("\t",$_);
my @lens = split(",",$data_str);
my @sorted = sort { $a <=> $b } @lens;
my $nbr = scalar(@lens);
my $s_str = join(",",@sorted);
my $v1 = vector(@lens);
my $med = median($v1);
my $avg = mean($v1);
my $stddev = stddev($v1);
print "$chr_lbl\t'$ALL_MEDIAN'\t$med\t$avg\t$stddev\t$nbr\t$s_str\n";
' $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.data.tsv >> $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.tsv
rm $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.data.tsv
sqlite3 $PWD/${REF_ASSEMBLY_NAME}.sqlite '.separator "\t"' '.header on' "
SELECT
ts.len length,
case trm.extr
WHEN '5' THEN trm.chr_map || 'L'
WHEN '3' THEN trm.chr_map || 'R'
ELSE trm.chr_map || '?'
END telomere,
'"${REF_ASSEMBLY_NAME}"' genotype
from
telo_reads_mapping trm
join telo_stretch ts on ts.telo_read_id=trm.telo_read_id and ts.extr=trm.extr
join assembly_info g on trm.chr_map=g.ass_chr
join telo_reads tr on tr.id=trm.telo_read_id and tr.extr=trm.extr
WHERE
ts.len >= "$TELO_MINLEN"
AND (
trm.align_pos <= "$READ_TELOTAG_EXTR_CUTOFF"
or (trm.align_pos + length(tr.telo_trimmed_seq)) >= (g.len - "$READ_TELOTAG_EXTR_CUTOFF")
)
ORDER BY trm.chr_map ASC, trm.extr DESC
" > $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.rdata.tsv
echo "filtering chromosome telomere length where there is only a single data point"
perl -e '
open(my $FH,"<'$PWD'/report/'${REF_ASSEMBLY_NAME}'.telomotif.rdata.tsv");
my @lines = <$FH>;
chomp(@lines);
my %struct;
foreach my $l (@lines){
if($l =~ /^length\t/){
next;
}
my($len,$chr,$lbl) = split("\t",$l);
if(!exists($struct{$chr})){
$struct{$chr} = [];
}
push(@{$struct{$chr}},$l);
}
print "length\ttelomere\tgenotype\n";
foreach my $c (keys(%struct)){
if(scalar(@{$struct{$c}}) > 1){
foreach my $i (@{$struct{$c}}){
print $i . "\n";
}
}
else{
print STDERR "#### CHR $c removed since it contains only a single length data: $i\n"
}
}
' > $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.rdata.filtered.tsv
echo "ouputting distribution plots"
Rscript ${E2EAssembler}/gen_telo_length.R $PWD/report/${REF_ASSEMBLY_NAME}.telomotif.rdata.filtered.tsv
echo "done"