Class: Bio::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-alignment/tree.rb

Overview

Here we add to BioRuby’s Bio::Tree classes

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Instance Method Details

#cloneObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/bio-alignment/tree.rb', line 164

def clone
  new_tree = self.class.new
  nodes.each do |x|
    new_tree.add_node(x)
  end
  self.each_edge do |node1, node2, edge|
    if new_tree.include?(node1) and new_tree.include?(node2) then
      new_tree.add_edge(node1, node2, edge)
    end
  end
  new_tree
end

#clone_subtree(start_node) ⇒ Object

Create a deep clone of the tree



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bio-alignment/tree.rb', line 128

def clone_subtree start_node 
  new_tree = self.class.new
  list = [start_node] + start_node.descendents
  list.each do |x|
    new_tree.add_node(x)
  end
  each_edge do |node1, node2, edge|
    if new_tree.include?(node1) and new_tree.include?(node2) 
      new_tree.add_edge(node1, node2, edge)
    end
  end
  new_tree
end

#clone_tree_without_branch(node) ⇒ Object

Clone a tree without the branch starting at node



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bio-alignment/tree.rb', line 143

def clone_tree_without_branch node
  new_tree = self.class.new
  original = [root] + root.descendents
  # p "Original",original
  skip = [node] + node.descendents
  # p "Skip",skip
  # p "Retain",root.descendents - skip
  nodes.each do |x|
    if not skip.include?(x)
      new_tree.add_node(x) 
    else
    end
  end
  each_edge do |node1, node2, edge|
    if new_tree.include?(node1) and new_tree.include?(node2) 
      new_tree.add_edge(node1, node2, edge)
    end
  end
  new_tree
end

#find(name) ⇒ Object

End of injecting Node functionality



118
119
120
# File 'lib/bio-alignment/tree.rb', line 118

def find name
  get_node_by_name(name)
end

#mapObject

Walk the ordered tree leaves, calling into the block, and return an array



123
124
125
# File 'lib/bio-alignment/tree.rb', line 123

def map 
  leaves.map { | leaf | yield leaf }
end