Class: Wrnap::Rna::TreeStem
- Inherits:
-
Tree::TreeNode
- Object
- Tree::TreeNode
- Wrnap::Rna::TreeStem
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/wrnap/rna/tree.rb
Constant Summary collapse
- STEM_NOTATION_REGEX =
/^[pt]_?(\d+_)*\d+(_?[ijkl])?$/
Instance Method Summary collapse
- #detached_copy ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #postorder_traversal {|_self| ... } ⇒ Object
- #preorder_traversal {|_self| ... } ⇒ Object
- #unpaired_regions ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/wrnap/rna/tree.rb', line 82 def method_missing(name, *args, &block) if (method_name = name.to_s) =~ STEM_NOTATION_REGEX call_type = method_name[0] indices = method_name.gsub(/\D+/, ?_).split(?_).reject(&:empty?).map(&:to_i) helix_index = method_name.match(/([ijkl])$/) ? $1 : "" if indices.size > 1 && child = children[indices[0] - 1] child.send(call_type + indices[1..-1].join(?_) + helix_index) elsif child = children[indices[0] - 1] case call_type when ?p then helix_index.empty? ? child.content : child.send(helix_index) when ?t then child end end else super end end |
Instance Method Details
#detached_copy ⇒ Object
66 67 68 |
# File 'lib/wrnap/rna/tree.rb', line 66 def detached_copy self.class.new(@name, @content ? @content.clone : nil) end |
#postorder_traversal {|_self| ... } ⇒ Object
76 77 78 79 80 |
# File 'lib/wrnap/rna/tree.rb', line 76 def postorder_traversal(&block) return enum_for(:postorder_traversal) unless block_given? children.each { |child| child.postorder_traversal(&block) } yield self end |
#preorder_traversal {|_self| ... } ⇒ Object
70 71 72 73 74 |
# File 'lib/wrnap/rna/tree.rb', line 70 def preorder_traversal(&block) return enum_for(:preorder_traversal) unless block_given? yield self children.map { |child| child.preorder_traversal(&block) } end |
#unpaired_regions ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wrnap/rna/tree.rb', line 22 def unpaired_regions Wrnap.debugger { "Collecting unpaired regions for %s" % [root.content.name] } postorder_traversal.inject([]) do |array, node| array.tap do if node.is_leaf? array << Loop.new(node.k + 1, node.l - 1) end if node != self if node.is_only_child? Wrnap.debugger { "Interior node: %s, parent: %s" % [node.content.inspect, node.parent.content.inspect] } if node.i - node.parent.k > 0 Wrnap.debugger { "Left bulge." } array << Loop.new(node.parent.k + 1, node.i - 1) end if node.parent.l - node.j > 0 Wrnap.debugger { "Right bulge." } array << Loop.new(node.j + 1, node.parent.l - 1) end else node_index = node.parent.children.each_with_index.find { |child, _| child == node }.last if node.is_last_sibling? Wrnap.debugger { "Leaf node, last child: %s" % node.content.inspect } array << Loop.new(node.j + 1, node.parent.l - 1) else if node.is_first_sibling? Wrnap.debugger { "Leaf node, first child: %s" % node.content.inspect } array << Loop.new(node.parent.k + 1, node.i - 1) end Wrnap.debugger { "Connecting node, middle child: %s" % node.content.inspect } alexa = node.siblings[node_index] array << Loop.new(node.j + 1, alexa.i - 1) end end end end end end |