-
Notifications
You must be signed in to change notification settings - Fork 8
/
dinumt.pl
executable file
·1199 lines (1072 loc) · 48.3 KB
/
dinumt.pl
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
my $version = "0.0.23";
#version update
# 0.0.23
# -fixed oversight on mask overlap to consider all possible overlaps of reference numt
#
# 0.0.22
# -changed name to "dinumt" (dynumite!)
# -added option --mt_names for discrete MT identifiers
#
# 0.0.21
# -added option for ensemble genomes (chrMT)
# -fix usage of masking when --include-mask isn't present
# -include reference paremeter in vcf output
#
# 0.0.20
# -added option to output GL information
#
# 0.0.19
# -added option to output supporting reads to auxilliary file
#
# 0.0.18
# -added mito position estimation
#
# 0.0.17
# -implemented likelihood scoring for events
# -added quality, evidence and depth filters
# -implemented VCF format reporting
#
# 0.0.15
# -bug fixes
#
# 0.0.14
# -fixed bug with parsing read group information
# -fixed bug where read group information was not being utilized in findBreakpoint()
#
# 0.0.13
# -added option for minimum clip size to consider
# -added option for maximum limit of putative breakpoints
# -added estimated numt size and mitochondria coordinates to output report
#
# 0.0.12
# -added option to additionally attempt to cluster reads mapping in known numt regions
# whose mates map elsewhere
# -added prefix option for report
#
# 0.0.11
# -added restriction that mates of linked clusters must be consistent with direct
# or inverted sequence (incl changing dir to dnext in input_hash)
#
# 0.0.10
# -added option for maximun read coverage when considering breakpoints
# -moved mate quality filtering to getInput()
#
# 0.0.9
# -changed default cluster reads to 1
# -added option to consider total evidence from read pairs and breakpoints
# -moved mask file comparison to getInput()
# -added option for minimum mapping quality
# -added filter in seqcluster() to remove low quality reads/clusters
#
# 0.0.8
# -added option to restrict analysis to one or more read groups
# -added option to use UCSC naming conventions (e.g. chrM instead of MT)
# -added option to use genomes segregated by chromosome
my %opts = ();
$opts{len_cluster_include} = 600;
$opts{len_cluster_link} = 800;
$opts{filter_quality} = 50;
$opts{filter_evidence} = 4;
$opts{filter_depth} = 5;
$opts{min_reads_cluster} = 1;
$opts{min_clipped_seq} = 5;
$opts{max_num_clipped} = 5;
$opts{include_mask} = 0;
$opts{min_evidence} = 4;
$opts{min_map_qual} = 10;
$opts{max_read_cov} = 200;
$opts{mask_filename} = "numtS.bed";
$opts{samtools} = "samtools";
$opts{prefix} = "numt";
$opts{len_mt} = 16569; #eventually should be read in by BAM header
$opts{ploidy} = 2;
$opts{output_support} = 0;
$opts{output_gl} = 0;
my $optResult = GetOptions(
"input_filename=s" => \$opts{input_filename},
"output_filename=s" => \$opts{output_filename},
"mask_filename=s" => \$opts{mask_filename},
"support_filename=s" => \$opts{support_filename},
"include_mask" => \$opts{include_mask},
"output_support" => \$opts{output_support},
"len_cluster_include=i" => \$opts{len_cluster_include},
"len_cluster_link=i" => \$opts{len_cluster_link},
"filter_quality=i" => \$opts{filter_quality},
"filter_evidence=i" => \$opts{filter_evidence},
"filter_depth=i" => \$opts{filter_evidence},
"min_reads_cluster=i" => \$opts{min_reads_cluster},
"min_evidence=i" => \$opts{min_evidence},
"min_clipped_seq=i" => \$opts{min_clipped_seq},
"max_num_clipped=i" => \$opts{max_num_clipped},
"min_map_qual=i" => \$opts{min_map_qual},
"max_read_cov=i" => \$opts{max_read_cov},
"mean_read_cov=f" => \$opts{mean_read_cov},
"insert_size=s" => \$opts{insert_size},
"read_groups=s" => \$opts{read_groups},
"mt_names=s" => \$opts{mt_names},
"by_chr_dir=s" => \$opts{by_chr_dir},
"reference=s" => \$opts{reference},
"prefix=s" => \$opts{prefix},
"output_gl" => \$opts{output_gl},
"ucsc" => \$opts{ucsc},
"ensembl" => \$opts{ensembl},
"help" => \$opts{help},
"verbose" => \$opts{verbose}
);
checkOptions( $optResult, \%opts, $version );
my $seq_num = 0;
my %seq_hash = ();
my %sorted_hash = ();
my %readgroup_hash = ();
my $i = 1;
my %infile_hash = ();
my %group_hash = ();
my %outfile_hash = ();
my %mask_hash = ();
my %mt_hash = ();
if ( defined( $opts{read_groups} ) ) {
my @rgs = split( /,/, $opts{read_groups} );
%readgroup_hash = map { $_, 1 } @rgs;
}
if (defined( $opts{mt_names} ) ) {
my @mts = split (/,/, $opts{mt_names} );
%mt_hash = map { $_, 1 } @mts;
}
getInput( \%infile_hash, \%readgroup_hash, \%mask_hash, \%mt_hash );
seqCluster( \%infile_hash );
linkCluster( \%infile_hash );
mapCluster( \%infile_hash, \%outfile_hash, \%readgroup_hash );
findBreakpoint( \%outfile_hash, \%readgroup_hash, \%mask_hash );
scoreData( \%outfile_hash );
report( \%outfile_hash );
################################################################################################################
sub scoreData {
my ($outfile_hash) = @_;
print "entering scoreData()\n" if $opts{verbose};
foreach my $group ( keys %$outfile_hash ) {
print "Group: $group\n" if $opts{verbose};
my $sumGP = 0;
my $numRef = $$outfile_hash{$group}{numRefRP};
my $numAlt = $$outfile_hash{$group}{numAltRP};
if ( $$outfile_hash{$group}{numAltSR} > 0 ) {
$numRef += $$outfile_hash{$group}{numRefSR};
$numAlt += $$outfile_hash{$group}{numAltSR};
}
print "\t$numRef\t$numAlt\n" if $opts{verbose};
foreach my $g ( 0 .. $opts{ploidy} ) {
my $geno = $opts{ploidy} - $g; #need to reverse as calculation is reference allele based
if ( $numAlt + $numRef > 0 && 1 / $opts{ploidy}**( $numAlt + $numRef ) > 0 ) {
$$outfile_hash{$group}{gl}{$geno} = calcGl( $opts{ploidy}, $g, $numAlt + $numRef, $numRef, $$outfile_hash{$group}{avgQ} );
$$outfile_hash{$group}{gp}{$geno} = 10**$$outfile_hash{$group}{gl}{$geno};
$sumGP += $$outfile_hash{$group}{gp}{$geno};
print "\t$geno\t$$outfile_hash{$group}{gl}{$geno}\t$$outfile_hash{$group}{gp}{$geno}\n" if $opts{verbose};
}
}
print "\tsumGP: $sumGP\n" if $opts{verbose};
if ( $sumGP == 0 ) {
foreach my $geno ( 0 .. $opts{ploidy} ) {
$$outfile_hash{$group}{pl}{$geno} = 0;
$$outfile_hash{$group}{gl}{$geno} = 0;
}
$$outfile_hash{$group}{gq} = 0;
$$outfile_hash{$group}{gt} = "./.";
$$outfile_hash{$group}{ft} = "NC";
}
else {
my $maxGP = 0;
my $maxGeno = 0;
foreach my $geno ( 0 .. $opts{ploidy} ) {
if ( $$outfile_hash{$group}{gp}{$geno} == 0 ) { $$outfile_hash{$group}{gp}{$geno} = 1e-200; }
$$outfile_hash{$group}{gp}{$geno} /= $sumGP;
$$outfile_hash{$group}{pl}{$geno} = int( -10 * log10( $$outfile_hash{$group}{gp}{$geno} ) );
if ( $$outfile_hash{$group}{gp}{$geno} > $maxGP ) { $maxGP = $$outfile_hash{$group}{gp}{$geno}; $maxGeno = $geno; }
}
$maxGP = 1 - $$outfile_hash{$group}{gp}{0}; #calculate P(not 0/0 | data)
if ( 1 - $maxGP == 0 ) {
$$outfile_hash{$group}{gq} = 199;
}
else {
$$outfile_hash{$group}{gq} = int( -10 * log10( 1 - $maxGP ) );
}
my $gt = "0/0";
if ( $maxGeno == 1 ) { $gt = "0/1"; }
elsif ( $maxGeno == 2 ) { $gt = "1/1"; }
$$outfile_hash{$group}{gt} = $gt;
my @filters = ();
if ( $$outfile_hash{$group}{gq} < $opts{filter_quality} ) {
push @filters, "q" . $opts{filter_quality};
}
if ( $numAlt < $opts{filter_evidence} ) {
push @filters, "e" . $opts{filter_evidence};
}
if ( $numAlt + $numRef < $opts{filter_depth} ) {
push @filters, "d" . $opts{filter_depth};
}
$$outfile_hash{$group}{ft} = ( defined( $filters[0] ) ) ? join( ";", @filters ) : "PASS";
}
}
print "exiting scoreData()\n" if $opts{verbose};
}
sub getDate {
my ( $second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings ) = localtime();
my $year = 1900 + $yearOffset;
$month++;
my $fmonth = sprintf( "%.2d", $month );
my $fday = sprintf( "%.2d", $dayOfMonth );
return "$year$fmonth$fday";
}
sub report {
my ($outfile_hash) = @_;
print "entering report()\n" if $opts{verbose};
#open output file
if ( defined( $opts{output_filename} ) ) {
open( foutname1, ">$opts{output_filename}" ) or die("error opening file $opts{output_filename}\n");
}
else {
open( foutname1, ">&", \*STDOUT ) or die;
}
if ( $opts{output_support} ) {
#open support file
open( support1, ">$opts{support_filename}" ) or die("could not open $opts{support_filename} for output, $!\n");
}
my $filedate = getDate();
print foutname1 <<HEADER;
##fileformat=VCFv4.1
##ALT=<ID=DEL,Description="Deletion">
##ALT=<ID=INS:MT,Description="Nuclear Mitochondrial Insertion">
##FILTER=<ID=q$opts{filter_quality},Description="Phred-scaled quality filter">
##FILTER=<ID=e$opts{filter_evidence},Description="Support reads filter">
##FILTER=<ID=d$opts{filter_depth},Description="Sequence depth filter">
##FORMAT=<ID=CN,Number=1,Type=Integer,Description="Copy number genotype for imprecise events">
##FORMAT=<ID=CNL,Number=.,Type=Float,Description="Copy number likelihoods">
##FORMAT=<ID=CNL0,Number=.,Type=Float,Description="Copy number likelihoods with no frequency prior">
##FORMAT=<ID=CNQ,Number=1,Type=Float,Description="Copy number genotype quality for imprecise events">
##FORMAT=<ID=FT,Number=1,Type=String,Description="Per-sample genotype filter">
##FORMAT=<ID=GL,Number=G,Type=Float,Description="Genotype likelihoods">
##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=PL,Number=G,Type=Integer,Description="Normalized, Phred-scaled likelihoods for genotypes as defined in the VCF specification">
##INFO=<ID=CIEND,Number=2,Type=Integer,Description="Confidence interval around END for imprecise variants">
##INFO=<ID=CIPOS,Number=2,Type=Integer,Description="Confidence interval around POS for imprecise variants">
##INFO=<ID=END,Number=1,Type=Integer,Description="End coordinate of this variant">
##INFO=<ID=HOMLEN,Number=.,Type=Integer,Description="Length of base pair identical micro-homology at event breakpoints">
##INFO=<ID=HOMSEQ,Number=.,Type=String,Description="Sequence of base pair identical micro-homology at event breakpoints">
##INFO=<ID=IMPRECISE,Number=0,Type=Flag,Description="Imprecise structural variation">
##INFO=<ID=MSTART,Number=1,Type=Flag,Description="Mitochondrial start coordinate of inserted sequence">
##INFO=<ID=MEND,Number=1,Type=Flag,Description="Mitochondrial end coordinate of inserted sequence">
##INFO=<ID=MLEN,Number=1,Type=Flag,Description="Estimated length of mitochondrial insert">
##INFO=<ID=NOVEL,Number=0,Type=Flag,Description="Indicates a novel structural variation">
##INFO=<ID=SVLEN,Number=.,Type=Integer,Description="Difference in length between REF and ALT alleles">
##INFO=<ID=SVTYPE,Number=1,Type=String,Description="Type of structural variant">
##fileDate=$filedate
##reference=$opts{reference}
##source=dinumt-$version
HEADER
my @vars = sort { $$outfile_hash{$a}{chr} cmp $$outfile_hash{$b}{chr} || $$outfile_hash{$a}{leftBkpt} <=> $$outfile_hash{$b}{leftBkpt} } keys %$outfile_hash;
if ( $opts{output_gl} ) {
print foutname1 "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t$opts{prefix}\n";
}
else {
print foutname1 "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n";
}
my $index = 1;
foreach my $group (@vars) {
print "$group in report()\n" if $opts{verbose};
if ( $$outfile_hash{$group}{gt} eq "0/0" || $$outfile_hash{$group}{gt} eq "./." ) {
print "\thomref or nc, skipping\n" if $opts{verbose};
next;
}
my $chrom = $$outfile_hash{$group}{chr};
unless ( $opts{ucsc} || $opts{ensembl} ) {
$chrom =~ s/chr//g;
}
my $id = $opts{prefix} . "_$index";
my $alt = "<INS:MT>";
my $qual = $$outfile_hash{$group}{gq};
my $filter = $$outfile_hash{$group}{ft};
my %info = ();
my $pos = $$outfile_hash{$group}{leftBkpt} - 1;
my $end = $$outfile_hash{$group}{rightBkpt};
my $ciDelta = $end - $pos + 1;
$info{IMPRECISE} = undef;
$info{CIPOS} = "0,$ciDelta";
$info{CIEND} = "-$ciDelta,0";
$info{END} = $end;
$info{SVTYPE} = "INS";
if ( $$outfile_hash{$group}{m_len} ne "NA" ) {
$info{MSTART} = $$outfile_hash{$group}{l_m_pos};
$info{MEND} = $$outfile_hash{$group}{r_m_pos};
$info{MLEN} = $$outfile_hash{$group}{m_len};
}
my $refline = `$opts{samtools} faidx $opts{reference} $chrom:$pos-$pos`;
my $ref = ( split( /\n/, $refline ) )[1];
if ( !defined($ref) ) { $ref = "N"; }
my $format = "GT:FT:GL:GQ:PL";
my $info = "";
my @sKeys = sort { $a cmp $b } keys %info;
for ( my $i = 0 ; $i <= $#sKeys ; $i++ ) {
if ( $i > 0 ) { $info .= ";"; }
if ( defined( $info{ $sKeys[$i] } ) ) {
$info .= "$sKeys[$i]=$info{$sKeys[$i]}";
}
else {
$info .= "$sKeys[$i]";
}
}
if ( $opts{output_gl} ) {
print foutname1 "$chrom\t$pos\t$id\t$ref\t$alt\t$qual\t$filter\t$info\t$format";
my @gls = ();
my @pls = ();
foreach my $geno ( 0 .. $opts{ploidy} ) {
push @gls, sprintf( "%.2f", $$outfile_hash{$group}{gl}{$geno} );
push @pls, $$outfile_hash{$group}{pl}{$geno};
}
my $gl = join( ",", @gls );
my $pl = join( ",", @pls );
print foutname1 "\t$$outfile_hash{$group}{gt}:$$outfile_hash{$group}{ft}:$gl:$$outfile_hash{$group}{gq}:$pl\n";
}
else {
print foutname1 "$chrom\t$pos\t$id\t$ref\t$alt\t$qual\t$filter\t$info\n";
}
if ( $opts{output_support} ) {
print support1 "$$outfile_hash{$group}{support}";
}
$index++;
}
close(foutname1);
if ( $opts{output_support} ) { close(support1); }
print "exiting report()\n" if $opts{verbose};
}
sub calcGl {
my ( $m, $g, $k, $l, $e ) = @_;
print "in calcGl():\n" if $opts{verbose};
print "\t$m\t$g\t$k\t$l\t$e\n" if $opts{verbose};
if ( 1 / $m**$k <= 0 ) { die "problem in calcGL 1, \t$m\t$g\t$k\t$l\t$e\n"; }
my $gl = log10( 1 / $m**$k );
if ( ( ( $m - $g ) * $e ) + ( ( 1 - $e ) * $g ) <= 0 ) { die "problem in calcGL 2, \t$m\t$g\t$k\t$l\t$e\n"; }
$gl += log10( ( ( $m - $g ) * $e ) + ( ( 1 - $e ) * $g ) ) for 1 .. $l;
if ( ( $m - $g ) * ( 1 - $e ) + ( $g * $e ) <= 0 ) { die "problem in calcGL 3, \t$m\t$g\t$k\t$l\t$e\n"; }
$gl += log10( ( $m - $g ) * ( 1 - $e ) + ( $g * $e ) ) for ( $l + 1 ) .. $k;
return $gl;
}
sub log10 {
my $n = shift;
return log($n) / log(10);
}
sub getInput {
my ( $infile_hash, $readgroup_hash, $mask_hash, $mt_hash ) = @_;
my @input_lines = ();
print "Reading input files...\n" if $opts{verbose};
#open input file
if ( $opts{by_chr_dir} ) {
if (defined( $opts{mt_names} ) ) {
foreach my $mt_name (keys %{$mt_hash}) {
push @input_lines, "samtools view $opts{by_chr_dir}/$mt_name.*bam |";
}
}
elsif ( $opts{ucsc} ) {
push @input_lines, "samtools view $opts{by_chr_dir}/chrM.*bam |";
}
elsif ( $opts{ensembl} ) {
push @input_lines, "samtools view $opts{by_chr_dir}/chrMT.*bam |";
}
else {
push @input_lines, "samtools view $opts{by_chr_dir}/MT*bam |";
}
}
else {
if (defined( $opts{mt_names} ) ) {
foreach my $mt_name (keys %{$mt_hash}) {
push @input_lines, "samtools view $opts{input_filename} $mt_name |";
}
}
elsif ( $opts{ucsc} ) {
push @input_lines, "samtools view $opts{input_filename} chrM |";
}
elsif ( $opts{ensembl} ) {
push @input_lines, "samtools view $opts{input_filename} chrMT |";
}
else {
push @input_lines, "samtools view $opts{input_filename} MT |";
}
}
#input mask coordinates
if ( $opts{include_mask} ) {
open( mask1, $opts{mask_filename} ) || die "Could not open $opts{mask_filename} for input, $!\n";
while (<mask1>) {
chomp;
my ( $chr, $start, $end, $id ) = split(/\t/);
$chr =~ s/chr//g;
$$mask_hash{$chr}{$start} = $end;
if ( $opts{include_mask} ) {
if ( $opts{by_chr_dir} ) {
if ( $opts{ucsc} || $opts{ensembl} ) {
$chr = "chr" . $chr;
}
push @input_lines, "samtools view $opts{by_chr_dir}/$chr*bam $chr:$start-$end |";
}
else {
if ( $opts{ucsc} || $opts{ensembl} ) {
$chr = "chr" . $chr;
}
push @input_lines, "samtools view $opts{input_filename} $chr:$start-$end |";
}
}
$$mask_hash{$chr}{$start} = $end;
}
close mask1;
}
foreach my $input_line (@input_lines) {
print "command: $input_line\n" if $opts{verbose};
open( fname1, $input_line ) || die "error in opening file, $!\n";
while ( my $line1 = <fname1> ) {
$seq_num = $i;
chomp($line1);
my ( $qname, $flag, $rname, $pos, $mapq, $cigar, $rnext, $pnext, $tlen, $seq, $qual ) = split( /\t/, $line1 );
my $pnextend = $pnext + length($seq); #use first read length as proxy for paired read length
my ($read_group) = $line1 =~ /RG:Z:(\S+)/;
if ( $opts{read_groups} && !defined($read_group) ) { next; }
elsif ( $opts{read_groups} && !defined( $$readgroup_hash{$read_group} ) ) { next; }
if ( $rnext eq '=' || $rnext eq '*' ) { next; }
if (defined( $opts{mt_names} ) ) {
if (!defined($$mt_hash{$rname}) && defined($$mt_hash{$rnext}) ) { next; }
}
else {
if ( $rname !~ /M/ && $rnext =~ /M/ ) { next; }
}
my $dnext = 0;
if ( $flag & 32 ) {
$dnext = 1;
}
my $dir = 0;
if ( $flag & 16 ) {
$dir = 1;
}
#Compare to masked regions
my $isMaskOverlap = 0;
if ( $opts{include_mask} ) {
foreach my $maskStart ( keys %{ $$mask_hash{$rnext} } ) {
my $maskEnd = $$mask_hash{$rnext}{$maskStart};
if ( $pnext >= $maskStart && $pnext <= $maskEnd ) {
$isMaskOverlap = 1;
last;
}
elsif ($pnextend >= $maskStart && $pnextend <= $maskEnd) {
$isMaskOverlap = 1;
last;
}
elsif ($pnext <= $maskStart && $pnextend >= $maskEnd) {
$isMaskOverlap = 1;
last;
}
}
}
if ($isMaskOverlap) { next; }
#get mate information
if ( $opts{by_chr_dir} ) {
open( SAM, "samtools view $opts{by_chr_dir}/$rnext.*bam $rnext:$pnext-$pnext | " ) || die "Could not find MT bam file in $opts{by_chr_dir}, $!\n";
}
else {
open( SAM, "samtools view $opts{input_filename} $rnext:$pnext-$pnext | " ) || die "Could not open $opts{input_filename}, $!\n";
}
my $c_mapq = 0;
my $cnext = 0;
while (<SAM>) {
chomp;
my ( $m_qname, $m_flag, $m_rname, $m_pos, $m_mapq, $m_cigar, $m_rnext, $m_pnext, $m_tlen, $m_seq, $m_qual, $opt ) = split(/\t/);
if ( $m_qname ne $qname ) { next; }
$c_mapq = $m_mapq;
$cnext = $m_cigar;
#if ( $c_mapq < $opts{min_map_qual} ) {
# print "MAPQ Filtering:\t$_\n" if $opts{verbose};
#}
}
close SAM;
if ( $c_mapq < $opts{min_map_qual} ) {
next;
}
$$infile_hash{$seq_num}{group} = 0;
$$infile_hash{$seq_num}{seq} = $seq;
$$infile_hash{$seq_num}{dir} = $dir;
$$infile_hash{$seq_num}{qname} = $qname;
$$infile_hash{$seq_num}{rname} = $rname;
$$infile_hash{$seq_num}{pos} = $pos;
$$infile_hash{$seq_num}{cigar} = $cigar;
$$infile_hash{$seq_num}{cnext} = $cnext;
$$infile_hash{$seq_num}{rnext} = $rnext;
$$infile_hash{$seq_num}{pnext} = $pnext;
$$infile_hash{$seq_num}{dnext} = $dnext;
$$infile_hash{$seq_num}{tlen} = $tlen;
$$infile_hash{$seq_num}{qual} = $qual;
$$infile_hash{$seq_num}{line} = $line1;
$i++;
}
close(fname1);
}
}
sub findBreakpoint {
my ( $outfile_hash, $readgroup_hash, $mask_hash ) = @_;
print "entering findBreakpoint()\n" if $opts{verbose};
foreach my $group ( keys %$outfile_hash ) {
print "\tAssessing group $group for breakpoints\n" if $opts{verbose};
my $l_pos = $$outfile_hash{$group}{l_pos};
my $r_pos = $$outfile_hash{$group}{r_pos};
my $chro = $$outfile_hash{$group}{chr};
$$outfile_hash{$group}{numAltSR} = 0;
$$outfile_hash{$group}{numRefSR} = 0;
$$outfile_hash{$group}{leftBkpt} = $l_pos;
$$outfile_hash{$group}{rightBkpt} = $r_pos;
#Compare to masked regions
my $isMaskOverlap = 0;
if ( $opts{include_mask} ) {
foreach my $maskStart ( keys %{ $$mask_hash{$chro} } ) {
my $maskEnd = $$mask_hash{$chro}{$maskStart};
if ( ( $l_pos >= $maskStart && $l_pos <= $maskEnd ) || ( $r_pos >= $maskStart && $r_pos <= $maskEnd ) || ( $l_pos <= $maskStart && $r_pos >= $maskEnd ) ) {
$isMaskOverlap = 1;
last;
}
}
}
if ($isMaskOverlap) { next; }
my %clippedPos = ();
#open input file
if ( $opts{by_chr_dir} ) {
open( SAM, "samtools view $opts{by_chr_dir}/$chro.*bam $chro:$l_pos-$r_pos |" ) || die "Could not find MT bam file in $opts{by_chr_dir}, $!\n";
}
else {
open( SAM, "samtools view $opts{input_filename} $chro:$l_pos-$r_pos |" ) || die "Could not open $opts{input_filename}, $!\n";
}
my %cnt = ();
while (<SAM>) {
chomp;
my ( $qname, $flag, $rname, $pos, $mapq, $cigar, $rnext, $pnext, $tlen, $seq, $qual ) = split(/\t/);
my ($read_group) = $_ =~ /RG:Z:(\S+)/;
if ( $opts{read_groups} && !defined($read_group) ) { next; }
elsif ( $opts{read_groups} && !defined( $$readgroup_hash{$read_group} ) ) { next; }
#Check for read positions outside max_read_cov
my $break = 0;
for ( my $p = 0 ; $p <= length($seq) ; $p++ ) {
$cnt{ $pos + $p }++;
if ( $cnt{ $pos + $p } > $opts{max_read_cov} ) { $break = 1; last; }
}
if ($break) {
print "Read count has reached limit of $opts{max_read_cov}, removing group $group\n" if $opts{verbose};
delete( $$outfile_hash{$group} );
last;
}
#Mark Clipped Positions
my ( $cPos, $clipside, $clipsize ) = getSoftClipInfo( $pos, $cigar, $qual );
if ( $cPos > -1 ) {
$clippedPos{$cPos}++;
}
}
close SAM;
if ( !defined( $$outfile_hash{$group} ) ) { next; }
my %bkpts = ();
my $numBkpts = 0;
foreach my $cPos ( sort keys %clippedPos ) {
if ( $clippedPos{$cPos} > 1 ) {
$bkpts{$cPos} = $clippedPos{$cPos};
$numBkpts++;
}
}
my $num_bkpt_support = 0;
my $num_ref_support = 0;
my $leftBkpt = $l_pos;
my $rightBkpt = $r_pos;
if ( $numBkpts > 0 && scalar keys %clippedPos <= $opts{max_num_clipped} ) {
#take two most prevelant breaks for now
my @sorted = sort { $bkpts{$b} <=> $bkpts{$a} } keys %bkpts;
if ( $numBkpts == 1 ) {
$leftBkpt = $sorted[0];
$rightBkpt = $leftBkpt + 1;
$num_bkpt_support = $bkpts{ $sorted[0] };
$num_ref_support = $cnt{ $sorted[0] } - $num_bkpt_support;
}
else {
if ( $sorted[0] < $sorted[1] ) {
$leftBkpt = $sorted[0];
$rightBkpt = $sorted[1];
}
else {
$leftBkpt = $sorted[1];
$rightBkpt = $sorted[0];
}
$num_bkpt_support = $bkpts{ $sorted[0] } + $bkpts{ $sorted[1] };
$num_ref_support = $cnt{ $sorted[0] } + $cnt{ $sorted[1] } - $num_bkpt_support;
}
}
$$outfile_hash{$group}{leftBkpt} = $leftBkpt;
$$outfile_hash{$group}{rightBkpt} = $rightBkpt;
$$outfile_hash{$group}{numAltSR} = $num_bkpt_support;
$$outfile_hash{$group}{numRefSR} = $num_ref_support;
}
print "exiting findBreakpoints()\n" if $opts{verbose};
}
sub seqCluster {
my ($infile_hash) = @_;
my $k = 0;
my %d = ();
$d{0}{k} = 0;
$d{0}{pnext} = 0;
$d{0}{last} = ();
$d{0}{rnext} = 0;
$d{1}{k} = 0;
$d{1}{pnext} = 0;
$d{1}{last} = ();
$d{1}{rnext} = 0;
my @sorted = sort { $$infile_hash{$a}->{rnext} cmp $$infile_hash{$b}->{rnext} || $$infile_hash{$a}->{pnext} <=> $$infile_hash{$b}->{pnext} } keys %{$infile_hash};
print scalar @sorted . " total reads to process for clustering\n" if $opts{verbose};
foreach my $c_seq_num (@sorted) {
my $c_pnext = $$infile_hash{$c_seq_num}{pnext};
my $c_dnext = $$infile_hash{$c_seq_num}{dnext};
my $c_rnext = $$infile_hash{$c_seq_num}{rnext};
my $c_qname = $$infile_hash{$c_seq_num}{qname};
print "$c_dnext\n" if $opts{verbose};
if ( $c_pnext - $d{$c_dnext}{pnext} > $opts{len_cluster_include} || $d{$c_dnext}{k} == 0 || $c_rnext ne $d{$c_dnext}{rnext} ) {
#print "c_pnext:$c_pnext \t d_pnext:$d{$c_dnext}{pnext} \t dir:$d{$c_dnext}{k}\n" if $opts{verbose};
if ( $d{$c_dnext}{k} > 0 && scalar @{ $d{$c_dnext}{last} } < $opts{min_reads_cluster} ) {
foreach my $seq_num ( @{ $d{$c_dnext}{last} } ) {
delete $$infile_hash{$seq_num};
}
}
$k++;
$$infile_hash{$c_seq_num}{'group'} = $k;
$d{$c_dnext}{k} = $k;
$d{$c_dnext}{last} = ();
}
else {
$$infile_hash{$c_seq_num}{'group'} = $d{$c_dnext}{k};
}
print "$d{$c_dnext}{k}\t$c_qname\t$c_rnext\t$c_pnext" if $opts{verbose};
push @{ $d{$c_dnext}{last} }, $c_seq_num;
$d{$c_dnext}{pnext} = $c_pnext;
$d{$c_dnext}{rnext} = $c_rnext;
#print "k:$k \t grp:$$infile_hash{$c_seq_num}{'group'} \t $d{$c_dnext}{k} \t pre_pos:$d{$c_dnext}{pnext}\n" if $opts{verbose};
#print "$$infile_hash{$c_seq_num}->{'group'}\n chr_num:$c_rnext\n" if $opts{verbose};
}
}
sub linkCluster {
my ($infile_hash) = @_;
#this can link multiple F's to a single leftmost R
my @sorted = sort { $$infile_hash{$a}->{rnext} cmp $$infile_hash{$b}->{rnext} || $$infile_hash{$a}->{pnext} <=> $$infile_hash{$b}->{pnext} } keys %{$infile_hash};
print scalar @sorted . " total reads to process for linking clusters\n" if $opts{verbose};
for ( my $c = 0 ; $c <= $#sorted ; $c++ ) {
my $c_seq_num = $sorted[$c];
$$infile_hash{$c_seq_num}{link} = 0;
my $c_pnext = $$infile_hash{$c_seq_num}{pnext};
my $c_dnext = $$infile_hash{$c_seq_num}{dnext};
my $c_rnext = $$infile_hash{$c_seq_num}{rnext};
my $c_dir = $$infile_hash{$c_seq_num}{dir};
if ( $c_dnext == 1 ) { next; }
for ( my $d = $c + 1 ; $d <= $#sorted ; $d++ ) {
my $d_seq_num = $sorted[$d];
my $d_pnext = $$infile_hash{$d_seq_num}{pnext};
my $d_dnext = $$infile_hash{$d_seq_num}{dnext};
my $d_rnext = $$infile_hash{$d_seq_num}{rnext};
my $d_dir = $$infile_hash{$d_seq_num}{dir};
if ( $d_dnext == 0 ) { next; }
my $delta = $d_pnext - $c_pnext;
if (
$delta < $opts{len_cluster_link}
&& $c_rnext eq $d_rnext
&& ( ( $c_dir == 0 && $c_dnext == 1 && $d_dnext == 0 && $d_dir == 1 )
|| ( $c_dir == 0 && $c_dnext == 0 && $d_dnext == 1 && $d_dir == 1 )
|| ( $c_dir == 1 && $c_dnext == 0 && $d_dnext == 1 && $d_dir == 0 ) )
)
{
#0.0.11 - must have consistent orientation between left and right sides of insertions
$$infile_hash{$c_seq_num}{link} = $$infile_hash{$d_seq_num}{group};
$$infile_hash{$d_seq_num}{link} = $$infile_hash{$c_seq_num}{group};
}
elsif ( !defined( $$infile_hash{$c_seq_num}{link} ) ) {
$$infile_hash{$c_seq_num}{link} = 0;
}
}
}
}
sub mapCluster {
my ( $infile_hash, $outfile_hash, $readgroup_hash ) = @_;
my %l_linked_groups;
my %linked_group_pnext;
foreach my $key ( sort { $infile_hash{$a}->{group} <=> $infile_hash{$b}->{group} } keys %infile_hash ) {
if ( ( $infile_hash{$key}{'group'} > 0 ) && ( $infile_hash{$key}{'dnext'} == 0 ) && ( $infile_hash{$key}{'link'} > 0 ) ) {
$l_linked_groups{ $infile_hash{$key}{'group'} } = $infile_hash{$key}{'link'};
}
}
my $i = 1;
while ( my ( $group, $link ) = each(%l_linked_groups) ) {
print "group = $group ;; link = $link \n" if $opts{verbose};
my @l_rnext = map { $infile_hash{$_}{'rnext'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_rname = map { $infile_hash{$_}{'rname'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_pos = map { $infile_hash{$_}{'pos'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_dir = map { $infile_hash{$_}{'dir'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_qname = map { $infile_hash{$_}{'qname'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_pnext = map { $infile_hash{$_}{'pnext'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_dnext = map { $infile_hash{$_}{'dnext'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_cigar = map { $infile_hash{$_}{'cigar'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @l_cnext = map { $infile_hash{$_}{'cnext'} } grep { $infile_hash{$_}{group} == $group } keys %infile_hash;
my @r_rnext = map { $infile_hash{$_}{'rnext'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_pos = map { $infile_hash{$_}{'pos'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_dir = map { $infile_hash{$_}{'dir'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_rname = map { $infile_hash{$_}{'rname'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_pnext = map { $infile_hash{$_}{'pnext'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_dnext = map { $infile_hash{$_}{'dnext'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_qname = map { $infile_hash{$_}{'qname'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_cigar = map { $infile_hash{$_}{'cigar'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @r_cnext = map { $infile_hash{$_}{'cnext'} } grep { $infile_hash{$_}{group} == $link } keys %infile_hash;
my @rc_pnext = ();
my @lc_pnext = ();
my $chr = $l_rnext[0];
#update right coordinates based on cigar length
for ( my $c = 0 ; $c <= $#r_cnext ; $c++ ) {
my $cigar = $r_cnext[$c];
$rc_pnext[$c] = $r_pnext[$c];
while ( $cigar =~ /(\d+)M/g ) {
$rc_pnext[$c] += $1;
}
while ( $cigar =~ /(\d+)N/g ) {
$rc_pnext[$c] += $1;
}
while ( $cigar =~ /(\d+)D/g ) {
$rc_pnext[$c] += $1;
}
}
#update left coordinates based on cigar length
for ( my $c = 0 ; $c <= $#l_cnext ; $c++ ) {
my $cigar = $l_cnext[$c];
$lc_pnext[$c] = $l_pnext[$c];
while ( $cigar =~ /(\d+)M/g ) {
$lc_pnext[$c] += $1;
}
while ( $cigar =~ /(\d+)N/g ) {
$lc_pnext[$c] += $1;
}
while ( $cigar =~ /(\d+)D/g ) {
$lc_pnext[$c] += $1;
}
}
my @s_l_pnext = sort { $a <=> $b } @l_pnext;
my @s_r_pnext = sort { $a <=> $b } @r_pnext;
my @s_lc_pnext = sort { $a <=> $b } @lc_pnext;
my @s_rc_pnext = sort { $a <=> $b } @rc_pnext;
my $l_brk_point = $s_l_pnext[$#s_l_pnext];
my $r_brk_point = $s_rc_pnext[0];
my $win_l_s = $s_l_pnext[0];
my $win_l_e = $s_lc_pnext[$#s_lc_pnext];
my $win_r_s = $s_r_pnext[0];
my $win_r_e = $s_rc_pnext[$#s_rc_pnext];
$linked_group_pnext{$i}{l_group} = $group;
$linked_group_pnext{$i}{r_group} = $link;
#Check for crossing clusters; occurs in regions of high coverage, but not ascertained
#until next step so stopgap here
if ( $l_brk_point > $r_brk_point ) {
my $temp = $l_brk_point;
$l_brk_point = $r_brk_point;
$r_brk_point = $temp;
}
my $commandLeft = "";
my $commandRight = "";
if ( $opts{by_chr_dir} ) {
$commandLeft = "samtools view $opts{by_chr_dir}/$chr.*bam $chr:$win_l_s-$win_l_e |";
$commandRight = "samtools view $opts{by_chr_dir}/$chr.*bam $chr:$win_r_s-$win_r_e |";
}
else {
$commandLeft = "samtools view $opts{input_filename} $chr:$win_l_s-$win_l_e |";
$commandRight = "samtools view $opts{input_filename} $chr:$win_r_s-$win_r_e |";
}
my $numRefRP = 0;
my $numAltRP = scalar @l_pnext + scalar @r_pnext;
my $sumE = 0;
#left
open( SAM, $commandLeft ) || die "Could not open sam file for input, $!\n";
while (<SAM>) {
chomp;
my ( $qname, $flag, $rname, $pos, $mapq, $cigar, $rnext, $pnext, $tlen, $seq, $qual ) = split(/\t/);
my $mapE = 10**( -1 * $mapq / 10 );
my ($read_group) = $_ =~ /RG:Z:(\S+)/;
if ( $opts{read_groups} && !defined($read_group) ) { next; }
elsif ( $opts{read_groups} && !defined( $$readgroup_hash{$read_group} ) ) { next; }
if ( $mapq < $opts{min_map_qual} ) { next; }
my $dir = 0; #F
if ( $flag & 16 ) { $dir = 1; } #R
if ( $pos >= $win_l_s && $pos <= $win_l_e && $dir == 0 ) {
$numRefRP++;
$sumE += $mapE;
}
}
close SAM;
#right
open( SAM, $commandRight ) || die "Could not open sam file for input, $!\n";
while (<SAM>) {
chomp;
my ( $qname, $flag, $rname, $pos, $mapq, $cigar, $rnext, $pnext, $tlen, $seq, $qual ) = split(/\t/);
my $mapE = 10**( -1 * $mapq / 10 );
my ($read_group) = $_ =~ /RG:Z:(\S+)/;
if ( $opts{read_groups} && !defined($read_group) ) { next; }
elsif ( $opts{read_groups} && !defined( $$readgroup_hash{$read_group} ) ) { next; }
if ( $mapq < $opts{min_map_qual} ) { next; }
my $dir = 0; #F
if ( $flag & 16 ) { $dir = 1; } #R
if ( $pos >= $win_r_s && $pos <= $win_r_e && $dir == 1 ) {
$numRefRP++;
$sumE += $mapE;
}
}
close SAM;
my $avgQ = $sumE / $numRefRP; #calculate average over all reads, ref and alt
$numRefRP -= $numAltRP; #correct for alt reads
#estimate mitochondrial coordinates from mated sequence alignments
my $l_m_min = 1e10;
my $l_m_max = 0;
my $l_m_min_i = -1;
my $l_m_max_i = -1;
my $l_n_dir = -1;
my $l_m_dir = -1;
for ( my $i = 0 ; $i <= $#l_qname ; $i++ ) {
if ( $l_rname[$i] !~ /M/ ) { next; } #don't include nuclear homologous regions
if ( $l_pos[$i] < $l_m_min ) {
$l_m_min = $l_pos[$i];
$l_m_min_i = $i;
}
if ( $l_pos[$i] > $l_m_max ) {
$l_m_max = $l_pos[$i];
$l_m_max_i = $i;
}
$l_n_dir = $l_dnext[$i];
$l_m_dir = $l_dir[$i];
}
my $r_m_min = 1e10;
my $r_m_max = 0;
my $r_m_min_i = -1;
my $r_m_max_i = -1;
my $r_n_dir = -1;
my $r_m_dir = -1;
for ( my $i = 0 ; $i <= $#r_qname ; $i++ ) {
if ( $r_rname[$i] !~ /M/ ) { next; } #don't include nuclear homologous regions
if ( $r_pos[$i] < $r_m_min ) {
$r_m_min = $r_pos[$i];
$r_m_min_i = $i;
}
if ( $r_pos[$i] > $r_m_max ) {
$r_m_max = $r_pos[$i];
$r_m_max_i = $i;
}
$r_n_dir = $r_dnext[$i];
$r_m_dir = $r_dir[$i];
}
$$outfile_hash{$group}{l_m_pos} = "NA";
$$outfile_hash{$group}{r_m_pos} = "NA";
$$outfile_hash{$group}{m_len} = "NA";
if ( $l_m_dir > -1 && $r_m_dir > -1 ) { #have mitochondrial mappings
if ( $l_n_dir == 0 && $l_m_dir == 1 && $r_m_dir == 0 && $r_n_dir == 1 ) {
my $cigar = $r_cigar[$r_m_max_i];
while ( $cigar =~ /(\d+)M/g ) {
$r_m_max += $1;
}
while ( $cigar =~ /(\d+)N/g ) {
$r_m_max += $1;
}
while ( $cigar =~ /(\d+)D/g ) {
$r_m_max += $1;
}
}
elsif ( $l_n_dir == 0 && $l_m_dir == 0 && $r_m_dir == 1 && $r_n_dir == 1 ) {
my $cigar = $l_cigar[$l_m_max_i];
while ( $cigar =~ /(\d+)M/g ) {
$l_m_max += $1;
}
while ( $cigar =~ /(\d+)N/g ) {
$l_m_max += $1;
}
while ( $cigar =~ /(\d+)D/g ) {
$l_m_max += $1;
}
}