lib/Bio/MUST/Core/Roles/Listable.pm

filter: Statementstmt 82.9% 146 / 176 Branchbran 53.8% 14 / 26 Conditioncond 66.6% 10 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 94.1% 32 / 34 Podpod 100% 14 / 14 totaltotal 80.5% 220 / 273 CC 24 SCAR 11.8
CC
24
Cov
78%
store_nbs
12.0
long_branch_list
6.4
asc_br_len_list
6.0
CRAP 3.3
« lib/Bio/MUST/Core/IdMapper.pm ↑ Summary lib/Bio/MUST/Core/Utils.pm »
1 package Bio::MUST::Core::Roles::Listable;
2 # ABSTRACT: Listable Moose role for objects with implied id lists
3 18 $Bio::MUST::Core::Roles::Listable::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 18 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use List::AllUtils qw(nsort_by rev_nsort_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use Statistics::Descriptive;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use Bio::MUST::Core::Constants qw(:seqids);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 18 requires 'all_seq_ids';
22  
23  
24 # IdList factory methods
25  
26  
27 # alias for std_list emphasizing its use as a lookup
28 3 ▶sub new_lookup {
Subroutine new_lookup: called CC=1 SCAR=0.0
Pod documented
29 3 return shift->_list_from_seq_ids(0);
30 }
31  
32  
33 1 ▶sub std_list {
Subroutine std_list: called CC=1 SCAR=0.0
Pod documented
34 1 return shift->_list_from_seq_ids(0);
35 }
36  
37  
38 1 ▶sub alphabetical_list {
Subroutine alphabetical_list: called CC=1 SCAR=0.0
Pod documented
39 1 return shift->_list_from_seq_ids(1);
40 }
41  
42 5 ▶sub _list_from_seq_ids {
Subroutine _list_from_seq_ids: called CC=2 SCAR=6.9
43 5 my $self = shift;
44 5 my $sort = shift;
45  
46 5 my @ids = map { $_->full_id } $self->all_seq_ids;
47 5 ▶ @ids = sort @ids if $sort; # optionally sort list
Branch: 1 branch
BranchTrueFalseTotal
if $sort 1 4 5
48 5 return Bio::MUST::Core::IdList->new( ids => \@ids );
49 }
50  
51 7 ▶around qw(complete_seq_list len_mapper) => sub {
Subroutine __ANON__: called CC=2 SCAR=7.9
52 7 my $method = shift;
53 7 my $self = shift;
54  
55 # ensure that seqs are available (e.g., the object is an Ali)
56 7 ▶ unless ( $self->can('all_seqs') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("all_seqs")) 0 7 7
57 0 carp '[BMC] Warning: cannot proceed without seqs; returning undef!';
58 0 return;
59 }
60  
61 7 return $self->$method(@_);
62 18 };
63  
64  
65 1 ▶sub desc_seq_len_list {
Subroutine desc_seq_len_list: called CC=1 SCAR=0.0
Pod documented
66 1 my $self = shift;
67 1 return $self->complete_seq_list(0); # explicit 0 for clarity
68 }
69  
70  
71 6 ▶sub complete_seq_list {
Subroutine complete_seq_list: called CC=5 SCAR=16.5
Pod documented
72 6 my $self = shift;
73 6 my $min_res = shift;
74  
75 # sort seqs in desc order according to their (non-missing) length
76 # Note: accessor call is optimized
77 6 ▶ my @seqs = rev_nsort_by { $_->nomiss_seq_len } $self->all_seqs;
Subroutine __ANON__: called CC=1 SCAR=0.0
78  
79 # optionally truncate list
80 6 ▶ if ($min_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_res) 5 1 6
81  
82 # take max seq length from first seq
83 5 my $max_len = $seqs[0]->nomiss_seq_len;
84  
85 # convert fractional min_res to conservative integer (if needed)
86 5 ▶ $min_res = ceil($min_res * $max_len)
Branch: 1 branch
BranchTrueFalseTotal
if 0 < $min_res and $min_res < 1 2 3 5
Condition66%2/3
0 < $min_res and $min_res < 1
!ll&&!rl&&r
032
MC/DC50%1/2
0 < $min_res and $min_res < 1
0 < $min_res $min_res < 1
Truth table66%2/3
A and B
A= 0 < $min_resB= $min_res < 1
ABresult
0X0
100
111
87 if 0 < $min_res && $min_res < 1;
88  
89 # filter out seqs with less than min_res non-missing chars
90 # Note: this might seem inefficient but results in clearer code
91 31 ▶ @seqs = List::AllUtils::before {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 31 $_->nomiss_seq_len < $min_res
93 5 } @seqs;
94 }
95  
96 # get ids from ordered (and possibly truncated list)
97 6 my @ids = map { $_->full_id } @seqs;
98  
99 # in scalar context return bare IdList
100 6 my $list = Bio::MUST::Core::IdList->new( ids => \@ids );
101 6 ▶ return $list unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 6 0 6
102  
103 # in list context further return the corresponding lengths
104 # Note: consider recycling old code below for efficiency
105 0 my @lens = map { $_->nomiss_seq_len } @seqs;
106 0 return ($list, \@lens);
107 }
108  
109 # old code (maybe faster but more complex and without sort)
110  
111 # sub complete_seq_list {
112 # my $self = shift;
113 # my $min_res = shift;
114 #
115 # # get (non-missing char) lengths of all seqs and record max_len
116 # my @lengths = map { $_->nomiss_seq_len } $self->all_seqs;
117 # my $max_len = List::AllUtils::max @lengths;
118 #
119 # # convert fractional min_res to conservative integer (if needed)
120 # $min_res = ceil($min_res * $max_len)
121 # if 0 < $min_res && $min_res < 1;
122 #
123 # # filter out seqs with less than min_res non-missing chars
124 # my @ids = map { $_->full_id } $self->all_seq_ids;
125 # my @indices = grep { $lengths[$_] >= $min_res } 0..$#ids;
126 #
127 # return Bio::MUST::Core::IdList->new( ids => [ @ids[@indices] ] );
128 # }
129  
130 1 ▶around qw(long_branch_list) => sub {
Subroutine __ANON__: called CC=2 SCAR=7.9
131 1 my $method = shift;
132 1 my $self = shift;
133  
134 # ensure that tips are available (e.g., the object is a Tree)
135 1 ▶ unless ( $self->can('tree') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("tree")) 0 1 1
136 0 carp '[BMC] Warning: cannot proceed without a tree; returning undef!';
137 0 return;
138 }
139  
140 1 return $self->$method(@_);
141 18 };
142  
143  
144 0 ▶sub asc_br_len_list {
Subroutine asc_br_len_list: not called CC=2 SCAR=17.9
Pod documented
145 0 my $self = shift;
146 0 ▶ return $self->long_branch_list( { %{ shift // {} }, iqr_fact => 0 } );
Condition0%0/2
shift() // {}
l!l
00
MC/DC0%0/1
shift() // {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
147 } # explicitly disable list truncation but leave other args "as is"
148  
149 # Note: very naive approach and not applicable in practice.
150 # Should probably use the derivative of branch length increase in log space.
151 # Meanwhile: use treeshrink
152  
153  
154 1 ▶sub long_branch_list {
Subroutine long_branch_list: called CC=6 SCAR=18.6
Pod documented
155 1 my $self = shift;
156 1 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition50%1/2
shift() // {}
l!l
10
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
157  
158 1 my $iqr_fact = $args->{iqr_fact};
159 1 my $to_root = $args->{to_root};
160  
161 # TODO: consider caching lengths if too slow with calc_path_to_root
162 1 my $method = 'get_branch_length';
163 1 ▶ if ($to_root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_root) 0 1 1
164 0 $method = 'calc_path_to_root';
165 0 ▶ carp '[BMC] Warning: tree should be rooted for distances to root!'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->tree->is_rooted 0 0 0
166 unless $self->tree->is_rooted;
167 }
168  
169 # sort tips in desc order according to their branch length
170 # Note: accessor call is optimized
171 255 ▶ my @tips = nsort_by { $_->$method }
Subroutine __ANON__: called CC=1 SCAR=0.0
172 1 @{ $self->tree->get_terminals };
173  
174 # optionally truncate list
175 # TODO: check if this makes sense with calc_path_to_root
176 1 ▶ if ($iqr_fact) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iqr_fact) 1 0 1
177  
178 # define threshold using `iqr_fact` x IQR beyond Q3
179 1 my $stat = Statistics::Descriptive::Full->new;
180 1 $stat->add_data( [ map { $_->$method } @tips ] );
181 1 my ($q1, $q3) = ( $stat->quantile(1), $stat->quantile(3) );
182 #### $q1
183 #### $q3
184 #### iqr: $q3-$q1
185 #### $iqr_fact
186 1 my $threshold = $q3 + $iqr_fact * ($q3 - $q1);
187 #### $threshold
188  
189 # filter out tips with branch length over the threshold
190 # Note: this might seem inefficient but results in clearer code
191 224 ▶ @tips = List::AllUtils::after_incl {
Subroutine __ANON__: called CC=1 SCAR=0.0
192 224 $_->$method > $threshold
193 1 } @tips;
194 }
195  
196 # get ids from ordered (and possibly truncated list)
197 my @ids = map {
198 1 Bio::MUST::Core::SeqId->new( full_id => $_->get_name )->full_id
199 } @tips;
200  
201 # in scalar context return bare IdList
202 1 my $list = Bio::MUST::Core::IdList->new( ids => \@ids );
203 1 ▶ return $list unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1 1
204  
205 # in list context further return the corresponding branch lengths
206 # Note: consider recycling old code below for efficiency
207 1 my @lens = map { $_->$method } @tips;
208 1 return ($list, \@lens);
209 }
210  
211 # old code (maybe faster but more complex and without sort)
212  
213 # sub long_leaf_list {
214 # my $self = shift;
215 # my $fact = shift // 1.5;
216 #
217 # my @tips = @{ $self->tree->get_terminals };
218 #
219 # # compute terminal branch length distribution
220 # my @lengths = map { $_->get_branch_length } @tips;
221 # #### list: sort { $a <=> $b } @lengths
222 # my $stat = Statistics::Descriptive::Full->new;
223 # $stat->add_data( \@lengths );
224 #
225 # my ($q1, $q3) = ( $stat->quantile(1), $stat->quantile(3) );
226 # #### $q1
227 # #### $q3
228 # #### iqr: $q3-$q1
229 # #### $fact
230 # my $threshold = $q3 + $fact * ($q3 - $q1);
231 # #### $threshold
232 #
233 # my @seq_ids = map { SeqId->new( full_id => $_->get_name ) }
234 # grep { $_->get_branch_length > $threshold } @tips;
235 #
236 # #### n: scalar @seq_ids
237 # return IdList->new( ids => \@seq_ids );
238 # }
239  
240 # IdMapper factory methods
241  
242  
243 12 ▶sub std_mapper {
Subroutine std_mapper: called CC=4 SCAR=13.9
Pod documented
244 12 my $self = shift;
245 12 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition100%2/2
shift() // {}
l!l
102
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
246  
247 12 ▶ my $prefix = $args->{id_prefix} // 'seq';
Condition100%2/2
$args->{"id_prefix"} // "seq"
l!l
210
MC/DC100%1/1
$args->{"id_prefix"} // "seq"
$args->{"id_prefix"}
Truth table100%2/2
A
A= $args->{"id_prefix"}
Aresult
11
00
248 12 ▶ my $offset = $args->{offset} // 0;
Condition100%2/2
$args->{"offset"} // 0
l!l
111
MC/DC100%1/1
$args->{"offset"} // 0
$args->{"offset"}
Truth table100%2/2
A
A= $args->{"offset"}
Aresult
11
00
249  
250 12 my @seq_ids = $self->all_seq_ids;
251 return Bio::MUST::Core::IdMapper->new(
252 73 long_ids => [ map { $_->full_id } @seq_ids ], # list
253 12 abbr_ids => [ map { $prefix . ($_+$offset) } 1..@seq_ids ], # scalar
254 );
255 }
256  
257 1 ▶sub acc_mapper {
Subroutine acc_mapper: called CC=2 SCAR=7.0
Pod documented
258 1 my $self = shift;
259 1 ▶ my $prefix = shift // q{};
Condition50%1/2
shift() // ""
l!l
01
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
260  
261 # Note: this mapper could fail with non-GenBank Seqs
262 1 my @seq_ids = $self->all_seq_ids;
263 return Bio::MUST::Core::IdMapper->new(
264 10 long_ids => [ map { $_->full_id } @seq_ids ],
265 1 abbr_ids => [ map { $prefix . $_->accession } @seq_ids ],
266 );
267 }
268  
269  
270 1 ▶sub len_mapper {
Subroutine len_mapper: called CC=1 SCAR=0.0
Pod documented
271 1 my $self = shift;
272  
273 1 my @seq_ids = $self->all_seq_ids;
274 1 my @lengths = map { $_->nomiss_seq_len } $self->all_seqs;
275 return Bio::MUST::Core::IdMapper->new(
276 10 long_ids => [ map { $_->full_id . '@' . shift @lengths } @seq_ids ],
277 1 abbr_ids => [ map { $_->full_id } @seq_ids ],
278 );
279 }
280  
281  
282 6 ▶sub regex_mapper { ## no critic (RequireArgUnpacking)
Subroutine regex_mapper: called CC=1 SCAR=0.0
Pod documented
283 6 my $self = shift;
284 # my $prefix = shift // q{}; # note the currying below
285 # my $regex = shift // $DEF_ID;
286  
287 6 my @long_ids = map { $_->full_id } $self->all_seq_ids;
288 6 my @abbr_ids = map { $_->abbr_with_regex(@_) } $self->all_seq_ids;
289  
290 6 return Bio::MUST::Core::IdMapper->new(
291 long_ids => \@long_ids,
292 abbr_ids => \@abbr_ids
293 );
294 }
295  
296  
297 1 ▶sub org_mapper_from_long_ids {
Subroutine org_mapper_from_long_ids: called CC=3 SCAR=11.0
Pod documented
298 1 my $self = shift;
299 1 my $mapper = shift; # mapper long_org => abbr_org
300  
301 1 my @long_ids;
302 my @abbr_ids;
303  
304 ID:
305 1 for my $seq_id ( $self->all_seq_ids ) {
306 8 ▶ next ID if $seq_id->is_foreign; # needed to skip already abbr_ids
Branch: 1 branch
BranchTrueFalseTotal
if $seq_id->is_foreign 0 8 8
307  
308 8 push @long_ids, $seq_id->full_id;
309 8 push @abbr_ids, $mapper->abbr_id_for( $seq_id->full_org )
310 . '|' . $seq_id->accession;
311 }
312  
313 1 return Bio::MUST::Core::IdMapper->new(
314 long_ids => \@long_ids,
315 abbr_ids => \@abbr_ids
316 );
317 }
318  
319  
320 1 ▶sub org_mapper_from_abbr_ids {
Subroutine org_mapper_from_abbr_ids: called CC=3 SCAR=11.0
Pod documented
321 1 my $self = shift;
322 1 my $mapper = shift; # mapper long_org => abbr_org
323  
324 1 my @long_ids;
325 my @abbr_ids;
326  
327 ID:
328 1 for my $seq_id ( $self->all_seq_ids ) {
329 8 my $abbr_id = $seq_id->full_id;
330 8 my ($abbr_org, $accession) = split /\|/xms, $abbr_id, 2;
331 8 ▶ next ID unless $accession; # needed to skip already long_ids
Branch: 1 branch
BranchTrueFalseTotal
unless $accession 0 8 8
332  
333 8 push @long_ids, $mapper->long_id_for($abbr_org) . '@' . $accession;
334 8 push @abbr_ids, $abbr_id;
335 }
336  
337 1 return Bio::MUST::Core::IdMapper->new(
338 long_ids => \@long_ids,
339 abbr_ids => \@abbr_ids
340 );
341 }
342  
343  
344 18 const my $NBS_ID_LEN => 79;
345  
346 0 ▶sub store_nbs {
Subroutine store_nbs: not called CC=3 SCAR=24.8
Pod documented
347 0 my $self = shift;
348 0 my $outfile = shift;
349  
350 # #Sequences extracted from c111_78.ali of the 5 May 2009 at 11 hours 40
351 # #File c111_78.nbs created on Tuesday 5 May 2009 at 11 hours 40
352 # #184 positions remain on the 184 aligned positions
353 # #life.col,life.nom
354 # #Here is the list of the 78 species used:
355 # Aciduliprofundum_boonei_T469___________________________________________________ Aeropyrum_pernix_K1____________________________________________________________
356 # Archaeoglobus_fulgidus_DSM_4304________________________________________________ Archaeoglobus_profundus_Av18__DSM_5631_________________________________________
357 # ...
358  
359 0 my @ids = $self->all_seq_ids;
360  
361 0 open my $out, '>', $outfile;
362  
363 # print minimum header
364 0 print {$out} "#File $outfile created on " . ctime(time);
365 0 say {$out} '#Here is the list of the ' . scalar @ids . ' species used:';
366  
367 # print padded ids on two columns
368 0 for my $i (0..$#ids) {
369 0 my $id = $ids[$i]->foreign_id;
370 0 my $pad_id = $id . '_' x ($NBS_ID_LEN - length $id);
371 0 ▶ my $term = $i % 2 ? "\n" : q{ };
Branch: 1 branch
BranchTrueFalseTotal
$i % 2 ? : 0 0 0
372 0 print {$out} $pad_id . $term;
373 }
374 0 say {$out} q{};
375  
376 0 close $out;
377  
378 0 return;
379 }
380  
381 18 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
382 18 1;
383  
384 __END__
« lib/Bio/MUST/Core/IdMapper.pm ↑ Summary lib/Bio/MUST/Core/Utils.pm »