Class: Nexml::Document::Tree

Inherits:
Nexml::DummyBase show all
Defined in:
lib/nexml.rb

Overview

need to refact to allow for parent->child nodes

Instance Attribute Summary collapse

Attributes inherited from Nexml::DummyBase

#attributes, #text

Instance Method Summary collapse

Methods inherited from Nexml::DummyBase

#attribute

Constructor Details

#initialize(opts) ⇒ Tree

Returns a new instance of Tree.



257
258
259
260
261
# File 'lib/nexml.rb', line 257

def initialize(opts)
  super
  @nodes = []
  @edges = []
end

Instance Attribute Details

#edgesObject

Returns the value of attribute edges.



256
257
258
# File 'lib/nexml.rb', line 256

def edges
  @edges
end

#nodesObject

Returns the value of attribute nodes.



256
257
258
# File 'lib/nexml.rb', line 256

def nodes
  @nodes
end

Instance Method Details

#children_of_node(node) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/nexml.rb', line 270

def children_of_node(node)
  @children = []
  @edges.each do |e|
    @children.push(self.node_by_id(e.attributes['target'])) if (e.attributes['source'] ==  node.attributes['id'])
  end
  @children 
end

#newick_string(node, string = '') ⇒ Object

NOT WORKING - needs refactoring



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/nexml.rb', line 286

def newick_string(node, string = '')
  @str = string
  nodes = self.children_of_node(node)
    nodes.each do |n|
      @str += self.otu_by_id(node.attributes['otu']).attributes['label']           
      self.newick_string(n, @str)
      @str += "," 
    end 

 #    case nodes.size
 #   when 0   
 #     @str += ""
 #   when 1 
 #    @str += ",(" 
 #   else 
 #     @str += ")"    
 #   end        
 
  @str 
end

#node_by_id(id) ⇒ Object

make a big hash store to handle these



279
280
281
282
283
# File 'lib/nexml.rb', line 279

def node_by_id(id)
  @nodes.each do |n|
    return n if n.attributes['id'] == id 
  end
end

#root_nodeObject

yuck



264
265
266
267
268
# File 'lib/nexml.rb', line 264

def root_node
  @nodes.each do |n|
    return n if n.attributes['root'] 
  end 
end