| 1 | # BioPerl module for Bio::Community::Tools::Sampler | ||||||||||||||||||||||||||||||||||||
| 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::Sampler - Sample organisms according to their abundance | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | use Bio::Community::Tools::Sampler; | ||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | # Sample members from a reference community | ||||||||||||||||||||||||||||||||||||
| 19 | my $sampler = Bio::Community::Tools::Sampler->new( -community => $ref_community ); | ||||||||||||||||||||||||||||||||||||
| 20 | my $member1 = $sampler->get_rand_member(); | ||||||||||||||||||||||||||||||||||||
| 21 | my $member2 = $sampler->get_rand_member(); | ||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | # Or sample 100 members in one step | ||||||||||||||||||||||||||||||||||||
| 24 | my $rand_community = $sampler->get_rand_community( 100 ); | ||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | Pick individuals at random (without replacement) from a community. | ||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||
| 30 | Note that the sampling is done based on relative abundances, and is hence | ||||||||||||||||||||||||||||||||||||
| 31 | affected by weights. If you need to sample based on counts instead, simply set | ||||||||||||||||||||||||||||||||||||
| 32 | $community->use_weights(0), before using Bio::Community::Tools::Sampler. | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | Florent Angly L<florent.angly@gmail.com> | ||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | =head1 SUPPORT AND BUGS | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | User feedback is an integral part of the evolution of this and other Bioperl | ||||||||||||||||||||||||||||||||||||
| 41 | modules. Please direct usage questions or support issues to the mailing list, | ||||||||||||||||||||||||||||||||||||
| 42 | L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many | ||||||||||||||||||||||||||||||||||||
| 43 | experienced and reponsive experts will be able look at the problem and quickly | ||||||||||||||||||||||||||||||||||||
| 44 | address it. Please include a thorough description of the problem with code and | ||||||||||||||||||||||||||||||||||||
| 45 | data examples if at all possible. | ||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | If you have found a bug, please report it on the BioPerl bug tracking system | ||||||||||||||||||||||||||||||||||||
| 48 | to help us keep track the bugs and their resolution: | ||||||||||||||||||||||||||||||||||||
| 49 | L<https://redmine.open-bio.org/projects/bioperl/> | ||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com> | ||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||
| 56 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||||||||||||||||||||||||||||||||||||
| 57 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | =head1 APPENDIX | ||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | The rest of the documentation details each of the object | ||||||||||||||||||||||||||||||||||||
| 62 | methods. Internal methods are usually preceded with a _ | ||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | =head2 new | ||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||
| 66 | Function: Create a new Bio::Community::Tool::Sampler object | ||||||||||||||||||||||||||||||||||||
| 67 | Usage : my $sampler = Bio::Community::Tool::Sampler->new( ); | ||||||||||||||||||||||||||||||||||||
| 68 | Args : -community: See community(). | ||||||||||||||||||||||||||||||||||||
| 69 | -seed : See set_seed(). | ||||||||||||||||||||||||||||||||||||
| 70 | Returns : a new Bio::Community::Tools::Sampler object | ||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | package Bio::Community::Tools::Sampler; | ||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | use MooseX::NonMoose; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | use Method::Signatures; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | use Bio::Community; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||
| 84 | 1 | extends 'Bio::Root::Root'; | |||||||||||||||||||||||||||||||||||
| 85 | 1 | with 'Bio::Community::Role::PRNG'; | |||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | =head2 community | ||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | Function: Get or set the community to sample from. | ||||||||||||||||||||||||||||||||||||
| 91 | Usage : my $community = $sampler->community(); | ||||||||||||||||||||||||||||||||||||
| 92 | Args : a Bio::Community object | ||||||||||||||||||||||||||||||||||||
| 93 | Returns : a Bio::Community object | ||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | 1 | has community => ( | |||||||||||||||||||||||||||||||||||
| 98 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 99 | isa => 'Bio::Community', | ||||||||||||||||||||||||||||||||||||
| 100 | required => 0, | ||||||||||||||||||||||||||||||||||||
| 101 | lazy => 0, | ||||||||||||||||||||||||||||||||||||
| 102 | init_arg => '-community', | ||||||||||||||||||||||||||||||||||||
| 103 | trigger => \&_clear_cdf, | ||||||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | has _cdf => ( | ||||||||||||||||||||||||||||||||||||
| 108 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 109 | isa => 'ArrayRef', # ArrayRef[PositiveNum] | ||||||||||||||||||||||||||||||||||||
| 110 | lazy => 1, | ||||||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | default => sub { shift->_init_cdf( ) }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 112 | 1 | init_arg => undef, | |||||||||||||||||||||||||||||||||||
| 113 | clearer => '_clear_cdf', | ||||||||||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | has _members => ( | ||||||||||||||||||||||||||||||||||||
| 118 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 119 | isa => 'ArrayRef', # ArrayRef[Bio::Community::Member] | ||||||||||||||||||||||||||||||||||||
| 120 | lazy => 1, | ||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | default => sub{ [] }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 122 | 1 | init_arg => undef, | |||||||||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | =head2 get_seed, set_seed | ||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | Usage : $sampler->set_seed(1234513451); | ||||||||||||||||||||||||||||||||||||
| 129 | Function: Get or set the seed used to pick the random members. | ||||||||||||||||||||||||||||||||||||
| 130 | Args : Positive integer | ||||||||||||||||||||||||||||||||||||
| 131 | Returns : Positive integer | ||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | =head2 get_rand_member | ||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | Function: Get a random member from a community (sample with replacement). | ||||||||||||||||||||||||||||||||||||
| 139 | Usage : my $member = $sampler->get_rand_member(); | ||||||||||||||||||||||||||||||||||||
| 140 | Args : None | ||||||||||||||||||||||||||||||||||||
| 141 | Returns : A Bio::Community::Member object | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | method get_rand_member () { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
get_rand_member: called CC=3 SCAR=15.3
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 146 | # Pick a random member based on the community's cdf | ||||||||||||||||||||||||||||||||||||
| 147 | 1 | my $rand_pick = $self->rand(); | |||||||||||||||||||||||||||||||||||
| 148 | 1 | my $cdf = $self->_cdf; | |||||||||||||||||||||||||||||||||||
| 149 | 0 | my $index = 0; | |||||||||||||||||||||||||||||||||||
| 150 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | last if $rand_pick < $cdf->[$index]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 152 | 0 | $index++; | |||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||
| 154 | 0 | return ${$self->_members}[$index]; | |||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||
| 158 | =head2 get_rand_community | ||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||
| 160 | Function: Create a community from random members of a community | ||||||||||||||||||||||||||||||||||||
| 161 | Usage : my $community = $sampler->get_rand_community(1000); | ||||||||||||||||||||||||||||||||||||
| 162 | Args : Number of members (positive integer) | ||||||||||||||||||||||||||||||||||||
| 163 | Returns : A Bio::Community object | ||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||
| 167 | 1 | ▶ | method get_rand_community ( PositiveInt $total_count = 1 ) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=5.0
Subroutine
get_rand_community: not called CC=7 SCAR=38.4
Branch: 3 branches
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"PositiveInt"} ||= "Method::Signatures"->_make_constraint("PositiveInt")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"PositiveInt"} ||= "Method::Signatures"->_make_constraint("PositiveInt")
$Method::Signatures::mutc{"cache"}{"PositiveInt"}
"Method::Signatures"->_make_constraint("PositiveInt")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"PositiveInt"}B= "Method::Signatures"->_make_constraint("PositiveInt")
| |||||||||||||||||||||||||||||||||||||
| 168 | # Adding random members 1 by 1 in a communty is slow. Generate all the members | ||||||||||||||||||||||||||||||||||||
| 169 | # first. Then add them all at once to a community. | ||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | # 1/ Generate all random members | ||||||||||||||||||||||||||||||||||||
| 172 | 0 | my $members = {}; | |||||||||||||||||||||||||||||||||||
| 173 | 0 | my $counts = {}; | |||||||||||||||||||||||||||||||||||
| 174 | 0 | for (1 .. $total_count) { | |||||||||||||||||||||||||||||||||||
| 175 | 0 | my $member = $self->get_rand_member; | |||||||||||||||||||||||||||||||||||
| 176 | 0 | my $id = $member->id; | |||||||||||||||||||||||||||||||||||
| 177 | 0 | $members->{$id} = $member; | |||||||||||||||||||||||||||||||||||
| 178 | 0 | $counts->{$id}++; | |||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | # 2/ Add all members in a community object | ||||||||||||||||||||||||||||||||||||
| 182 | 0 | my $community = Bio::Community->new(); | |||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | while (my ($id, $member) = each %$members) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 184 | 0 | my $count = $counts->{$id}; | |||||||||||||||||||||||||||||||||||
| 185 | 0 | $community->add_member( $member, $count ); | |||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | 0 | return $community; | |||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||
| 192 | 1 | ▶ | method _init_cdf () { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_init_cdf: called CC=5 SCAR=26.3
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 193 | # Sort the members of the community by decreasing rank and calculate the | ||||||||||||||||||||||||||||||||||||
| 194 | # cumulative density function of their relative abundance. Store the | ||||||||||||||||||||||||||||||||||||
| 195 | # resulting CDF and members; | ||||||||||||||||||||||||||||||||||||
| 196 | 1 | ▶ | my $community = $self->community || $self->throw('No community was provided'); | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->community || $self->throw('No community was provided')
MC/DC0%0/2
$self->community || $self->throw('No community was provided')
$self->community
$self->throw('No community was provided')
Truth table33%1/3
A || B
A= $self->communityB= $self->throw('No community was provided')
| |||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||
| 198 | 0 | my @cdf; | |||||||||||||||||||||||||||||||||||
| 199 | 0 | my @members = (); | |||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | while ( my $member = $community->next_member('_calc_cdf_ite') ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 201 | 0 | my $rank = $community->get_rank($member); | |||||||||||||||||||||||||||||||||||
| 202 | 0 | $members[$rank-1] = $member; | |||||||||||||||||||||||||||||||||||
| 203 | 0 | my $rel_ab = $community->get_rel_ab($member); | |||||||||||||||||||||||||||||||||||
| 204 | 0 | $cdf[$rank-1] = $rel_ab / 100; | |||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||
| 207 | 0 | for my $i ( 1 .. scalar @cdf - 1 ) { | |||||||||||||||||||||||||||||||||||
| 208 | 0 | $cdf[$i] += $cdf[$i-1]; | |||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||
| 211 | 0 | $self->_cdf( \@cdf ); | |||||||||||||||||||||||||||||||||||
| 212 | 0 | $self->_members( \@members ); | |||||||||||||||||||||||||||||||||||
| 213 | 0 | return \@cdf; | |||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||
| 218 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||
| 220 | 1 | 1; | |||||||||||||||||||||||||||||||||||