lib/Bio/MUST/Core/IdList.pm

filter: Statementstmt 100% 129 / 129 Branchbran 96.4% 27 / 28 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 22 / 22 Podpod 100% 8 / 8 totaltotal 96.6% 204 / 211 CC 23 SCAR 16.1
CC
23
Cov
98%
_ali_from_list_
10.0
load_lis
7.1
load
7.0
CRAP 5.0
« lib/Bio/MUST/Core/Taxonomy/Classifier.pm ↑ Summary script/export-itol.pl »
1 package Bio::MUST::Core::IdList;
2 # ABSTRACT: Id list for selecting specific sequences
3 18 $Bio::MUST::Core::IdList::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 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # use Smart::Comments;
11  
12 18 ▶use Carp;
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(:files);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use aliased 'Bio::MUST::Core::Ali';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 with 'Bio::MUST::Core::Roles::Commentable',
19 'Bio::MUST::Core::Roles::Listable';
20  
21  
22 # public array
23 has 'ids' => (
24 traits => ['Array'],
25 is => 'ro',
26 isa => 'Bio::MUST::Core::Types::full_ids',
27 4 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 18 coerce => 1,
29 writer => '_set_ids',
30 handles => {
31 count_ids => 'count',
32 all_ids => 'elements',
33 add_id => 'push',
34 get_id => 'get',
35 },
36 );
37  
38  
39 # private hash for faster querying
40 18 has '_index_for' => (
41 traits => ['Hash'],
42 is => 'ro',
43 isa => 'HashRef[Num]',
44 init_arg => undef,
45 lazy => 1,
46 builder => '_build_index_for',
47 handles => {
48 count_indices => 'count',
49 is_listed => 'defined',
50 index_for => 'get',
51 set_index => 'set',
52 },
53 );
54  
55  
56 ## no critic (ProhibitUnusedPrivateSubroutines)
57  
58 # Note: we don't store SeqId objects in the list but dynamically build them
59 # to benefit from SeqId methods (e.g., auto-removal of first '_'). This is
60 # the most flexible approach without costing too much in CPU-time.
61  
62 31 ▶sub _build_index_for {
Subroutine _build_index_for: called CC=1 SCAR=0.0
63 31 my $self = shift;
64  
65 # build private hash from internal array
66 31 my $i = 0;
67 31 return { map { $_->full_id => $i++ } $self->all_seq_ids };
68 }
69  
70 ## use critic
71  
72 4 ▶after 'add_id' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
73 4 my $self = shift;
74  
75 # check if there are indeed ids not yet in private hash
76 # Note: this might not be the case when adding ids in an empty list
77 4 my $n = $self->count_ids;
78 4 my $i = $self->count_indices;
79 4 ▶ return if $n == $i;
Branch: 1 branch
BranchTrueFalseTotal
if $n == $i 1 3 4
80  
81 # update private hash from internal array
82 $self->set_index(
83 3 map { $_->full_id => $i++ } ($self->all_seq_ids)[$i..$n-1]
84 );
85 3 return;
86 18 };
87  
88  
89 49 ▶sub all_seq_ids {
Subroutine all_seq_ids: called CC=1 SCAR=0.0
Pod documented
90 49 my $self = shift;
91 49 return map { SeqId->new( full_id => $_ ) } $self->all_ids;
92 }
93  
94  
95  
96 5 ▶sub negative_list {
Subroutine negative_list: called CC=1 SCAR=0.0
Pod documented
97 5 my $self = shift;
98 5 my $listable = shift;
99  
100 # filter out seq ids that are in the original list
101 5 my @ids = map { $_->full_id } $listable->all_seq_ids;
102 5 return $self->new( ids => [ grep { not $self->is_listed($_) } @ids ] );
103 }
104  
105  
106 # IdList-based Ali factory methods
107  
108  
109 4 ▶sub reordered_ali { ## no critic (RequireArgUnpacking)
Subroutine reordered_ali: called CC=1 SCAR=0.0
Pod documented
110 4 return shift->_ali_from_list_(1, @_);
111 }
112  
113  
114  
115 54 ▶sub filtered_ali { ## no critic (RequireArgUnpacking)
Subroutine filtered_ali: called CC=1 SCAR=0.0
Pod documented
116 54 return shift->_ali_from_list_(0, @_);
117 }
118  
119  
120 58 ▶sub _ali_from_list_ {
Subroutine _ali_from_list_: called CC=10 SCAR=23.0
121 58 my $self = shift;
122 58 my $reorder = shift;
123 58 my $ali = shift;
124 58 my $lookup = shift; # optional IdList indexing the Ali
125  
126 # override passed lookup with internal lookup if available
127 # Note: this allows Stash lookups to be used transparently
128 58 ▶ $lookup = $ali->lookup if $ali->can('lookup');
Branch: 1 branch
BranchTrueFalseTotal
if $ali->can("lookup") 1 57 58
129  
130 # TODO: warn for missing ids in Ali?
131  
132 # create new Ali object (extending header comment)
133 # TODO: allow custom comments
134 58 ▶ my $new_ali = Ali->new(
Branch: 1 branch
BranchTrueFalseTotal
$reorder ? : 4 54 58
135 comments => [ $ali->all_comments,
136 'built by ' . ($reorder ? 'reordered_ali' : 'filtered_ali')
137 ],
138 );
139  
140 # case 1: use lookup when available
141 58 ▶ if (defined $lookup) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lookup) { } 3 55 58
142 ### Using lookup...
143  
144 # get slot list from lookup
145 # Note1: Since this list follows the list in $self it is 'reordered'.
146 # We thus sort it by ascending slot if the Ali order must be kept.
147 # Note2: We go through SeqId objects to correctly handle MUST ids
148 3 my @ids = map { $_->full_id } $self->all_seq_ids;
149 3 my @slots = $lookup->index_for(@ids);
150 3 ▶ @slots = sort { $a <=> $b } @slots unless $reorder;
Branch: 1 branch
BranchTrueFalseTotal
unless $reorder 1 2 3
151  
152 # populate new Ali with deep copies of Seqs in slot list
153 3 $new_ali->add_seq( $ali->get_seq($_)->clone ) for @slots;
154 }
155  
156 # case 2: scan all seqs to find those that are listed
157 else {
158  
159 SEQ:
160 55 for my $seq ($ali->all_seqs) {
161 4536 ▶ next SEQ unless $self->is_listed($seq->full_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_listed($seq->full_id) 4469 67 4536
162  
163 # add Seq to new Ali honoring either IdList order...
164 67 ▶ if ($reorder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reorder) 8 59 67
165 8 $new_ali->set_seq(
166 $self->index_for($seq->full_id), $seq->clone
167 );
168 8 next SEQ;
169 }
170  
171 # ...or original Ali order
172 59 $new_ali->add_seq($seq->clone);
173 }
174  
175 # when reordering an Ali, ensure that new Ali does not contain
176 # empty slots due to some missing ids in the original Ali
177 $new_ali->_set_seqs(
178 55 ▶ [ $new_ali->filter_seqs( sub { defined } ) ]
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $reorder 2 53 55
179 ) if $reorder;
180 }
181  
182 58 return $new_ali;
183 }
184  
185  
186 # I/O methods
187  
188  
189 2 ▶sub load {
Subroutine load: called CC=7 SCAR=19.5
Pod documented
190 2 my $class = shift;
191 2 my $infile = shift;
192 2 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition100%2/2
shift() // {}
l!l
11
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
193  
194 2 ▶ my $col = $args->{column} // 0;
Condition100%2/2
$args->{"column"} // 0
l!l
11
MC/DC100%1/1
$args->{"column"} // 0
$args->{"column"}
Truth table100%2/2
A
A= $args->{"column"}
Aresult
11
00
195 2 ▶ my $sep = $args->{separator} // qr{\t}xms;
Condition100%2/2
$args->{"separator"} // qr/\t/msx
l!l
11
MC/DC100%1/1
$args->{"separator"} // qr/\t/msx
$args->{"separator"}
Truth table100%2/2
A
A= $args->{"separator"}
Aresult
11
00
196  
197 2 open my $in, '<', $infile;
198  
199 2 my $list = $class->new();
200  
201 2 my @ids;
202  
203 LINE:
204 2 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 12 2 14
205 12 chomp $line;
206  
207 # skip empty lines and process comment lines
208 12 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ or $list->is_comment($line) 2 10 12
Condition66%2/3
$line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ or $list->is_comment($line)
l!l&&r!l&&!r
0210
MC/DC50%1/2
$line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ or $list->is_comment($line)
$line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ $list->is_comment($line)
Truth table66%2/3
A or B
A= $line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/B= $list->is_comment($line)
ABresult
1X1
011
000
209 || $list->is_comment($line);
210  
211 10 my @fields = split $sep, $line;
212 10 push @ids, $fields[$col];
213 }
214  
215 2 $list->_set_ids( \@ids );
216  
217 2 return $list;
218 }
219  
220  
221  
222 1 ▶sub load_lis {
Subroutine load_lis: called CC=7 SCAR=19.5
Pod documented
223 1 my $class = shift;
224 1 my $infile = shift;
225  
226 1 open my $in, '<', $infile;
227  
228 1 my $list = $class->new();
229  
230 1 my $count;
231 my @ids;
232  
233 LINE:
234 1 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 8 1 9
235 8 chomp $line;
236  
237 # skip empty lines and process comment lines
238 8 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ or $list->is_comment($line) 2 6 8
Condition66%2/3
$line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ or $list->is_comment($line)
l!l&&r!l&&!r
026
MC/DC50%1/2
$line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ or $list->is_comment($line)
$line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/ $list->is_comment($line)
Truth table66%2/3
A or B
A= $line =~ /$Bio::MUST::Core::IdList::EMPTY_LINE/B= $list->is_comment($line)
ABresult
1X1
011
000
239 || $list->is_comment($line);
240  
241 # read id count as first lone number if not yet defined
242 6 ▶ if (!defined $count && $line =~ $COUNT_LINE) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $count and $line =~ /$Bio::MUST::Core::IdList::COUNT_LINE/) 1 5 6
Condition66%2/3
not defined $count and $line =~ /$Bio::MUST::Core::IdList::COUNT_LINE/
!ll&&!rl&&r
501
MC/DC50%1/2
not defined $count and $line =~ /$Bio::MUST::Core::IdList::COUNT_LINE/
not defined $count $line =~ /$Bio::MUST::Core::IdList::COUNT_LINE/
Truth table66%2/3
not(A) and B
A= not defined $countB= $line =~ /$Bio::MUST::Core::IdList::COUNT_LINE/
ABresult
0X0
100
111
243 1 $count = $line;
244 1 next LINE;
245 }
246  
247 5 push @ids, $line;
248 }
249  
250 1 $list->_set_ids( \@ids );
251  
252 1 ▶ carp '[BMC] Warning: id list size does not match id count in header!'
Branch: 1 branch
BranchTrueFalseTotal
unless $list->count_ids == $count 0 1 1
253 unless $list->count_ids == $count;
254  
255 1 return $list;
256 }
257  
258  
259  
260 2 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod documented
261 2 my $self = shift;
262 2 my $outfile = shift;
263  
264 2 open my $out, '>', $outfile;
265  
266 2 print {$out} $self->header;
267 2 say {$out} join "\n", $self->all_ids;
268  
269 2 close $out;
270  
271 2 return;
272 }
273  
274  
275  
276 1 ▶sub store_lis {
Subroutine store_lis: called CC=1 SCAR=0.0
Pod documented
277 1 my $self = shift;
278 1 my $outfile = shift;
279  
280 1 open my $out, '>', $outfile;
281  
282 1 print {$out} $self->header;
283 1 say {$out} $self->count_ids;
284 1 say {$out} join "\n", $self->all_ids;
285  
286 1 close $out;
287  
288 1 return;
289 }
290  
291 18 __PACKAGE__->meta->make_immutable;
292 18 1;
293  
294 __END__
« lib/Bio/MUST/Core/Taxonomy/Classifier.pm ↑ Summary script/export-itol.pl »