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
A file level unique identifier.
A human readable description.
A rootedge to indicate a time span leading up to the root.
Public Class Methods
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
# 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
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
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 an edge to the tree.
# File lib/bio/db/nexml/trees.rb, line 270 def add_edge( edge ) super( edge.source, edge.target, edge ) __add_edge__( edge ) end
Add a node to the tree.
# File lib/bio/db/nexml/trees.rb, line 261 def add_node( node ) super( node ) __add_node__( node ) end
# File lib/bio/db/nexml/trees.rb, line 275 def add_rootedge( edge ) @rootedge = edge end
# 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
# 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
# File lib/bio/db/nexml/trees.rb, line 185 def create_edge( options = {} ) raise "This is supposed to be an abstract method" end
# 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
# File lib/bio/db/nexml/trees.rb, line 189 def create_rootedge( options = {} ) raise "This is supposed to be an abstract method" end
# 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
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
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
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
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
Fetch an edge by the given id.
# File lib/bio/db/nexml/trees.rb, line 314 def get_edge_by_id( id ); end
Fetch a node by the given id.
# File lib/bio/db/nexml/trees.rb, line 307 def get_node_by_id( id ); end
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.
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
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
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
# 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
Return the number of edges in the tree.
# File lib/bio/db/nexml/trees.rb, line 353 def number_of_edges; end
Return the number of nodes in the tree.
# File lib/bio/db/nexml/trees.rb, line 350 def number_of_nodes; end
Returns the parent of the given node corresponding to each root.
# 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 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.
# 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
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.
# File lib/bio/db/nexml/trees.rb, line 285 def remove_node( node ) return unless include?( node ) super( node ) __delete_node__( node ) end
Returns an array of root nodes.
# File lib/bio/db/nexml/trees.rb, line 241 def roots nodes.select{ |n| n.root? } end
# 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