Last active
August 29, 2015 14:01
-
-
Save hyphaltip/0126513aa47979fa87ba to your computer and use it in GitHub Desktop.
Get Nearest neighbor on tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Bio::TreeIO; | |
my ($treefile,$taxon_name) = @ARGV; | |
my $in = Bio::TreeIO->new(-format => 'newick', -file => $treefile); | |
my $tree = $in->next_tree; | |
if( ! $tree ) { | |
die "cannot parse treefile $treefile and find a valid tree"; | |
} | |
my $node = $tree->find_node(-id => $taxon_name); | |
if( $node ) { | |
my $parent = $node->ancestor; | |
my @neighbors; | |
for my $c ( grep { $_->is_Leaf } $parent->get_all_Descendents ) { | |
push @neighbors, $c->id if $c->id ne $node->id; | |
} | |
printf "Neighbor(s) of %s are the node(s): %s\n", $node->id, join(",", @neighbors); | |
} else { | |
warn("cannot find node $taxon_name in the tree, make sure you spelled it correctly, this is an exact match search"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment