blib/lib/Bio/Graphics/Glyph/merge_parts.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 75 CC 3 SCAR 16.7
CC
3
Cov
0%
merge_parts
12.0
my_description
2.0
my_options
2.0
CRAP 5.3
1 package Bio::Graphics::Glyph::merge_parts;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph);
5  
6 sub my_description {
7 return <<END;
8 This is a base class for graded_segments, heterogeneous_segments,
9 and merged_alignment.
10  
11 It adds internal methods to support semantic zooming of scored
12 alignment features. It is not intended for end users.
13 END
14 }
15  
16 sub my_options {
17 {
18 max_gap => [
19 'integer',
20 undef,
21 'This is the maximum gap, measured in bp, across which the glyph will',
22 'attempt to merge subfeatures in an attempt to simplify the appearance',
23 'at low magnifications. If undef, the max_gap will be calculated using',
24 'a simple exponential heuristic.'],
25 }
26 }
27  
28 sub merge_parts {
29 my ($self,@parts) = @_;
30
31 # This is the largest gap across which adjacent segments will be merged
32 my $max_gap = $self->max_gap;
33  
34 my $last_part;
35  
36 my @sorted_parts = sort {$a->start <=> $b->start} @parts;
37  
38 for my $part (@sorted_parts) {
39 if ($last_part) {
40 my $start = $part->start;
41 my $end = $part->stop;
42 my $score = $part->score;
43 my $pstart = $last_part->start;
44 my $pend = $last_part->stop;
45 my $pscore = $last_part->score || 0;
46 my $len = 1 + abs($end - $start);
47 my $plen = 1 + abs($pend - $pstart);
48  
49 # weighted average score
50 my $new_score = (($score*$len)+($pscore*$plen))/($len+$plen);
51  
52 # don't merge if there is a gap > than the allowed size
53 my $gap = abs($start - $pend);
54 my $total = abs($end - $pstart);
55  
56 my $last_f = $last_part->feature;
57 if ($gap > $max_gap) {
58 $last_part = $part;
59 next;
60 }
61  
62 $part->{start} = $pstart;
63 $part->{score} = $new_score;
64 my ($left,$right) = $self->map_pt($pstart,$end+1);
65 $part->{left} = $left;
66 $part->{width} = ($right - $left) + 1;
67  
68 # flag the left feature for removal
69 $last_part->{remove} = 1;
70 }
71  
72 $last_part = $part;
73  
74 }
75  
76 @parts = grep {!defined $_->{remove}} @parts;
77  
78 return @parts;
79 }
80  
81 sub max_gap {
82 my $self = shift;
83 $self->panel->{max_gap} ||= $self->option('max_gap');
84 return $self->panel->{max_gap} || $self->calculate_max_gap;
85 }
86  
87 sub calculate_max_gap {
88 my $self = shift;
89 my $segment_length = $self->panel->length;
90  
91 # allow more aggressive merging for larger segments
92 # by exponentially increasing max_gap
93 my $max_gap = ($segment_length/10000)*($segment_length/500);
94  
95 $self->panel->{max_gap} = $max_gap;
96  
97 return $max_gap;
98 }
99  
100 1;
101  
102 __END__