Class: NewickNode
- Inherits:
-
Object
- Object
- NewickNode
- Defined in:
- lib/Newick.rb
Overview
Represents a single node in a NewickTree
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
child nodes of node.
-
#edgeLen ⇒ Object
edge length of node.
-
#name ⇒ Object
name of node.
-
#parent ⇒ Object
parent node of node.
-
#x ⇒ Object
x position of node.
-
#y ⇒ Object
y position of node.
Instance Method Summary collapse
-
#addChild(child) ⇒ Object
adds child node to list of children and sets child’s parent to self.
-
#calcXPos ⇒ Object
calculates node X positions.
-
#calcYPos ⇒ Object
calculates node Y positions.
-
#descendants ⇒ Object
returns array of all descendant nodes.
-
#distToAncestor(ancestor) ⇒ Object
returns the distance to the ancestor node.
-
#findNode(name) ⇒ Object
returns node with given name, or nil if not found.
-
#include?(node) ⇒ Boolean
True if given node is child (or grandchild, etc.) of self.
-
#initialize(name, edgeLen) ⇒ NewickNode
constructor
A new instance of NewickNode.
-
#intNodes ⇒ Object
returns array of non leaves (taxa) that are contained in the node.
-
#lca(node) ⇒ Object
returns the last common ancestor node of self and given node.
-
#leaf? ⇒ Boolean
True if node has no children (and therefore is a leaf).
-
#leaves ⇒ Object
returns array of leaves (taxa) are contained in the node.
-
#nodesToAncestor(ancestor) ⇒ Object
returns number of nodes to the ancestor node.
-
#nodesToNode(node) ⇒ Object
returns number of nodes to other node.
-
#removeChild(child) ⇒ Object
removes child node from list of children and sets child’s parent to nil.
-
#reorder ⇒ Object
reorders descendant nodes alphabetically and by size.
-
#reverseChildParent ⇒ Object
reverses the parent-child relationship (used in rerooting tree).
-
#siblings ⇒ Object
return array of all sibling nodes.
-
#taxa(bootstrap = false) ⇒ Object
returns array of names of leaves (taxa) that are contained in the node.
-
#to_s(showLen = true, bootStrap = "node") ⇒ Object
returns string representation of node.
-
#xMax ⇒ Object
returns the maximum X value in node.
-
#yMax ⇒ Object
returns the maximum Y value in node.
-
#yMin ⇒ Object
returns the minimum Y value in node.
Constructor Details
#initialize(name, edgeLen) ⇒ NewickNode
Returns a new instance of NewickNode.
91 92 93 94 95 96 |
# File 'lib/Newick.rb', line 91 def initialize(name, edgeLen) @parent = nil @name = name @edgeLen = edgeLen @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
child nodes of node
85 86 87 |
# File 'lib/Newick.rb', line 85 def children @children end |
#edgeLen ⇒ Object
edge length of node
81 82 83 |
# File 'lib/Newick.rb', line 81 def edgeLen @edgeLen end |
#name ⇒ Object
name of node
83 84 85 |
# File 'lib/Newick.rb', line 83 def name @name end |
#parent ⇒ Object
parent node of node
79 80 81 |
# File 'lib/Newick.rb', line 79 def parent @parent end |
#x ⇒ Object
x position of node
87 88 89 |
# File 'lib/Newick.rb', line 87 def x @x end |
#y ⇒ Object
y position of node
89 90 91 |
# File 'lib/Newick.rb', line 89 def y @y end |
Instance Method Details
#addChild(child) ⇒ Object
adds child node to list of children and sets child’s parent to self
99 100 101 102 |
# File 'lib/Newick.rb', line 99 def addChild(child) child.parent = self @children.push(child) end |
#calcXPos ⇒ Object
calculates node X positions
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/Newick.rb', line 306 def calcXPos if (parent.nil?) @x = 0 else #@edgeLen = 1 if (@edgeLen == 0) @x = parent.x + @edgeLen end if (!leaf?) @children.each {|child| child.calcXPos } end end |
#calcYPos ⇒ Object
calculates node Y positions
297 298 299 300 301 302 303 |
# File 'lib/Newick.rb', line 297 def calcYPos ySum = 0 @children.each {|child| ySum += child.y } @y = ySum / @children.size end |
#descendants ⇒ Object
returns array of all descendant nodes
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/Newick.rb', line 211 def descendants descendants = [] @children.each {|child| descendants.push(child) child.descendants.each {|grandchild| descendants.push(grandchild) } } return descendants end |
#distToAncestor(ancestor) ⇒ Object
returns the distance to the ancestor node
259 260 261 262 263 264 265 266 267 |
# File 'lib/Newick.rb', line 259 def distToAncestor(ancestor) dist = 0 node = self while(node != ancestor) dist += node.edgeLen node = node.parent end return dist end |
#findNode(name) ⇒ Object
returns node with given name, or nil if not found
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/Newick.rb', line 166 def findNode(name) found = nil if (@name =~/#{name}/) found = self else @children.each {|child| found = child.findNode(name) break if found } end return found end |
#include?(node) ⇒ Boolean
True if given node is child (or grandchild, etc.) of self. False otherwise
193 194 195 196 197 198 199 |
# File 'lib/Newick.rb', line 193 def include?(node) while(node.parent != nil) return true if (node.parent == self) node = node.parent end return false end |
#intNodes ⇒ Object
returns array of non leaves (taxa) that are contained in the node
157 158 159 160 161 162 163 |
# File 'lib/Newick.rb', line 157 def intNodes nodes = [] descendants.each {|child| nodes.push(child) if (!child.leaf?) } return nodes end |
#lca(node) ⇒ Object
returns the last common ancestor node of self and given node
248 249 250 251 252 253 254 255 256 |
# File 'lib/Newick.rb', line 248 def lca(node) if (self.include?(node)) return self elsif (node.include?(self)) return node else return @parent.lca(node) end end |
#leaf? ⇒ Boolean
True if node has no children (and therefore is a leaf)
202 203 204 205 206 207 208 |
# File 'lib/Newick.rb', line 202 def leaf? if (@children.empty?) return true else return false end end |
#leaves ⇒ Object
returns array of leaves (taxa) are contained in the node
148 149 150 151 152 153 154 |
# File 'lib/Newick.rb', line 148 def leaves nodes = [] descendants.each {|node| nodes.push(node) if (node.leaf?) } return nodes end |
#nodesToAncestor(ancestor) ⇒ Object
returns number of nodes to the ancestor node
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/Newick.rb', line 271 def nodesToAncestor(ancestor) if (!ancestor.include?(self)) return nil elsif (ancestor == self) return 0 elsif (ancestor == @parent) return 1 else return 1 + @parent.nodesToAncestor(ancestor) end end |
#nodesToNode(node) ⇒ Object
returns number of nodes to other node
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/Newick.rb', line 285 def nodesToNode(node) lca = lca(node) if (lca == self) return node.nodesToAncestor(self) elsif (lca == node) return nodesToAncestor(node) else return nodesToAncestor(lca) + node.nodesToAncestor(lca) end end |
#removeChild(child) ⇒ Object
removes child node from list of children and sets child’s parent to nil
105 106 107 108 |
# File 'lib/Newick.rb', line 105 def removeChild(child) @children.delete(child) child.parent = nil end |
#reorder ⇒ Object
reorders descendant nodes alphabetically and by size
236 237 238 239 240 241 242 243 |
# File 'lib/Newick.rb', line 236 def reorder return if (@children.empty?) @children.sort! {|x, y| x.name <=> y.name} @children.each {|child| child.reorder } return self end |
#reverseChildParent ⇒ Object
reverses the parent-child relationship (used in rerooting tree)
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/Newick.rb', line 180 def reverseChildParent return if (@parent.nil?) oldParent = @parent oldParent.removeChild(self) if (!oldParent.parent.nil?) oldParent.reverseChildParent end addChild(oldParent) oldParent.edgeLen = @edgeLen @edgeLen = 0 end |
#siblings ⇒ Object
return array of all sibling nodes
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/Newick.rb', line 223 def siblings siblings = [] if (parent.nil?) return siblings else @parent.children.each {|child| siblings.push(child) if (child!=self) } return siblings end end |
#taxa(bootstrap = false) ⇒ Object
returns array of names of leaves (taxa) that are contained in the node
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/Newick.rb', line 132 def taxa(bootstrap = false) taxa = [] if (!leaf?) taxa.push(@name) if (bootstrap) @children.each {|child| child.taxa.each {|taxon| taxa.push(taxon) } } else taxa.push(name) end return taxa.sort end |
#to_s(showLen = true, bootStrap = "node") ⇒ Object
returns string representation of node
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/Newick.rb', line 111 def to_s(showLen = true, bootStrap = "node") s = "" if (!leaf?) s += "(" @children.each {|child| s += child.to_s(showLen, bootStrap) s += "," if (child != @children.last) } s += ")" end if (leaf? || bootStrap == "node") s += @name end s += ":#{@edgeLen}" if (showLen && @edgeLen != 0) if (!leaf? && name.to_i > 0 && bootStrap == "branch") s += ":#{name}" end return s end |
#xMax ⇒ Object
returns the maximum X value in node
321 322 323 324 325 326 327 |
# File 'lib/Newick.rb', line 321 def xMax xMax = 0 children.each {|child| xMax = child.edgeLen if (child.x > xMax) } return xMax end |
#yMax ⇒ Object
returns the maximum Y value in node
330 331 332 333 334 335 336 |
# File 'lib/Newick.rb', line 330 def yMax yMax = 0 children.each {|child| yMax = child.y if (child.y > yMax) } return yMax end |
#yMin ⇒ Object
returns the minimum Y value in node
339 340 341 342 343 344 345 |
# File 'lib/Newick.rb', line 339 def yMin yMin = 1e6 children.each {|child| yMin = child.y if (child.y < yMin) } return yMin end |