-
Notifications
You must be signed in to change notification settings - Fork 1
/
prophage_tracer.sh
823 lines (688 loc) · 22.2 KB
/
prophage_tracer.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
#!/bin/bash -e
set -eo pipefail
##########################################################
# Prophage Tracer V1.0.3
#########################################################
## usage
usage() {
echo "
Prophage Tracer V1.0.3 Oct 16, 2024
usage: bash prophage_tracer.sh [options] -m <in.sam> -r <in.fasta> -p <prefix>
requirement: locally installed BLAST+ software
options:
-m FILE a full SAM file (required)
-r FILE a reference genome sequence (required)
-p STRING prefix of output files (required; usually a strain name or a sample name)
-x INT maximal size of prophage (default: 150000)
-n INT minimal size of prophage (default: 5000)
-a INT minimal length of attchment site (default: 10)
-t INT number of threads used for BlastN (default: 1)
-s INT minimal event of split reads required for supporting a prophage candidate (default: 1)
-d INT minimal event of discordant read pairs required for supporting a prophage candidate (default: 1)
"
}
while getopts ":hm:r:p:x:n:a:t:s:d:" opt
do
case $opt in
h) usage
exit 0
;;
m) sam_file=$OPTARG ;;
r) fasta=$OPTARG ;;
p) prefix=$OPTARG ;;
x) prophage_size_max=$OPTARG ;;
n) prophage_size_min=$OPTARG ;;
a) att_length_min=$OPTARG ;;
t) threads=$OPTARG ;;
s) SR_COUNT=$OPTARG ;;
d) DRP_COUNT=$OPTARG ;;
:)
echo "Option -$OPTARG requires an argument."
usage
exit 1
;;
?)
echo "Invalid option: -$OPTARG"
usage
exit 1
;;
esac
done
#If options of 'x', 'n', 'a' and 't' are not defined, setting to default values
prophage_size_max=${prophage_size_max:-150000}
prophage_size_min=${prophage_size_min:-5000}
att_length_min=${att_length_min:-3}
threads=${threads:-1}
SR_COUNT=${SR_COUNT:-1}
DRP_COUNT=${DRP_COUNT:-1}
#Check locally installed BLAST+ software
BLASTN=`which blastn || true`
if [[ -z "$BLASTN" ]]
then
usage
echo "blastn not found"
exit 1
fi
###########contig length
cat $fasta | awk '$0 ~ ">" {print c; c=0;split($0,a,"[> ]"); printf a[2] "\t"; } $0 !~ ">" {c+=length($0);} END { print c; }' > contiglength.file
#Get read length and insert size
#read_length=`head -n 10000 $sam_file | gawk 'BEGIN { max=0 } { if (length($10)>max) max=length($10) } END { print max }'`
read_length=150
#insert_size=`head -n 10000 $sam_file | awk '($2 ~ /163|83|99|147/ )' | cut -f9 | awk '{print sqrt($0^2)}' | awk '$0<10000'| awk '{ sum += $0;i++ } END { print int(sum/i) }'`
insert_size=500
#Step 1: Extracting split reads
echo -e "Step 1: Extracting split reads"
awk '($1 ~ /^@/)||($2 ~ /145|81/ && $6 ~ /^...?S...?M$/) || ($2 ~ /97|161/ && $6 ~ /^...?M...?S$/ )||($2 ~ /99|163/ && $6 ~ /^...?S...?M$/ ) || ($2 ~ /147|83/ && $6 ~ /^...?M...?S$/)' $sam_file > $prefix.sr.temp.1
#Extracting split reads.If paried reads are overlapping, only one kept.
awk '!a[$2"_"$3"_"$4"_"$5"_"$6"_"$7"_"$8]++' $prefix.sr.temp.1 | awk '($1 !~ /^@/)'| awk '!a[$1]++'| awk '{printf ">"$1"_"$2"\n"$10"\n"}' > $prefix.reads.fasta
#Blastn searching reads against the reference genome.
makeblastdb -in $fasta -dbtype nucl -out $prefix.nuclDB > makeblastdb.log 2>&1 ||true
blastn -query $prefix.reads.fasta -db $prefix.nuclDB -out $prefix.blastn.result -evalue 1e-3 -outfmt 6 -word_size 11 -num_threads $threads > blastn.log 2>&1
#Manipulate blastn result ######################################################################################
awk 'BEGIN{FS="\t";OFS="\t"} {if ($3 > 90) a[$1,++b[$1]]=$0}END{for(i in b) if (b[i] == 2) print a[i,1]"\n"a[i,2]}' $prefix.blastn.result| awk '{if($0!="") print}' |sort -k1,1 -k7n,7 > $prefix.sr.temp.2
#Classify reads containing attB or attP
awk '
BEGIN {FS="\t";OFS="\t"}
function calculation1() {
attL_start=send1-qend1+qstart2
attL_end=send1
attR_start=sstart2
attR_end=sstart2+qend1-qstart2
prophage_size=attR_end-attL_end
print SR_type,SR_DRP_type,evidence,attL_start,attL_end,attR_start,attR_end,prophage_size,$0
}
function calculation2() {
attL_start=send2-qend2+qstart1
attL_end=send2
attR_start=sstart1
attR_end=sstart1+qend2-qstart1
prophage_size=attR_end-attL_end
print SR_type,SR_DRP_type,evidence,attL_start,attL_end,attR_start,attR_end,prophage_size,$0
}
function calculation3() {
attL_start=sstart2
attL_end=sstart2+qend1-qstart2
attR_start=send1-qend1+qstart2
attR_end=send1
prophage_size=attR_end-attL_end
print SR_type,SR_DRP_type,evidence,attL_start,attL_end,attR_start,attR_end,prophage_size,$0
}
function calculation4() {
attL_start=sstart1
attL_end=sstart1+qend2-qstart1
attR_start=send2-qend2+qstart1
attR_end=send2
prophage_size=attR_end-attL_end
print SR_type,SR_DRP_type,evidence,attL_start,attL_end,attR_start,attR_end,prophage_size,$0
}
{
if (NR==1)
{
query = $1; contig=$2; qstart1 = $7; qend1 = $8; sstart1 = $9; send1 = $10; next
}
##make sure to extract correct flag value if "_" was used in read names
Flag_pos=split ($1,flag,"_")
if ($1 == query && $2 == contig)
{ qstart2 = $7; qend2 = $8; sstart2 = $9; send2 = $10
if (flag[Flag_pos] == 83)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_1"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_1"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_5"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_5"
evidence="attP"
calculation4()
}
}
if (flag[Flag_pos] == 147)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_2"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_2"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_6"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_6"
evidence="attP"
calculation4()
}
}
if (flag[Flag_pos] == 97)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_1"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2< 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_1"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_5"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_5"
evidence="attP"
calculation4()
}
}
if (flag[Flag_pos] == 161)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_2"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_2"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_6"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_6"
evidence="attP"
calculation4()
}
}
####Second
if (flag[Flag_pos] == 99)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_7"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_7"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_3"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_3"
evidence="attP"
calculation4()
}
}
if (flag[Flag_pos] == 163)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_8"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_8"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_PR_4"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_PR_4"
evidence="attP"
calculation4()
}
}
if (flag[Flag_pos] == 81)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_7"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_7"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_3"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_3"
evidence="attP"
calculation4()
}
}
if (flag[Flag_pos] == 145)
{
#attB
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_8"
evidence="attB"
calculation1()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_8"
evidence="attB"
calculation2()
}
#attP
if ( (qend2 > qend1 && qend1 > qstart2 && qstart2 > qstart1 && qstart1 < 10) && (send1 > sstart1 && sstart1 > send2 && send2 > sstart2) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_4"
evidence="attP"
calculation3()
}
else if ( (qend1 > qend2 && qend2 > qstart1 && qstart1 > qstart2 && qstart2 < 10) && (send2 > sstart2 && sstart2 > send1 && send1 > sstart1) )
{
SR_type="SR"
SR_DRP_type="SR_DRP_4"
evidence="attP"
calculation4()
}
}
}
else if ($1 != query)
{
query = $1; contig = $2; qstart1 = $7; qend1 = $8; sstart1 = $9; send1 = $10
}
}' $prefix.sr.temp.2 | awk '
#Cluster split reads
BEGIN{FS="\t";OFS="\t"}
{
if( $8 > '"$prophage_size_min"' && $8 < '"$prophage_size_max"' ) a[$9]=$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8"\t"$10
}
END{
count=1
for (i in a)
{
split(a[i],b,"\t")
print i,b[1],b[2],b[3],b[4],b[5],b[6],b[7],"candidate_"count
for (j in a)
{
split(a[j],c,"\t")
if ( i != j && c[7] == b[7])
{
if (b[7]==c[7] && b[2]-c[2] > -5 && b[2]-c[2] < 5 && b[4]-c[4] > -5 && b[4]-c[4] < 5 && b[6]-c[6] > -10 && b[6]-c[6] < 10)
{
print j,c[1],c[2],c[3],c[4],c[5],c[6],c[7],"candidate_"count
delete a[j]
}
}
}
count++
delete a[i]
}
}' |awk '{if (!($3 == "")) print $0}' >$prefix.sr.temp.3
awk '
BEGIN{FS="\t";OFS="\t"}
{
if ( !(a[$NF,1] >0) )
{
a[$NF,1]=0
}
if ( !(a[$NF,2] >0) )
{
a[$NF,2]=0
}
if ($2 == "attB")
{
a[$NF,1]+=1
sum[$NF,3]+=$3
sum[$NF,4]+=$4
sum[$NF,5]+=$5
sum[$NF,6]+=$6
sum[$NF,7]+=$7
a[$NF,8]=$8
}
else if ($2 == "attP")
{
a[$NF,2]+=1
sum[$NF,3]+=$3
sum[$NF,4]+=$4
sum[$NF,5]+=$5
sum[$NF,6]+=$6
sum[$NF,7]+=$7
a[$NF,8]=$8
}
}
END{
for(i in a)
{
split(i,idx,SUBSEP)
a[idx[1],3]=int(sum[idx[1],3]/(a[idx[1],1]+a[idx[1],2]))
a[idx[1],4]=int(sum[idx[1],4]/(a[idx[1],1]+a[idx[1],2]))
a[idx[1],5]=int(sum[idx[1],5]/(a[idx[1],1]+a[idx[1],2]))
a[idx[1],6]=int(sum[idx[1],6]/(a[idx[1],1]+a[idx[1],2]))
a[idx[1],7]=int(sum[idx[1],7]/(a[idx[1],1]+a[idx[1],2]))
print idx[1],a[idx[1],1],a[idx[1],2],a[idx[1],1]+a[idx[1],2],a[idx[1],3],a[idx[1],4],a[idx[1],5],a[idx[1],6],a[idx[1],7],a[idx[1],8]
}
}
' $prefix.sr.temp.3 | awk '!a[$0]++' > $prefix.sr.temp.out
if [ $? -eq 0 ]
then
echo -e "Step 2: Extracting discordant read pairs "
else
echo "!!!Unsuccessfully summarizing informations of split reads!!!"
exit
fi
#Step 2: Extracting discordant read pairs
awk --re-interval '$1 ~ /^@/ || ($2 ~ /97|145|81|161/ && ($6 ~ /^[1][0-5][0-9]M/ || $6 ~ /^[6-9][0-9]M/) && $7 == "=" )' $sam_file | awk '!a[$2"_"$3"_"$4"_"$5"_"$6"_"$7"_"$8]++' | sort -k1,1 -k2n,2 | awk '
BEGIN {FS="\t";OFS="\t"}
{
if (NR==1)
{
read = $1; read_flag = $2; len=$9; a[$1] = $0; next
}
if ($1 == read)
{
if ((read_flag == 97 && len < 0 && $2 ==145 && $9 >0) || (read_flag == 145 && len > 0 && $2 == 97 && $9 <0) || (read_flag == 161 && len < 0 && $2 == 81 && $9 >0) || (read_flag == 81 && len > 0 && $2 == 161 && $9 <0))
{
print "DRP","attP",a[$1]"\n""DRP","attP",$0
}
else if ((read_flag == 145 && len < 0 && $2 ==97 && $9 >0) || (read_flag == 97 && len > 0 && $2 == 145 && $9 <0) || (read_flag == 81 && len < 0 && $2 == 161 && $9 >0) || (read_flag == 161 && len > 0 && $2 == 81 && $9 <0))
{
print "DRP","attB",a[$1]"\n""DRP","attB",$0
}
}
else if ($1 != read)
{
read = $1; read_flag = $2; len=$9; a[$1] = $0; next
}
}' | awk '($4 ~ /97|161|81|145/ && $11 > 0)' | awk 'BEGIN {FS="\t";OFS="\t"} NR==FNR {split($1,readname,"_"); a[readname[1]]=readname[1]} NR>FNR { if(!($3 in a)) print $0}' $prefix.sr.temp.2 - >$prefix.drp.temp.1
##First round merging DRP reads accroding to the SR position. Not merged DRP reads collected in the '$prefix'.drp.temp.left file
awk ' BEGIN {FS="\t";OFS="\t"}
FILENAME==ARGV[1] { a[$3]=$0; next}
FILENAME==ARGV[2] {
T_L=int('"$insert_size"'*1.6) #threshold low for clsutering
T_H=int('"$insert_size"'*3.5) #threshold low for clsutering
attP_count = 0
attB_count = 0
for (i in a)
{
split(a[i],b,"\t")
if ($6-b[6] > -T_L && $6-b[6] <T_L && $7-b[10] >-T_L && $7-b[10] <T_L && $9-b[11] > -T_H && $9-b[11] < T_H && b[5] == $10)
{
if (b[2] == "attP" && b[6] >= $5 && $8-b[10]+10 > '"$read_length"' )
{
attP_count++
delete a[i]
}
else if (b[2] == "attB" && $6-b[6]+10 > '"$read_length"' && b[10] >= $7)
{
attB_count++
delete a[i]
}
}
}
c[$1]=$0"\t"attB_count"\t"attP_count"\t"attB_count+attP_count
}
END {
for (i in c)
{
print c[i]
}
for (i in a)
{
print a[i] >> "'$prefix'.drp.temp.left"
}
}
' $prefix.drp.temp.1 $prefix.sr.temp.out > $prefix.sr-drp.temp.out
##Cluster the left DRP reads independent of SR reads: first candidate assign
if test -s $prefix.drp.temp.left; then
awk '
BEGIN{FS="\t";OFS="\t"}
{
if($11 > '"$prophage_size_min"' && $11 < '"$prophage_size_max"') a[$3]=$2"\t"$4"\t"$6"\t"$10"\t"$11"\t"$5
}
END{
count=1
T_L=int('"$insert_size"'*2.2) #threshold low for clsutering
T_H=int('"$insert_size"'*3.5) #threshold low for clsutering
for (i in a)
{
split(a[i],b,"\t")
print i,b[1],b[2],b[3],b[4],b[5],b[6],"DRP_candidate_"count
for (j in a)
{
split(a[j],c,"\t")
if ( i != j )
{
if (b[6]==c[6] && b[3]-c[3] > -T_L && b[3]-c[3] < T_L && b[4]-c[4] > -T_L && b[4]-c[4] < T_L && b[5]-c[5] > -T_H && b[5]-c[5] < T_H)
{
if ((b[1] == "attB" && c[1] == "attB")||(b[1] == "attP" && c[1] == "attP")||(b[1] == "attB" && c[1] == "attP" && b[3] < c[3] && b[4] > c[4])||(b[1] == "attP" && c[1] == "attB" && b[3] > c[3] && b[4] < c[4]))
{
print j,c[1],c[2],c[3],c[4],c[5],c[6],"DRP_candidate_"count
delete a[j]
}
}
}
}
count++
delete a[i]
}
}' $prefix.drp.temp.left >$prefix.drp.temp.2
else
touch $prefix.drp.temp.2
fi
##Cluster the left DRP reads independent of SR reads: merging assigned candidate
if test -s $prefix.drp.temp.2; then
awk '{if (!($3 == "")) print $0}' $prefix.drp.temp.2| awk '
BEGIN{FS="\t";OFS="\t"}
{
#if (NR==1)
#{
# a[$NF,3] = $4+0; a[$NF,4] = $5+0
#}
a[$NF,4]=10000000000
if ( !(a[$NF,1] >0) )
{
a[$NF,1]=0
}
if ( !(a[$NF,2] >0) )
{
a[$NF,2]=0
}
if ($2 == "attB")
{
a[$NF,1]+=1
a[$NF,3]=a[$NF,3]>=$4?a[$NF,3]:$4
a[$NF,4]=a[$NF,4]<=$5?a[$NF,4]:$5
a[$NF,5]=$7
}
else if ($2 == "attP")
{
a[$NF,2]+=1
a[$NF,3]=a[$NF,3]>=$4?a[$NF,3]:$4
a[$NF,4]=a[$NF,4]<=$5?a[$NF,4]:$5
a[$NF,5]=$7
}
}
END{
for(i in a)
{
split(i,idx,SUBSEP)
print idx[1],a[idx[1],1],a[idx[1],2],a[idx[1],1]+a[idx[1],2],a[idx[1],3],a[idx[1],4],a[idx[1],4]-a[idx[1],3],a[idx[1],5]
}
}
' | awk '!a[$0]++' > $prefix.drp.temp.out
else
touch $prefix.drp.temp.out
fi
if [ $? -eq 0 ]
then
echo -e "Step 3: Combine split reads and discordant read pairs"
else
echo "!!!Unsuccessfully extracting discordant read pairs!!!"
exit
fi
#Step 2: Extracting discordant read pairs
# Step 3: Combining split reads and discordant read pairs
##Check the input file
if test -s $prefix.drp.temp.out; then
cat $prefix.sr-drp.temp.out | awk '
BEGIN {FS="\t";OFS="\t"}
FILENAME==ARGV[1] { print $0; next}
FILENAME==ARGV[2] { print $1,"0","0","0",$5,"-",$6,"-",$7,$8,$2,$3,$4}
' - $prefix.drp.temp.out >$prefix.temp.out
else
cat $prefix.sr-drp.temp.out >$prefix.temp.out
fi
#Delet existed SR_evidence.list
if [ -f "$prefix.SR_evidence.list1" ]
then
rm $prefix.SR_evidence.list1
fi
awk ' BEGIN {FS="\t";OFS="\t";i=1;print "prophage_candidate","contig","attL_start","attL_end","attR_start","attR_end","prophage_size","SR_evidence_attB","SR_evidence_attP","DRP_evidence_attB","DRP_evidence_attP"}
{
if ( $9 > '"$prophage_size_min"' && $9 < '"$prophage_size_max"')
{
if(($4 >= '"$SR_COUNT"' && $13 >='"$DRP_COUNT"'))
{
if ($4 != 0 && $5 > 1 && $7 >1)
{
if ( $6-$5 >= '"$att_length_min"' )
{
print "candidate_"i,$10,$5,$6,$7,$8,$9,$2,$3,$11,$12
print $1 >> "'$prefix'.SR_evidence.list1"
i++
}
}
else if ($5 > 1 && $7 >1)
{
print "candidate_"i,$10,$5,$6,$7,$8,$9,$2,$3,$11,$12
i++
}
}
}
}' $prefix.temp.out > $prefix.prophage.out
#blastn using split reads
if [ -f "$prefix.SR_evidence.list1" ]
then
awk 'BEGIN {FS="\t";OFS="\t"} NR==FNR {a[$1]=$0} NR>FNR {if ($9 in a) print $1} ' $prefix.SR_evidence.list1 $prefix.sr.temp.3 > $prefix.SR_evidence.list
awk '!a[$2"_"$3"_"$4"_"$5"_"$6"_"$7"_"$8]++' $prefix.sr.temp.1 | awk '!a[$1]++'| awk 'BEGIN {FS="\t";OFS="\t"} NR==FNR {a[$1]=$0} NR>FNR {if ($1"_"$2 in a) print $0} ' $prefix.SR_evidence.list - | awk '{printf ">"$1"_"$2"\n"$10"\n"}' > $prefix.SR.reads.fasta
blastn -query $prefix.SR.reads.fasta -db $prefix.nuclDB -out $prefix.SR.blastn.result -evalue 1e-3 -outfmt 1 -word_size 11
fi
#Clean up
if [ -f "$prefix.SR_evidence.list1" ]
then
rm $prefix.SR_evidence.list1
fi
rm contiglength.file $prefix.sr.temp.1 $prefix.reads.fasta makeblastdb.log blastn.log $prefix.sr.temp.2 $prefix.sr.temp.3 $prefix.sr.temp.out $prefix.drp.temp.1 $prefix.sr-drp.temp.out $prefix.drp.temp.2 $prefix.drp.temp.left $prefix.drp.temp.out $prefix.temp.out $prefix.nuclDB.*
if [ $? -eq 0 ]
then
echo "Successfully running prophage_tracer.sh"
echo -e "checking predicted prophage in $prefix.prophage.out\n\n"
else
echo "!!!Unsuccessfully running prophage_GPS.sh!!!"
exit
fi
exit 0