3 # BioPerl module for Bio::TreeIO
5 # Cared for by Jason Stajich <jason@bioperl.org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::TreeIO - Parser for Tree files
21 my $treeio = new Bio::TreeIO('-format' => 'newick',
22 '-file' => 'globin.dnd');
23 while( my $tree = $treeio->next_tree ) {
24 print "Tree is ", $tree->size, "\n";
30 This is the driver module for Tree reading from data streams and
31 flatfiles. This is intended to be able to create Bio::Tree::TreeI
38 User feedback is an integral part of the evolution of this and other
39 Bioperl modules. Send your comments and suggestions preferably to
40 the Bioperl mailing list. Your participation is much appreciated.
42 bioperl-l@bioperl.org - General discussion
43 http://bioperl.org/MailList.shtml - About the mailing lists
47 Report bugs to the Bioperl bug tracking system to help us keep track
48 of the bugs and their resolution. Bug reports can be submitted via
51 bioperl-bugs@bioperl.org
52 http://bioperl.org/bioperl-bugs/
54 =head1 AUTHOR - Jason Stajich
56 Email jason@bioperl.org
58 Describe contact details here
62 Additional contributors names and emails here
66 The rest of the documentation details each of the object methods.
67 Internal methods are usually preceded with a _
72 # Let the code begin...
79 # Object preamble - inherits from Bio::Root::Root
83 use Bio::Event::EventGeneratorI;
84 use Bio::TreeIO::TreeEventBuilder;
85 use Bio::Factory::TreeFactoryI;
87 @ISA = qw(Bio::Root::Root Bio::Root::IO
88 Bio::Event::EventGeneratorI Bio::Factory::TreeFactoryI);
93 Usage : my $obj = new Bio::TreeIO();
94 Function: Builds a new Bio::TreeIO object
102 my($caller,@args) = @_;
103 my $class = ref($caller) || $caller;
105 # or do we want to call SUPER on an object if $caller is an
107 if( $class =~ /Bio::TreeIO::(\S+)/ ) {
108 my ($self) = $class->SUPER::new
(@args);
109 $self->_initialize(@args);
114 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
115 my $format = $param{'-format'} ||
116 $class->_guess_format( $param{'-file'} || $ARGV[0] ) ||
118 $format = "\L$format"; # normalize capitalization to lower case
120 # normalize capitalization
121 return undef unless( $class->_load_format_module($format) );
122 return "Bio::TreeIO::$format"->new(@args);
130 Usage : my $tree = $treeio->next_tree;
131 Function: Gets the next tree off the stream
132 Returns : Bio::Tree::TreeI or undef if no more trees
139 $self->throw("Cannot call method next_tree on Bio::TreeIO object must use a subclass");
145 Usage : $treeio->write_tree($tree);
146 Function: Writes a tree onto the stream
148 Args : Bio::Tree::TreeI
154 my ($self,$tree) = @_;
155 $self->throw("Cannot call method write_tree on Bio::TreeIO object must use a subclass");
159 =head2 attach_EventHandler
161 Title : attach_EventHandler
162 Usage : $parser->attatch_EventHandler($handler)
163 Function: Adds an event handler to listen for events
165 Args : Bio::Event::EventHandlerI
169 sub attach_EventHandler
{
170 my ($self,$handler) = @_;
171 return if( ! $handler );
172 if( ! $handler->isa('Bio::Event::EventHandlerI') ) {
173 $self->warn("Ignoring request to attatch handler ".ref($handler). ' because it is not a Bio::Event::EventHandlerI');
175 $self->{'_handler'} = $handler;
181 Title : _eventHandler
183 Function: Get the EventHandler
184 Returns : Bio::Event::EventHandlerI
192 return $self->{'_handler'};
196 my($self, @args) = @_;
197 $self->{'_handler'} = undef;
199 # initialize the IO part
200 $self->_initialize_io(@args);
201 $self->attach_EventHandler(new Bio
::TreeIO
::TreeEventBuilder
(-verbose
=> $self->verbose(), @args));
204 =head2 _load_format_module
206 Title : _load_format_module
207 Usage : *INTERNAL TreeIO stuff*
208 Function: Loads up (like use) a module at run time on demand
215 sub _load_format_module
{
216 my ($self,$format) = @_;
217 my $module = "Bio::TreeIO::" . $format;
221 $ok = $self->_load_module($module);
225 $self: $format cannot be found
227 For more information about the TreeIO system please see the TreeIO docs.
228 This includes ways of checking for formats at compile time, not run time
238 Title : _guess_format
239 Usage : $obj->_guess_format($filename)
242 Returns : guessed format of filename (lower case)
249 return unless $_ = shift;
250 return 'newick' if /\.(dnd|newick|nh)$/i;
251 return 'phyloxml' if /\.(xml)$/i;
262 return bless {'treeio' => shift},$class;
267 return $self->{'treeio'}->next_tree() unless wantarray;
269 push @list,$obj while $obj = $self->{'treeio'}->next_tree();
275 $self->{'treeio'}->write_tree(@_);