Class: Wrnap::Global::Rna

Inherits:
Object
  • Object
show all
Includes:
Extensions
Defined in:
lib/wrnap/global/rna.rb,
lib/wrnap/global/rna/context.rb

Direct Known Subclasses

Context

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions

included

Constructor Details

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

Returns a new instance of Rna.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wrnap/global/rna.rb', line 64

def initialize(sequence: "", structure: "", second_structure: "", comment: "")
  @sequence, @comment = (sequence.kind_of?(Rna) ? sequence.seq : sequence).upcase, 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
          Wrnap.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
    Wrnap.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
    Wrnap.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



150
151
152
153
154
# File 'lib/wrnap/global/rna.rb', line 150

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/wrnap/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/wrnap/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/wrnap/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/wrnap/global/rna.rb', line 7

def structure
  @structure
end

Class Method Details

.init_from_array(array) ⇒ Object



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

def init_from_array(array)
  init_from_string(*array)
end

.init_from_context(*context, coords: {}, rna: {}) ⇒ Object



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

def init_from_context(*context, coords: {}, rna: {})
  Context.init_from_entrez(*context, coords: coords, rna: rna)
end

.init_from_fasta(string) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wrnap/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/wrnap/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



51
52
53
54
55
56
57
58
59
# File 'lib/wrnap/global/rna.rb', line 51

def init_from_self(rna)
  # This happens when you call a Wrnap library function with the output of something like Wrnap::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/wrnap/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

#==(other_rna) ⇒ Object



146
147
148
# File 'lib/wrnap/global/rna.rb', line 146

def ==(other_rna)
  other_rna.kind_of?(Wrnap::Global::Rna) ? [seq, str_1, str_2] == [other_rna.seq, other_rna.str_1, other_rna.str_2] : super
end

#empty_structureObject Also known as: empty_str



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

def empty_structure
  "." * seq.length
end

#inspectObject



156
157
158
159
160
161
162
163
# File 'lib/wrnap/global/rna.rb', line 156

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

#no_structureObject Also known as: no_str



105
106
107
# File 'lib/wrnap/global/rna.rb', line 105

def no_structure
  self.class.init_from_string(seq, nil, nil, name)
end

#one_structure(structure_1) ⇒ Object Also known as: one_str



111
112
113
# File 'lib/wrnap/global/rna.rb', line 111

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



142
143
144
# File 'lib/wrnap/global/rna.rb', line 142

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

#temp_fa_file!Object



138
139
140
# File 'lib/wrnap/global/rna.rb', line 138

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

#two_structures(structure_1, structure_2) ⇒ Object Also known as: two_str



117
118
119
120
121
122
123
# File 'lib/wrnap/global/rna.rb', line 117

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



127
128
129
130
131
132
133
134
135
136
# File 'lib/wrnap/global/rna.rb', line 127

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