Class: Dendrograms::ConsensusNode

Inherits:
Object
  • Object
show all
Defined in:
lib/Dendrograms/Consensus.rb

Constant Summary collapse

@@index =
0
@@leaves =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children) ⇒ ConsensusNode

Returns a new instance of ConsensusNode.



6
7
8
9
10
11
# File 'lib/Dendrograms/Consensus.rb', line 6

def initialize(children)
  @children = children
  @index = @@index
  @@index += 1
  @leaves = children.dup
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



4
5
6
# File 'lib/Dendrograms/Consensus.rb', line 4

def index
  @index
end

Class Method Details

.leaf(leaf) ⇒ Object



14
15
16
17
# File 'lib/Dendrograms/Consensus.rb', line 14

def ConsensusNode.leaf(leaf)
  @@leaves[leaf] ||= @@leaves.size
  return @@leaves[leaf]
end

Instance Method Details

#add_child(node) ⇒ Object



23
24
25
26
# File 'lib/Dendrograms/Consensus.rb', line 23

def add_child(node)
  @children = @children - node.children
  @children.push node
end

#childrenObject



19
20
21
# File 'lib/Dendrograms/Consensus.rb', line 19

def children
  @children.map { |x| x.is_a?(ConsensusNode) ? x.children : x }.flatten
end

#contains(set) ⇒ Object



28
29
30
# File 'lib/Dendrograms/Consensus.rb', line 28

def contains(set)
  (@leaves & set).size == set.size
end

#sizeObject



32
33
34
# File 'lib/Dendrograms/Consensus.rb', line 32

def size
  @leaves.size
end

#to_dot(wordmap) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/Dendrograms/Consensus.rb', line 36

def to_dot(wordmap)
  puts "\tINTERNAL_#{@index} [shape=point, label=\"\"];"
  @children.each do |child|
    if child.is_a?(ConsensusNode)
      puts "\tINTERNAL_#{@index} -- INTERNAL_#{child.index};"
      child.to_dot(wordmap)
    else
      puts "\tLEAF_#{ConsensusNode.leaf(child)} [shape=none, label=\"#{wordmap[child.to_s]}\"];"
      puts "\tINTERNAL_#{@index} -- LEAF_#{ConsensusNode.leaf(child)};"
    end
  end
end