lib/Bio/MUST/Core/Tree/Splits.pm

filter: Statementstmt 96.7% 205 / 212 Branchbran 90.0% 45 / 50 Conditioncond 70.5% 12 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 97.2% 35 / 36 Podpod 100% 14 / 14 totaltotal 92.0% 313 / 340 CC 37 SCAR 13.0
CC
37
Cov
94%
load_splits
9.0
get_node_for_split
6.2
load_consense
5.0
CRAP 3.7
« lib/Bio/MUST/Core/SeqMask/Freqs.pm ↑ Summary lib/Bio/MUST/Core/IdMapper.pm »
1 package Bio::MUST::Core::Tree::Splits;
2 # ABSTRACT: Tree splits (bipartitions)
3 18 $Bio::MUST::Core::Tree::Splits::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 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use List::AllUtils qw(max_by zip_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 18 ▶use Smart::Comments '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use aliased 'Bio::MUST::Core::IdList';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use aliased 'Bio::MUST::Core::Tree';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 # public attributes
24  
25 18 has 'rep_n' => (
26 is => 'ro',
27 isa => 'Maybe[Int]',
28 lazy => 1,
29 builder => '_build_rep_n',
30 );
31  
32 18 has 'lookup' => (
33 is => 'ro',
34 isa => 'Bio::MUST::Core::IdList',
35 required => 1,
36 handles => qr{.*}xms, # expose all IdList methods (and attributes)
37 );
38  
39 # private attributes
40  
41 has '_bp_for' => (
42 traits => ['Hash'],
43 is => 'ro',
44 isa => 'HashRef[Str]',
45 lazy => 1,
46 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 18 writer => '_set_bp_for',
48 handles => {
49 bp_for => 'get',
50 all_bp_keys => 'keys',
51 all_bp_vals => 'values',
52 },
53 );
54  
55 18 has '_comp_bp_for' => (
56 traits => ['Hash'],
57 is => 'ro',
58 isa => 'HashRef[Str]',
59 init_arg => undef,
60 lazy => 1,
61 builder => '_build_comp_bp_for',
62 handles => {
63 comp_bp_for => 'get',
64 all_comp_bp_keys => 'keys',
65 },
66 );
67  
68 ## no critic (ProhibitUnusedPrivateSubroutines)
69  
70 4 ▶sub _build_rep_n {
Subroutine _build_rep_n: called CC=4 SCAR=15.2
71 4 my $self = shift;
72  
73 # check for non-numeric node metadata
74 4 ▶ if (List::AllUtils::any { !looks_like_number $_ } $self->all_bp_vals) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&List::AllUtils::any(sub { not &looks_like_number($_); } , $self->all_bp_vals)) 0 4 4
75 0 carp '[BMC] Warning: node metadata are not all numeric!';
76 0 return;
77 }
78  
79 # determine number of replicates from max BP value
80 4 my $max_bp = List::AllUtils::max $self->all_bp_vals;
81 4 ▶ carp '[BMC] Warning: max BP value (rep_n) is nor 1 nor a multiple of 10!'
Branch: 1 branch
BranchTrueFalseTotal
if $max_bp != 1 and $max_bp % 10 0 4 4
Condition66%2/3
$max_bp != 1 and $max_bp % 10
!ll&&!rl&&r
130
MC/DC0%0/2
$max_bp != 1 and $max_bp % 10
$max_bp != 1 $max_bp % 10
Truth table66%2/3
A and B
A= $max_bp != 1B= $max_bp % 10
ABresult
0X0
100
111
82 if $max_bp != 1 && $max_bp % 10;
83  
84 4 return $max_bp;
85 }
86  
87 16 ▶sub _build_comp_bp_for {
Subroutine _build_comp_bp_for: called CC=1 SCAR=0.0
88 16 my $self = shift;
89  
90 my %comp_bp_for = map {
91 16 ( tr/.*/*./r ) => $self->bp_for($_)
92 } $self->all_bp_keys;
93  
94 16 return \%comp_bp_for;
95 }
96  
97 ## use critic
98  
99  
100 2513 ▶sub ids2key {
Subroutine ids2key: called CC=4 SCAR=14.0
Pod documented
101 2513 my $self = shift;
102 2513 my $ids = shift;
103 # TODO: handle array?
104  
105 2513 my $bp_key = q{.} x $self->count_ids;
106  
107 ID:
108 2513 for my $seq_id ( @{$ids} ) {
109  
110 # get id from SeqId or full_id
111 29614 ▶ unless ( $seq_id->can('full_id') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seq_id->can("full_id")) 29245 369 29614
112 29245 $seq_id = SeqId->instance( full_id => $seq_id );
113 }
114  
115 # get index for seq_id
116 29614 my $id = $seq_id->full_id;
117 29614 my $index = $self->index_for($id);
118 29614 ▶ unless (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 0 29614 29614
119 0 carp "[BMC] Warning: $id missing from id list; ignoring!";
120 0 next ID;
121 }
122  
123 # put star for corresponding id
124 29614 substr $bp_key, $index, 1, q{*};
125 }
126  
127 2513 return $bp_key;
128 }
129  
130  
131 2248 ▶sub key2ids {
Subroutine key2ids: called CC=1 SCAR=0.0
Pod documented
132 2248 my $self = shift;
133 2248 my $bp_key = shift;
134  
135 my @seq_ids = map {
136 2248 SeqId->instance( full_id => $self->get_id($_) )
137 } _indices($bp_key, q{*});
138  
139 # TODO: handle wantarray?
140 2248 return \@seq_ids;
141 }
142  
143  
144 2320 ▶sub node2key {
Subroutine node2key: called CC=2 SCAR=6.9
Pod documented
145 2320 my $self = shift;
146 2320 my $node = shift;
147  
148 #### $node
149 #### r: $node->is_root
150 #### t: $node->is_terminal
151 #### n: $node->get_name
152  
153 # skip root (as it includes all tips)
154 2320 ▶ return if $node->is_root;
Branch: 1 branch
BranchTrueFalseTotal
if $node->is_root 29 2291 2320
155  
156 # skip trivial bipartitions
157 2291 my @tips = @{ $node->get_terminals };
158 #### @tips
159 # Note: also handle keys for trivial splits (for rooting on single OTUs)
160 # return if @tips < 2 || ( $self->count_ids - @tips ) < 2;
161 #### n2k: join qq{\n}, q{}, map { $_->get_name } @tips
162  
163 2291 return $self->ids2key( [ map { $_->get_name } @tips ] );
164 }
165  
166  
167 210 ▶sub is_a_clan {
Subroutine is_a_clan: called CC=3 SCAR=11.0
Pod documented
168 210 my $self = shift;
169 210 my $bp_key = shift;
170  
171 210 ▶ return 1 if $self->bp_for( $bp_key); # standard clan (*)
Branch: 1 branch
BranchTrueFalseTotal
if $self->bp_for($bp_key) 62 148 210
172 148 ▶ return -1 if $self->comp_bp_for($bp_key); # complementary clan (.)
Branch: 1 branch
BranchTrueFalseTotal
if $self->comp_bp_for($bp_key) 4 144 148
173  
174 144 return; # undef if not found
175 }
176  
177  
178 39 ▶sub clan_support {
Subroutine clan_support: called CC=2 SCAR=7.0
Pod documented
179 39 my $self = shift;
180 39 my $bp_key = shift;
181  
182 39 my $support = $self->bp_for( $bp_key); # standard clan (*)
183 39 ▶ $support //= $self->comp_bp_for($bp_key); # complementary clan (.)
Condition66%2/3
$support //= $self->comp_bp_for($bp_key)
l!l&&r!l&&!r
28110
MC/DC0%0/2
$support //= $self->comp_bp_for($bp_key)
$support $self->comp_bp_for($bp_key)
Truth table66%2/3
A //= B
A= $supportB= $self->comp_bp_for($bp_key)
ABresult
1X1
011
000
184  
185 39 return $support; # silent undef if not found
186 }
187  
188  
189 3 ▶sub sub_clans {
Subroutine sub_clans: called CC=2 SCAR=6.9
Pod documented
190 3 my $self = shift;
191 3 my $bp_key = shift;
192  
193 # TODO: use some bit masking approach instead of explicit indices?
194 3 my %is_wanted = map { $_ => 1 } _indices($bp_key, q{*});
195  
196 3 my @sub_clans;
197 3 ▶ for (my $size = keys(%is_wanted) - 1; $size > 1; $size--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 1) 24 3 27
198 push @sub_clans,
199 264 ▶ grep { List::AllUtils::all { $is_wanted{$_} } _indices($_, q{*}) }
Subroutine __ANON__: called CC=1 SCAR=0.0
200 24 grep { tr/*// == $size }
201 $self->all_bp_keys, # standard clans (*)
202 $self->all_comp_bp_keys # complementary clans (.)
203 ;
204 } # examine clans of target size and keep those including only wanted ids
205  
206 # TODO: handle wantarray?
207 3 return @sub_clans;
208 }
209  
210  
211 18 my %xor_for = (
212 '..' => '.',
213 '.*' => '*',
214 '*.' => '*',
215 '**' => '.',
216 );
217  
218 24 ▶sub xor_clans { ## no critic (RequireArgUnpacking)
Subroutine xor_clans: called CC=1 SCAR=0.0
Pod documented
219 return join q{},
220 24 ▶ zip_by { $xor_for{"$_[0]$_[1]"} } map { [ split // ] } @_[1..2];
Subroutine __ANON__: called CC=1 SCAR=0.0
221 }
222  
223  
224 2 ▶sub get_node_for_split {
Subroutine get_node_for_split: called CC=6 SCAR=18.3
Pod documented
225 2 my $self = shift;
226 2 my $tree = shift;
227 2 my $bp_key = shift;
228  
229 # transparently fetch Bio::Phylo component object
230 # TODO: avoid code repetition?
231 2 ▶ $tree = $tree->tree if $tree->isa('Bio::MUST::Core::Tree');
Branch: 1 branch
BranchTrueFalseTotal
if $tree->isa("Bio::MUST::Core::Tree") 2 0 2
232  
233 2 my $comp_bp_key = $bp_key =~ tr/.*/*./r;
234  
235 NODE:
236 2 for my $node ( @{ $tree->get_entities } ) {
237 652 my $node_key = $self->node2key($node);
238 652 ▶ next NODE unless $node_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $node_key 2 650 652
239 650 ▶ return $node if $node_key eq $bp_key || $node_key eq $comp_bp_key;
Branch: 1 branch
BranchTrueFalseTotal
if $node_key eq $bp_key or $node_key eq $comp_bp_key 2 648 650
Condition66%2/3
$node_key eq $bp_key or $node_key eq $comp_bp_key
l!l&&r!l&&!r
20648
MC/DC50%1/2
$node_key eq $bp_key or $node_key eq $comp_bp_key
$node_key eq $bp_key $node_key eq $comp_bp_key
Truth table66%2/3
A or B
A= $node_key eq $bp_keyB= $node_key eq $comp_bp_key
ABresult
1X1
011
000
240 }
241  
242 0 carp "[BMC] Warning: cannot find split with key: $bp_key; returning undef";
243  
244 0 return;
245 }
246  
247  
248 1100 ▶sub score_split {
Subroutine score_split: called CC=3 SCAR=11.0
Pod documented
249 1100 my $self = shift;
250 1100 my $filter = shift;
251 1100 my $bp_key = shift;
252  
253 #### $bp_key
254 1100 my $comp_bp_key = $bp_key =~ tr/.*/*./r;
255 #### 1: join qq{\n}, q{}, map { $_->full_id } @{ $self->key2ids( $bp_key) }
256 #### 2: join qq{\n}, q{}, map { $_->full_id } @{ $self->key2ids($comp_bp_key) }
257  
258 # compute score...
259 1100 my ($score1, $seen1) = $filter->score( @{ $self->key2ids( $bp_key) } );
260 1100 my ($score2, $seen2) = $filter->score( @{ $self->key2ids($comp_bp_key) } );
261 # ... considering both possible split keys (standard and complementary)
262 1100 my $score = List::AllUtils::max( $score1 - $score2, $score2 - $score1 );
263 #### $score
264  
265 # Note: the following would be useful to decorate nodes with scores...
266 # ... but it would not work for terminals!
267 # $self->_bp_for->{$bp_key} = $score;
268  
269 # TODO: warn only once?
270 1100 ▶ carp '[BMC] Warning: filter could not match any seq id!'
Branch: 1 branch
BranchTrueFalseTotal
unless $seen1 or $seen2 0 1100 1100
Condition66%2/3
$seen1 or $seen2
l!l&&r!l&&!r
5545460
MC/DC0%0/2
$seen1 or $seen2
$seen1 $seen2
Truth table66%2/3
A or B
A= $seen1B= $seen2
ABresult
1X1
011
000
271 unless $seen1 || $seen2;
272  
273 1100 return $score;
274 }
275  
276  
277 2 ▶sub get_split_that_maximizes {
Subroutine get_split_that_maximizes: called CC=1 SCAR=0.0
Pod documented
278 2 my $self = shift;
279 2 my $filter = shift;
280  
281 # return split for which method yields the highest value
282 2 ▶ my $split = max_by { $self->score_split($filter, $_) } $self->all_bp_keys,
Subroutine __ANON__: called CC=1 SCAR=0.0
283 $self->_trivial_bp_keys; # also consider single seqs
284 # Note: scalar context to get only one node!
285 # TODO: handle ties to allow for additional criteria
286  
287 2 return $split;
288 }
289  
290 2515 ▶sub _indices {
Subroutine _indices: called CC=2 SCAR=6.9
291 2515 my $haystack = shift;
292 2515 my $needle = shift;
293  
294 2515 my @indices;
295  
296 2515 my $pos = 0;
297 2515 ▶ while ( ($pos = index($haystack, $needle, $pos)) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($pos = index($haystack, $needle, $pos)) >= 0) 304815 2515 307330
298 304815 push @indices, $pos++;
299 } # Note: 0-based indices to match IdList (lookup) slots
300  
301 2515 return @indices;
302 }
303  
304 2 ▶sub _trivial_bp_keys {
Subroutine _trivial_bp_keys: called CC=2 SCAR=6.9
305 2 my $self = shift;
306  
307 2 my @bp_keys;
308  
309 # build all possible trivial keys (one per OTU)
310 2 my $id_n = $self->count_ids;
311 2 for my $index (0..$id_n-1) {
312 552 my $bp_key = q{.} x $id_n;
313 552 substr $bp_key, $index, 1, q{*};
314 552 push @bp_keys, $bp_key;
315 }
316  
317 2 return @bp_keys;
318 }
319  
320  
321 # I/O METHODS
322  
323  
324 # refactored from parse_consense_out.pl
325  
326 1 ▶sub load_consense {
Subroutine load_consense: called CC=5 SCAR=16.1
Pod documented
327 1 my $class = shift;
328 1 my $infile = shift;
329  
330 1 open my $in, '<', $infile;
331  
332 1 my $rep_n;
333 my @ids;
334 1 my %bp_for;
335  
336 LINE:
337 1 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 172 1 173
338 172 chomp $line;
339  
340 # read number of replicates
341 172 ▶ if ($line =~ m/How\ many\ times\ out\ of \s+ ([0-9\.]+)/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /How\ many\ times\ out\ of \s+ ([0-9\.]+)/msx) 2 170 172
342 2 $rep_n = sprintf '%.0f', $1; # turn number to integer
343 }
344  
345 # process OTU line
346 # TODO: check robustness here due to consense truncating seq_ids
347 172 ▶ if ($line =~ m/\A \s* (\d+) \. \s+ (.*)/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A \s* (\d+) \. \s+ (.*)/msx) 31 141 172
348 31 my $index = $1;
349 31 my $id = $2;
350 31 $id =~ tr/ /_/; # replace inner spaces by underscores
351 31 $id =~ s/_+\z//xmsg; # delete trailing underscores (if any)
352 31 push @ids, $id; # ignore 1-based index
353 }
354  
355 # process bipartition line
356 172 ▶ if ($line =~ m/\A ([\ .*]+) \s+ (\d+\.\d+) \z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A ([\ .*]+) \s+ (\d+\.\d+) \z/msx) 48 124 172
357 48 (my $bp_key = $1) =~ tr/ //d;
358 48 my $bp_val = $2;
359 48 $bp_for{$bp_key} = 0 + $bp_val;
360 }
361 }
362  
363 1 my $splits = $class->new(
364 rep_n => $rep_n,
365 lookup => IdList->new( ids => \@ids ),
366 _bp_for => \%bp_for,
367 );
368  
369 1 return $splits;
370 }
371  
372  
373 # refactored from parse_consense_out.pl
374  
375 1 ▶sub load_splits {
Subroutine load_splits: called CC=9 SCAR=22.0
Pod documented
376 1 my $class = shift;
377 1 my $infile = shift;
378  
379 1 open my $in, '<', $infile;
380  
381 1 my @ids;
382 my $id_n;
383 1 my %bp_for;
384  
385 LINE:
386 1 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 1810 1 1811
387 1810 chomp $line;
388  
389 # process OTU line
390 1810 ▶ if ($line =~ m/\A TAXLABELS/xms .. $line =~ m/\A ;/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A TAXLABELS/msx .. $line =~ /\A ;/msx) 290 1520 1810
391 290 my ($index, $id) = $line =~ m/\A \[ (\d+) \] \s+ \' (\S+) \'/xms;
392 290 ▶ push @ids, $id if $index; # ignore 1-based index
Branch: 1 branch
BranchTrueFalseTotal
if $index 288 2 290
393 }
394  
395 # process bipartition line
396 1810 ▶ if ($line =~ m/\A MATRIX/xms .. $line =~ m/\A ;/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A MATRIX/msx .. $line =~ /\A ;/msx) 1509 301 1810
397 1509 my ($bp_val, @indices) = $line =~ m/\b (\d+) \b/xmsg;
398  
399 # skip null and trivial bipartitions
400 1509 ▶ next LINE unless $bp_val; # new wrt parse_consense_out.pl
Branch: 1 branch
BranchTrueFalseTotal
unless $bp_val 489 1020 1509
401 1020 ▶ next LINE if @indices < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @indices < 2 288 732 1020
402  
403 # build bipartition from OTU index list
404 732 ▶ $id_n //= @ids; # only compute once
Condition66%2/3
$id_n //= @ids
l!l&&r!l&&!r
73110
MC/DC0%0/2
$id_n //= @ids
$id_n @ids
Truth table66%2/3
A //= B
A= $id_nB= @ids
ABresult
1X1
011
000
405 732 my $bp_key = q{.} x $id_n;
406 732 substr( $bp_key, $_-1, 1, q{*} ) for @indices;
407 732 $bp_for{$bp_key} = 0 + $bp_val;
408 }
409 }
410  
411 1 my $splits = $class->new(
412 # no data about rep_n here
413 lookup => IdList->new( ids => \@ids ),
414 _bp_for => \%bp_for,
415 );
416  
417 1 return $splits;
418 }
419  
420  
421 3 ▶sub load_newick {
Subroutine load_newick: called CC=1 SCAR=0.0
Pod documented
422 3 my $class = shift;
423 3 my $infile = shift;
424  
425 # return $class->new_from_newick_str(
426 # Tree->load($infile)->newick_str( -nodelabels => 1 )
427 # ); # node labels are needed to recover BP values directly at nodes
428  
429 3 return $class->new_from_tree( Tree->load($infile) );
430 }
431  
432 # Note: in principle useless now that new_from_tree is available
433  
434 # =method new_from_newick_str
435 #
436 # =cut
437 #
438 # sub new_from_newick_str {
439 # my $class = shift;
440 # my $str = shift;
441 #
442 # # TODO: upgrade to handle non-numeric metadata?
443 #
444 # # 1. extract topology (refactored from parse_consense_out.pl' get_topology)
445 #
446 # # strip chars unrelated to topology
447 # $str =~ s/_{2,}//xmsg; # delete series of underscores
448 # $str =~ s/: $RE{num}{real}//xmsg; # delete branch lengths (if any)
449 #
450 # # restrict str to topology (not fool-proof for bad trees, really needed?)
451 # my ($tpl) = $str
452 # =~ m/\A .*? ($RE{balanced}{-parens=>'()'} $RE{num}{real}? ;) /xms;
453 #
454 # unless ($tpl) {
455 # carp '[BMC] Warning: unable to extract topology from tree string;'
456 # . ' returning undef!';
457 # return;
458 # }
459 #
460 # # 2. build OTU index (refactored from pco's get_species_hash)
461 # # TODO: consider refactoring using Tree::std_list?
462 #
463 # # extract OTU list from topology str
464 # # Note1: for this BP values must be first deleted (+look-behind assert)
465 # # Note2: we also trim surrounding whitespace for each OTU
466 # # Note3: but Newick str is assumed to be already stripped of quotes
467 # ( my $bare_tpl = $tpl ) =~ s/(?<=\)) $RE{num}{real}//xmsg;
468 # my $lookup = IdList->new( ids => [
469 # map { $RE{ws}{crop}->subs($_) } split q{,}, $bare_tpl =~ tr/();//dr
470 # ] );
471 # my $id_n = $lookup->count_ids;
472 #
473 # # 3. build splits hash (refactored from pco's read_tree)
474 # my %bp_for;
475 # my @bp_keys;
476 #
477 # # parse topology
478 # my $chunk;
479 # my $char = substr $tpl, 0, 1;
480 #
481 # CHUNK:
482 # while ($char ne q{;}) {
483 # $chunk = 1; # defaults to advancing one char
484 #
485 # # skip commas and spaces
486 # if ($char =~ m/[,\s]/xms) {
487 # next CHUNK;
488 # }
489 #
490 # # open new bipartition
491 # if ($char eq q{(}) {
492 # my $bp_key =q {.} x $id_n;
493 # push @bp_keys, $bp_key;
494 # next CHUNK;
495 # }
496 #
497 # # close most recently opened bipartition
498 # if ($tpl =~ m/\A \) $RE{num}{real}{-keep}?/xms) {
499 #
500 # # handle optional bootstrap support values
501 # my $bp_val = $1 // 1; # defaults to 1 if no support
502 # $chunk += length $1 if defined $1;
503 #
504 # # throw error if not balanced parentheses
505 # unless (@bp_keys) {
506 # carp '[BMC] Warning: unbalanced parentheses in tree string;'
507 # . ' returning undef!';
508 # return;
509 # }
510 #
511 # # store bipartition...
512 # my $bp_key = pop @bp_keys;
513 # my $star_n = $bp_key =~ tr/*//;
514 # # ... skipping full tree and trivial bipartitions
515 # $bp_for{$bp_key} = 0 + $bp_val if $star_n > 1 && $star_n < $id_n;
516 # next CHUNK;
517 # }
518 #
519 # # process OTU id
520 # # TODO: consider using a variation of $FULL_ID to match this?
521 # if ($tpl =~ m/\A ([A-Za-z0-9_\@\-\.\ \#]+)/xms) {
522 #
523 # # put stars for corresponding OTU in all open bipartitions
524 # my $seq_id = SeqId->new( full_id => $1 );
525 # my $offset = $lookup->index_for( $seq_id->full_id );
526 # substr $bp_keys[$_], $offset, 1, q{*} for 0..$#bp_keys;
527 # $chunk = length $1;
528 # next CHUNK;
529 # }
530 #
531 # # throw error if anything else found than expected cases
532 # carp "[BMC] Warning: unexpected char in tree string: '$char';"
533 # . ' returning undef!';
534 # return;
535 # }
536 #
537 # continue {
538 # # advance in topology string
539 # substr $tpl, 0, $chunk, q{};
540 #
541 # # read next char
542 # $char = substr $tpl, 0, 1;
543 # }
544 #
545 # my $splits = $class->new(
546 # # no data about rep_n here
547 # lookup => $lookup,
548 # _bp_for => \%bp_for,
549 # );
550 #
551 # return $splits;
552 # }
553  
554  
555 15 ▶sub new_from_tree {
Subroutine new_from_tree: called CC=5 SCAR=16.1
Pod documented
556 15 my $class = shift;
557 15 my $tree = shift;
558  
559 # transparently fetch Bio::Phylo component object
560 # TODO: avoid code repetition?
561 15 ▶ $tree = $tree->tree if $tree->isa('Bio::MUST::Core::Tree');
Branch: 1 branch
BranchTrueFalseTotal
if $tree->isa("Bio::MUST::Core::Tree") 3 12 15
562  
563 # build lookup as fast as possible (no tree visitor method)
564 my $lookup = IdList->new(
565 15 ids => [ map { $_->get_name } @{ $tree->get_terminals } ]
566 );
567  
568 # instantiate Splits object to benefit from ids2key method
569 15 my $splits = $class->new(
570 # no data about rep_n here
571 lookup => $lookup,
572 );
573  
574 # compute bipartitions and store node metadata
575 15 my %bp_for;
576  
577 NODE:
578 15 for my $node ( @{ $tree->get_internals } ) {
579 1000 my $bp_key = $splits->node2key($node);
580 1000 ▶ next NODE unless $bp_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $bp_key 15 985 1000
581  
582 985 ▶ $bp_for{$bp_key} = $node->get_name || 1; # defaults to "seen"
Condition100%2/2
$node->get_name || 1
l!l
867118
MC/DC100%1/1
$node->get_name || 1
$node->get_name
Truth table100%2/2
A
A= $node->get_name
Aresult
11
00
583 }
584  
585 # TODO: handle rooted trees where the root has a BP value but not children
586 # e.g., mullidae-well-rooted.tre
587  
588 # complete Splits object
589 15 $splits->_set_bp_for( \%bp_for );
590  
591 15 return $splits;
592 }
593  
594 18 __PACKAGE__->meta->make_immutable;
595 18 1;
596  
597 __END__
« lib/Bio/MUST/Core/SeqMask/Freqs.pm ↑ Summary lib/Bio/MUST/Core/IdMapper.pm »