class Bio::NeXML::Tree

An NeXML tree. A tree must have a unique ‘id’. It may optionally take a ‘label’ and a ‘rootedge’. This class inherits from Bio::Tree; naturally its functionality is leveraged here.

Attributes

id[RW]

A file level unique identifier.

label[RW]

A human readable description.

rootedge[RW]

A rootedge to indicate a time span leading up to the root.

Public Class Methods

new( id = nil, options = {}, &block ) click to toggle source

Create a new tree.

tree = Bio::NeXML::Tree.new( 'tree1' )
tree = Bio::NeXML::Tree.new( 'tree1', :label => 'A tree' )

nodes = %w|node1 node2 node3|.map{ |n| Bio::NeXML::Node.new( n ) }                
edge1 = Bio::NeXML::Edge.new( 'edge1', :source => nodes[0], :target => nodes[1] )
edge2 = Bio::NeXML::Edge.new( 'edge2', :source => nodes[0], :target => nodes[2] )

tree = Bio::NeXML::Tree.new( 'tree1', :nodes => nodes )
tree = Bio::NeXML::Tree.new do |t|                                        
         t.label = 'A tree'
         t.nodes = nodes
         t.edges = [ edge1, edge2 ]
         # or, t << edge1 << edge2
         # or, t.add_edge( edge1 ); t.add_edge( edge2 )

         root = Bio::NeXML::Node.new( 'root1', :root => true )
         rootedge = Bio::NeXML::RootEdge.new( 're1', :target => root )

         t << root # or, t.add_otu( root )
         t.rootedge = rootedge
       end
Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 233
def initialize( id = nil, options = {}, &block )
  super()
  @id = id
  properties( options ) unless options.empty?
  block.arity < 1 ? instance_eval( &block ) : block.call( self ) if block_given?
end

Public Instance Methods

<<( object ) click to toggle source

Append a node or an edge to the tree. The method delegates the actuall addition to add_node, add_edge methods.

# File lib/bio/db/nexml/trees.rb, line 247
def <<( object )
  case object
  when Node
    add_node( object )
  when Edge
    add_edge( object )
  end
  self
end
[]( id ) click to toggle source

Fetch a node, or an edge by its id.

# File lib/bio/db/nexml/trees.rb, line 317
def []( id )
  get_node_by_id( id ) ||
    get_edge_by_id( id )
end
add_edge( edge ) click to toggle source

Add an edge to the tree.

Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 270
def add_edge( edge )
  super( edge.source, edge.target, edge )
  __add_edge__( edge )
end
add_node( node ) click to toggle source

Add a node to the tree.

Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 261
def add_node( node )
  super( node )
  __add_node__( node )
end
add_rootedge( edge ) click to toggle source
# File lib/bio/db/nexml/trees.rb, line 275
def add_rootedge( edge )
  @rootedge = edge
end
ancestors( node, *root ) click to toggle source
Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 414
def ancestors( node, *root )
  if root.empty?
    raise IndexError, 'can not get parent for unrooted tree' if self.root.empty?
    root = self.root
  end
  ancestor = {}
  root.each do |r|
    ancestor[ r ] = super( node, r )
  end
  ancestor
end
children( node, *root ) click to toggle source
# File lib/bio/db/nexml/trees.rb, line 375
def children( node, *root )
  if root.empty?
    raise IndexError, 'can not get parent for unrooted tree' if self.root.empty?
    root = self.root
  end
  childrens = {}
  root.each do |r|
    c = adjacent_nodes(node)
    c.delete(parent(node, r)[ r ])
    childrens[ r ] = c
  end

  childrens
end
create_edge( options = {} ) click to toggle source
# File lib/bio/db/nexml/trees.rb, line 185
def create_edge( options = {} )
  raise "This is supposed to be an abstract method"
end
create_node( options = {} ) click to toggle source
# File lib/bio/db/nexml/trees.rb, line 179
def create_node( options = {} )
  node = Node.new( Bio::NeXML.generate_id( Node ), options )
  self << node
  node        
end
create_rootedge( options = {} ) click to toggle source
# File lib/bio/db/nexml/trees.rb, line 189
def create_rootedge( options = {} )
  raise "This is supposed to be an abstract method"
end
delete_edge( edge )
Alias for: remove_edge
delete_node( node )
Alias for: remove_node
descendents( node, *root ) click to toggle source
Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 390
def descendents( node, *root )
  if root.empty?
    raise IndexError, 'can not get parent for unrooted tree' if self.root.empty?
    root = self.root
  end
  descendent = {}
  root.each do |r|
    descendent[ r ] = super( node, r )
  end
  descendent
end
each_edge() { |edge| ... } click to toggle source

Iterate over each edge. Return an Enumerator if no block is given.

# File lib/bio/db/nexml/trees.rb, line 343
def each_edge; end
each_edge_with_id() click to toggle source

Iterate over each node passing id and the node itself to the given block. Returns an Enumerator if no block is given.

# File lib/bio/db/nexml/trees.rb, line 347
def each_edge_with_id; end
each_node( &block ) click to toggle source

Iterate over each node. Return an Enumerator if no block is given.

# File lib/bio/db/nexml/trees.rb, line 336
def each_node( &block ); end
each_node_with_id() click to toggle source

Iterate over each node passing id and the node itself to the given block. Returns an Enumerator if no block is given.

# File lib/bio/db/nexml/trees.rb, line 340
def each_node_with_id; end
get_edge_by_id( id ) click to toggle source

Fetch an edge by the given id.

# File lib/bio/db/nexml/trees.rb, line 314
def get_edge_by_id( id ); end
get_node_by_id( id ) click to toggle source

Fetch a node by the given id.

# File lib/bio/db/nexml/trees.rb, line 307
def get_node_by_id( id ); end
Also aliased as: get_node_by_name
get_node_by_name( id )

get_node_by_name is actually defined in Bio::Tree. I have aliased it to get_node_by_id as hash lookup is faster than searching through an enumerable.

Alias for: get_node_by_id
has?( object )
Alias for: include?
has_edge?( edge ) click to toggle source

Returns true if the given edge is a part of self.

# File lib/bio/db/nexml/trees.rb, line 326
def has_edge?( edge ); end
has_node?( node ) click to toggle source

Returns true if the given node is a part of self.

# File lib/bio/db/nexml/trees.rb, line 323
def has_node?( node ); end
include?( object ) click to toggle source

Returns true if the given node or the edge object is a part of this tree; false otherwise.

# File lib/bio/db/nexml/trees.rb, line 329
def include?( object )
  has_node?( object ) ||
    has_edge?( object )
end
Also aliased as: has?
lowest_common_ancestor( node1, node2, *root ) click to toggle source
Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 402
def lowest_common_ancestor( node1, node2, *root )
  if root.empty?
    raise IndexError, 'can not get parent for unrooted tree' if self.root.empty?
    root = self.root
  end
  lca = {}
  root.each do |r|
    lca[ r ] = super( node1, node2, r )
  end
  lca
end
number_of_edges() click to toggle source

Return the number of edges in the tree.

# File lib/bio/db/nexml/trees.rb, line 353
def number_of_edges; end
number_of_nodes() click to toggle source

Return the number of nodes in the tree.

# File lib/bio/db/nexml/trees.rb, line 350
def number_of_nodes; end
parent( node, *roots ) click to toggle source

Returns the parent of the given node corresponding to each root.

Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 363
def parent( node, *roots )
  if roots.empty?
    raise IndexError, 'can not get parent for unrooted tree' if self.roots.empty?
    roots = self.roots
  end
  parents = {}
  roots.each do |r|
    parents[ r ] = super( node, r )
  end
  parents
end
remove_edge( edge ) click to toggle source

Remove an edge from the tree. Returns the edge deleted. If more than one edge exists between the source and the target, both of them will be removed. Raises IndexError if the edge is not found in the tree.

Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 299
def remove_edge( edge )
  return unless include?( edge )
  super( edge.source, edge.target )
  __delete_edge__( edge )
end
Also aliased as: delete_edge
remove_node( node ) click to toggle source

Remove a node from the tree. Returns the deleted node. It automatically removes all edges connected to that node. Raises IndexError if the node is not found in the tree.

Calls superclass method
# File lib/bio/db/nexml/trees.rb, line 285
def remove_node( node )
  return unless include?( node )
  super( node )
  __delete_node__( node )
end
Also aliased as: delete_node
roots() click to toggle source

Returns an array of root nodes.

# File lib/bio/db/nexml/trees.rb, line 241
def roots
  nodes.select{ |n| n.root? }
end
to_xml() click to toggle source
# File lib/bio/db/nexml/trees.rb, line 162
def to_xml
  node = @@writer.create_node( "tree", @@writer.attributes( self, :id, :'xsi:type', :label ) )

  self.each_node do |n|
    node << n.to_xml
  end

  rootedge = self.rootedge
  node << rootedge.to_xml if rootedge

  self.each_edge do |edge|
    node << edge.to_xml
  end

  node
end