Class: ViennaRna::Base

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

Direct Known Subclasses

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.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vienna_rna/modules/base.rb', line 68

def initialize(data)
  data  = [data] unless data.is_a?(Array)
  
  @data = case data.map(&:class)
  when [Rna]                      then data.first
  when [String], [String, String] then Rna.init_from_string(*data)
  when [Hash]                     then Rna.init_from_hash(*data)
  when [Array]                    then Rna.init_from_array(*data)
  else raise TypeError.new("Unsupported ViennaRna::Rna#initialize format: #{data}")
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



49
50
51
# File 'lib/vienna_rna/modules/base.rb', line 49

def data
  @data
end

#responseObject (readonly)

Returns the value of attribute response.



49
50
51
# File 'lib/vienna_rna/modules/base.rb', line 49

def response
  @response
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



49
50
51
# File 'lib/vienna_rna/modules/base.rb', line 49

def runtime
  @runtime
end

Class Method Details

.batch(fastas = []) ⇒ Object



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

def batch(fastas = [])
  ViennaRna::Batch.new(self, fastas).tap do |batch|
    if const_defined?(:Batch)
      @@me = self
      
      batch.singleton_class.class_eval { include @@me.const_get(:Batch) }
    end
  end
end

.bootstrap(data, output) ⇒ Object



28
29
30
31
32
# File 'lib/vienna_rna/modules/base.rb', line 28

def bootstrap(data, output)
  new(data).tap do |object|
    object.instance_variable_set(:@response, output)
  end
end

.debuggerObject



44
45
46
# File 'lib/vienna_rna/modules/base.rb', line 44

def debugger
  STDERR.puts yield if ViennaRna.debug
end

.exec_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/vienna_rna/modules/base.rb', line 19

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

.method_added(name) ⇒ Object



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

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



23
24
25
26
# File 'lib/vienna_rna/modules/base.rb', line 23

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



122
123
124
# File 'lib/vienna_rna/modules/base.rb', line 122

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

#exec_nameObject



51
52
53
54
55
56
57
# File 'lib/vienna_rna/modules/base.rb', line 51

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



59
60
61
62
63
64
65
66
# File 'lib/vienna_rna/modules/base.rb', line 59

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

#pre_run_checkObject



96
97
98
99
100
# File 'lib/vienna_rna/modules/base.rb', line 96

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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vienna_rna/modules/base.rb', line 110

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
  
  debugger { command }
    
  %x[#{command}]
end

#run_with_hooks(flags = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vienna_rna/modules/base.rb', line 80

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
    
      debugger { "Total runtime: %.3f sec." % runtime.real }
    end
  else
    self
  end
end

#stringify_flags(flags) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/vienna_rna/modules/base.rb', line 102

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