Class: Dphil::Tree

Inherits:
Object
  • Object
show all
Includes:
LDOutput
Defined in:
lib/dphil/tree.rb

Overview

Phylogenetic Tree generated from parsing PAUP output.

Immutable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LDOutput

#as_jsonld, #to_jsonld

Constructor Details

#initialize(id = nil, lengths = nil, stats = nil, **opts) ⇒ Tree

Returns a new instance of Tree.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dphil/tree.rb', line 13

def initialize(id = nil, lengths = nil, stats = nil, **opts)
  @id = (opts[:id] || id).to_i
  if lengths.respond_to?(:to_str) && stats.respond_to?(:to_str)
    @nodes = nodes_from_lengths(parse_paup_lengths(lengths))
    @stats = parse_paup_stats(stats)
  elsif (opts.keys & %i[nodes stats]).length == 2
    @nodes = parse_json_nodes(opts[:nodes])
    @stats = parse_json_stats(opts[:stats])
  end
  @tree = tree_from_nodes(nodes)
  IceNine.deep_freeze(self)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/dphil/tree.rb', line 11

def id
  @id
end

#nodesObject (readonly)

Returns the value of attribute nodes.



11
12
13
# File 'lib/dphil/tree.rb', line 11

def nodes
  @nodes
end

#statsObject (readonly)

Returns the value of attribute stats.



11
12
13
# File 'lib/dphil/tree.rb', line 11

def stats
  @stats
end

#treeObject (readonly)

Returns the value of attribute tree.



11
12
13
# File 'lib/dphil/tree.rb', line 11

def tree
  @tree
end

Instance Method Details

#as_json(options = nil) ⇒ Object



35
36
37
# File 'lib/dphil/tree.rb', line 35

def as_json(options = nil)
  to_h.as_json(options)
end

#ciObject



59
60
61
# File 'lib/dphil/tree.rb', line 59

def ci
  stats[:ci]
end

#get_children(node) ⇒ Object



51
52
53
# File 'lib/dphil/tree.rb', line 51

def get_children(node)
  node.children&.map { |id| nodes[id] }
end

#get_node(id) ⇒ Object



43
44
45
# File 'lib/dphil/tree.rb', line 43

def get_node(id)
  nodes[id]
end

#get_parent(node) ⇒ Object



47
48
49
# File 'lib/dphil/tree.rb', line 47

def get_parent(node)
  nodes[node.parent]
end

#rootObject



39
40
41
# File 'lib/dphil/tree.rb', line 39

def root
  nodes[tree.id]
end

#to_hObject



26
27
28
29
30
31
32
33
# File 'lib/dphil/tree.rb', line 26

def to_h
  {
    id: id,
    root_id: tree.id,
    nodes: nodes,
    stats: stats,
  }
end

#tree_lengthObject



55
56
57
# File 'lib/dphil/tree.rb', line 55

def tree_length
  stats[:length]
end