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

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

Instance Method Summary collapse

Instance Method Details

#base_pairs(structure) ⇒ Object



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

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



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

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



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

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