lib/Bio/MUST/Core/Taxonomy/ColorScheme.pm

filter: Statementstmt 46.3% 50 / 108 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 56.5% 13 / 23 Podpod 50.0% 2 / 4 totaltotal 40.1% 65 / 162 CC 13 SCAR 20.7
CC
13
Cov
38%
attach_colors_to_entities
20.0
load
20.0
BUILD
12.0
CRAP 7.9
« script/ali2phylip.pl ↑ Summary lib/Bio/MUST/Core/Taxonomy/Filter.pm »
1 package Bio::MUST::Core::Taxonomy::ColorScheme;
2 # ABSTRACT: Helper class providing color scheme for taxonomic annotations
3 # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be>
4 18 $Bio::MUST::Core::Taxonomy::ColorScheme::VERSION = '0.252040';
5 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 ▶use Smart::Comments '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # use Color::Spectrum::Multi;
15 18 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use Graphics::ColorNames;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use Graphics::ColorNames::WWW;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use List::AllUtils qw(mesh uniq each_array);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use Bio::MUST::Core::Constants qw(:files);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 with 'Bio::MUST::Core::Roles::Commentable',
24 'Bio::MUST::Core::Roles::Taxable';
25  
26  
27 # names and colors public arrays
28 has $_ . 's' => (
29 traits => ['Array'],
30 is => 'ro',
31 isa => 'ArrayRef[Str]',
32 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 handles => {
34 'count_' . $_ . 's' => 'count',
35 'all_' . $_ . 's' => 'elements',
36 'add_' . $_ => 'push',
37 },
38 18 ) for qw(name color);
39  
40  
41 # _color_for private hash for faster mapping
42 18 has '_color_for' => (
43 traits => ['Hash'],
44 is => 'ro',
45 isa => 'HashRef[Str]',
46 init_arg => undef,
47 lazy => 1,
48 builder => '_build_color_for',
49 handles => {
50 color_for => 'get',
51 },
52 );
53  
54  
55 # private Graphics::ColorNames object for named colors
56 18 has '_gcn' => (
57 is => 'ro',
58 isa => 'Graphics::ColorNames',
59 init_arg => undef,
60 lazy => 1,
61 builder => '_build_gcn',
62 handles => qr{hex|rgb}xms,
63 );
64  
65  
66 # private hash for indexed colors (e.g., gnuplot colors)
67 18 has '_icol_for' => (
68 traits => ['Hash'],
69 is => 'ro',
70 isa => 'HashRef[Str]',
71 init_arg => undef,
72 lazy => 1,
73 builder => '_build_icol_for',
74 handles => {
75 icol => 'get',
76 icol_for => 'get',
77 all_icols => 'elements',
78 }, # Note: should be index_for but this makes more sense
79 );
80  
81  
82 # private labeler
83 18 has '_labeler' => (
84 is => 'ro',
85 isa => 'Bio::MUST::Core::Taxonomy::Labeler',
86 init_arg => undef,
87 lazy => 1,
88 builder => '_build_labeler',
89 handles => [ qw(classify) ],
90 );
91  
92 ## no critic (ProhibitUnusedPrivateSubroutines)
93  
94 # "magic" name used when a lineage has no colored taxon
95 18 const my $NOCOLOR => '_NOCOLOR_';
96  
97 0 ▶sub _build_color_for {
Subroutine _build_color_for: not called CC=1 SCAR=6.9
98 0 my $self = shift;
99 return {
100 $NOCOLOR => 'black', # default color
101 0 mesh @{ $self->names }, @{ $self->colors } # scheme colors
102 };
103 }
104  
105  
106 0 ▶sub _build_gcn {
Subroutine _build_gcn: not called CC=1 SCAR=6.9
107 0 return Graphics::ColorNames->new('WWW');
108 }
109  
110 0 ▶sub _build_icol_for {
Subroutine _build_icol_for: not called CC=2 SCAR=17.9
111 0 my $self = shift;
112  
113 0 my %icol_for;
114 0 my $index = 0;
115  
116 0 my @colors = uniq $self->all_colors;
117 0 for my $color (@colors) {
118 0 $icol_for{$color} = ++$index;
119 }
120  
121 0 return \%icol_for;
122 }
123  
124 0 ▶sub _build_labeler {
Subroutine _build_labeler: not called CC=1 SCAR=6.9
125 0 my $self = shift;
126 0 return $self->tax->tax_labeler_from_list( $self->names );
127 }
128  
129 ## use critic
130  
131  
132 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
133 0 my $self = shift;
134  
135 # TODO: check that is has any effect at all!
136 0 ▶ carp '[BMC] Warning: name and color list sizes differ!'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_names == $self->count_colors 0 0 0
137 unless $self->count_names == $self->count_colors;
138 carp '[BMC] Warning: non unique names!'
139 0 ▶ unless $self->count_names == uniq @{ $self->names };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_names == &uniq(@{$self->names;}) 0 0 0
140  
141 0 return;
142 }
143  
144  
145 0 ▶around qw(hex rgb icol) => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
146 0 my $method = shift;
147 0 my $self = shift;
148 0 my $seq_id = shift;
149  
150 # intercept delegated method calls and translate color names on the fly
151  
152 # consider input as a SeqId object (or NCBI lineage)
153 # ... and select the lowest taxon to which a color is associated
154 # if a lineage has no colored taxon then it will be black
155 0 ▶ my $label = $self->classify($seq_id) // $NOCOLOR;
Condition0%0/3
$self->classify($seq_id) // $NOCOLOR
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->classify($seq_id) // $NOCOLOR
$self->classify($seq_id) $NOCOLOR
Truth table0%0/3
A // B
A= $self->classify($seq_id)B= $NOCOLOR
ABresult
1X1
011
000
156  
157 # return color (possibly doubly translated)
158 # ... e.g., taxon => color-name => color-hex-code
159 0 my $color = $self->$method( $self->color_for($label), @_ );
160 0 ▶ return wantarray ? ($color, $label) : $color;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
161 18 };
162  
163  
164  
165 0 ▶sub attach_colors_to_entities {
Subroutine attach_colors_to_entities: not called CC=4 SCAR=30.0
Pod undocumented
166 0 my $self = shift;
167 0 my $tree = shift;
168 0 ▶ my $key = shift // 'taxonomy';
Condition0%0/2
shift() // "taxonomy"
l!l
00
MC/DC0%0/1
shift() // "taxonomy"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
169  
170 0 for my $node ( @{ $tree->tree->get_entities } ) {
171 0 my ($color, $label) = $self->hex( $node->get_generic($key), '#' );
172 0 $node->set_generic( '!color' => $color );
173 0 ▶ $node->set_generic( taxon_label => $label ) unless $label eq $NOCOLOR;
Branch: 1 branch
BranchTrueFalseTotal
unless $label eq $NOCOLOR 0 0 0
174 }
175  
176 0 return;
177 }
178  
179 # class methods
180  
181  
182 # TODO: implement auto spectrum methods (based on name list?)
183  
184 # sub spectrum {
185 # my $class = shift;
186 # my $steps = shift;
187 #
188 # my $spectrum = Color::Spectrum::Multi->new();
189 # my @colors = $spectrum->generate($steps, '#FF0000', '#00FF00', '#0000FF');
190 #
191 # }
192  
193  
194 # I/O methods
195  
196  
197 0 ▶sub load {
Subroutine load: not called CC=4 SCAR=30.0
Pod documented
198 0 my $self = shift;
199 0 my $infile = shift;
200  
201 0 open my $in, '<', $infile;
202  
203 LINE:
204 0 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 0 0 0
205 0 chomp $line;
206  
207 # skip empty lines and comment lines
208 0 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::Taxonomy::ColorScheme::EMPTY_LINE/ or $self->is_comment($line) 0 0 0
Condition0%0/3
$line =~ /$Bio::MUST::Core::Taxonomy::ColorScheme::EMPTY_LINE/ or $self->is_comment($line)
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /$Bio::MUST::Core::Taxonomy::ColorScheme::EMPTY_LINE/ or $self->is_comment($line)
$line =~ /$Bio::MUST::Core::Taxonomy::ColorScheme::EMPTY_LINE/ $self->is_comment($line)
Truth table0%0/3
A or B
A= $line =~ /$Bio::MUST::Core::Taxonomy::ColorScheme::EMPTY_LINE/B= $self->is_comment($line)
ABresult
1X1
011
000
209 || $self->is_comment($line);
210  
211 # extract name and color
212 0 my ($name, $color) = split /\t/xms, $line;
213 0 $self->add_name( $name );
214 0 $self->add_color($color);
215 }
216  
217 0 return $self;
218 }
219  
220  
221  
222 0 ▶sub store {
Subroutine store: not called CC=2 SCAR=17.9
Pod documented
223 0 my $self = shift;
224 0 my $outfile = shift;
225  
226 0 open my $out, '>', $outfile;
227  
228 # note the use of a twin array iterator
229 0 print {$out} $self->header;
230 0 my $ea = each_array @{ $self->names }, @{ $self->colors };
231 0 ▶ while (my ($name, $color) = $ea->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 0 0 0
232 0 say {$out} join "\t", $name, $color;
233 }
234  
235 0 close $out;
236  
237 0 return;
238 }
239  
240 18 __PACKAGE__->meta->make_immutable;
241 18 1;
242  
243 __END__
« script/ali2phylip.pl ↑ Summary lib/Bio/MUST/Core/Taxonomy/Filter.pm »