Bio-MUST-Core

 view release on metacpan or  search on metacpan

lib/Bio/MUST/Core/Taxonomy/Labeler.pm  view on Meta::CPAN

package Bio::MUST::Core::Taxonomy::Labeler;
# ABSTRACT: Helper class for simple labeler based on taxonomy
$Bio::MUST::Core::Taxonomy::Labeler::VERSION = '0.252040';
use Moose;
use namespace::autoclean;

use Carp;

use Bio::MUST::Core::Types;
with 'Bio::MUST::Core::Roles::Taxable';


has 'labels' => (
    is       => 'ro',
    isa      => 'Bio::MUST::Core::IdList',
    required => 1,
    coerce   => 1,
    handles  => {
         all_labels => 'all_ids',
        is_a_label  => 'is_listed',
    },
);


sub BUILD {
    my $self = shift;

    # warn in case of ambiguous taxa
    for my $taxon ( $self->all_labels ) {
        carp "[BMC] Warning: $taxon is taxonomically ambiguous in labeler!"
            if $self->tax->is_dupe($taxon);
    }

    return;
}



sub classify {
    my $self   = shift;
    my $seq_id = shift;
    my $args   = shift // {};

    my $greedy = $args->{greedy} // 0;

    my @lineage = $self->tax->fetch_lineage($seq_id);
    while (my $taxon = $greedy ? shift @lineage : pop @lineage) {
        return $taxon if $self->is_a_label($taxon);
    }

    # return undef if no suitable taxon
    return;
}

__PACKAGE__->meta->make_immutable;
1;

__END__

=pod

=head1 NAME

Bio::MUST::Core::Taxonomy::Labeler - Helper class for simple labeler based on taxonomy



( run in 2.046 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )