Module: ViennaRna::Global::RnaExtensions::OneStructureBasedMethods

Defined in:
lib/vienna_rna/global/rna_extensions.rb

Instance Method Summary collapse

Instance Method Details

#base_pairs(structure) ⇒ Object



53
54
55
56
57
# File 'lib/vienna_rna/global/rna_extensions.rb', line 53

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vienna_rna/global/rna_extensions.rb', line 59

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



49
50
51
# File 'lib/vienna_rna/global/rna_extensions.rb', line 49

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