Class: ViennaRna::Global::Rna

Inherits:
Object
  • Object
show all
Includes:
RnaExtensions
Defined in:
lib/vienna_rna/global/rna.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RnaExtensions

included

Constructor Details

#initialize(sequence: "", structure: "", second_structure: "", comment: "") ⇒ Rna

Returns a new instance of Rna.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vienna_rna/global/rna.rb', line 60

def initialize(sequence: "", structure: "", second_structure: "", comment: "")
  @sequence, @comment = sequence.kind_of?(Rna) ? sequence.seq : sequence, comment

  [:structure, :second_structure].each do |structure_symbol|
    instance_variable_set(
      :"@#{structure_symbol}", 
      case structure_value = eval("#{structure_symbol}")
      when :empty then empty_structure
      when :mfe   then RNA(sequence).run(:fold).mfe_rna.structure
      when String then structure_value
      when Hash   then 
        if structure_value.keys.count > 1
          ViennaRna.debugger { "The following options hash has more than one key. This will probably produce unpredictable results: %s" % structure_value.inspect }
        end
        
        RNA(sequence).run(*structure_value.keys, *structure_value.values).mfe_rna.structure
      end
    )
  end

  if str && seq.length != str.length
    ViennaRna.debugger { "The sequence length (%d) doesn't match the structure length (%d)" % [seq, str].map(&:length) }
  end

  if str_2 && str_1.length != str_2.length
    ViennaRna.debugger { "The first structure length (%d) doesn't match the second structure length (%d)" % [str_1, str_2].map(&:length) }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



132
133
134
135
136
# File 'lib/vienna_rna/global/rna.rb', line 132

def method_missing(name, *args, &block)
  if (name_str = "#{name}") =~ /^run_\w+$/
    run(name_str.gsub(/^run_/, ""), *args)
  else super end
end

Instance Attribute Details

#commentObject Also known as: name

Returns the value of attribute comment.



6
7
8
# File 'lib/vienna_rna/global/rna.rb', line 6

def comment
  @comment
end

#second_structureObject (readonly) Also known as: str_2

Returns the value of attribute second_structure.



7
8
9
# File 'lib/vienna_rna/global/rna.rb', line 7

def second_structure
  @second_structure
end

#sequenceObject (readonly) Also known as: seq

Returns the value of attribute sequence.



7
8
9
# File 'lib/vienna_rna/global/rna.rb', line 7

def sequence
  @sequence
end

#structureObject (readonly) Also known as: str, str_1

Returns the value of attribute structure.



7
8
9
# File 'lib/vienna_rna/global/rna.rb', line 7

def structure
  @structure
end

Class Method Details

.init_from_array(array) ⇒ Object



28
29
30
# File 'lib/vienna_rna/global/rna.rb', line 28

def init_from_array(array)
  init_from_string(*array)
end

.init_from_fasta(string) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vienna_rna/global/rna.rb', line 32

def init_from_fasta(string)
  if File.exist?(string)
    comment = File.basename(string, string.include?(?.) ? ".%s" % string.split(?.)[-1] : "")
    string  = File.read(string).chomp
  end
  
  init_from_string(*string.split(/\n/).reject { |line| line.start_with?(">") }[0, 3]).tap do |rna|
    if (line = string.split(/\n/).first).start_with?(">") && !(file_comment = line.gsub(/^>\s*/, "")).empty?
      rna.comment = file_comment
    elsif comment
      rna.comment = comment
    end
  end
end

.init_from_hash(hash) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/vienna_rna/global/rna.rb', line 19

def init_from_hash(hash)
  new(
    sequence:         hash[:sequence]         || hash[:seq], 
    structure:        hash[:structure]        || hash[:str_1] || hash[:str], 
    second_structure: hash[:second_structure] || hash[:str_2], 
    comment:          hash[:comment]          || hash[:name]
  )
end

.init_from_self(rna) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/vienna_rna/global/rna.rb', line 47

def init_from_self(rna)
  # This happens when you call a ViennaRna library function with the output of something like ViennaRna::Fold.run(...).mfe
  new(
    sequence:         rna.sequence, 
    strucutre:        rna.structure, 
    second_strucutre: rna.second_structure, 
    comment:          rna.comment
  )
end

.init_from_string(sequence, structure = nil, second_structure = nil, comment = nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/vienna_rna/global/rna.rb', line 10

def init_from_string(sequence, structure = nil, second_structure = nil, comment = nil)
  new(
    sequence:         sequence, 
    structure:        structure, 
    second_structure: second_structure,
    comment:          comment
  )
end

Instance Method Details

#empty_structureObject Also known as: empty_str



95
96
97
# File 'lib/vienna_rna/global/rna.rb', line 95

def empty_structure
  "." * seq.length
end

#inspectObject



138
139
140
141
142
143
144
145
# File 'lib/vienna_rna/global/rna.rb', line 138

def inspect
  "#<RNA: %s>" % [
    ("#{seq[0, 20]   + (seq.length > 20   ? '... [%d]' % seq.length : '')}" if seq   && !seq.empty?),
    ("#{str_1[0, 20] + (str_1.length > 20 ? ' [%d]'    % seq.length : '')}" if str_1 && !str_1.empty?),
    ("#{str_2[0, 20] + (str_2.length > 20 ? ' [%d]'    % seq.length : '')}" if str_2 && !str_1.empty?),
    (name ? name : "#{self.class.name}")
  ].compact.join(", ")
end

#one_structure(structure_1) ⇒ Object



101
102
103
# File 'lib/vienna_rna/global/rna.rb', line 101

def one_structure(structure_1)
  self.class.init_from_string(seq, structure_1.is_a?(Symbol) ? send(structure_1) : structure_1, nil, name)
end

#run(package_name, options = {}) ⇒ Object



128
129
130
# File 'lib/vienna_rna/global/rna.rb', line 128

def run(package_name, options = {})
  ViennaRna::Package.lookup(package_name).run(self, options)
end

#temp_fa_file!Object



124
125
126
# File 'lib/vienna_rna/global/rna.rb', line 124

def temp_fa_file!
  write_fa!(Tempfile.new("rna")).path
end

#two_structures(structure_1, structure_2) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/vienna_rna/global/rna.rb', line 105

def two_structures(structure_1, structure_2)
  self.class.init_from_string(
    seq, 
    *[structure_1, structure_2].map { |argument| argument.is_a?(Symbol) ? send(argument) : argument },
    name
  )
end

#write_fa!(filename) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/vienna_rna/global/rna.rb', line 113

def write_fa!(filename)
  filename.tap do |filename|
    File.open(filename, ?w) do |file|
      file.write("> %s\n" % name) if name
      file.write("%s\n" % seq)    if seq
      file.write("%s\n" % str_1)  if str_1
      file.write("%s\n" % str_2)  if str_2
    end
  end
end