[Bioperl-l] Bio::TreeIO
Rob Edwards
redwards at utmem.edu
Tue Dec 16 21:48:25 EST 2003
On Tuesday, December 16, 2003, at 09:28 AM, Iain Wallace wrote:
> Hi all,
>
> I am trying to use the Bio::TreeIO module to split my sequences into
> two
> groups.
>
> My problem is that I want these two groups to contain as many sequences
> as possible. At the moment I am using NJplot to define an outgroup, so
> that the tree is as evenly split as i can make it.
>
> I am wondering if any one knows an easier way to do this.....?
>
> Any help would be great
>
> Thanks
>
> Iain
Here is one (totally inelegant) way to get parts of a tree.
The two attached scripts work together. The first adds CountXXX (where
XXX is the number of the node) to each node as an id on internal nodes
and writes out the tree. You can then look at the tree (e.g. using atv
or treeview) and decide which node you want to cut the tree at.
The second script takes the tree file and the node id (CountXXX) and
just writes out that part of the tree.
Its not exactly what you want (I don't think) but it may put you on the
right track.
add_tree_node_counts.pl
--------begin
#!/usr/bin/perl -w
# add node counts to a tree
use strict;
use Bio::TreeIO;
my $file=shift || die "tree file?";
my $tio=Bio::TreeIO->new(-file=>$file, -format=>'newick');
my $tout=Bio::TreeIO->new(-file=>">$file.nwk", -format=>'newick');
my $count=1;
while (my $tree=$tio->next_tree) {
foreach my $node ($tree->get_nodes) {
$node->id("Count$count") unless ($node->id);
$count++;
}
$tout->write_tree($tree);
}
-------end
trim_tree.pl
-------begin
#!/usr/bin/perl -w
# trim a tree to a selected node
use strict;
use Bio::TreeIO;
use Bio::Tree::Tree;
my ($file, $nodeid)=@ARGV;
unless ($file && $nodeid) {die "$0 <treefile> <node id>"}
my $tio=Bio::TreeIO->new(-file=>$file, -format=>'newick');
my $tout=Bio::TreeIO->new(-file=>">$nodeid.nwk", -format=>'newick');
while (my $tree=$tio->next_tree) {
print "Whole tree total length is ", $tree->total_branch_length, "\n";
foreach my $node ($tree->get_nodes) {
my $val=$node->id;
if ($val && $val eq $nodeid) {
my $newtree=Bio::Tree::Tree->new(-root=>$node);
print "Trimmed total length is ", $newtree->total_branch_length,
"\n";
$tout->write_tree($newtree);
}
}
}
------end
More information about the Bioperl-l
mailing list