Module: Bioroebe::NucleotideModule

Included in:
DNA, RNA
Defined in:
lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb

Overview

Bioroebe::NucleotideModule

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.complementary_strand(i = @sequence.chars) ⇒ Object

#

Bioroebe::NucleotideModule.complementary_strand

This method will return the complementary strand. So, the complementary strand to “A” will be “T”; and from “T” it will be “A”; from “G” it will be “C” and from “C” it will be “G”.

Note that special “nucleotides” such as R or Y, may be used as well - R refers to a Purine, and Y refers to a Pyrimidine.

Usage example:

x = Bioroebe::Sequence.new('ATGCAA'); x.complementary # => "TACGTT"
#


31
32
33
34
35
36
37
38
39
40
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 31

def self.complementary_strand(
    i = @sequence.chars
  )
  if i and i.is_a?(String)
    i = i.chars
  end
  return i.map {|line|
    ::Bioroebe.complementary_nucleotide(line)
  }.join
end

Instance Method Details

#allow_only_valid_dna(i = @sequence.dup) ⇒ Object Also known as: only_valid_dna_nucleotides

#

allow_only_valid_dna

Allow only valid DNA. It will discard anything that is not A, T, C or G.

“ATCGIIII”.allow_only_valid_dna

#


237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 237

def allow_only_valid_dna(
    i = @sequence.dup
  )
  return_string = ''.dup
  i.each_char { |entry|
    case entry.downcase
    when 'a','t','c','g' # Uracil is only in RNA, not in DNA.
      return_string << entry # But don't put the downcased string into that.
    end
  }
  return return_string
end

#at_percentageObject Also known as: at_percent, AT_percentage

#

at_percentage

This is the complement of GC percentage, thus why we deduct from 100.

#


165
166
167
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 165

def at_percentage
  (100 - gc_percentage)
end

#back_from_rna(i = seq?) ) ⇒ Object Also known as: back_transcribe

#

back_from_rna

#


127
128
129
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 127

def back_from_rna(i = seq?)
  i.tr('U','T')
end

#codon_to_aminoacid(codon = sequence?) ) ⇒ Object Also known as: translate_aminoacid_into_dna

#

codon_to_aminoacid

This method will translate a DNA-codon (or RNA-codon) into the corresponding aminoacid.

Usage example:

codon_to_aminoacid TCC
#


264
265
266
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 264

def codon_to_aminoacid(codon = sequence?)
  ::Bioroebe.codon_to_aminoacid(codon)
end

#complementary_strand(i = '') ⇒ Object Also known as: complementary, build_complementary_dna_strand, build_second_strand, build_complementary_strand

#

complementary_strand

#


48
49
50
51
52
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 48

def complementary_strand(
    i = ''
  )
  return ::Bioroebe.complementary_strand(i)
end

#cut_with_enzyme(this_enzyme) ⇒ Object

#

cut_with_enzyme

Use this method to cut the given sequence via a restriction enzyme. This is only relevant for DNA or RNA, not proteins - at the least in regards to restriction enzymes. Obviously proteins may be cleavable via proteolytic enzymes.

Usage example:

sequence = Bioroebe::Sequence.new(::Bioroebe.return_random_aminoacid(50)); result = sequence.cut_with_enzyme 'EcoRI'
sequence = Bioroebe::Sequence.new(150); result = sequence.cut_with_enzyme 'EcoRI'
#


145
146
147
148
149
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 145

def cut_with_enzyme(this_enzyme)
  require 'bioroebe/enzymes/restriction_enzyme.rb'
  target_sequence = ::Bioroebe.restriction_enzyme("#{this_enzyme}.site")
  main_string?.split(/#{target_sequence}/)
end

#gc_percentageObject Also known as: gc_percent

#

gc_percentage

This will return, as a float (aka number), the percentage of GC of a given RNA or DNA string. An example value may be 55.3, which stands for 55.3%.

It will not work if .type? is a protein, though, which is why it is part of this module, so that we can avoid calling it on a protein.

Usage example:

x = Bioroebe::Sequence.new('AGCT'); x.gc_percentage
#


113
114
115
116
117
118
119
120
121
122
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 113

def gc_percentage(
    round_to_n_positions = 2
  )
  sum = @sequence.count('G') + @sequence.count('C') # Count G + C here.
  result = (sum * 100.0 / @sequence.size)
  if round_to_n_positions
    result = result.round(round_to_n_positions)
  end
  return result
end

#has_stop_codon?(optional_use_this_frame = nil) ⇒ Boolean

#

has_stop_codon?

This method will return true if the sequence includes at the least one stop codon; otherwise this method will return false.

Usage examples:

require 'bioroebe'; include Bioroebe; x = Seq('ATC GTC GGA ATAG'); puts x.has_stop_codon?         # false
require 'bioroebe'; include Bioroebe; x = Seq('ATG GTC GGA ATAG'); puts x.has_stop_codon? :frame1 # true
require 'bioroebe'; include Bioroebe; x = Seq('ATG GTC GGA ATAG'); puts x.has_stop_codon? :frame2
require 'bioroebe'; include Bioroebe; x = Seq('ATG GTC GGA ATAG'); puts x.has_stop_codon? :frame3
#

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 184

def has_stop_codon?(
    optional_use_this_frame = nil
  )
  _ = @sequence
  stop_codons = ::Bioroebe.stop_codons?
  # ======================================================================= #
  # We must ensure that the stop-codons are actually initialized. If not
  # then we will do so here quickly, then re-assign.
  # ======================================================================= #
  if stop_codons.empty?
    ::Bioroebe.initialize_stop_codons
    stop_codons = ::Bioroebe.stop_codons?
  end
  if optional_use_this_frame
    case optional_use_this_frame
    # ===================================================================== #
    # === :frame1
    # ===================================================================== #
    when :frame1, :in_frame1
      _ = _.scan(/.../)
    # ===================================================================== #
    # === :frame2
    # ===================================================================== #
    when :frame2, :in_frame2
      _ = _[1..-1].scan(/.../)
    # ===================================================================== #
    # === :frame3
    # ===================================================================== #
    when :frame3, :in_frame3
      _ = _[2..-1].scan(/.../)
    else
      e 'Unknown input: '+orange(optional_use_this_frame.to_s)+
        ' (class '+optional_use_this_frame.class.to_s+')'
      return false
    end
    stop_codons.any? {|this_stop_codon_triplett|
      _.include? this_stop_codon_triplett
    }
  else
    stop_codons.any? {|this_stop_codon_triplett|
      _.include? this_stop_codon_triplett
    }
  end
end

#n_random_dna(n_elements = :default, do_also_assign = false) ⇒ Object

#

n_random_dna

This method will assign a random DNA sequence.

The first argument tells us how long that sequence should be, defaulting to 100.

The second argument means that we will also invoke set_string(), which may lead to an infinite loop if you are not careful, so be mindful of that.

#


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 89

def n_random_dna(
    n_elements     = :default,
    do_also_assign = false
  )
  case n_elements
  when :default
    n_elements = 100
  end
  _ = n_elements.to_i.times.map { DNA_NUCLEOTIDES.sample }.join.strip
  set_string(_) if do_also_assign
  return _
end

#random(n_elements = :default) ⇒ Object

#

random

This method is only for DNA really.

#


273
274
275
276
277
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 273

def random(n_elements = :default)
  n_random_dna(
    n_elements, true
  ) if is_dna?
end

#remove_invalid_entries_from_the_dna_sequenceObject

#

remove_invalid_entries_from_the_dna_sequence

Get rid of all entries except for A, T, G and C.

#


156
157
158
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 156

def remove_invalid_entries_from_the_dna_sequence
  @sequence.tr!('B,D-F,H-S,U-Z','') if @sequence
end

#to_aminoacid_sequence(i = sequence?, , use_this_reading_frame = :default) ⇒ Object Also known as: translate

#

to_aminoacid_sequence

If you wish to convert the main sequence into the corresponding aminoacid sequence, use this method here.

You can specify a different reading frame as well - see the usage example that follows.

Usage example if you have a sequence object called seq:

seq = Bioroebe::Sequence.new(50); seq.translate(:frame_two)
seq = Bioroebe::Sequence.new(50); seq.translate(table: "Vertebrate Mitochondrial")
seq = Bioroebe::Sequence.new("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG"); seq.translate(table: "Vertebrate Mitochondrial")
#


295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 295

def to_aminoacid_sequence(
    i                      = sequence?,
    use_this_reading_frame = :default # This is :frame_one.
  )
  if i.is_a? Hash
    if i.has_key? :table
      Bioroebe.set_use_this_codon_table(i.delete(:table))
      use_this_reading_frame = :default
      i = sequence?
    end
  end
  # ======================================================================= #
  # We must sanitize the first argument if it is a Symbol, at the least
  # for 3 specific symbols.
  # ======================================================================= #
  if i.is_a? Symbol
    case i
    when :frame_one,
         :default
      i = sequence?
      use_this_reading_frame = :frame_one
    when :frame_two
      i = sequence?
      use_this_reading_frame = :frame_two
    when :frame_three
      i = sequence?
      use_this_reading_frame = :frame_three
    end
  end
  unless Bioroebe.const_defined? :DnaToAminoacidSequence
    require 'bioroebe/conversions/dna_to_aminoacid_sequence.rb'
  end
  _ = ::Bioroebe::DnaToAminoacidSequence.new(
    i, :do_not_run_yet
  ) # bl $BIOROEBE/conversions/dna_to_aminoacid_sequence.rb
  _.be_quiet_and_no_colours
  case i
  # ======================================================================= #
  # === :frame_one
  # ======================================================================= #
  when :frame_one,
       :from_frame_one,
       'one','1',1
    use_this_reading_frame = :frame_one
    i = sequence?
    _.use_this_sequence = i
  # ======================================================================= #
  # === :frame_two
  # ======================================================================= #
  when :frame_two,
       :from_frame_two,'two','2',2
    use_this_reading_frame = :frame_two
    i = sequence?
    _.use_this_sequence = i
  when :frame_three,
       :from_frame_three,'three','3',3
    use_this_reading_frame = :frame_three
    i = sequence?
    _.use_this_sequence = i
  end
  # ======================================================================= #
  # We may also use another reading frame, which will be handled next.
  # ======================================================================= #
  _.use_this_reading_frame = use_this_reading_frame
  _.run
  return _.sequence?
end

#to_dna(i = sequence?, , upcase_me = true) ⇒ Object Also known as: dna

#

to_dna

This method will convert a RNA sequence into a DNA sequence.

It will return the translation.

Usage example:

require 'bioroebe'; puts Bioroebe::DNA.new('actgggcgagaguuuuUUUUUU').to_dna
#


401
402
403
404
405
406
407
408
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 401

def to_dna(
    i         = sequence?,
    upcase_me = true
  )
  i = sequence? if i.nil?
  i = ::Bioroebe.to_dna(i, upcase_me)
  return i
end

#to_rna(i = seq?) ) ⇒ Object Also known as: rna, rna?, transcribe, to_RNA

#

to_rna

This method will simply return the RNA sequence corresponding to the DNA sequence at hand.

#


63
64
65
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 63

def to_rna(i = seq?)
  i.tr('T','U')
end

#to_T(i = seq?) ) ⇒ Object

#

to_T

#


73
74
75
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 73

def to_T(i = seq?)
  i.tr('U','T')
end