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: "", raw_data: {}) ⇒ Rna

Returns a new instance of Rna.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vienna_rna/global/rna.rb', line 48

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

  [: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

Instance Attribute Details

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



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

def raw_data
  @raw_data
end

#second_structureObject (readonly) Also known as: str_2

Returns the value of attribute second_structure.



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

def second_structure
  @second_structure
end

#sequenceObject (readonly) Also known as: seq

Returns the value of attribute sequence.



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

def sequence
  @sequence
end

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

Returns the value of attribute structure.



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

def structure
  @structure
end

Class Method Details

.init_from_array(array) ⇒ Object



26
27
28
# File 'lib/vienna_rna/global/rna.rb', line 26

def init_from_array(array)
  init_from_string(*array)
end

.init_from_fasta(string) ⇒ Object



30
31
32
33
# File 'lib/vienna_rna/global/rna.rb', line 30

def init_from_fasta(string)
  string = File.read(string).chomp if File.exist?(string)
  init_from_string(*string.split(/\n/).reject { |line| line.start_with?(">") }[0, 3])
end

.init_from_hash(hash) ⇒ Object



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

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], 
    raw_data:         hash
  )
end

.init_from_self(rna) ⇒ Object



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

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, 
    raw_data:         rna.raw_data
  )
end

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



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

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

Instance Method Details

#empty_structureObject Also known as: empty_str



82
83
84
# File 'lib/vienna_rna/global/rna.rb', line 82

def empty_structure
  "." * seq.length
end

#inspectObject



107
108
109
110
111
112
113
114
# File 'lib/vienna_rna/global/rna.rb', line 107

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

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



103
104
105
# File 'lib/vienna_rna/global/rna.rb', line 103

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

#temp_fa_file!Object



99
100
101
# File 'lib/vienna_rna/global/rna.rb', line 99

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

#write_fa!(filename, comment = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/vienna_rna/global/rna.rb', line 88

def write_fa!(filename, comment = nil)
  filename.tap do |filename|
    File.open(filename, ?w) do |file|
      file.write("> %s\n" % comment) if comment
      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