| 1 | package Bio::MUST::Core::Roles::Filterable; | ||||||||||||||||||||
| 2 | # ABSTRACT: Filterable Moose role for objects that behave as filters | ||||||||||||||||||||
| 3 | 18 | $Bio::MUST::Core::Roles::Filterable::VERSION = '0.252040'; | |||||||||||||||||||
| 4 | 18 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 18 | ▶ | use autodie; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 18 | ▶ | use feature qw(say); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 18 | ▶ | use Smart::Comments '###'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 18 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 18 | ▶ | use Const::Fast; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 18 | ▶ | use Bio::MUST::Core::Types; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 18 | ▶ | use Bio::MUST::Core::Constants qw(:seqids); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 18 | requires 'is_allowed'; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 18 | has '_specs' => ( | |||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||
| 22 | isa => 'Bio::MUST::Core::IdList', | ||||||||||||||||||||
| 23 | required => 1, | ||||||||||||||||||||
| 24 | coerce => 1, | ||||||||||||||||||||
| 25 | handles => { | ||||||||||||||||||||
| 26 | all_specs => 'all_ids', | ||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | has '_is_' . $_ => ( | ||||||||||||||||||||
| 32 | traits => ['Hash'], | ||||||||||||||||||||
| 33 | is => 'ro', | ||||||||||||||||||||
| 34 | isa => 'HashRef', | ||||||||||||||||||||
| 35 | init_arg => undef, | ||||||||||||||||||||
| 36 | writer => '_set_is_' . $_, | ||||||||||||||||||||
| 37 | handles => { | ||||||||||||||||||||
| 38 | 'all_' . $_ => 'keys', | ||||||||||||||||||||
| 39 | 'is_' . $_ => 'defined', | ||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||
| 41 | 18 | ) for qw(wanted unwanted); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # TODO: allow specifying taxa as partial lineages to solve ambiguities | ||||||||||||||||||||
| 45 | # TODO: allow specifying taxa as taxid and/or mustids (for strains) | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # regexes for deriving filter from specifications | ||||||||||||||||||||
| 48 | 18 | const my $WANTED => qr{\A \+ \s* (.*) }xms; | |||||||||||||||||||
| 49 | 18 | const my $UNWANTED => qr{\A \- \s* (.*) }xms; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 2 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||
| 52 | 2 | my $self = shift; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # parse filter specifications | ||||||||||||||||||||
| 55 | 2 | ▶ | my @wanted = map { $_ =~ $WANTED ? $1 : () } $self->all_specs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 2 | ▶ | my @unwanted = map { $_ =~ $UNWANTED ? $1 : () } $self->all_specs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # only for tax_filters | ||||||||||||||||||||
| 59 | 2 | ▶ | if ( $self->can('tax') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | #### TAXONOMIC FILTER | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # warn in case of ambiguous taxa | ||||||||||||||||||||
| 63 | 0 | for my $taxon (@wanted, @unwanted) { | |||||||||||||||||||
| 64 | 0 | ▶ | carp "[BMC] Warning: $taxon is taxonomically ambiguous in filter!" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | if $self->tax->is_dupe($taxon); | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # build filtering hashes from wanted and unwanted arrays | ||||||||||||||||||||
| 69 | # Note: we want no virus by default but exclude nothing | ||||||||||||||||||||
| 70 | 0 | ▶ | push @wanted, 'cellular organisms' unless @wanted; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 2 | my %is_wanted = map { $_ => 1 } @wanted; | |||||||||||||||||||
| 74 | 0 | my %is_unwanted = map { $_ => 1 } @unwanted; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # store HashRefs for filter | ||||||||||||||||||||
| 77 | 2 | $self->_set_is_wanted( \%is_wanted ); | |||||||||||||||||||
| 78 | 2 | $self->_set_is_unwanted(\%is_unwanted); | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 2 | return; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 2200 | ▶ | sub score { ## no critic (RequireArgUnpacking) | ||||||||||||||||||
|
Subroutine
score: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 85 | 2200 | my $self = shift; | |||||||||||||||||||
| 86 | 2200 | my @seq_ids = @_; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 2200 | my $seen; | |||||||||||||||||||
| 89 | 2200 | my $score = 0; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2200 | for my $seq_id (@seq_ids) { | |||||||||||||||||||
| 92 | #### s: $seq_id->full_id | ||||||||||||||||||||
| 93 | 303600 | my $res = $self->is_allowed($seq_id); | |||||||||||||||||||
| 94 | #### $res | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # track if at least one id passed filter | ||||||||||||||||||||
| 97 | 303600 | ▶ | $seen ||= $res; | ||||||||||||||||||
|
Condition100%3/3
$seen ||= $res
MC/DC100%2/2
$seen ||= $res
$seen
$res
Truth table100%3/3
A ||= B
A= $seenB= $res
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | # increment score: undef => 0 ; otherwise +1 / -1 | ||||||||||||||||||||
| 100 | 303600 | ▶ | $score += !defined $res ? 0 : $res ? +1 : -1; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 101 | #### $score | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | # further return seen flag in list context | ||||||||||||||||||||
| 105 | 2200 | ▶ | return wantarray ? ($score, $seen) : $score; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 18 | ▶ | no Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 109 | 18 | 1; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||