Bio-MUST-Core

 view release on metacpan or  search on metacpan

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


sub tax_labeler_from_list {
    my $self = shift;
    my $list = shift;

    return Labeler->new( tax => $self, labels => $list );
}



sub load_color_scheme {                     ## no critic (RequireArgUnpacking)
    my $self = shift;
    my $scheme = ColorScheme->new( tax => $self );
    return $scheme->load(@_);
}



sub eq_tax {                                ## no critic (RequireArgUnpacking)
    my $self       = shift;
    my $got        = shift;
    my $expect     = shift;
    my $classifier = shift;

    # classify got and expect orgs
    my $got_taxon = $classifier->classify($got,    @_);
    my $exp_taxon = $classifier->classify($expect, @_);

    # use context to decide what to return
    # list context: return taxon labels
    return ($got_taxon, $exp_taxon)
        if wantarray;

    # scalar context: compare taxon labels if both are defined
    return undef                    ## no critic (ProhibitExplicitReturnUndef)
        unless $got_taxon && $exp_taxon;

    return $got_taxon eq $exp_taxon;
}


# I/O METHODS

const my $CACHEDB => 'cachedb.bin';


sub new_from_cache {                        ## no critic (RequireArgUnpacking)
    my $class = shift;
    my %args  = @_;                         # TODO: handle HashRef?

    ### Loading NCBI (or GTDB) Taxonomy from binary cache file...
    my $tax_dir = dir( glob $args{tax_dir} );
    my $cachefile = file($tax_dir, $CACHEDB);
    my $tax = $class->load($cachefile, inject => { tax_dir => $tax_dir } );

    ### Done!
    return $tax;
}


sub update_cache {
    my $self = shift;

    my $cachefile = file($self->tax_dir, $CACHEDB);
    ### Updating binary cache file: $cachefile->stringify
    $self->store($cachefile);

    ### Done!
    return 1;
}


# class method to setup local taxonomy database


sub setup_taxdir {
    my $class   = shift;
    my $tax_dir = shift;
    my $args    = shift // {};          # HashRef (should not be empty...)

    my $source = $args->{source};

    $class->_setup_ncbi_taxdir($tax_dir, $args)
        if $source eq 'ncbi';

    $class->_setup_gtdb_taxdir($tax_dir)
        if $source eq 'gtdb';

    return;
}


sub _setup_ncbi_taxdir {
    my $class   = shift;
    my $tax_dir = shift;
    my $args    = shift // {};          # HashRef (should not be empty...)

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

    # setup local directory
    $tax_dir = dir( glob $tax_dir );
    $tax_dir->mkpath();

    ### Installing NCBI Taxonomy database to: $tax_dir->stringify
    ### Please be patient...

    # setup remote archive access
    my $base = 'https://ftp.ncbi.nih.gov/pub/taxonomy';
    my @targets = (
        'taxdump.tar.gz',
        $gi_mapper ? qw(gi_taxid_nucl.dmp.gz gi_taxid_prot.dmp.gz) : ()
    );

    # download and install file(s)...
    # ... first, the taxon_id version...
    my @dmpfiles;
    for my $target (@targets) {
        my $url  = "$base/$target";

        ### Downloading: $url
        my $zipfile = file($tax_dir, $target)->stringify;



( run in 2.234 seconds using v1.01-cache-2.11-cpan-9581c071862 )