forked from Dfam-consortium/RepeatMasker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRSearchResult.pm
executable file
·1165 lines (970 loc) · 29.8 KB
/
PRSearchResult.pm
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/local/bin/perl -w
##---------------------------------------------------------------------------##
## File:
## @(#) PRSearchResult.pm
## Author:
## Robert M. Hubley [email protected]
## Description:
## An object for holding ProcessRepeats meta data related to a
## search result.
##
#******************************************************************************
#* Copyright (C) Institute for Systems Biology 2003-2011 Developed by
#* Arian Smit and Robert Hubley.
#*
#* This work is licensed under the Open Source License v2.1. To view a copy
#* of this license, visit http://www.opensource.org/licenses/osl-2.1.php or
#* see the license.txt file contained in this distribution.
#*
###############################################################################
# ChangeLog
#
# $Log$
#
###############################################################################
# To Do:
#
=head1 NAME
PRSearchResult
=head1 SYNOPSIS
use PRSearchResult
Usage:
my $PRSearchResult = PRSearchResult->new();
or
my $PRSearchResultsCollection = PRSearchResult->new(
queryName=>value, subjName=>value,
pctInsert=>value, pctDelete=>value,
queryStart=>value, queryEnd=>value,
score=>value, pctDiverge=>value,
subjRemaining=>value, subjType=>value,
queryRemaining=>value, id=>value,
orientation=>value, queryString=>value,
subjString=>value, matrix=>value,
id=>value, lineageId=>value );
=head1 DESCRIPTION
A subclass of SearchResult which holds a ProcessRepeats specific search
result.
=head1 SEE ALSO
=back
=head1 COPYRIGHT
Copyright 2011 Institute for Systems Biology
=head1 AUTHOR
Robert Hubley <[email protected]>
=head1 INSTANCE METHODS
=cut
package PRSearchResult;
use strict;
use SearchResult;
use Data::Dumper;
use Carp;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
require Exporter;
@ISA = qw(SearchResult Exporter);
@EXPORT = qw();
@EXPORT_OK = qw();
%EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
my $CLASS = "PRSearchResult";
my $DEBUG = 0;
##-------------------------------------------------------------------------##
## Constructor:
##-------------------------------------------------------------------------##
sub new {
my $class = shift;
my %nameValuePairs = @_;
my $this = $class->SUPER::new( @_ );
# Bless this hash in the name of the father, the son...
bless $this, $class;
# Allow import of values
#if ( %nameValuePairs )
#{
# while ( my ( $name, $value ) = each( %nameValuePairs ) )
# {
# my $method = "set" . _ucFirst( $name );
# unless ( $this->can( $method ) )
# {
# croak(
# "PRSearchResult::add: Instance variable $name doesn't exist." . "" );
# }
# $this->$method( $value );
# }
#}
return $this;
}
##-------------------------------------------------------------------------##
=head2 clone()
Use: my $newObj = $obj->clone();
Clone a PRSearchResult *duplicating* all the values of the old
object in the new one.
=cut
##-------------------------------------------------------------------------##
sub clone {
my $this = shift;
my %newHash = %{$this};
my $newObj = \%newHash;
bless $newObj, ref( $this );
return $newObj;
}
##-------------------------------------------------------------------------##
## Get and Set Methods
##-------------------------------------------------------------------------##
# Just these fields for now
sub setFrom {
my $this = shift;
my $source = shift;
$this->setScore( $source->getScore() );
$this->setPctDiverge( $source->getPctDiverge() );
$this->setPctKimuraDiverge( $source->getPctKimuraDiverge() );
$this->setPctDelete( $source->getPctDelete() );
$this->setPctInsert( $source->getPctInsert() );
$this->setQueryName( $source->getQueryName() );
$this->setQueryStart( $source->getQueryStart() );
$this->setQueryEnd( $source->getQueryEnd() );
$this->setQueryRemaining( $source->getQueryRemaining() );
$this->setOrientation( $source->getOrientation() );
$this->setHitName( $source->getHitName() );
$this->setClassName( $source->getClassName() );
$this->setSubjStart( $source->getSubjStart() );
$this->setSubjEnd( $source->getSubjEnd() );
$this->setSubjRemaining( $source->getSubjRemaining() );
$this->setLineageId( $source->getLineageId() );
$this->setId( $source->getId() );
$this->setPRID( $source->getPRID() );
$this->setEquivHash( $source->getEquivHash() );
$this->setSubjString( $source->getSubjString() );
$this->setQueryString( $source->getQueryString() );
$this->setMatrixName( $source->getMatrixName() );
$this->setPctRawKimuraDiverge( $source->getPctRawKimuraDiverge() );
$this->setCpGSites( $source->getCpGSites() );
# WARNING: Do not inherit "derivedFromAnnots".
# These are the fields we are ignoring currently
# setStatus
# setLeftLinkedHit
# setRightLinkedHit
# ..
return $this;
}
## DEPRECATED
# The only use of this by PR was to clear the data to save memory
sub setAlignData {
my $this = shift;
$this->setSubjString( "" );
$this->setQueryString( "" );
}
## DEPRECATED
sub getAlignData {
my $this = shift;
my $str = $this->toStringFormatted( SearchResult::AlignWithQuerySeq );
$str =~ s/^\s*\d+[^\n\r]+$//;
return $str;
}
sub getSeq2Len {
my $this = shift;
return ( $this->getSubjEnd() + $this->getSubjRemaining() );
}
## beware the orientation is "+" "C"
sub getEquivHash {
my $this = shift;
return $this->{'equivHash'};
}
sub setEquivHash {
my $this = shift;
my $value = shift;
$this->{'equivHash'} = $value;
}
# *.out: id lineageId overlap
# *.cat: stage refid ( getLastField, getCatID )
# get/set prID
# get/set lineageId
# The final ID tag in Process Repeats
sub setPRID {
my $this = shift;
my $value = shift;
$this->{'prID'} = $value;
}
sub getPRID {
my $this = shift;
return $this->{'prID'};
}
sub isCut {
my $this = shift;
if ( $this->getLineageId() =~ /c_b\d+s\d+i\d+/ ) {
return 1;
}
return 0;
}
sub isMasked {
my $this = shift;
if ( $this->getLineageId() =~ /m_b\d+s\d+i\d+/ ) {
return 1;
}
return 0;
}
sub getStage {
my $this = shift;
if ( $this->getLineageId() =~ /[mc]_b\d+s(\d+)i\d+/ ) {
return ( $1 );
}
return -1;
}
## This is used in PR to hold the state of an annotation.
## This is most often used to flag records to be deleted.
sub getStatus {
my $this = shift;
return $this->{'status'};
}
sub setStatus {
my $this = shift;
my $value = shift;
$this->{'status'} = $value;
}
sub getBlockLeftJoin {
my $this = shift;
return $this->{'blockLeftJoin'};
}
sub setBlockLeftJoin {
my $this = shift;
my $value = shift;
$this->{'blockLeftJoin'} = $value;
}
sub getBlockRightJoin {
my $this = shift;
return $this->{'blockRightJoin'};
}
sub setBlockRightJoin {
my $this = shift;
my $value = shift;
$this->{'blockRightJoin'} = $value;
}
sub getLeftLinkedHit {
my $this = shift;
return $this->{'leftHit'};
}
sub setLeftLinkedHit {
my $this = shift;
my $value = shift;
$this->{'leftHit'} = $value;
}
#returns element ( if set ) who is linked to our right edge ( Seq1End )
#
sub getRightLinkedHit {
my $this = shift;
return $this->{'rightHit'};
}
sub setRightLinkedHit {
my $this = shift;
my $value = shift;
$this->{'rightHit'} = $value;
}
sub getClassName {
my $this = shift;
my ( $hitName, $className ) =
split( /\#/, $this->getSubjName() );
return $className;
}
# TODO Add some error checking
sub setClassName {
my $this = shift;
my $newClassName = shift;
my ( $hitName, $className ) =
split( /\#/, $this->getSubjName() );
$this->setSubjName( $hitName . "#" . $newClassName );
}
sub getHitName {
my $this = shift;
my ( $hitName, $className ) =
split( /\#/, $this->getSubjName() );
return $hitName;
}
sub setHitName {
my $this = shift;
my $newHitName = shift;
my ( $hitName, $className ) =
split( /\#/, $this->getSubjName() );
$this->setSubjName( $newHitName . "#" . $className );
}
sub getUniqID {
my $this = shift;
return ( $this->getScore()
. $this->getPctDiverge()
. $this->getPctDelete()
. $this->getPctInsert()
. $this->getQueryName()
. $this->getQueryStart()
. $this->getQueryEnd()
. $this->getQueryRemaining()
. $this->getOrientation()
. $this->getSubjName()
. $this->getSubjStart()
. $this->getSubjEnd()
. $this->getSubjRemaining() );
}
sub getDerivedFromAnnot {
my $this = shift;
return ( $this->{'derivedFrom'} );
}
sub setDerivedFromAnnot {
my $this = shift;
my $member = shift;
my $annot = new PRSearchResult;
$annot->setFrom( $member );
$this->{'derivedFrom'} = [ $member ];
}
sub addDerivedFromAnnot {
my $this = shift;
my $member = shift;
my $annot = new PRSearchResult;
$annot->setFrom( $member );
## This needs to be a deep copy
if ( $member->getDerivedFromAnnot() ) {
foreach my $subAnnot ( @{ $member->getDerivedFromAnnot() } ) {
$annot->addDerivedFromAnnot( $subAnnot );
}
}
push @{ $this->{'derivedFrom'} }, $annot;
}
sub print {
my $this = shift;
my $noLineTerm = shift;
my $lineTerm = "\n" if ( !defined $noLineTerm );
my $outStr = sprintf(
"%4d %3.1f %3.1f %3.1f %-18.18s %8d %8d "
. "%8d %1s %-18.18s %8d %8d %8d %8s %6s$lineTerm",
$this->getScore(), $this->getPctDiverge(),
$this->getPctDelete(), $this->getPctInsert(),
$this->getQueryName(), $this->getQueryStart(),
$this->getQueryEnd(), $this->getQueryRemaining(),
$this->getOrientation(), $this->getSubjName(),
$this->getSubjStart(), $this->getSubjEnd(),
$this->getSubjRemaining(), $this->getId(),
$this->getStatus()
);
print $outStr;
}
sub printBrief {
my $this = shift;
my $excludeNewline = shift;
my $suffix = "\n";
$suffix = "" if ( $excludeNewline );
my $outStr = sprintf(
"%4d %-18.18s %8d %8d " . "%1s %-18.18s %8d %8d %20s$suffix",
$this->getScore(),
$this->getQueryName(), $this->getQueryStart(),
$this->getQueryEnd(),
$this->getOrientation(), $this->getSubjName(),
$this->getSubjStart(), $this->getSubjEnd(), $this->getLineageId()
);
print $outStr;
}
sub printLeftRightLinks {
my $this = shift;
if ( $this->getLeftLinkedHit() != undef ) {
if ( $this->getLeftLinkedHit() == $this ) {
print " BLOCKED ( links to self )\n";
}
else {
print " ";
$this->getLeftLinkedHit()->printBrief();
}
}
else {
print " UNDEF\n";
}
print " -->";
$this->printBrief();
if ( $this->getRightLinkedHit() != undef ) {
if ( $this->getRightLinkedHit() == $this ) {
print " BLOCKED ( links to self )\n";
}
else {
print " ";
$this->getRightLinkedHit()->printBrief();
}
}
else {
print " UNDEF\n";
}
}
sub sanityCheckConsPos {
my $this = shift;
my $hashRef = shift;
# Find begining
print "Sanity Checking:\n";
my $firstInChain = $this;
my $detectLoop = 0;
while ( $firstInChain->getLeftLinkedHit() != undef
&& $detectLoop < 50 )
{
# Ok to link to self.
if ( $firstInChain == $firstInChain->getLeftLinkedHit() ) {
last;
}
$firstInChain = $firstInChain->getLeftLinkedHit();
$detectLoop++;
}
if ( $detectLoop >= 50 ) {
print "WARNING! This chain contains a loop!!!!\n";
}
# Now print
my $nextInChain = $firstInChain;
do {
if ( $nextInChain == $this ) {
print "--> (";
}
else {
print " (";
}
print "" . $hashRef->{ $nextInChain->getId() } . "): ";
$nextInChain->printBrief();
# Link to itself is also allowed
if ( $nextInChain == $nextInChain->getRightLinkedHit() ) {
last;
}
$nextInChain = $nextInChain->getRightLinkedHit();
} while ( $nextInChain )
}
sub checkLinkOrder {
my $this = shift;
# Find begining
my $firstInChain = $this;
my $detectLoop = 0;
while ( $firstInChain->getLeftLinkedHit() != undef
&& $firstInChain != $firstInChain->getLeftLinkedHit()
&& $detectLoop < 50 )
{
$firstInChain = $firstInChain->getLeftLinkedHit();
$detectLoop++;
}
if ( $detectLoop >= 50 ) {
print STDERR "WARNING! This chain contains a loop!!!!\n";
}
# Now print
my $nextInChain = $firstInChain;
my @resGroup = ();
my $inOrder = 1;
do {
push @resGroup, $nextInChain;
if ( $nextInChain->getRightLinkedHit() ) {
if ( $nextInChain->getQueryStart() < $nextInChain->getRightLinkedHit()->getQueryStart() ) {
$inOrder = 0;
}
}
$nextInChain = $nextInChain->getRightLinkedHit();
} while ( $nextInChain && $nextInChain != $nextInChain->getRightLinkedHit() );
unless ( $inOrder == 1 ) {
@resGroup = sort { $a->getQueryStart() <=> $b->getQueryStart() ||
$b->getQueryEnd() <=> $a->getQueryEnd() } @resGroup;
$resGroup[0]->setLeftLinkedHit(undef);
$resGroup[$#resGroup]->setRightLinkedHit(undef);
for( my $i = 1; $i <= $#resGroup; $i++ ) {
$resGroup[$i]->setLeftLinkedHit($resGroup[$i-1]);
$resGroup[$i-1]->setRightLinkedHit($resGroup[$i]);
}
}
}
#
# Print an element and ( if exists ) all of it chain members in order
#
sub printLinks {
my $this = shift;
# Find begining
my $firstInChain = $this;
my $detectLoop = 0;
while ( $firstInChain->getLeftLinkedHit() != undef
&& $firstInChain != $firstInChain->getLeftLinkedHit()
&& $detectLoop < 50 )
{
$firstInChain = $firstInChain->getLeftLinkedHit();
$detectLoop++;
}
if ( $detectLoop >= 50 ) {
print STDERR "WARNING! This chain contains a loop!!!!\n";
}
# Now print
my $nextInChain = $firstInChain;
do {
if ( $nextInChain == $this ) {
print "--> ";
}
else {
print " ";
}
$nextInChain->printBrief(1);
my $left = $nextInChain->getLeftLinkedHit();
my $leftRight;
$leftRight = $left->getRightLinkedHit() if ($left);
my $right = $nextInChain->getRightLinkedHit();
my $rightLeft;
$rightLeft = $right->getLeftLinkedHit() if ($right);
if ( $left == $nextInChain ) {
print " U ";
}elsif ( $left ) {
if ( $nextInChain != $leftRight ) {
print " ! ";
}else {
print " <- ";
}
}else {
print " * ";
}
if ( $right == $nextInChain ) {
print " U\n";
}elsif ( $right ) {
if ( $nextInChain != $rightLeft ) {
print " !\n";
}else {
print " ->\n";
}
}else {
print " *\n";
}
# Link to itself is also allowed
if ( $nextInChain == $nextInChain->getRightLinkedHit() ) {
last;
}
$nextInChain = $nextInChain->getRightLinkedHit();
} while ( $nextInChain )
}
# Intelligently relink an element's edges prior to removal
sub removeFromJoins {
my $this = shift;
my $DEBUG = 0;
my $left = $this->getLeftLinkedHit();
my $right = $this->getRightLinkedHit();
if ( $DEBUG ) {
print "Remove from joins:\n";
$this->printLinks();
}
# Just reset these since we are being removed
$this->setLeftLinkedHit( undef )
unless ( $this == $left ); # don't removedlinks to itself.
$this->setRightLinkedHit( undef )
unless ( $this == $right ); # don't remove links to itself.
$left = undef if ( $this == $left );
$right = undef if ( $this == $right );
if ( defined $left && !defined $right ) {
# Remove left's edge reference
$left->setRightLinkedHit( undef );
}
elsif ( defined $left && defined $right ) {
# Join our neighbors together
$left->setRightLinkedHit( $right );
$right->setLeftLinkedHit( $left );
}
elsif ( !defined $left && defined $right ) {
# Remove rights's edge reference
$right->setLeftLinkedHit( undef );
}
}
# Intelligently relink an elements edges for insertion
sub join {
my $this = shift;
my $partner = shift;
if ( $DEBUG ) {
print "join this:\n";
$this->printLinks();
print "to partner\n";
$partner->printLinks();
}
die "join(): Invalid join! \$this == \$partner" if ( $this == $partner );
my @cluster = ();
foreach my $chain ( $this, $partner ) {
push @cluster, $chain;
my $nextInChain = $chain;
my %seen = ();
while ( $nextInChain->getLeftLinkedHit()
&& $nextInChain->getLeftLinkedHit() != $nextInChain )
{
if ( $seen{ $nextInChain->getLeftLinkedHit() } ) {
warn "WARN: breaking loop in fragments\n";
$nextInChain->setLeftLinkedHit( undef );
last;
}
$nextInChain = $nextInChain->getLeftLinkedHit();
$seen{$nextInChain}++;
push @cluster, $nextInChain;
}
$nextInChain = $chain;
while ( $nextInChain->getRightLinkedHit()
&& $nextInChain->getRightLinkedHit() != $nextInChain )
{
if ( $seen{ $nextInChain->getRightLinkedHit() } ) {
warn "WARN: breaking loop in fragments\n";
$nextInChain->setRightLinkedHit( undef );
last;
}
$nextInChain = $nextInChain->getRightLinkedHit();
$seen{$nextInChain}++;
push @cluster, $nextInChain;
}
}
# Sort cluster
@cluster = sort { $a->comparePositionOrder( $b ) } ( @cluster );
my $lastAnnot = undef;
foreach my $annot ( @cluster ) {
next if ( $annot == $lastAnnot );
$annot->setLeftLinkedHit( $lastAnnot );
$annot->setRightLinkedHit( undef );
if ( $lastAnnot ) {
$lastAnnot->setRightLinkedHit( $annot );
}
$lastAnnot = $annot;
}
if ( $DEBUG ) {
print "Now look what we did with this thing:\n";
$this->printLinks();
}
}
# Intelligently relink an elements edges for insertion
sub resortJoins {
my $this = shift;
my @cluster = ();
push @cluster, $this;
my $nextInChain = $this;
while ( $nextInChain->getLeftLinkedHit()
&& $nextInChain->getLeftLinkedHit() != $nextInChain )
{
$nextInChain = $nextInChain->getLeftLinkedHit();
push @cluster, $nextInChain;
}
$nextInChain = $this;
while ( $nextInChain->getRightLinkedHit()
&& $nextInChain->getRightLinkedHit() != $nextInChain )
{
$nextInChain = $nextInChain->getRightLinkedHit();
push @cluster, $nextInChain;
}
# Sort cluster
@cluster = sort { $a->comparePositionOrder( $b ) } ( @cluster );
my $lastAnnot = undef;
foreach my $annot ( @cluster ) {
next if ( $annot == $lastAnnot );
$annot->setLeftLinkedHit( $lastAnnot );
$annot->setRightLinkedHit( undef );
if ( $lastAnnot ) {
$lastAnnot->setRightLinkedHit( $annot );
}
$lastAnnot = $annot;
}
if ( $DEBUG ) {
print "Now look what we did with this thing:\n";
$this->printLinks();
}
}
# Intelligently merge $partner information with us. Then remove
# our partner's links to ready it for removal.
sub mergeSimpleLow {
my $this = shift;
my $partner = shift;
my $DEBUG = 0;
if ( $DEBUG ) {
print "mergeSimpleLow(): THIS links:\n";
$this->printLinks();
print "mergeSimpleLow(): PARTNER links\n";
$partner->printLinks();
}
# Pick appropriate substitution/insertion/deletion stats for
# merged element.
if ( $this->getHitName() eq $partner->getHitName() ) {
$this->adjustSubstLevel( $partner, "noQueryOverlapAdj" );
$this->setScore( $partner->getScore() )
if ( $this->getScore() < $partner->getScore() );
}
else {
if ( $this->getScore() < $partner->getScore() ) {
$this->setPctDiverge( $partner->getPctDiverge() );
$this->setPctKimuraDiverge( $partner->getPctKimuraDiverge() );
$this->setPctDelete( $partner->getPctDelete() );
$this->setPctInsert( $partner->getPctInsert() );
$this->setScore( $partner->getScore() );
$this->setSubjName( $partner->getSubjName() );
} # else....keep ours
}
# Pick the lower query begin position
$this->setQueryStart( $partner->getQueryStart() )
if ( $this->getQueryStart() > $partner->getQueryStart() );
# Pick the higher query end position
if ( $this->getQueryEnd() < $partner->getQueryEnd() ) {
$this->setQueryEnd( $partner->getQueryEnd() );
$this->setQueryRemaining( $partner->getQueryRemaining() );
}
my $newQuerySize = $this->getQueryEnd() - $this->getQueryStart() + 1;
my $newSeq2End =
$newQuerySize + ( $this->getPctDelete() - $this->getPctInsert() ) *
$newQuerySize / 100;
$this->setSubjStart( 1 );
$this->setSubjEnd( sprintf( "%d", $newSeq2End ) );
## TODO: What happens when our partner has joins that
## should point to the new merger?
$partner->removeFromJoins();
if ( $DEBUG ) {
print "mergeSimpleLow(): Final THIS links:\n";
$this->printLinks();
print "mergeSimpleLow(): Leaving\n";
}
}
# Intelligently merge $partner information with us. Then remove
# our partner's links to ready it for removal.
sub merge {
my $this = shift;
my $partner = shift;
my $DEBUG = 0;
if ( $DEBUG ) {
print "merge(): Entered\n";
print "merge(): THIS links:\n";
$this->printLinks();
print "merge(): PARTNER links\n";
$partner->printLinks();
}
# Pick appropriate substitution/insertion/deletion stats for
# merged element.
$this->adjustSubstLevel( $partner );
# Pick the lower query begin position
$this->setQueryStart( $partner->getQueryStart() )
if ( $this->getQueryStart() > $partner->getQueryStart() );
# Pick the higher query end position
if ( $this->getQueryEnd() < $partner->getQueryEnd() ) {
$this->setQueryEnd( $partner->getQueryEnd() );
$this->setQueryRemaining( $partner->getQueryRemaining() );
}
# Find the lower consensus begin position
my $minBeg = $partner->getSubjStart();
$minBeg = $this->getSubjStart()
if ( $this->getSubjStart() < $minBeg );
# Find the higher consensus end position
my $maxEnd = $partner->getSubjEnd();
$maxEnd = $this->getSubjEnd()
if ( $this->getSubjEnd() > $maxEnd );
# Sanity check consensus position choices. If
# the hybrid range ( lowest begin & highest end ) of
# the two elements is > 2* query length then
# use the original range of the higher scoring
# element instead of the hybrid.
if ( ( $maxEnd - $minBeg + 1 ) >
( 2 * ( $this->getQueryEnd() - $this->getQueryStart() + 1 ) ) )
{
# Use the highest scoring annotation's range
if ( $this->getScore() < $partner->getScore() ) {
$this->setSubjStart( $partner->getSubjStart() );
$this->setSubjEnd( $partner->getSubjEnd() );
$this->setSubjRemaining( $partner->getSubjRemaining() );
}
}
else {
# Use a hybrid range
$this->setSubjStart( $minBeg );
if ( $this->getSubjEnd() < $partner->getSubjEnd() ) {
$this->setSubjEnd( $maxEnd );
$this->setSubjRemaining( $partner->getSubjRemaining() );
}
}
# Use the higher score and it's associated consensus name
if ( $this->getScore() < $partner->getScore() ) {
$this->setScore( $partner->getScore() );
$this->setSubjName( $partner->getSubjName() );
}
$partner->removeFromJoins();
if ( $DEBUG ) {
print "merge(): Final THIS links:\n";
$this->printLinks();
print "merge(): Leaving\n";
}
}
#### NEW TESTING
sub getAdjustedSubstLevel {
my $this = shift;
my $partner = shift;
my $method = shift;
my $DEBUG = 0;
if ( $DEBUG ) {
print "getAdjustedSubstLevel( this, partner, method = $method ): Entered\n";
print "this: ";
$this->print();
print "partner: ";
$partner->print();
}
warn "getAdjustedSubstLevel(): Unknown method $method"
if ( defined $method && $method !~ /noQueryOverlapAdj/ );
# For calculation, choose best matching consensus in overlapped region
my $subBases;
my $delBases;
my $insBases;
#
# This change allows us to calculate these stats based on
# the complete alignemnt returned by RM. Previously this would
# only account for one fragment.
# TODO: Allow this change after a sanity check!
#
my $thisLength = $this->getQueryEnd() - $this->getQueryStart() + 1;
my $partnerLength = $partner->getQueryEnd() - $partner->getQueryStart() + 1;
my $qo = 0;
$qo = $this->getQueryOverlap( $partner )
unless ( $method eq "noQueryOverlapAdj" );
my $co = $this->getConsensusOverlap( $partner );
print "adjustSubstLevel(): qo = $qo, co = $co, "
. "thisLength = $thisLength, "
. "partnerLength = $partnerLength\n"
if ( $DEBUG );
if ( $this->getPctDiverge() <= $partner->getPctDiverge() ) {
$subBases =
$thisLength * $this->getPctDiverge() + ( $partnerLength - $qo ) *
$partner->getPctDiverge();
$delBases = $thisLength * $this->getPctDelete() + ( $partnerLength - $qo ) *
$partner->getPctDelete();
$insBases = $thisLength * $this->getPctInsert() + ( $partnerLength - $qo ) *
$partner->getPctInsert();
}
else {
$subBases =
$partnerLength * $partner->getPctDiverge() + ( $thisLength - $qo ) *
$this->getPctDiverge();
$delBases =
$partnerLength * $partner->getPctDelete() + ( $thisLength - $qo ) *
$this->getPctDelete();
$insBases =
$partnerLength * $partner->getPctInsert() + ( $thisLength - $qo ) *
$this->getPctInsert();
}
# Include the query or consensus gap in $PctInsert or $PctDelete
# Expressed in percent (not bases)
if ( $qo <= 10 && $method ne "noQueryOverlapAdj" ) {
if ( $co - $qo > 0 ) {
$insBases += ( $co - $qo ) * 100;
}
else {
$delBases -= ( $co - $qo ) * 100;
}
}