[Bioperl-l] tree splice remove nodes

Mark A. Jensen maj at fortinbras.us
Sat Sep 12 02:00:53 UTC 2009


Hi Janet-
The trouble here is that 
$tree2 = $tree
doesn't create an independent copy of the entire
tree data structure. So, $tree2 and $tree
essentially point to the same thing. 
The easiest way to get two independent copies 
is probably to read the file twice:

$treeIO = new Bio::TreeIO(-file => "testtree2.nwk", -format=>'newick');
$tree = $treeIO->next_tree;
$treeIO = new Bio::TreeIO(-file => "testtree2.nwk", -format=>'newick');
$tree2 = $treeIO->next tree;

which will create two copies. This is a little kludgy, but 
unfortunately, there doesn't seem to be any easy way to 
rewind the TreeIO object. 

When you want a copy of a complex object, generally 
you need to "clone" it, and there are variety of modules
you can use to create clones. [It's probably worth adding 
a clone() method to TreeFunctionsI--maybe I'll do that.]
Get the module Clone from CPAN and do

use Clone qw(clone);
....
$tree2 = clone($tree);
...

hope this helps- cheers 
MAJ
----- Original Message ----- 
From: "Janet Young" <jayoung at fhcrc.org>
To: <bioperl-l at lists.open-bio.org>
Sent: Friday, September 11, 2009 9:11 PM
Subject: [Bioperl-l] tree splice remove nodes


> Hi,
> 
> I'm having a problem in a script that I'm hoping someone can help me  
> figure out.  I'm using splice(-remove_id) to prune a Bio::Tree::Tree  
> object, and it looks like it worked fine.
> 
> However, I'm also trying to keep a separate copy of the original  
> (unpruned) tree in a different object but that second object seems to  
> get pruned as well.
> 
> Here's my tree, stored in a file called testtree2.nwk:
> 
> (((A,(B,b)),C),D,E);
> 
> ---------------------------------------
> Here's my script:
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> use Bio::TreeIO;
> 
> my $treeIO = new Bio::TreeIO(-file => "testtree2.nwk", - 
> format=>'newick');
> while (my $tree = $treeIO->next_tree() ) {
> 
>       print "\nfound a tree\n\n";
>       my @originalleaves = $tree -> get_leaf_nodes();
>       foreach my $originalleaf (@originalleaves) {print "original  
> tree has node with id " . $originalleaf->id() . "\n";}
> 
>       my $tree2 = $tree;
> 
>       my @remove = ("D","E");
>       print "\nremoving nodes @remove\n\n";
> 
>       $tree2 -> splice(-remove_id => \@remove);
>       my @leaves2 = $tree2 -> get_leaf_nodes();
>       foreach my $leaf2 (@leaves2) {print "after removing tree2 has  
> node with id " . $leaf2->id() . "\n";}
> 
>       print "\n";
> 
>       my @originalleavesafter = $tree -> get_leaf_nodes();
>       foreach my $leaf3 (@originalleavesafter) {print "after removing  
> original tree has node with id " . $leaf3->id() . "\n";}
> 
> }
> 
> ---------------------------------------
> 
> 
> And here's my output:
> 
> found a tree
> 
> original tree has node with id A
> original tree has node with id B
> original tree has node with id b
> original tree has node with id C
> original tree has node with id D
> original tree has node with id E
> 
> removing nodes D E
> 
> after removing tree2 has node with id A
> after removing tree2 has node with id B
> after removing tree2 has node with id b
> after removing tree2 has node with id C
> 
> after removing original tree has node with id A
> after removing original tree has node with id B
> after removing original tree has node with id b
> after removing original tree has node with id C
> 
> 
> -------------------------
> 
> I want to splice the specified nodes out of $tree2 and leave $tree  
> untouched, but both $tree and $tree2 seem to be affected by the splice  
> operation. Am I failing to understand something about references/ 
> dereferencing?   I'm not sure if I just haven't figured this out right  
> or if it's a bug.  If it looks like a bug let me know and I'll post it  
> to bugzilla.
> 
> thanks in advance for any advice,
> 
> Janet
> 
> -------------------------------------------------------------------
> 
> Dr. Janet Young (Trask lab)
> 
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Avenue N., C3-168,
> P.O. Box 19024, Seattle, WA 98109-1024, USA.
> 
> tel: (206) 667 1471 fax: (206) 667 6524
> email: jayoung  ...at...  fhcrc.org
> 
> http://www.fhcrc.org/labs/trask/
> 
> -------------------------------------------------------------------
> 
> 
> 
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/bioperl-l
> 
>



More information about the Bioperl-l mailing list