Module: Wrnap::Global::Rna::Extensions::OneStructureBasedMethods

Defined in:
lib/wrnap/global/rna/extensions.rb

Instance Method Summary collapse

Instance Method Details

#base_pairs(structure) ⇒ Object



61
62
63
64
65
# File 'lib/wrnap/global/rna/extensions.rb', line 61

def base_pairs(structure)
  get_pairings(structure).each_with_index.inject(Set.new) do |set, (j, i)|
    j >= 0 ? set << Set[i, j] : set
  end
end

#get_pairings(structure) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wrnap/global/rna/extensions.rb', line 67

def get_pairings(structure)
  stack = []

  structure.each_char.each_with_index.inject(Array.new(structure.length, -1)) do |array, (symbol, index)|
    array.tap do
      case symbol
      when "(" then stack.push(index)
      when ")" then
        if stack.empty?
          raise "Too many ')' in '#{structure}'"
        else
          stack.pop.tap do |opening|
            array[opening] = index
            array[index]   = opening
          end
        end
      end
    end
  end.tap do
    raise "Too many '(' in '#{structure}'" unless stack.empty?
  end
end

#max_bp_distance(structure) ⇒ Object



57
58
59
# File 'lib/wrnap/global/rna/extensions.rb', line 57

def max_bp_distance(structure)
  base_pairs(structure).count + ((structure.length - 3) / 2.0).floor
end