Class: Wrnap::Rna::TreeStem

Inherits:
Tree::TreeNode
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, MetaMissing
Defined in:
lib/wrnap/rna/tree.rb

Constant Summary collapse

STEM_NOTATION_REGEX =
/^[pt]_?(\d+_)*\d+(_?[ijkl])?$/

Instance Method Summary collapse

Instance Method Details

#detached_copyObject



71
72
73
74
75
# File 'lib/wrnap/rna/tree.rb', line 71

def detached_copy
  # The implementation of this in RubyTree explicitly returns a new Tree::TreeNode instance rather than self.class.new, so we
  # maintain ducktyping by intercepting the method.
  self.class.new(@name, @content ? @content.clone : nil)
end

#in(sequence) ⇒ Object



23
24
25
# File 'lib/wrnap/rna/tree.rb', line 23

def in(sequence)
  sequence[i..j]
end

#postorder_traversal {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



83
84
85
86
87
# File 'lib/wrnap/rna/tree.rb', line 83

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

Yields:

  • (_self)

Yield Parameters:



77
78
79
80
81
# File 'lib/wrnap/rna/tree.rb', line 77

def preorder_traversal(&block)
  return enum_for(:preorder_traversal) unless block_given?
  yield self
  children.map { |child| child.preorder_traversal(&block) }
end

#unpaired_regionsObject



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
65
66
67
68
69
# File 'lib/wrnap/rna/tree.rb', line 27

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