Class: Wrnap::Rna

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Global::Yaml, Constraints, Extensions, Metadata, TreeFunctions, Wrnapper
Defined in:
lib/wrnap/rna.rb,
lib/wrnap/rna/box.rb,
lib/wrnap/rna/tree.rb,
lib/wrnap/rna/motifs.rb,
lib/wrnap/rna/context.rb,
lib/wrnap/rna/wrapper.rb,
lib/wrnap/rna/metadata.rb,
lib/wrnap/rna/extensions.rb,
lib/wrnap/rna/constraints.rb

Direct Known Subclasses

Context

Defined Under Namespace

Modules: Constraints, Extensions, Metadata, TreeFunctions, Wrnapper Classes: Box, Context, Helix, Loop, TreePlanter, TreeStem

Constant Summary collapse

CANONICAL_BASES =
Set.new << Set.new([?G, ?C]) << Set.new([?A, ?U]) << Set.new([?G, ?U])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Constraints

included

Methods included from TreeFunctions

#trunk, #with_tree

Methods included from Metadata

included

Methods included from Wrnapper

#wrnap

Methods included from Extensions

included

Methods included from Global::Yaml

#deserialize, #serialize

Constructor Details

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

Returns a new instance of Rna.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wrnap/rna.rb', line 81

def initialize(sequence: "", structure: "", second_structure: "", comment: "", &block)
  @sequence, @comment, @metadata = (sequence.kind_of?(Rna) ? sequence.seq : sequence).upcase, comment, Metadata::Container.new(self)

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

  .instance_eval(&block) if block_given?

  if str && len != 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



183
184
185
186
187
# File 'lib/wrnap/rna.rb', line 183

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.



13
14
15
# File 'lib/wrnap/rna.rb', line 13

def comment
  @comment
end

#metadataObject (readonly)

Returns the value of attribute metadata.



14
15
16
# File 'lib/wrnap/rna.rb', line 14

def 
  @metadata
end

#second_structureObject (readonly) Also known as: str_2

Returns the value of attribute second_structure.



14
15
16
# File 'lib/wrnap/rna.rb', line 14

def second_structure
  @second_structure
end

#sequenceObject (readonly) Also known as: seq

Returns the value of attribute sequence.



14
15
16
# File 'lib/wrnap/rna.rb', line 14

def sequence
  @sequence
end

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

Returns the value of attribute structure.



14
15
16
# File 'lib/wrnap/rna.rb', line 14

def structure
  @structure
end

Class Method Details

.init_from_array(array, &block) ⇒ Object



37
38
39
# File 'lib/wrnap/rna.rb', line 37

def init_from_array(array, &block)
  init_from_string(*array, &block)
end

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



63
64
65
# File 'lib/wrnap/rna.rb', line 63

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

.init_from_fasta(string, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wrnap/rna.rb', line 41

def init_from_fasta(string, &block)
  if File.exist?(string)
    comment = File.basename(string, string.include?(?.) ? ".%s" % string.split(?.)[-1] : "")
    string  = File.read(string).chomp
  end
    
  if string.count(?>) > 1
    string.split(/>/).reject(&:empty?).map do |rna_string|
      rna_data = rna_string.split(?\n)
      init_from_string(*rna_data[1..-1]).copy_name_from(rna_data[0])
    end.wrnap
  else
    init_from_string(*string.split(/\n/).reject { |line| line.start_with?(">") }[0, 3], &block).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
end

.init_from_hash(hash, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/wrnap/rna.rb', line 27

def init_from_hash(hash, &block)
  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],
    &block
  )
end

.init_from_self(rna, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/wrnap/rna.rb', line 67

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

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



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

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

Instance Method Details

#==(other_rna) ⇒ Object



179
180
181
# File 'lib/wrnap/rna.rb', line 179

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

#copy_name_from(nameish) ⇒ Object



120
121
122
# File 'lib/wrnap/rna.rb', line 120

def copy_name_from(nameish)
  tap { @comment = nameish.is_a?(String) ? nameish : nameish.name }
end

#empty_structureObject Also known as: empty_str



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

def empty_structure
  "." * len
end

#eql?(other_rna) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/wrnap/rna.rb', line 175

def eql?(other_rna)
  self == other_rna
end

#formatted_stringObject



152
153
154
155
156
157
158
159
# File 'lib/wrnap/rna.rb', line 152

def formatted_string
  [
    (">%s" % name  if name),
    ("%s"  % seq   if seq),
    ("%s"  % str_1 if str_1),
    ("%s"  % str_2 if str_2)
  ].compact.join(?\n)
end

#inspectObject



193
194
195
196
197
198
199
200
201
# File 'lib/wrnap/rna.rb', line 193

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

#no_structureObject Also known as: no_str



130
131
132
# File 'lib/wrnap/rna.rb', line 130

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

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



136
137
138
# File 'lib/wrnap/rna.rb', line 136

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

#ppObject



189
190
191
# File 'lib/wrnap/rna.rb', line 189

def pp
  puts(formatted_string)
end

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



171
172
173
# File 'lib/wrnap/rna.rb', line 171

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

#temp_fa_file!Object



167
168
169
# File 'lib/wrnap/rna.rb', line 167

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

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



142
143
144
145
146
147
148
# File 'lib/wrnap/rna.rb', line 142

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



161
162
163
164
165
# File 'lib/wrnap/rna.rb', line 161

def write_fa!(filename)
  filename.tap do |filename|
    File.open(filename, ?w) { |file| file.write(formatted_string) }
  end
end