blib/lib/Bio/Gonzales/Phylo/Dendroscope.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 127 CC 8 SCAR 33.6
CC
8
Cov
0%
_update_id_background
72.0
_mean
2.0
color_by_groups
2.0
CRAP 28.7
1 #Copyright (c) 2010 Joachim Bargsten <code at bargsten dot org>. All rights reserved.
2  
3 package Bio::Gonzales::Phylo::Dendroscope;
4  
5 use Mouse;
6  
7 use warnings;
8 use strict;
9 use Carp;
10  
11 use 5.010;
12 use Color::Spectrum qw/generate/;
13 use Data::Dumper;
14 use List::MoreUtils qw/any/;
15 use List::Util qw/sum/;
16 use Bio::Gonzales::Util::Graphics::Color::Generator;
17  
18 our $VERSION = '0.058'; # VERSION
19  
20 has file => ( is => 'rw', required => 1 );
21  
22 sub _mean {
23 return sum(@_) / @_;
24 }
25  
26 =head1 NAME
27  
28 Bio::Gonzales::Phylo::Dendroscope - Color phylogenetic trees in Dendroscope format
29  
30 =head1 SYNOPSIS
31  
32 use Bio::Gonzales::Phylo::Dendroscope;
33  
34 my $file = "/path/to/dendroscope.tree";
35 my $v->Bio::Gonzales::Phylo::Dendroscope(file => $file);
36
37 $v->color_by_groups( $newfile, [ [ node_label1, node_label3, node_label5], [node_label2, node_label22, node_label51] ]);
38  
39 =head1 DESCRIPTION
40
41 Colors node labels of phylogenetic trees in Dendroscope file format
42  
43 =head1 METHODS
44  
45 =head2 $v->color_by_groups($newfile, $groups)
46  
47 Colors the labels given in $groups and saves the resulting tree in $newfile.
48 See L<SYNOPSIS> for structure of $groups.
49  
50 =cut
51  
52 sub color_by_groups {
53 my ( $self, $to_file, $id_groups ) = @_;
54  
55 my @colors = $self->_create_distinct_colors( scalar @{$id_groups} );
56  
57 say STDERR "using colors: " . Dumper \@colors;
58  
59 my %id_color_map;
60 for my $id_group ( @{$id_groups} ) {
61 my $color = shift @colors;
62  
63 %id_color_map = ( %id_color_map, map { $_ => $color } @{$id_group} );
64 }
65 $self->_update_id_background( $to_file, \%id_color_map );
66 }
67  
68 sub _update_id_background {
69 my ( $self, $file, $id_color_map ) = @_;
70  
71 open my $phy_fh, '<', $self->file or croak "Can't open filehandle: $!";
72 open my $new_phy_fh, '>', $file or croak "Can't open filehandle: $!";
73  
74 my $node_section;
75 while (<$phy_fh>) {
76  
77 $node_section = 0 if (/^edges$/);
78  
79 my $color = 'null';
80 my $textcolor = '0 0 0';
81 if (
82 $node_section && # are we in a node section?
83 /lb='([^']+)'/ && # find node id in dendroscope file
84 any { /^\Q$1\E/ || $1 =~ /\Q$_\E/ } keys %{$id_color_map}
85 # check for matches, firstly does a dendroscope id match the beginning of a
86 # group id or secondly is a group id part of a dendroscope id?
87 )
88 {
89 my @ids = grep { $1 =~ /\Q$_\E/ || /^\Q$1\E/ } keys %{$id_color_map};
90 say STDERR "Found matching group: $ids[0] -- $1";
91 die "ids ambigous $1:" . join( "//", @ids ) if ( @ids != 1 );
92 $color = $id_color_map->{ $ids[0] };
93  
94 if ( _mean( split /\s+/, $color ) > 127 ) {
95 $textcolor = '0 0 0';
96 } else {
97 $textcolor = '255 255 255';
98 }
99  
100 say STDERR "$ids[0] -- $color -- $textcolor";
101 }
102  
103 #substitute foreground/text color
104 if (/lc=(\d+\s+){3}/) {
105 s/lc=(\d+\s+){3}/lc=$textcolor /;
106 } else {
107 s/^(\d+:.*\s+)(lb=)/$1lc=$textcolor $2/;
108 }
109  
110 #substitute background color
111 if (/lk=(\d+\s+){3}/) {
112 s/lk=(\d+\s+){3}/lk=$color /;
113 } else {
114 s/^(\d+:.*\s+)(lb=)/$1lk=$color $2/;
115 }
116  
117 $node_section = 1 if (/^nodes$/);
118  
119 print $new_phy_fh $_;
120 }
121 }
122  
123 sub _create_distinct_colors {
124 my ( $self, $num_colors ) = @_;
125  
126 my $t = Bio::Gonzales::Util::Graphics::Color::Generator->new;
127 my @colors = $t->generate_as_string($num_colors);
128 return @colors;
129 }
130  
131 1;
132  
133 __END__
134  
135 =head1 SEE ALSO
136  
137 =head1 AUTHOR
138  
139 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
140  
141 =cut