Class: Wrnap::Rna::Constraints::ConstraintBox

Inherits:
Object
  • Object
show all
Defined in:
lib/wrnap/rna/constraints.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rna) ⇒ ConstraintBox

Returns a new instance of ConstraintBox.



27
28
29
# File 'lib/wrnap/rna/constraints.rb', line 27

def initialize(rna)
  @rna, @constraints = rna, []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/wrnap/rna/constraints.rb', line 105

def method_missing(name, *args, &block)
  method_name = name.to_s
  
  if method_name =~ TreeStem::STEM_NOTATION_REGEX
    rna.trunk.send(method_name)
  elsif mask_type = method_name.match(/^(prohibit|force)(_(left|right)_stem)?$/)
    side_symbol = mask_type[3] ? mask_type[3].to_sym : :both

    case mask_type[1]
    when "prohibit" then mask!(args[0], side: side_symbol, symbol: args[1] || ?x)
    when "force"    then mask!(args[0], side: side_symbol, symbol: args[1] || "()")
    end
  else super end
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



25
26
27
# File 'lib/wrnap/rna/constraints.rb', line 25

def constraints
  @constraints
end

#rnaObject (readonly)

Returns the value of attribute rna.



25
26
27
# File 'lib/wrnap/rna/constraints.rb', line 25

def rna
  @rna
end

Instance Method Details

#between(i, j) ⇒ Object



35
36
37
# File 'lib/wrnap/rna/constraints.rb', line 35

def between(i, j)
  Loop.new(i, j)
end

#end_to(i) ⇒ Object



47
48
49
# File 'lib/wrnap/rna/constraints.rb', line 47

def end_to(i)
  between(i, n)
end

#freeze(mask_object) ⇒ Object



51
52
53
54
# File 'lib/wrnap/rna/constraints.rb', line 51

def freeze(mask_object)
  force mask_object
  prohibit mask_object.unpaired_regions
end

#inside(i, j) ⇒ Object



39
40
41
# File 'lib/wrnap/rna/constraints.rb', line 39

def inside(i, j)
  between(i + 1, j - 1)
end

#inspectObject



93
94
95
# File 'lib/wrnap/rna/constraints.rb', line 93

def inspect
  "#<Constraints: %s>" % constraints.map(&:name).join(", ")
end

#mask!(mask_object, *args) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/wrnap/rna/constraints.rb', line 56

def mask!(mask_object, *args)
  case mask_object
  when TreeStem then mask_object.preorder_traversal { |node| mask!(node.content, *args) }
  when Array    then mask_object.map { |node| mask!(node, *args) }
  when Helix    then mask_helix!(mask_object, *args)
  when Loop     then mask_loop!(mask_object, symbol: args[0][:symbol])
  end
end

#mask_helix!(helix, side: :both, symbol: "()") ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wrnap/rna/constraints.rb', line 65

def mask_helix!(helix, side: :both, symbol: "()")
  left_loop, right_loop = helix.to_loops

  if symbol.length > 1
    left_symbol, right_symbol = symbol.split(//)
  else
    left_symbol = right_symbol = symbol
  end

  mask_loop!(left_loop,  symbol: left_symbol)  if side == :left  || side == :both
  mask_loop!(right_loop, symbol: right_symbol) if side == :right || side == :both
end

#mask_loop!(l00p, symbol: "x") ⇒ Object



78
79
80
# File 'lib/wrnap/rna/constraints.rb', line 78

def mask_loop!(l00p, symbol: "x")
  mask_region!(l00p.i, l00p.j, symbol: symbol)
end

#mask_region!(i, j, symbol: "x") ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
87
# File 'lib/wrnap/rna/constraints.rb', line 82

def mask_region!(i, j, symbol: "x")
  raise ArgumentError.new("Trying to apply symbol '%s' from %d to %d, all symbols must be 1 char long." % [symbol, i, j]) if symbol.length > 1
  
  constraints << ConstraintData.new(i, j, symbol)
  prune!
end

#nObject



31
32
33
# File 'lib/wrnap/rna/constraints.rb', line 31

def n
  rna.len - 1
end

#prune!Object



89
90
91
# File 'lib/wrnap/rna/constraints.rb', line 89

def prune!
  @constraints = constraints.group_by(&:name).map(&:last).map(&:first)
end

#start_to(i) ⇒ Object



43
44
45
# File 'lib/wrnap/rna/constraints.rb', line 43

def start_to(i)
  between(0, i)
end

#to_sObject Also known as: mask



97
98
99
100
101
# File 'lib/wrnap/rna/constraints.rb', line 97

def to_s
  (?. * rna.len).tap do |string|
    constraints.each { |constraint| string[constraint.from..constraint.to] = constraint.symbol * constraint.length }
  end
end