Module: Wrnap::Rna::Extensions::OneStructureBasedMethods
- Defined in:
- lib/wrnap/rna/extensions.rb
Instance Method Summary collapse
- #base_pairs(structure) ⇒ Object
- #collapsed_helices(structure, lonely_bp: false) ⇒ Object
- #get_pairings(structure) ⇒ Object
- #helices(structure) ⇒ Object
- #loop_regions(structure) ⇒ Object
- #loops_and_helices(structure) ⇒ Object
- #max_bp_distance(structure) ⇒ Object
Instance Method Details
#base_pairs(structure) ⇒ Object
132 133 134 135 136 |
# File 'lib/wrnap/rna/extensions.rb', line 132 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
128 129 130 |
# File 'lib/wrnap/rna/extensions.rb', line 128 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/wrnap/rna/extensions.rb', line 138 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
118 119 120 121 122 123 124 125 126 |
# File 'lib/wrnap/rna/extensions.rb', line 118 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
109 110 111 112 113 114 115 116 |
# File 'lib/wrnap/rna/extensions.rb', line 109 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
105 106 107 |
# File 'lib/wrnap/rna/extensions.rb', line 105 def loops_and_helices(structure) [loop_regions(structure), collapsed_helices(structure, lonely_bp: true)] end |
#max_bp_distance(structure) ⇒ Object
101 102 103 |
# File 'lib/wrnap/rna/extensions.rb', line 101 def max_bp_distance(structure) base_pairs(structure).count + ((structure.length - 3) / 2.0).floor end |