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

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

Instance Method Summary collapse

Instance Method Details

#base_pairs(structure) ⇒ Object



141
142
143
144
145
# File 'lib/wrnap/rna/extensions.rb', line 141

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

#collapsed_helices(structure, lonely_bp: false) ⇒ Object



137
138
139
# File 'lib/wrnap/rna/extensions.rb', line 137

def collapsed_helices(structure, lonely_bp: false)
  helices(structure).map { |((i, j), *rest)| Helix.new(i, j, rest.length + 1) }.select { |helix| lonely_bp ? helix : helix.length > 1 }
end

#get_pairings(structure) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/wrnap/rna/extensions.rb', line 147

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

#helices(structure) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/wrnap/rna/extensions.rb', line 127

def helices(structure)
  unless (array = base_pairs(structure).sort_by(&:first).map(&:to_a)).empty?
    array[1..-1].inject([[array.first]]) do |bins, (i, j)|
      bins.tap { bins[-1][-1] == [i - 1, j + 1] ? bins[-1] << [i, j] : bins << [[i, j]] }
    end
  else
    []
  end
end

#loop_regions(structure) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/wrnap/rna/extensions.rb', line 118

def loop_regions(structure)
  [structure.split(//), (0...structure.length).to_a].transpose.select { |char, _| char == ?. }.inject([]) do |array, (_, index)|
    array.tap do
      matching_loop = array.map(&:last).each_with_index.find { |end_of_loop, _| end_of_loop + 1 == index }
      matching_loop ? array[matching_loop[-1]][-1] += 1 : array << [index, index]
    end
  end.map { |loop_indices| Loop.new(*loop_indices) }
end

#loops_and_helices(structure) ⇒ Object



114
115
116
# File 'lib/wrnap/rna/extensions.rb', line 114

def loops_and_helices(structure)
  [loop_regions(structure), collapsed_helices(structure, lonely_bp: true)]
end

#max_bp_distance(structure) ⇒ Object



110
111
112
# File 'lib/wrnap/rna/extensions.rb', line 110

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