<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#
# BioPerl module for Bio::TreeIO::TreeEventBuilder
#
# Please direct questions and support issues to &lt;bioperl-l@bioperl.org&gt; 
#
# Cared for by Jason Stajich &lt;jason@bioperl.org&gt;
#
# Copyright Jason Stajich
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::TreeIO::TreeEventBuilder - Build Bio::Tree::Tree's and 
  Bio::Tree::Node's from Events 

=head1 SYNOPSIS

# internal use only

=head1 DESCRIPTION

This object will take events and build a Bio::Tree::TreeI compliant
object makde up of Bio::Tree::NodeI objects.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support 

Please direct usage questions or support issues to the mailing list:

I&lt;bioperl-l@bioperl.org&gt;

rather than to the module maintainer directly. Many experienced and 
reponsive experts will be able look at the problem and quickly 
address it. Please include a thorough description of the problem 
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via the
web:

  https://github.com/bioperl/bioperl-live/issues

=head1 AUTHOR - Jason Stajich

Email jason-at-bioperl.org

=head1 APPENDIX

The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _

=cut


# Let the code begin...


package Bio::TreeIO::TreeEventBuilder;
use strict;

use Bio::Tree::Tree;
use Bio::Tree::Node;

use base qw(Bio::Root::Root Bio::Event::EventHandlerI);

=head2 new

 Title   : new
 Usage   : my $obj = Bio::TreeIO::TreeEventBuilder-&gt;new();
 Function: Builds a new Bio::TreeIO::TreeEventBuilder object 
 Returns : Bio::TreeIO::TreeEventBuilder
 Args    :


=cut

sub new {
  my($class,@args) = @_;

  my $self = $class-&gt;SUPER::new(@args);
  my ($treetype, $nodetype) = $self-&gt;_rearrange([qw(TREETYPE 
						    NODETYPE)], @args);
  $treetype ||= 'Bio::Tree::Tree';
  $nodetype ||= 'Bio::Tree::Node';

  eval { 
      $self-&gt;_load_module($treetype);
      $self-&gt;_load_module($nodetype);
  };

  if( $@ ) {
      $self-&gt;throw("Could not load module $treetype or $nodetype. \n$@\n")
  }
  $self-&gt;treetype($treetype);
  $self-&gt;nodetype($nodetype);
  $self-&gt;{'_treelevel'} = 0;
  return $self;
}

=head2 treetype

 Title   : treetype
 Usage   : $obj-&gt;treetype($newval)
 Function: 
 Returns : value of treetype
 Args    : newvalue (optional)


=cut

sub treetype{
   my ($self,$value) = @_;
   if( defined $value) {
      $self-&gt;{'treetype'} = $value;
    }
    return $self-&gt;{'treetype'};
}

=head2 nodetype

 Title   : nodetype
 Usage   : $obj-&gt;nodetype($newval)
 Function: 
 Returns : value of nodetype
 Args    : newvalue (optional)


=cut

sub nodetype{
   my ($self,$value) = @_;
   if( defined $value) {
      $self-&gt;{'nodetype'} = $value;
    }
    return $self-&gt;{'nodetype'};
}


=head2 SAX methods

=cut

=head2 start_document

 Title   : start_document
 Usage   : $handler-&gt;start_document
 Function: Begins a Tree event cycle
 Returns : none 
 Args    : none

=cut

sub start_document {
   my ($self) = @_;   
   $self-&gt;{'_lastitem'} = {};
   $self-&gt;{'_currentitems'} = [];
   $self-&gt;{'_currentnodes'} = [];
   return;
}

=head2 end_document

 Title   : end_document
 Usage   : my @trees = $parser-&gt;end_document
 Function: Finishes a Phylogeny cycle
 Returns : An array  Bio::Tree::TreeI
 Args    : none

=cut

sub end_document {
    my ($self,$label) = @_; 

    my ($root) = @{$self-&gt;{'_currentnodes'}};

    $self-&gt;debug("Root node is " . $root-&gt;to_string()."\n");
    if( $self-&gt;verbose &gt; 0 ) { 
	foreach my $node ( $root-&gt;get_Descendents  ) {
	    $self-&gt;debug("node is ". $node-&gt;to_string(). "\n");
	}
    }
    my $tree = $self-&gt;treetype-&gt;new(-verbose =&gt; $self-&gt;verbose,
				    -root =&gt; $root);
    return $tree;       
}

=head2 start_element

 Title   : start_element
 Usage   :
 Function:
 Example :
 Returns : 
 Args    : $data =&gt; hashref with key 'Name'

=cut

sub start_element{
   my ($self,$data) =@_;
   $self-&gt;{'_lastitem'}-&gt;{$data-&gt;{'Name'}}++;   

   $self-&gt;debug("starting element: $data-&gt;{Name}\n");   
   push @{$self-&gt;{'_lastitem'}-&gt;{'current'}},$data-&gt;{'Name'};
   
   my %data;
   
   if( $data-&gt;{'Name'} eq 'node' ) {
       push @{$self-&gt;{'_currentitems'}}, \%data; 
       $self-&gt;{'_treelevel'}++;
   } elsif ( $data-&gt;{Name} eq 'tree' ) {
   }
}

=head2 end_element

 Title   : end_element
 Usage   : 
 Function:
 Returns : none
 Args    : $data =&gt; hashref with key 'Name'

=cut

sub end_element{
   my ($self,$data) = @_;   

   $self-&gt;debug("end of element: $data-&gt;{Name}\n");
   # this is the stack where we push/pop items from it
   my $curcount = scalar @{$self-&gt;{'_currentnodes'}};
   my $level   = $self-&gt;{'_treelevel'};
   my $levelct = $self-&gt;{'_nodect'}-&gt;[$self-&gt;{'_treelevel'}+1] || 0;

   if( $data-&gt;{'Name'} eq 'node' ) {
       my $tnode;
       my $node = pop @{$self-&gt;{'_currentitems'}};	   

       $tnode = $self-&gt;nodetype-&gt;new( -verbose =&gt; $self-&gt;verbose,
				      %{$node});       
       $self-&gt;debug( "new node will be ".$tnode-&gt;to_string."\n");
       if ( !$node-&gt;{'-leaf'} &amp;&amp; $levelct &gt; 0) {
	   $self-&gt;debug(join(',', map { $_-&gt;to_string } 
			     @{$self-&gt;{'_currentnodes'}}). "\n");
	   $self-&gt;throw("something wrong with event construction treelevel ".
			"$level is recorded as having $levelct nodes  ".
			"but current nodes at this level is $curcount\n")
	       if( $levelct &gt; $curcount);	
	   for ( splice( @{$self-&gt;{'_currentnodes'}}, - $levelct)) {
	       $self-&gt;debug("adding desc: " . $_-&gt;to_string . "\n");
	       $tnode-&gt;add_Descendent($_);
	   }
	   $self-&gt;{'_nodect'}-&gt;[$self-&gt;{'_treelevel'}+1] = 0;
       }
       push @{$self-&gt;{'_currentnodes'}}, $tnode;
       $self-&gt;{'_nodect'}-&gt;[$self-&gt;{'_treelevel'}]++;
       
       $curcount = scalar @{$self-&gt;{'_currentnodes'}};
       $self-&gt;debug ("added node: count is now $curcount, treelevel: $level, nodect: $levelct\n");

       $self-&gt;{'_treelevel'}--;       
   } elsif(  $data-&gt;{'Name'} eq 'tree' ) { 
       $self-&gt;debug("end of tree: nodes in stack is $curcount\n");
   }

   $self-&gt;{'_lastitem'}-&gt;{ $data-&gt;{'Name'} }--; 
   pop @{$self-&gt;{'_lastitem'}-&gt;{'current'}};
}


=head2 in_element

 Title   : in_element
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub in_element{
   my ($self,$e) = @_;

   return 0 if ! defined $self-&gt;{'_lastitem'} || 
       ! defined $self-&gt;{'_lastitem'}-&gt;{'current'}-&gt;[-1];
   return ($e eq $self-&gt;{'_lastitem'}-&gt;{'current'}-&gt;[-1]);

}

=head2 within_element

 Title   : within_element
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub within_element{
   my ($self,$e) = @_;
   return $self-&gt;{'_lastitem'}-&gt;{$e};
}

=head2 characters

 Title   : characters
 Usage   : $handler-&gt;characters($text);
 Function: Processes characters 
 Returns : none
 Args    : text string


=cut

sub characters{
   my ($self,$ch) = @_;
   if( $self-&gt;within_element('node') ) {
       my $hash = pop @{$self-&gt;{'_currentitems'}};
       if( $self-&gt;in_element('bootstrap') ) {
	   # leading/trailing Whitespace-B-Gone
	   $ch =~ s/^\s+//; $ch =~ s/\s+$//;  
	   $hash-&gt;{'-bootstrap'} = $ch;
       } elsif( $self-&gt;in_element('branch_length') ) {
	   # leading/trailing Whitespace-B-Gone
	   $ch =~ s/^\s+//; $ch =~ s/\s+$//;
	   $hash-&gt;{'-branch_length'} = $ch;
       } elsif( $self-&gt;in_element('id')  ) {
	   $hash-&gt;{'-id'} = $ch;
       } elsif( $self-&gt;in_element('description') ) {
	   $hash-&gt;{'-desc'} = $ch;
       } elsif ( $self-&gt;in_element('tag_name') ) {
	   $hash-&gt;{'-NHXtagname'} = $ch;
       } elsif ( $self-&gt;in_element('tag_value') ) {
	   $hash-&gt;{'-nhx'}-&gt;{$hash-&gt;{'-NHXtagname'}} = $ch;
	   delete $hash-&gt;{'-NHXtagname'};
       } elsif( $self-&gt;in_element('leaf') ) {
	   $hash-&gt;{'-leaf'} = $ch;
       }
       push @{$self-&gt;{'_currentitems'}}, $hash;
   }
   $self-&gt;debug("chars: $ch\n");
}


1;
</pre></body></html>