Module: Bioroebe::NucleotideModule

Included in:
DNA
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"
#

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

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

#

225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 225

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.

#

163
164
165
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 163

def at_percentage
  (100 - gc_percentage)
end

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

#

back_from_rna

#

125
126
127
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 125

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
#

250
251
252
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 250

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
# 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'
#

143
144
145
146
147
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 143

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
#

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

def gc_percentage(
    round_to_n_positions = 2
  )
  sum = @sequence.count('G') + @sequence.count('C')
  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)

182
183
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
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 182

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
    when :frame1, :in_frame1
      _ = _.scan(/.../)
    when :frame2, :in_frame2
      _ = _[1..-1].scan(/.../)
    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.

#

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

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.

#

259
260
261
262
263
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 259

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.

#

154
155
156
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 154

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")
#

281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 281

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
#

387
388
389
390
391
392
393
394
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 387

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.

#

61
62
63
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 61

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

#to_T(i = seq?) ) ⇒ Object

#

to_T

#

71
72
73
# File 'lib/bioroebe/sequence/nucleotide_module/nucleotide_module.rb', line 71

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