[Bioperl-l] Need help for implementing a new TreeIO module

Jason Stajich jason.stajich at duke.edu
Mon Jan 3 12:01:21 EST 2005


On Jan 3, 2005, at 11:38 AM, Guillaume Rousse wrote:
>> The thing is you need to build the tree by connecting the nodes, so 
>> the order they are created in is very important.  You can't just 
>> build the leaves first and then the (internal) nodes later.  You need 
>> to build from the top down - if you read a newick format from left to 
>> right, that is exactly how we are building the tree up using the 
>> EventListener.
>> In a way the builder basically assumes you have already have the tree 
>> built, just encoded.  So you start with a root node, you add 
>> children.  For each child you add more children where appropriate 
>> until you get to a leaf node and you are done with that recursion.
> OK, thanks for the explanations. However, I don't understand how to 
> add branch length informations. I guess leave labels are just 
> introduced using characters() method, right ?

This would set a branch length for a node. The 'leaf' event is sort of 
a hack - I can't remember why I had to introduce it - I think to deal 
with the labeled internal nodes.

So to build a leaf node with branch_length $branch_length and name 
$idstring you want to do:
# leaf node
$self->_eventHandler->start_element({'Name' => 'node'});
$self->_eventHandler->start_element( { 'Name' => 'branch_length'});
$self->_eventHandler->characters($branch_length);
$self->_eventHandler->end_element( {'Name' => 'branch_length'});
$self->_eventHandler->start_element( { 'Name' => 'id'});
$self->_eventHandler->characters($idstring);
$self->_eventHandler->end_element( {'Name' => 'id'});
$self->_eventHandler->start_element({'Name' => 'leaf'});
$self->_eventHandler->characters(1);
$self->_eventHandler->end_element({'Name' => 'leaf'});
$self->_eventHandler->end_element({'Name' => 'node'});

To build an internal node which has a branch length but no label for 
example:
# Internal Node
$self->_eventHandler->start_element({'Name' => 'node'});
$self->_eventHandler->start_element( { 'Name' => 'branch_length'});
$self->_eventHandler->characters($branch_length);
$self->_eventHandler->end_element( {'Name' => 'branch_length'});
$self->_eventHandler->start_element({'Name' => 'leaf'});
$self->_eventHandler->characters(0);
$self->_eventHandler->end_element({'Name' => 'leaf'});
$self->_eventHandler->end_element({'Name' => 'node'});

See the 'characters' function in Bio;:TreeIO::TreeEventHandler for the 
different field names and event labels that can be used.

If you want to build a node with two leaves, first you have to start 
with a 'tree' section to tell the handler that this is nested data.
Start a 'tree' event, build the node (like the section just above), 
then build two leaf nodes (like the leaf node section above), then end 
the 'tree' event.  'tree' is an unfortunate name for the event but 
don't feel like changing it - a throwback from when I thought I'd only 
need an initial 'tree' an just 'node' events.

$self->_eventHandler->start_document;
$self->_eventHandler->start_element({'Name' => 'tree'});
# do internal node
   # do leaf node
   # do leaf node
$self->_eventHandler->end_element({'Name' => 'tree'});
return $self->_eventHandler->end_document;


Hmm - I guess I need to go back and document the event system here and 
in SearchIO if people are going to develop with it.


> -- 
> You aren't Superman
> 		-- Murphy's Bush Fire Brigade Laws n°22
>
>
--
Jason Stajich
jason.stajich at duke.edu
http://www.duke.edu/~jes12/




More information about the Bioperl-l mailing list