Bio-FastParsers

 view release on metacpan or  search on metacpan

lib/Bio/FastParsers/Roles/Clusterable.pm  view on Meta::CPAN

package Bio::FastParsers::Roles::Clusterable;
# ABSTRACT: Attributes and methods common to CD-HIT and UCLUST drivers
# CONTRIBUTOR: Amandine BERTRAND <amandine.bertrand@doct.uliege.be>
$Bio::FastParsers::Roles::Clusterable::VERSION = '0.221230';
use Moose::Role;

use autodie;
use feature qw(say);

use Carp;
use Sort::Naturally;
use Try::Tiny;


# private attributes

has '_members_for' => (
    traits   => ['Hash'],
    is       => 'ro',
    isa      => 'HashRef[ArrayRef[Str]]',
    init_arg => undef,
    writer   => '_set_members_for',
    handles  => {
        all_representatives => 'keys',
            members_for     => 'get',
    },
);

sub all_representatives_by_cluster_size {
    my $self = shift;

    # sort first on descending cluster size then on representative id
    # using natural sort and the Schwartzian transform
    my @list = map {                                       $_->[1]  }
              sort {  $b->[0] <=> $a->[0] || ncmp($a->[1], $b->[1]) }
               map { [ scalar @{ $self->members_for($_) }, $_    ]  }
        $self->all_representatives
    ;

    return @list;
}


sub clust_mapper {
    my $self = shift;
    my $sep  = shift // q{/};

    # do not force Bio::FastParsers to depend on Bio::MUST::Core
    my $bmc = try   { require Bio::MUST::Core }
              catch { return }
    ;
    unless ($bmc) {
        carp 'Warning: Bio::MUST::Core not installed; returning nothing!';
        return;
    }

    my @abbr_ids;
    my @long_ids;

    for my $repr ( $self->all_representatives_by_cluster_size ) {
    	push @abbr_ids, $repr;
    	push @long_ids, join $sep,
    	    nsort ( @{ $self->members_for($repr) }, $repr )
    	;
    }

    return Bio::MUST::Core::IdMapper->new(
        abbr_ids => \@abbr_ids,
        long_ids => \@long_ids,
    );
}



( run in 1.111 second using v1.01-cache-2.11-cpan-6de40a662fe )