| 1 | # BioPerl module for Bio::Community::Meta::Gamma | ||||||||||||||
| 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::Meta::Gamma - Calculate the gamma diversity of a metacommunity | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | use Bio::Community::Meta::Gamma; | ||||||||||||||
| 17 | |||||||||||||||
| 18 | my $gamma = Bio::Community::Meta::Gamma->new( -community => $community, | ||||||||||||||
| 19 | -type => 'richness' ); | ||||||||||||||
| 20 | my $richness = $gamma->get_gamma; | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||
| 23 | |||||||||||||||
| 24 | The Bio::Community::Meta::Gamma module calculates the gamma diversity of a group | ||||||||||||||
| 25 | of communities (provided as a metacommunity object). Higer gamma diversity values | ||||||||||||||
| 26 | indicate more diverse metacommunities. | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =head1 METRICS | ||||||||||||||
| 29 | |||||||||||||||
| 30 | This module supports the same diversity metrics provided in | ||||||||||||||
| 31 | L<Bio::Community::Alpha>. In addition, you can use: | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =over | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =item chao2 | ||||||||||||||
| 36 | |||||||||||||||
| 37 | Bias-corrected chao2 estimator, which is based on the number of members present | ||||||||||||||
| 38 | in exactly 1 and 2 samples. | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =item jack1_i | ||||||||||||||
| 41 | |||||||||||||||
| 42 | First-order jackknife estimator for incidence data. | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =item jack2_i | ||||||||||||||
| 45 | |||||||||||||||
| 46 | Second-order jackknife estimator for incidence data. | ||||||||||||||
| 47 | |||||||||||||||
| 48 | =item ice | ||||||||||||||
| 49 | |||||||||||||||
| 50 | Incidence-based Coverage Estimator (ICE). | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =back | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head1 AUTHOR | ||||||||||||||
| 55 | |||||||||||||||
| 56 | Florent Angly L<florent.angly@gmail.com> | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =head1 SUPPORT AND BUGS | ||||||||||||||
| 59 | |||||||||||||||
| 60 | User feedback is an integral part of the evolution of this and other Bioperl | ||||||||||||||
| 61 | modules. Please direct usage questions or support issues to the mailing list, | ||||||||||||||
| 62 | L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many | ||||||||||||||
| 63 | experienced and reponsive experts will be able look at the problem and quickly | ||||||||||||||
| 64 | address it. Please include a thorough description of the problem with code and | ||||||||||||||
| 65 | data examples if at all possible. | ||||||||||||||
| 66 | |||||||||||||||
| 67 | If you have found a bug, please report it on the BioPerl bug tracking system | ||||||||||||||
| 68 | to help us keep track the bugs and their resolution: | ||||||||||||||
| 69 | L<https://redmine.open-bio.org/projects/bioperl/> | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =head1 COPYRIGHT | ||||||||||||||
| 72 | |||||||||||||||
| 73 | Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com> | ||||||||||||||
| 74 | |||||||||||||||
| 75 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 76 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||||||||||||||
| 77 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =head1 APPENDIX | ||||||||||||||
| 80 | |||||||||||||||
| 81 | The rest of the documentation details each of the object | ||||||||||||||
| 82 | methods. Internal methods are usually preceded with a _ | ||||||||||||||
| 83 | |||||||||||||||
| 84 | =head2 new | ||||||||||||||
| 85 | |||||||||||||||
| 86 | Function: Create a new Bio::Community::Meta::Gamma object | ||||||||||||||
| 87 | Usage : my $gamma = Bio::Community::Meta::Gamma->new( ... ); | ||||||||||||||
| 88 | Args : -metacommunity : See metacommunity(). | ||||||||||||||
| 89 | -type : See type(). | ||||||||||||||
| 90 | Returns : a new Bio::Community::Meta::Gamma object | ||||||||||||||
| 91 | |||||||||||||||
| 92 | =cut | ||||||||||||||
| 93 | |||||||||||||||
| 94 | |||||||||||||||
| 95 | package Bio::Community::Meta::Gamma; | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 98 | 1 | ▶ | use MooseX::NonMoose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | 1 | ▶ | use MooseX::StrictConstructor; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 100 | 1 | ▶ | use Method::Signatures; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 102 | 1 | ▶ | use List::Util qw(max); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 103 | 1 | ▶ | use Bio::Community::Alpha; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 104 | |||||||||||||||
| 105 | 1 | extends 'Bio::Root::Root'; | |||||||||||||
| 106 | |||||||||||||||
| 107 | |||||||||||||||
| 108 | =head2 metacommunity | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Function: Get or set the communities to process, given as a metacommunity. | ||||||||||||||
| 111 | Usage : my $meta = $gamma->metacommunity; | ||||||||||||||
| 112 | Args : A Bio::Community::Meta object | ||||||||||||||
| 113 | Returns : A Bio::Community::Meta object | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =cut | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 1 | has metacommunity => ( | |||||||||||||
| 118 | is => 'ro', | ||||||||||||||
| 119 | isa => 'Bio::Community::Meta', | ||||||||||||||
| 120 | required => 1, | ||||||||||||||
| 121 | lazy => 0, | ||||||||||||||
| 122 | init_arg => '-metacommunity', | ||||||||||||||
| 123 | ); | ||||||||||||||
| 124 | |||||||||||||||
| 125 | |||||||||||||||
| 126 | =head2 type | ||||||||||||||
| 127 | |||||||||||||||
| 128 | Function: Get or set the type of gamma diversity metric to measure. | ||||||||||||||
| 129 | Usage : my $type = $gamma->type; | ||||||||||||||
| 130 | Args : String for the desired type of gamma diversity ('observed' by | ||||||||||||||
| 131 | default). See L</METRICS> for details. | ||||||||||||||
| 132 | Returns : String for the desired type of gamma diversity | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =cut | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 1 | has type => ( | |||||||||||||
| 137 | is => 'rw', | ||||||||||||||
| 138 | isa => 'GammaType', | ||||||||||||||
| 139 | required => 0, | ||||||||||||||
| 140 | lazy => 1, | ||||||||||||||
| 141 | default => 'observed', | ||||||||||||||
| 142 | init_arg => '-type', | ||||||||||||||
| 143 | ); | ||||||||||||||
| 144 | |||||||||||||||
| 145 | |||||||||||||||
| 146 | =head2 get_gamma | ||||||||||||||
| 147 | |||||||||||||||
| 148 | Function: Calculate the gamma diversity of a community. | ||||||||||||||
| 149 | Usage : my $metric = $gamma->get_gamma; | ||||||||||||||
| 150 | Args : None | ||||||||||||||
| 151 | Returns : A number for the gamma diversity measurement | ||||||||||||||
| 152 | |||||||||||||||
| 153 | =cut | ||||||||||||||
| 154 | |||||||||||||||
| 155 | 1 | ▶ | method get_gamma () { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
get_gamma: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||
| 156 | 10 | my $gamma; | |||||||||||||
| 157 | 10 | my $meta = $self->metacommunity; | |||||||||||||
| 158 | 10 | my $metric = '_'.$self->type; | |||||||||||||
| 159 | 10 | ▶ | if ($self->can($metric)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 4 | $gamma = $self->$metric($meta); | |||||||||||||
| 161 | } else { | ||||||||||||||
| 162 | 6 | my $alpha = Bio::Community::Alpha->new( | |||||||||||||
| 163 | -community => $meta->get_metacommunity, | ||||||||||||||
| 164 | -type => $self->type, | ||||||||||||||
| 165 | ); | ||||||||||||||
| 166 | 6 | $gamma = $alpha->get_alpha; | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | 10 | return $gamma; | |||||||||||||
| 169 | }; | ||||||||||||||
| 170 | |||||||||||||||
| 171 | |||||||||||||||
| 172 | 1 | ▶ | method _chao2 ($meta) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_chao2: called CC=3 SCAR=11.0
Branch: 2 branches
| |||||||||||||||
| 173 | # Calculate Chao's bias-corrected chao2 richness | ||||||||||||||
| 174 | # We use the bias-corrected version because it is always defined, even if | ||||||||||||||
| 175 | # there are no doubletons, contrary to the non-bias corrected version | ||||||||||||||
| 176 | # http://chao.stat.nthu.edu.tw/software/SPADE/SPADE_UserGuide.pdf page 18 | ||||||||||||||
| 177 | 1 | my $richness = scalar @{$meta->get_all_members}; | |||||||||||||
| 178 | 1 | my $m = scalar @{$meta->get_all_communities}; | |||||||||||||
| 179 | 1 | my ($q1, $q2) = $self->__calc_xtons($meta); | |||||||||||||
| 180 | 1 | return $richness + ($m-1) * $q1 * ($q1-1) / (2 * $m * ($q2+1)); | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | |||||||||||||||
| 183 | |||||||||||||||
| 184 | 1 | ▶ | method __calc_xtons ($meta) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
__calc_xtons: called CC=9 SCAR=22.2
Branch: 2 branches
| |||||||||||||||
| 185 | # Return the #spp. present in exactly 1 and 2 communities, respectively | ||||||||||||||
| 186 | 3 | my ($q1, $q2) = (0, 0); | |||||||||||||
| 187 | 3 | my $communities = $meta->get_all_communities; | |||||||||||||
| 188 | 3 | for my $member (@{$meta->get_all_members}) { | |||||||||||||
| 189 | 9 | my $k = 0; | |||||||||||||
| 190 | 9 | for my $community (@$communities) { | |||||||||||||
| 191 | 18 | ▶ | $k++ if $community->get_rel_ab($member); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 192 | 18 | ▶ | last if $k > 2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 193 | } | ||||||||||||||
| 194 | 9 | ▶ | if ($k == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 195 | 6 | $q1++; | |||||||||||||
| 196 | } elsif ($k == 2) { | ||||||||||||||
| 197 | 3 | $q2++; | |||||||||||||
| 198 | } | ||||||||||||||
| 199 | } | ||||||||||||||
| 200 | 3 | return $q1, $q2; | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | |||||||||||||||
| 203 | |||||||||||||||
| 204 | 1 | ▶ | method _jack1_i ($meta) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_jack1_i: called CC=3 SCAR=11.0
Branch: 2 branches
| |||||||||||||||
| 205 | # Calculate the first-order jackknife estimator for incidence data | ||||||||||||||
| 206 | # http://www.uvm.edu/~ngotelli/manuscriptpdfs/Chapter%204.pdf page 41 | ||||||||||||||
| 207 | 1 | my $richness = scalar @{$meta->get_all_members}; | |||||||||||||
| 208 | 1 | my $m = scalar @{$meta->get_all_communities}; | |||||||||||||
| 209 | 1 | my ($q1, $q2) = $self->__calc_xtons($meta); | |||||||||||||
| 210 | 1 | return $richness + $q1 * ($m-1) / $m; | |||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | |||||||||||||||
| 214 | 1 | ▶ | method _jack2_i ($meta) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_jack2_i: called CC=3 SCAR=11.0
Branch: 2 branches
| |||||||||||||||
| 215 | # Calculate the second-order jackknife estimator for incidence data | ||||||||||||||
| 216 | # http://www.uvm.edu/~ngotelli/manuscriptpdfs/Chapter%204.pdf page 41 | ||||||||||||||
| 217 | 1 | my $richness = scalar @{$meta->get_all_members}; | |||||||||||||
| 218 | 1 | my $m = scalar @{$meta->get_all_communities}; | |||||||||||||
| 219 | 1 | my ($q1, $q2) = $self->__calc_xtons($meta); | |||||||||||||
| 220 | 1 | return $richness + $q1 * (2*$m-3) / $m - $q2 * ($m-2)**2 / ($m * ($m-1)); | |||||||||||||
| 221 | } | ||||||||||||||
| 222 | |||||||||||||||
| 223 | |||||||||||||||
| 224 | 1 | ▶ | method _ice ($meta) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_ice: called CC=11 SCAR=24.2
Branch: 2 branches
| |||||||||||||||
| 225 | # Calculate the incidence-based coverage estimator (ICE) | ||||||||||||||
| 226 | # http://www.uvm.edu/~ngotelli/manuscriptpdfs/Chapter%204.pdf page 40 | ||||||||||||||
| 227 | 1 | my $d = 0; | |||||||||||||
| 228 | 1 | my $thresh = 10; | |||||||||||||
| 229 | 1 | my @q = (0) x $thresh; # number of species in only one, two, ... 10 communities | |||||||||||||
| 230 | 1 | my ($s_inf, $s_freq) = (0, 0); # number of infrequent and frequent (>10 samples) spp | |||||||||||||
| 231 | 1 | my $n_inf = 0; | |||||||||||||
| 232 | 1 | my $communities = $meta->get_all_communities; | |||||||||||||
| 233 | 1 | my $m_inf; # communities with at least one infrequent species | |||||||||||||
| 234 | 1 | MEMBER: for my $member (@{$meta->get_all_members}) { | |||||||||||||
| 235 | 3 | my $k = 0; | |||||||||||||
| 236 | 3 | my $abs = []; | |||||||||||||
| 237 | 3 | for my $community (@$communities) { | |||||||||||||
| 238 | 6 | my $ab = $community->get_rel_ab($member); | |||||||||||||
| 239 | 6 | push @$abs, $ab; | |||||||||||||
| 240 | 6 | ▶ | if ($ab) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 241 | 4 | $k++; | |||||||||||||
| 242 | } | ||||||||||||||
| 243 | 6 | ▶ | if ($k > 10) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 244 | 0 | $s_freq++; | |||||||||||||
| 245 | 0 | next MEMBER; | |||||||||||||
| 246 | } | ||||||||||||||
| 247 | } | ||||||||||||||
| 248 | 3 | $q[$k-1]++; | |||||||||||||
| 249 | 3 | $s_inf++; | |||||||||||||
| 250 | 3 | $n_inf += $k; | |||||||||||||
| 251 | # Keep track of communities with infrequent species | ||||||||||||||
| 252 | 3 | for my $i (0 .. scalar @$communities - 1) { | |||||||||||||
| 253 | 6 | my $community = $communities->[$i]; | |||||||||||||
| 254 | 6 | my $ab = $abs->[$i]; | |||||||||||||
| 255 | 6 | ▶ | if ($ab) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 256 | 4 | $m_inf->{$community} = 1; | |||||||||||||
| 257 | } | ||||||||||||||
| 258 | } | ||||||||||||||
| 259 | } | ||||||||||||||
| 260 | 1 | $m_inf = scalar keys %{$m_inf}; # number of samples with >=1 infrequent spp | |||||||||||||
| 261 | 1 | ▶ | if ($n_inf == $q[0]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 262 | # ICE is not defined when all infrequent species are uniques | ||||||||||||||
| 263 | # Fall back to chao2 (advised by Anne Chao, implemented in EstimateS) | ||||||||||||||
| 264 | 0 | $d = $self->_chao2(); | |||||||||||||
| 265 | } else { | ||||||||||||||
| 266 | 1 | my $tmp_sum = 0; | |||||||||||||
| 267 | 1 | for my $k (2 .. $thresh) { | |||||||||||||
| 268 | 9 | $tmp_sum += $k * ($k-1) * $q[$k-1]; | |||||||||||||
| 269 | } | ||||||||||||||
| 270 | 1 | my $C = 1 - $q[0] / $n_inf; | |||||||||||||
| 271 | 1 | my $gamma = ($s_inf * $m_inf * $tmp_sum) / ($C * ($m_inf-1) * $n_inf**2) - 1; | |||||||||||||
| 272 | 1 | $gamma = max($gamma, 0); | |||||||||||||
| 273 | 1 | $d = $s_freq + $s_inf/$C + $q[0]*$gamma/$C; | |||||||||||||
| 274 | } | ||||||||||||||
| 275 | 1 | return $d; | |||||||||||||
| 276 | } | ||||||||||||||
| 277 | |||||||||||||||
| 278 | |||||||||||||||
| 279 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 280 | |||||||||||||||
| 281 | 1 | 1; | |||||||||||||