Permalink
Browse files

Boolean return status for merge_lineage()

  • Loading branch information...
1 parent b180459 commit 66155f41a1ee44e1c4a1e83e94c90b0377e5627c @fangly fangly committed Jun 27, 2013
Showing with 20 additions and 18 deletions.
  1. +18 −16 Bio/Tree/TreeFunctionsI.pm
  2. +2 −2 t/RemoteDB/Taxonomy.t
View
@@ -465,7 +465,7 @@ sub get_lca {
Title : merge_lineage
Usage : merge_lineage($node)
Function: Merge a lineage of nodes with this tree.
- Returns : n/a
+ Returns : true for success, false (and a warning) otherwise
Args : Bio::Tree::TreeI with only one leaf, OR
Bio::Tree::NodeI which has an ancestor
@@ -495,37 +495,39 @@ sub merge_lineage {
my ($self, $thing) = @_;
$self->throw("Must supply an object reference") unless ref($thing);
- my ($lineage_tree, $lineage_leaf);
+ my $lineage_leaf;
if ($thing->isa('Bio::Tree::TreeI')) {
my @leaves = $thing->get_leaf_nodes;
$self->throw("The supplied Tree can only have one leaf") unless @leaves == 1;
- $lineage_tree = $thing;
$lineage_leaf = shift(@leaves);
}
elsif ($thing->isa('Bio::Tree::NodeI')) {
$self->throw("The supplied Node must have an ancestor") unless $thing->ancestor;
- $lineage_tree = $self->new(-node => $thing);
$lineage_leaf = $thing;
}
# see if any node in the supplied lineage is in our tree - that will be
# our lca and we can merge at the node below
my @lineage = ($lineage_leaf, reverse($self->get_lineage_nodes($lineage_leaf)));
- my $merged = 0;
- for my $i (0..$#lineage) {
- my $lca = $self->find_node(-internal_id => $lineage[$i]->internal_id) || next;
- if ($i == 0) {
- # the supplied thing to merge is already in the tree, nothing to do
- return;
+ my $merged;
+ for my $i (0..$#lineage) {
+ my $node = $self->find_node(-internal_id => $lineage[$i]->internal_id);
+ if (defined $node) {
+ # if $i == 0, the supplied thing to merge is already in the tree, nothing to do
+ if ($i > 0) {
+ # $i is the node, so the previous node is new to the tree and should
+ # be merged on
+ $node->add_Descendent($lineage[$i-1]);
+ }
+ $merged = 1;
+ last;
}
- # $i is the lca, so the previous node is new to the tree and should
- # be merged on
- $lca->add_Descendent($lineage[$i-1]);
- $merged = 1;
- last;
}
- $merged || ($self->warn("Couldn't merge the lineage of ".$lineage_leaf->id." with the rest of the tree!\n") && return);
+ if (not $merged) {
+ $self->warn("Could not merge the lineage of ".$lineage_leaf->id." with the rest of the tree");
+ }
+ return $merged;
}
View
@@ -7,7 +7,7 @@ BEGIN {
use lib '.';
use Bio::Root::Test;
- test_begin(-tests => 151,
+ test_begin(-tests => 152,
-requires_module => 'XML::Twig');
use_ok('Bio::DB::Taxonomy');
@@ -263,7 +263,7 @@ for my $name ('Human', 'Hominidae') {
my $node = $db_flatfile->get_taxon(-taxonid => $ncbi_id);
if ($tree) {
- $tree->merge_lineage($node);
+ ok $tree->merge_lineage($node);
}
else {
ok $tree = Bio::Tree::Tree->new(-node => $node);

0 comments on commit 66155f4

Please sign in to comment.