Class: Bio::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/nwk.rb

Defined Under Namespace

Classes: Node

Constant Summary collapse

DELIMITER =
';'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#index2tipObject

Returns the value of attribute index2tip.



8
9
10
# File 'lib/bio/nwk.rb', line 8

def index2tip
  @index2tip
end

#tip2indexObject

Returns the value of attribute tip2index.



8
9
10
# File 'lib/bio/nwk.rb', line 8

def tip2index
  @tip2index
end

Instance Method Details

#all_nodes_of_subtree(node) ⇒ Object



22
23
24
# File 'lib/bio/nwk.rb', line 22

def all_nodes_of_subtree(node)
  [leaves(node), tips(node)].flatten
end

#allSubtreeRepresentativesObject



87
88
89
90
91
92
93
# File 'lib/bio/nwk.rb', line 87

def allSubtreeRepresentatives
  arr = Array.new
  nodes.each do |node|
    arr << tips(node)
  end
  return(arr)
end

#allTipsObject



96
97
98
99
100
101
102
# File 'lib/bio/nwk.rb', line 96

def allTips
  a = Array.new
  nodes.each do |node|
    a << node if node.isTip?(self)
  end
  return(a)
end

#cleanName!Object



27
28
29
30
31
# File 'lib/bio/nwk.rb', line 27

def cleanName!
  allTips.each do |tip|
    tip.name.gsub!(' ', '_')
  end
end

#cleanNewickObject



117
118
119
120
121
122
# File 'lib/bio/nwk.rb', line 117

def cleanNewick
  output = output_newick
  output.gsub!(/[\n\s]/, '')
  output.gsub!(/[']/, '')
  return(output)
end

#getAlldistancesObject



153
154
155
156
157
158
159
160
# File 'lib/bio/nwk.rb', line 153

def getAlldistances()
  distances = Array.new
  each_edge do |node0, node1, edge|
    next if node0 == root or node1 == root
    distances << edge.distance
  end
  return(distances)
end

#getNameNodeRelaObject



194
195
196
197
198
199
200
201
# File 'lib/bio/nwk.rb', line 194

def getNameNodeRela()
  node2name, name2node = [Hash.new, Hash.new]
  nodes.each do |node|
    node2name[node] = node.name
    name2node[node.name] = node
  end
  return([name2node, node2name])
end

#getNameTipRelaObject



184
185
186
187
188
189
190
191
# File 'lib/bio/nwk.rb', line 184

def getNameTipRela()
  tip2name, name2tip = [Hash.new, Hash.new]
  allTips.each do |tip|
    tip2name[tip] = tip.name
    name2tip[tip.name] = tip
  end
  return([name2tip, tip2name])
end

#internal_nodesObject



76
77
78
79
80
81
82
83
84
# File 'lib/bio/nwk.rb', line 76

def internal_nodes
  a = Array.new
  nodes.each do |node|
    if not node.isTip?(self)
      a << node
    end
  end
  return(a)
end

#newickIndexToTipObject



144
145
146
147
148
149
150
# File 'lib/bio/nwk.rb', line 144

def newickIndexToTip()
  tipToIndex if @index2tip.empty?
  allTips.each do |tip|
    index = tip.name
    tip.name = @index2tip[index]
  end
end

#newickTipToIndexObject



135
136
137
138
139
140
141
# File 'lib/bio/nwk.rb', line 135

def newickTipToIndex()
  tipToIndex
  allTips.each do |tip|
    index = @tip2index[tip.name.gsub(' ', '_')]
    tip.name = (index).to_s
  end
end

#normalizeBranchLength!Object



163
164
165
166
167
168
# File 'lib/bio/nwk.rb', line 163

def normalizeBranchLength!()
  min, max = getAlldistances().minmax
  each_edge do |node0, node1, edge|
    edge.distance = (edge.distance-min+1e-10).to_f/(max-min)
  end
end

#normalizeBranchLengthGainAndLoss!Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bio/nwk.rb', line 171

def normalizeBranchLengthGainAndLoss!()
  min, max = getAlldistances().select{|i|i>=0}.minmax
  each_edge do |node0, node1, edge|
    edge.distance = (edge.distance-min+1e-10).to_f/(max-min) + 1 if edge.distance >= 0
  end

  min, max = getAlldistances().select{|i|i<0}.minmax
  each_edge do |node0, node1, edge|
    edge.distance = (edge.distance-min+1e-10).to_f/(max-min) if edge.distance < 0
  end
end

#outputNexus(isTranslate = false, isTip2Index = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/bio/nwk.rb', line 105

def outputNexus(isTranslate=false, isTip2Index=false)
  puts "#NEXUS"
  puts
  puts "BEGIN TREES;"
  translate if isTranslate
  tipToIndex and newickTipToIndex if isTip2Index
  puts ["\t"+'TREE TREE1', cleanNewick()].join('= ')
  puts ['ENDBLOCK', DELIMITER].join('')
  newickIndexToTip if isTip2Index
end

#sister(node) ⇒ Object



71
72
73
# File 'lib/bio/nwk.rb', line 71

def sister(node)
  return(sisters(node)[0])
end

#sisters(node) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/bio/nwk.rb', line 62

def sisters(node)
  if node == root
    return([])
  else
    return(children(parent(node)).select{|i|i!=node})
  end
end

#tips(node) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/bio/nwk.rb', line 11

def tips(node)
  rv = Array.new
  if node.isTip?(self)
    rv = [node]
  else
    rv = descendents(node).select{|n|children(n).empty?}
  end
  return(rv)
end

#tipToIndexObject



125
126
127
128
129
130
131
132
# File 'lib/bio/nwk.rb', line 125

def tipToIndex
  @tip2index = Hash.new
  @index2tip = Hash.new
  allTips.each_with_index do |tip, index|
    @tip2index[tip.name.gsub(' ','_')] = (index+1).to_s
    @index2tip[(index+1).to_s] = tip.name
  end
end

#twoTaxaNode(node) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/bio/nwk.rb', line 34

def twoTaxaNode(node)
  if node.isTip?(self)
    return(node)
  else
    return children(node).map{|child|tips(child).sort_by{|i|i.name}.shift}.sort_by{|i|i.name}
  end
end

#twoTaxaNodeName(node) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/bio/nwk.rb', line 43

def twoTaxaNodeName(node)
  if node.isTip?(self)
    return(node.name)
  else
    return children(node).map{|child|tips(child).sort_by{|i|i.name}.shift}.sort_by{|i|i.name}.map{|i|i.name}
  end
end

#twoTaxaNodeNameStr(node, sep = '|') ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/bio/nwk.rb', line 52

def twoTaxaNodeNameStr(node, sep='|')
  a = twoTaxaNodeName(node)
  if a.is_a?(Array)
    return(a.join(sep))
  else
    return(a)
  end
end