Module: Wrnap::Global::Extensions::OneStructureBasedMethods
- Defined in:
- lib/wrnap/global/rna/extensions.rb
Instance Method Summary collapse
- #base_pairs(structure) ⇒ Object
- #get_pairings(structure) ⇒ Object
- #helices(structure) ⇒ Object
- #max_bp_distance(structure) ⇒ Object
Instance Method Details
#base_pairs(structure) ⇒ Object
73 74 75 76 77 |
# File 'lib/wrnap/global/rna/extensions.rb', line 73 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/wrnap/global/rna/extensions.rb', line 79 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
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/wrnap/global/rna/extensions.rb', line 57 def helices(structure) array = base_pairs(structure).sort_by(&:first).map(&:to_a) unless array.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 |
#max_bp_distance(structure) ⇒ Object
69 70 71 |
# File 'lib/wrnap/global/rna/extensions.rb', line 69 def max_bp_distance(structure) base_pairs(structure).count + ((structure.length - 3) / 2.0).floor end |