Bio-Community

 view release on metacpan or  search on metacpan

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

use Method::Signatures;
use Bio::Community::Types;

extends 'Bio::Root::Root';

with 'Bio::Community::Role::Described' , # -desc
     'Bio::Community::Role::Classified', # -taxon
     'Bio::Community::Role::Sequenced' , # -seqs
     'Bio::Community::Role::Weighted'  ; # -weights


use constant PREFIX => 'bc';
my $max_num = 0;
# First ID is 'bc1'

my $id_re = '^'.PREFIX.'(\d+)$';
$id_re = qr/$id_re/;

my $mod_re = qr/^Bio::Community/;


=head2 id

 Function: my $description = $member->id();
 Usage   : Get or set the ID for the member. If an ID is not provided, a unique
           ID prefixed with 'bc' is generated, e.g. 'bc1', 'bc2', etc. This
           makes it easy to distinguish IDs assigned by Bio::Community::Member
           from IDs obtained from other sources, e.g. Greengenes taxa ID or
           QIIME arbitrary OTU ID. Use of the 'bc' prefix is restricted to the
           L<Bio::Community::Member> module; please refrain from using it yourself.
 Args    : A string
 Returns : A string

=cut

has id => (
   is => 'rw',
   isa => 'Str',
   required => 0,
   init_arg => '-id',
   lazy => 0,
   predicate => '_has_id',
   trigger => \&_auto_id, # only when setting the ID
);


method BUILD ($args) {
   # Ensure that a default ID is assigned if needed after object construction
   $self->_auto_id( $self->id );
}


method _auto_id ($id?, $old_id?) {
   # Validate given ID or assign a new ID automatically
   if (not defined $id) {
      # Assign a new ID
      #$self->id( PREFIX.$max_num++ ); # warns because of call to _register_id
      $self->{id} = PREFIX.++$max_num
   } else {
      # Validate ID
      if ( ($id =~ $id_re) && ((caller(0))[0] !~ $mod_re) && ((caller(1))[0] !~ $mod_re) ) {
         # Check validity of 'bcXX' IDs not requested by Bio::Community* modules
         my $num = $1;
         if ($num > $max_num) {
            $max_num = $num;
         } else {
            $self->warn("Request to assign ID $id to member but we are at ID ".
               PREFIX."$max_num already. ID might not be unique!");
         }
      }
   }
   return 1;
}


=head2 desc

 Usage   : my $description = $member->desc();
 Function: Get or set a description for this object.
           See L<Bio::Community::Role::Described>.
 Args    : A string
 Returns : A string

=head2 taxon

 Usage   : my $taxon = $member->taxon();
 Function: Get or set a taxon (or species) for this object.
           See L<Bio::Community::Role::Classified>.
 Args    : A Bio::Taxon object
 Returns : A Bio::Taxon object

=head2 seqs

 Usage   : my $seqs = $member->seqs();
 Function: Get or set some sequences for this object.
           See L<Bio::Community::Role::Sequenced>.
 Args    : An arrayref of Bio::SeqI objects
 Returns : An arrayref of Bio::SeqI objects

=head2 weights

 Usage   : my $weights = $member->weights();
 Function: Get or set some weights for this object. Weights represent how biased
           the sampling of this organism is. For example, when random shotgun
           sequencing microorganisms in the environment, the relative abundance
           of reads in the sequence library is not proportional to the relative
           abundance of the genomes because larger genomes contribute
           disproportionalely more reads than small genomes. In such a case, you
           could set the weight to the length of the genome. See
           L<Bio::Community::Role::Weighted>. Also see get_count() and get_rel_ab()
           in L<Bio::Community>.
 Args    : An arrayref of positive integers
 Returns : An arrayref of positive integers

=cut


####
# TODO:
# For on the fly attributes: http://stackoverflow.com/questions/3996067/how-can-i-flexibly-add-data-to-moose-objects
# For flexible attributes: https://github.com/cjfields/biome/blob/master/lib/Biome/Role/Identifiable.pm



( run in 1.639 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )