[BioRuby] Bio::Tree#children IndexError when called on a root node which does not have children.

Diana Jaunzeikare rozziite at gmail.com
Sun Jul 12 02:18:12 UTC 2009

Hi all,

While i was writing parser for Phyloxml I discovered such behavior in
Bio::Tree class.

Bio::Tree#children method gives IndexError if it is called on a root node
which does not have children.

irb(main):002:0> tree = Bio::Tree.new
=> #<Bio::Tree:0xb7d759e4 @root=nil, @pathway=#<Bio::Pathway:0xb7c490e8
@graph={}, @relations=[], @label={}, @undirected=true, @index={}>,
 irb(main):004:0> node = Bio::Tree::Node.new
=> (Node:b7c43f80)
irb(main):005:0> node.name = "node1"
=> "node1"
irb(main):006:0> tree.root = node
 => (Node:"node1")
irb(main):007:0> tree.children(tree.root)
IndexError: node1 not found
    from /usr/local/lib/site_ruby/1.8/bio/tree.rb:591:in `path'
    from /usr/local/lib/site_ruby/1.8/bio/tree.rb:640:in `children'
     from (irb):7

If the children method is called on other than root node (which does not
have children), then it correctly gives empty array:

irb(main):008:0> node2 = Bio::Tree::Node.new
=> (Node:b7c3b088)
irb(main):009:0> node2.name = "node2"
=> "node2"
irb(main):010:0> tree.add_node(node2)
=> #<Bio::Tree:0xb7d759e4 @root=(Node:"node1"),
@pathway=#<Bio::Pathway:0xb7c490e8 @graph={(Node:"node2")=>{}},
@relations=[], @label={}, @undirected=true, @index={}>, @options={}>
irb(main):011:0> tree.add_edge(tree.root, node2)
=> <Edge distance=nil>
irb(main):012:0> tree.children(node2)
=> []

If the root node has children, then everything is fine:

 irb(main):013:0> tree.children(tree.root)
=> [(Node:"node2")]


More information about the BioRuby mailing list