Bio-Community

 view release on metacpan or  search on metacpan

bin/bc_summarize  view on Meta::CPAN

=item -op <output_prefix> | -output_prefix <output_prefix>

Path and prefix for the output files. Default: output_prefix.default

=for Euclid:
   output_prefix.type: string
   output_prefix.default: 'bc_summarize'

=item -cr <convert_relab> | -convert_relab <convert_relab>

Convert counts into relative abundances (in percentage, taking into account
weights): 1 (yes), 0 (no). Default: convert_relab.default

=for Euclid:
   convert_relab.type: integer, convert_relab == 0 || convert_relab == 1
   convert_relab.type.error: <convert_relab> must be 0 or 1 (not convert_relab)
   convert_relab.default: 1

=item -md <merge_dups> | -merge_dups <merge_dups>

Merge community members with the exact same lineage. Default: merge_dups.default

bin/bc_summarize  view on Meta::CPAN

            $num++;
            $output_file = $output_prefix.'_'.$num;
         }
         if ($type) {
            $output_file .= '_'.$type;
         }
         $output_file .= '.'.$output_format;
         $out = Bio::Community::IO->new(
            -format         => $output_format,
            -file           => '>'.$output_file,
            -abundance_type => $convert2relab ? 'percentage' : 'count',
         );
      }
      print "Writing community '".$community->name."' to file '$output_file'\n";
      $out->write_community($community);
      if (not $multiple_communities) {
         $out->close;
         $out = undef;
      }
   }
   if (defined $out) {

lib/Bio/Community.pm  view on Meta::CPAN

=cut

#method get_count (Bio::Community::Member $member) {
method get_count ($member) {
   return $self->_counts->{$member->id} || 0;
}


=head2 get_rel_ab

 Function: Determine the relative abundance (in percent) of a member in the
           community.
 Usage   : my $rel_ab = $community->get_rel_ab($member);
 Args    : a Bio::Community::Member object
 Returns : an integer between 0 and 100 for the relative abundance of this member

=cut

#method get_rel_ab (Bio::Community::Member $member) {
method get_rel_ab ($member) {
   my $rel_ab = 0;

lib/Bio/Community/IO.pm  view on Meta::CPAN



#method _process_member (Bio::Community::Member $member, Bio::Community $community) {
method _process_member ($member, $community) {
   my $ab_value;
   my $ab_type = $self->abundance_type;
   if ($ab_type eq 'count') {
      $ab_value = $community->get_count($member);
   } elsif ($ab_type eq 'absolute') {
      $ab_value = $community->get_abs_ab($member);
   } elsif ($ab_type eq 'percentage') {
      $ab_value = $community->get_rel_ab($member);
   } elsif ($ab_type eq 'fraction') {
      $ab_value = $community->get_rel_ab($member) / 100;
   } else {
      $self->throw("$ab_value is not a valid abundance type.\n");
   }
   $self->write_member($member, $ab_value);
   return 1;
}

lib/Bio/Community/IO.pm  view on Meta::CPAN

);


=head2 abundance_type

 Usage   : $in->abundance_type();
 Function: When writing a community to a file, report member abundance in one
           of four possible representations:
            * count     : observed count
            * absolute  : absolute abundance
            * percentage: relative abundance, in percent (0-100%)
            * fraction  : relative abundance, as a fractional number (0-1)
           The default is specific to each driver
 Args    : count, absolute, percentage or fraction
 Returns : count, absolute, percentage or fraction

=cut

has 'abundance_type' => (
   is => 'ro',
   isa => 'AbundanceRepr',
   required => 0,
   lazy => 1,
   init_arg => '-abundance_type',
   default => sub { return eval('$'.ref(shift).'::default_abundance_type') || 'percentage' },
);


=head2 missing_string

 Usage   : $in->missing_string();
 Function: When writing a community to a file, specify what abundance string to
           use for members that are not present in the community. The default is
           specific to each driver used.
 Args    : string e.g. '', '0', 'n/a', '-'

lib/Bio/Community/Meta/Beta.pm  view on Meta::CPAN

=head1 DESCRIPTION

The Bio::Community::Beta module quantifies how dissimilar communities are
by calculating their beta diversity. The more different communities are, the
larger their beta diversity. Some beta diversity metrics are proper distance
measures (in the mathematical sense).

Since the relative abundance of community members is not always proportional to
member counts (see weights() in Bio::Community::Member and use_weights() in
Bio::Community), the beta diversity measured here are always based on relative
abundance (as a fractional number between 0 and 1, not as a percentage), even
for beta diversity metrics that are usually based on number of observations
(counts).

=head1 METRICS

Qualitative and quantitive measures of beta diversity are available and can be
specified with the C<type()> method:

=head2 Qualitative

lib/Bio/Community/Meta/Beta.pm  view on Meta::CPAN

relative to the overall richness of the metacommunity.

=item sorensen

The Sørensen dissimilarity, or Whittaker's species turnover (between 0 and 1),
i.e. the fraction of non-shared species relative to the average richness in the
metacommunity.

=item shared

The percentage of species shared (between 0 and 100), relative to the least
rich community. Note: this is the opposite of a beta diversity measure since
the higher the percent of species shared, the smaller the beta diversity.

=back

=head2 Quantitative

=over

=item 1-norm

The 1-norm, or Manhattan distance, i.e. the sum of difference in abundance for all species.

lib/Bio/Community/Meta/Beta.pm  view on Meta::CPAN

between 0 and 1.

=item morisita-horn

The Morisita-Horn dissimilarity, which varies between 0 and 1. Affected
strongly by the abundance of the most abundant species, but not by sample size
or richness.

=item permuted

A beta diversity measure between 0 and 100, representing the percentage of the
dominant species in the first community with a permuted abundance rank in the
second community. As a special case, when no species are shared (and the
percentage permuted is meaningless), undef is returned.

=item maxiphi

A beta diversity measure between 0 and 1, based on the percentage of species
shared and the percentage of top species permuted (that have had a change in
abundance rank).

=back

=head1 AUTHOR

Florent Angly L<florent.angly@gmail.com>

=head1 SUPPORT AND BUGS

lib/Bio/Community/Meta/Beta.pm  view on Meta::CPAN

}


method _permuted ($meta) {
   # Percent of top species with a permuted rank-abundance between 2 communities.
   # The exact number cannot be calculated for certain because the random
   # permutation of x species could generate the same sequence (but it would be
   # extremely unlikely). The best we can do is calculate a minimum bound for
   # the number of species permuted. Do this once for the species of community1
   # once, and then for the members of community2 and return the average of the
   # two.This should be a reasonable approximation of the true percent of
   # species permuted.


   #### should it really be the average or simply relative to the least rich community

   my $min_p1 = $self->_min_permuted($meta);
   my $min_p2 = $self->_min_permuted($meta);
   my $p;
   if ( (defined $min_p1) && defined($min_p2) ) {
      $p = ($min_p1 + $min_p2) / 2;
   }

   return $p;
}


method _min_permuted ($meta) {
   # Estimate the minimum percent of permuted species in community1. Do this by
   # going through members of community2 in increasing abundance rank order and
   # comparing their position to that of the same member in community1 (if
   # shared). If there are no species shared, return undef.

   my $min_permuted;

   my ($community1, $community2) = @{$meta->get_all_communities};

   my $i = 0;
   my $richness = $community2->get_richness;

lib/Bio/Community/Meta/Beta.pm  view on Meta::CPAN

   return $min_permuted;
}


method _maxiphi ($meta) {
   # Given S, the fraction shared, and P, the fraction permuted, calculate the
   # MaxiPhi beta diversity M as:
   #       M = 1 - S*(2-P)/2
   #
   # M ranges from 0 (low beta diversity, similar communities), to 1 (high beta
   # diversity, dissimilar communities). The weight of the percent permuted
   # parameter is proportional to the percent shared. At 0% shared, the fraction
   # permuted has no weight in the index, while at 100% shared, the fraction
   # permuted and fraction shared have the same weight.
   #
   # For example:
   #      for 100 % shared, 0   % permuted -> M = 0
   #      for 100 % shared, 100 % permuted -> M = 0.5
   #      for 0   % shared, 0   % permuted -> M = 1
   #      for 0   % shared, 100 % permuted -> M = 1


lib/Bio/Community/Tools/Sampler.pm  view on Meta::CPAN

   }
   return $randcomm;
}


####
# Implement sampling without replacement:
#    #Use gsl_ran_choose from GSL: https://www.gnu.org/software/gsl/manual/html_node/Shuffling-and-Sampling.html
#    Output == input if count required == count in reference community
#    Throw if count required > count in reference community
#    Throw if reference community has percentages
#    Make an array [Member1, Member1, Member2, Member3, Member3, Member3]
#    Take a random member

# Without replacement should be default. It is beneficial when:
#    sampling close to max count in community

# Sampling with replacement is beneficial when:
#    sampling from percentages (or with weights)
#    sampling beyond observed count in community
#    member count is so high that sampling without replacement would exhaust memory

# But which one is faster / more resource economic?
####


method _get_rand_members ( $total_count = 1 ) {
   # 1/ Get member probabilities (i.e. relative abundances)
   my @P = ();

lib/Bio/Community/Types.pm  view on Meta::CPAN

   => message { _gen_err_msg('an integer greater than two', $_) };


# Sort numerically
subtype 'NumericSort'
   => as enum( [ qw(-1 0 1) ] )
   => message { _gen_err_msg('0 (off), 1 (increasing) or -1 (decreasing)', $_) };


# Abundance representation
my @AbundanceRepr = qw(count absolute percentage fraction);
subtype 'AbundanceRepr'
   => as enum( \@AbundanceRepr )
   => message { _gen_err_msg(\@AbundanceRepr, $_) };


# Rank: a strictly positive integer
subtype 'AbundanceRank'
   => as 'StrictlyPositiveInt';




( run in 0.491 second using v1.01-cache-2.11-cpan-709fd43a63f )