« blib/lib/Bio/Community/Tools/Accumulator.pm
↑ Summary
blib/lib/Bio/Community/Tools/ShrapnelCleaner.pm »
| 1 | # BioPerl module for Bio::Community::Tools::Transformer | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Copyright 2011-2014 Florent Angly <florent.angly@gmail.com> | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # You may distribute this module under the same terms as perl itself | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Bio::Community::Tools::Transformer - Arbitrary transformation of member counts | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use Bio::Community::Tools::Transformer; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Hellinger-transform the counts of community members in a metacommunity | ||||||||||||||||||||||||||||
| 19 | my $transformer = Bio::Community::Tools::Transformer->new( | ||||||||||||||||||||||||||||
| 20 | -metacommunity => $meta, | ||||||||||||||||||||||||||||
| 21 | -type => 'hellinger', | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $transformed_meta = $summarizer->get_transformed_meta; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | This module takes a metacommunity and transform the count of the community | ||||||||||||||||||||||||||||
| 29 | members it contains. Several transformation methods are available: identity, | ||||||||||||||||||||||||||||
| 30 | binary, or hellinger. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Florent Angly L<florent.angly@gmail.com> | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 SUPPORT AND BUGS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | User feedback is an integral part of the evolution of this and other Bioperl | ||||||||||||||||||||||||||||
| 39 | modules. Please direct usage questions or support issues to the mailing list, | ||||||||||||||||||||||||||||
| 40 | L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many | ||||||||||||||||||||||||||||
| 41 | experienced and reponsive experts will be able look at the problem and quickly | ||||||||||||||||||||||||||||
| 42 | address it. Please include a thorough description of the problem with code and | ||||||||||||||||||||||||||||
| 43 | data examples if at all possible. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | If you have found a bug, please report it on the BioPerl bug tracking system | ||||||||||||||||||||||||||||
| 46 | to help us keep track the bugs and their resolution: | ||||||||||||||||||||||||||||
| 47 | L<https://redmine.open-bio.org/projects/bioperl/> | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 54 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||||||||||||||||||||||||||||
| 55 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 APPENDIX | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | The rest of the documentation details each of the object | ||||||||||||||||||||||||||||
| 60 | methods. Internal methods are usually preceded with a _ | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 new | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Function: Create a new Bio::Community::Tool::Transformer object | ||||||||||||||||||||||||||||
| 65 | Usage : my $transformer = Bio::Community::Tool::Transformer->new( ); | ||||||||||||||||||||||||||||
| 66 | Args : -metacommunity: see metacommunity() | ||||||||||||||||||||||||||||
| 67 | -type : see type() | ||||||||||||||||||||||||||||
| 68 | Returns : a new Bio::Community::Tools::Transformer object | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | package Bio::Community::Tools::Transformer; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | use MooseX::NonMoose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | use Method::Signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | use Bio::Community::Meta; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | extends 'Bio::Root::Root'; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 metacommunity | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Function: Get or set the metacommunity to normalize. | ||||||||||||||||||||||||||||
| 90 | Usage : my $meta = $transformer->metacommunity; | ||||||||||||||||||||||||||||
| 91 | Args : A Bio::Community::Meta object | ||||||||||||||||||||||||||||
| 92 | Returns : A Bio::Community::Meta object | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | has metacommunity => ( | |||||||||||||||||||||||||||
| 97 | is => 'rw', | ||||||||||||||||||||||||||||
| 98 | isa => 'Maybe[Bio::Community::Meta]', | ||||||||||||||||||||||||||||
| 99 | required => 0, | ||||||||||||||||||||||||||||
| 100 | default => undef, | ||||||||||||||||||||||||||||
| 101 | lazy => 1, | ||||||||||||||||||||||||||||
| 102 | init_arg => '-metacommunity', | ||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 type | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Function: Get or set the type of transformation that is to be applied to member | ||||||||||||||||||||||||||||
| 109 | counts (not relative abundance): | ||||||||||||||||||||||||||||
| 110 | * identity : Keep the counts as-is | ||||||||||||||||||||||||||||
| 111 | * binary : Assign 1 if member is present, 0 if absent | ||||||||||||||||||||||||||||
| 112 | * relative : Set count of member equal to its relative abundance (%) | ||||||||||||||||||||||||||||
| 113 | * chisquare : Chi-square transformation | ||||||||||||||||||||||||||||
| 114 | * chord : Chord transformation | ||||||||||||||||||||||||||||
| 115 | * hellinger : Hellinger transformation | ||||||||||||||||||||||||||||
| 116 | Usage : my $type = $transformer->type; | ||||||||||||||||||||||||||||
| 117 | Args : identity, binary, relative, chisquare, chord, or hellinger | ||||||||||||||||||||||||||||
| 118 | Returns : identity, binary, relative, chisquare, chord, or hellinger | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | has type => ( | |||||||||||||||||||||||||||
| 123 | is => 'rw', | ||||||||||||||||||||||||||||
| 124 | isa => 'TransformationType', | ||||||||||||||||||||||||||||
| 125 | required => 0, | ||||||||||||||||||||||||||||
| 126 | default => 'identity', | ||||||||||||||||||||||||||||
| 127 | lazy => 1, | ||||||||||||||||||||||||||||
| 128 | init_arg => '-type', | ||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 get_transformed_meta | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Function: Calculate and return a transformed metacommunity. | ||||||||||||||||||||||||||||
| 135 | Usage : my $meta = $transformer->get_transformed_meta; | ||||||||||||||||||||||||||||
| 136 | Args : none | ||||||||||||||||||||||||||||
| 137 | Returns : a new Bio::Community::Meta object | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | has transformed_meta => ( | ||||||||||||||||||||||||||||
| 142 | is => 'rw', | ||||||||||||||||||||||||||||
| 143 | isa => 'Bio::Community::Meta', | ||||||||||||||||||||||||||||
| 144 | required => 0, | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 146 | 1 | lazy => 1, | |||||||||||||||||||||||||||
| 147 | reader => 'get_transformed_meta', | ||||||||||||||||||||||||||||
| 148 | writer => '_set_transformed_meta', | ||||||||||||||||||||||||||||
| 149 | predicate => '_has_transformed_meta', | ||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | before get_transformed_meta => sub { | ||||||||||||||||||||||||||||
| 153 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 154 | 1 | ▶ | $self->_transform if not $self->_has_transformed_meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | return 1; | |||||||||||||||||||||||||||
| 156 | 1 | }; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | method _transform () { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_transform: called CC=13 SCAR=48.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | # Sanity check | ||||||||||||||||||||||||||||
| 161 | 1 | my $meta = $self->metacommunity; | |||||||||||||||||||||||||||
| 162 | 1 | ▶ | if ( (not $meta) || ($meta->get_communities_count == 0) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $meta or $meta->get_communities_count == 0
MC/DC0%0/2
not $meta or $meta->get_communities_count == 0
not $meta
$meta->get_communities_count == 0
Truth table33%1/3
not(A) or B
A= not $metaB= $meta->get_communities_count == 0
| |||||||||||||||||||||||||||||
| 163 | 1 | $self->throw('Should have a metacommunity containing at least one community'); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Register transformation functions | ||||||||||||||||||||||||||||
| 167 | 0 | my ($sub, $pre_sub); | |||||||||||||||||||||||||||
| 168 | 0 | my $type = $self->type; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ($type eq 'identity') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 170 | 0 | $pre_sub = undef; | |||||||||||||||||||||||||||
| 171 | $sub = sub { | ||||||||||||||||||||||||||||
| 172 | # Keep same count | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my ($com, $mem) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 174 | 0 | return $com->get_count($mem); | |||||||||||||||||||||||||||
| 175 | 0 | }; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | elsif ($type eq 'binary') { | ||||||||||||||||||||||||||||
| 178 | 0 | $pre_sub = undef; | |||||||||||||||||||||||||||
| 179 | $sub = sub { | ||||||||||||||||||||||||||||
| 180 | # 1 for presence, 0 otherwise | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | my ($com, $mem) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | return $com->get_count($mem) > 0 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | }; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | elsif ($type eq 'relative') { | ||||||||||||||||||||||||||||
| 186 | 0 | $pre_sub = undef; | |||||||||||||||||||||||||||
| 187 | $sub = sub { | ||||||||||||||||||||||||||||
| 188 | # Use relative abundance | ||||||||||||||||||||||||||||
| 189 | 0 | ▶ | my ($com, $mem) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 190 | 0 | return $com->get_rel_ab($mem); | |||||||||||||||||||||||||||
| 191 | 0 | }; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | elsif ($type eq 'chord') { | ||||||||||||||||||||||||||||
| 194 | $pre_sub = sub { | ||||||||||||||||||||||||||||
| 195 | # Add the squares of count | ||||||||||||||||||||||||||||
| 196 | 0 | ▶ | my ($com, $mem, $sum) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 197 | 0 | return $sum + $com->get_count($mem)**2; | |||||||||||||||||||||||||||
| 198 | 0 | }; | |||||||||||||||||||||||||||
| 199 | $sub = sub { | ||||||||||||||||||||||||||||
| 200 | # Count divided by sum of squares | ||||||||||||||||||||||||||||
| 201 | 0 | ▶ | my ($com, $mem, $sum) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 202 | 0 | return $com->get_count($mem) / $sum; | |||||||||||||||||||||||||||
| 203 | 0 | }; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | elsif ($type eq 'hellinger') { | ||||||||||||||||||||||||||||
| 206 | 0 | $pre_sub = undef; | |||||||||||||||||||||||||||
| 207 | $sub = sub { | ||||||||||||||||||||||||||||
| 208 | # Square root of count | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | my ($com, $mem) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 210 | 0 | return sqrt $com->get_count($mem); | |||||||||||||||||||||||||||
| 211 | 0 | }; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | else { | ||||||||||||||||||||||||||||
| 214 | 0 | $self->throw("Unsupported transformation type '$type'"); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Create new transformed metacommunity | ||||||||||||||||||||||||||||
| 218 | 0 | my $transformed_meta = Bio::Community::Meta->new( -name => $meta->name ); | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | while (my $community = $meta->next_community) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | my $name = $community->name; | |||||||||||||||||||||||||||
| 221 | 0 | my $transformed = Bio::Community->new( | |||||||||||||||||||||||||||
| 222 | -name => $name, | ||||||||||||||||||||||||||||
| 223 | -use_weights => $community->use_weights, | ||||||||||||||||||||||||||||
| 224 | ); | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # Pre-processing (if needed) | ||||||||||||||||||||||||||||
| 227 | 0 | my $pre = 0; | |||||||||||||||||||||||||||
| 228 | 0 | ▶ | if (defined $pre_sub) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | while ( my $member = $community->next_member('_transform') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | $pre = $pre_sub->($community, $member, $pre); | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | # Transform counts | ||||||||||||||||||||||||||||
| 235 | 0 | ▶ | while ( my $member = $community->next_member('_transform') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 0 | my $transf_count = $sub->($community, $member, $pre); | |||||||||||||||||||||||||||
| 237 | 0 | $transformed->add_member($member, $transf_count); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 0 | $transformed_meta->add_communities([$transformed]); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 0 | $self->_set_transformed_meta($transformed_meta); | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | return 1; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 1 | 1; | |||||||||||||||||||||||||||