Class: Wrnap::Rna

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
MetaMissing, 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: "", structures: [], comment: "", &block) ⇒ Rna

Returns a new instance of Rna.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wrnap/rna.rb', line 74

def initialize(sequence: "", structures: [], comment: "", &block)
  @sequence   = (sequence.kind_of?(Rna) ? sequence.seq : sequence).upcase
  @comment    = comment
  @metadata   = Metadata::Container.new(self)
  @structures = (structures ? [structures].flatten : []).each_with_index.map do |structure, i|
    case structure
    when :empty, :empty_str then empty_structure
    when :mfe   then RNA(@sequence).run(:fold).mfe_rna.structure
    when String then structure
    when Hash   then
      if structure.keys.count > 1
        Wrnap.debugger { "The following options hash has more than one key. This will probably produce unpredictable results: %s" % structure.inspect }
      end

      RNA(@sequence).run(*structure.keys, *structure.values).mfe_rna.structure
    end.tap do |parsed_structure|
      if parsed_structure.length != len
        Wrnap.debugger { "The sequence length (%d) doesn't match the structure length at index %d (%d)" % [len, i, parsed_structure.length] }
      end
    end
  end

  .instance_eval(&block) if block_given?
end

Instance Attribute Details

#commentObject Also known as: name

Returns the value of attribute comment.



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

def comment
  @comment
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

#sequenceObject (readonly) Also known as: seq

Returns the value of attribute sequence.



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

def sequence
  @sequence
end

#structuresObject (readonly) Also known as: strs

Returns the value of attribute structures.



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

def structures
  @structures
end

Class Method Details

.init_from_array(array, &block) ⇒ Object



41
42
43
# File 'lib/wrnap/rna.rb', line 41

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

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



67
68
69
# File 'lib/wrnap/rna.rb', line 67

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

.init_from_hash(hash, &block) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/wrnap/rna.rb', line 32

def init_from_hash(hash, &block)
  new(
    sequence:   hash[:sequence]   || hash[:seq],
    structures: hash[:structures] || hash[:structure] || hash[:strs] || hash[:str],
    comment:    hash[:comment]    || hash[:name],
    &block
  )
end

.init_from_string(sequence, *remaining_args, &block) ⇒ Object



18
19
20
# File 'lib/wrnap/rna.rb', line 18

def init_from_string(sequence, *remaining_args, &block)
  init_from_hash(parse_rna_attributes(sequence, remaining_args), &block)
end

.parse_rna_attributes(sequence, attributes = []) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/wrnap/rna.rb', line 22

def parse_rna_attributes(sequence, attributes = [])
  last_arg = (attributes = attributes.flatten).last
  
  if last_arg.is_a?(Symbol) || Regexp.compile("^[\\.\\(\\)]{%d}$" % sequence.length) =~ last_arg
    { seq: sequence, strs: attributes }
  else
    { seq: sequence, strs: attributes[0..-2], comment: last_arg }
  end
end

Instance Method Details

#==(other_rna) ⇒ Object



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

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



107
108
109
# File 'lib/wrnap/rna.rb', line 107

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

#eql?(other_rna) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/wrnap/rna.rb', line 157

def eql?(other_rna)
  self == other_rna
end

#formatted_stringObject



135
136
137
138
139
140
141
# File 'lib/wrnap/rna.rb', line 135

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

#inspectObject



169
170
171
172
173
174
175
176
# File 'lib/wrnap/rna.rb', line 169

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

#ppObject



165
166
167
# File 'lib/wrnap/rna.rb', line 165

def pp
  puts(formatted_string)
end

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



153
154
155
# File 'lib/wrnap/rna.rb', line 153

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

#temp_fa_file!Object



149
150
151
# File 'lib/wrnap/rna.rb', line 149

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

#write_fa!(filename) ⇒ Object



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

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