Class: ViennaRna::Package::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vienna_rna/package/base.rb

Direct Known Subclasses

EnergyGrid2d, Eval, Fold, Heat, Subopt, Xbor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vienna_rna/package/base.rb', line 53

def initialize(data)
  data  = [data] unless data.is_a?(Array)

  @data = case data.map(&:class)
  when [ViennaRna::Global::Rna]         then data.first
  when *(1..3).map { |i| [String] * i } then RNA.from_string(*data)
  when [Hash]                           then RNA.from_hash(*data)
  when [Array]                          then RNA.from_array(*data)
  when [NilClass]                       then ViennaRna::Global::Rna.placeholder
  else raise TypeError.new("Unsupported ViennaRna::Global::Rna#initialize format: #{data}")
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



34
35
36
# File 'lib/vienna_rna/package/base.rb', line 34

def data
  @data
end

#responseObject (readonly)

Returns the value of attribute response.



34
35
36
# File 'lib/vienna_rna/package/base.rb', line 34

def response
  @response
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



34
35
36
# File 'lib/vienna_rna/package/base.rb', line 34

def runtime
  @runtime
end

Class Method Details

.bootstrap(data: nil, output: "") ⇒ Object



27
28
29
30
31
# File 'lib/vienna_rna/package/base.rb', line 27

def bootstrap(data: nil, output: "")
  new(data).tap do |object|
    object.instance_variable_set(:@response, File.exist?(output) ? File.read(output).chomp : output)
  end
end

.exec_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/vienna_rna/package/base.rb', line 18

def exec_exists?(name)
  !%x[which RNA#{name.to_s.downcase}].empty?
end

.method_added(name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/vienna_rna/package/base.rb', line 7

def method_added(name)
  if name == :run
    unless @chaining
      @chaining = true
      alias_method_chain :run, :hooks
    else
      remove_instance_variable(:@chaining)
    end
  end
end

.run(*data) ⇒ Object



22
23
24
25
# File 'lib/vienna_rna/package/base.rb', line 22

def run(*data)
  flags = data.length > 1 && data.last.is_a?(Hash) ? data.pop : {}
  new(data).run(flags)
end

Instance Method Details

#debugger(&block) ⇒ Object



112
113
114
# File 'lib/vienna_rna/package/base.rb', line 112

def debugger(&block)
  self.class.debugger(&block)
end

#exec_nameObject



36
37
38
39
40
41
42
# File 'lib/vienna_rna/package/base.rb', line 36

def exec_name
  if executable_name
    executable_name.respond_to?(:call) ? self.class.module_exec(&executable_name) : executable_name
  else
    "RNA#{self.class.name.split('::').last.underscore}"
  end
end

#exec_sequence_formatObject



44
45
46
47
48
49
50
51
# File 'lib/vienna_rna/package/base.rb', line 44

def exec_sequence_format
  if data.str
    '"%s
    %s"' % [data.seq, data.str]
  else
    data.seq
  end
end

#pre_run_checkObject



82
83
84
85
86
# File 'lib/vienna_rna/package/base.rb', line 82

def pre_run_check
  if %x[which #{exec_name}].empty?
    raise RuntimeError.new("#{exec_name} is not defined on this machine")
  end
end

#run(flags = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vienna_rna/package/base.rb', line 96

def run(flags = {})
  command = if respond_to?(:run_command)
    method(:run_command).arity.zero? ? run_command : run_command(flags)
  else
    "echo #{exec_sequence_format} | #{exec_name} #{stringify_flags(flags)}"
  end

  ViennaRna.debugger { command }
  
  %x[#{command}]
end

#run_with_hooks(flags = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vienna_rna/package/base.rb', line 66

def run_with_hooks(flags = {})
  unless @response
    tap do
      @runtime = Benchmark.measure do
        pre_run_check unless respond_to?(:run_command)
        @response = run_without_hooks(flags)
        post_process if respond_to?(:post_process)
      end
  
      ViennaRna.debugger { "Total runtime: %.3f sec." % runtime.real }
    end
  else
    self
  end
end

#serializeObject



108
109
110
# File 'lib/vienna_rna/package/base.rb', line 108

def serialize
  YAML.dump(self)
end

#stringify_flags(flags) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/vienna_rna/package/base.rb', line 88

def stringify_flags(flags)
  base_flags = self.class.const_defined?(:BASE_FLAGS) ? self.class.const_get(:BASE_FLAGS) : {}

  flags.merge(base_flags).inject("") do |string, (flag, value)| 
    (string + (value == :empty ? " -%s" % flag : " -%s %s" % [flag, value])).strip
  end
end