Skip to content

Instantly share code, notes, and snippets.

@hyphaltip
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyphaltip/0126513aa47979fa87ba to your computer and use it in GitHub Desktop.
Save hyphaltip/0126513aa47979fa87ba to your computer and use it in GitHub Desktop.
Get Nearest neighbor on tree
#!/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