lib/Bio/MUST/Core/Taxonomy/Classifier.pm

filter: Statementstmt 41.9% 26 / 62 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.8% 7 / 13 Podpod 100% 3 / 3 totaltotal 41.3% 36 / 87 CC 5 SCAR 16.3
CC
5
Cov
38%
classify
12.0
tax_masks
12.0
all_labels
2.0
CRAP 5.1
« lib/Bio/MUST/Core/PostPred/Composition.pm ↑ Summary lib/Bio/MUST/Core/IdList.pm »
1 package Bio::MUST::Core::Taxonomy::Classifier;
2 # ABSTRACT: Helper class for multiple-criterion classifier based on taxonomy
3 18 $Bio::MUST::Core::Taxonomy::Classifier::VERSION = '0.252040';
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # use Smart::Comments;
8  
9 18 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use List::AllUtils qw(indexes mesh partition_by pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use aliased 'Bio::MUST::Core::IdList';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use aliased 'Bio::MUST::Core::SeqMask';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 18 has 'categories' => (
18 traits => ['Array'],
19 is => 'ro',
20 isa => 'ArrayRef[Bio::MUST::Core::Taxonomy::Category]',
21 required => 1,
22 handles => {
23 all_categories => 'elements',
24 },
25 );
26  
27  
28  
29 0 ▶sub all_labels {
Subroutine all_labels: not called CC=1 SCAR=6.9
Pod documented
30 0 my $self = shift;
31 0 return map { $_->label } $self->all_categories;
32 }
33  
34  
35  
36 0 ▶sub classify {
Subroutine classify: not called CC=3 SCAR=24.8
Pod documented
37 0 my $self = shift;
38 0 my $listable = shift;
39  
40 # loop through cats and return the first one matching input
41 # this means that the cat order may affect the classification
42 0 for my $cat ($self->all_categories) {
43 0 ▶ return $cat->label if $cat->matches($listable);
Branch: 1 branch
BranchTrueFalseTotal
if $cat->matches($listable) 0 0 0
44 }
45  
46 # return undef if no suitable cat
47 0 return;
48 }
49  
50  
51 # "magic" name used when a pattern has no category
52 18 const my $NOCAT => '_NOCAT_';
53  
54 # TODO: come with better name for method?
55 # TODO: provide a shortcut if only one cat?
56  
57 0 ▶sub tax_masks {
Subroutine tax_masks: not called CC=3 SCAR=24.8
Pod documented
58 0 my $self = shift;
59 0 my $ali = shift;
60  
61 # TODO: profile and optimize as ideal_mask ?!?
62  
63 0 my $width = $ali->width;
64 0 my $regex = $ali->gapmiss_regex;
65  
66 # collect site patterns in terms of valid states
67 0 my %sites_for;
68 0 ▶ for (my $site = 0; $site < $width; $site++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site < $width) 0 0 0
69 my @indexes = # get seq indexes of states
70 0 ▶ indexes { $_ !~ m/$regex/xms } # which are valid
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 map { $_->state_at($site) } # and found at that site
72 $ali->all_seqs; # across all seqs
73 ;
74 #### @indexes
75  
76 # store site for index pattern
77 0 my $key = join q{,}, @indexes;
78 0 push @{ $sites_for{$key} }, $site;
79 }
80 #### %sites_for
81  
82 # setup keys from patterns
83 0 my @patterns = keys %sites_for;
84 #### @patterns
85  
86 # fetch id lists for site patterns
87 # Note: type coercion allows building an IdList from an ArrayRef[Seq]
88 my @lists = map {
89 0 IdList->new( ids => [ @{ $ali->seqs }[ split q{,} ] ] )
90 } @patterns;
91 #### @lists
92  
93 # attribute categories to site patterns based on corresponding id lists
94 0 ▶ my @cats = map { $self->classify($_) // $NOCAT } @lists;
Condition0%0/3
$self->classify($_) // $NOCAT
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->classify($_) // $NOCAT
$self->classify($_) $NOCAT
Truth table0%0/3
A // B
A= $self->classify($_)B= $NOCAT
ABresult
1X1
011
000
95 #### @cats
96 0 my %cat_for = mesh @patterns, @cats;
97 #### %cat_for
98  
99 # partition patterns by category to build masks
100 # Note: masks are defined by flattening of patterns' site lists
101 0 ▶ my %patterns_for = partition_by { $cat_for{$_} } @patterns;
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 delete $patterns_for{$NOCAT};
103 #### %patterns_for
104  
105 0 ▶ my %mask_for = pairmap {
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 $a => SeqMask->custom_mask(
107 0 $width, [ map { @{$_} } @sites_for{ @{$b} } ]
108 )
109 0 } %patterns_for;
110 #### %mask_for
111  
112 0 return \%mask_for;
113 }
114  
115 18 __PACKAGE__->meta->make_immutable;
116 18 1;
117  
118 __END__
« lib/Bio/MUST/Core/PostPred/Composition.pm ↑ Summary lib/Bio/MUST/Core/IdList.pm »