Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/viral_seq/sequence.rb

Overview

functions added to Class::String for direct operation on sequence if it is a String object String.rc

# reverse complement
# example
"ACAGA".rc
=> "TCTGT"

String.mutation(error_rate)

# mutate a nt sequence (String class) randomly
# must define error rate, default value 0.01, aka 1%

USAGE

# example
seq = "TGGAAGGGCTAATTCACTCCCAACGAAGACAAGATATCCTTGATCTGTGGATCTACCACACACAAGGCTACTTCCCTG"
seq.mutation(0.05)
=> "TGGAAGGGCTAATGCACTCCCAACGAAGACACGATATCCTTGATCTGTGGATCTACGACACACAAGGCTGCTTCCCTG"

String.nt_parser

# parse the nucleotide sequences as a String object and return a Regexp object for possible matches

USAGE

"ATRWCG".nt_parser
=> /AT[A|G][A|T]CG/

Instance Method Summary collapse

Instance Method Details

#mutation(error_rate = 0.01) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/viral_seq/sequence.rb', line 364

def mutation(error_rate = 0.01)
  new_string = ""
  self.split("").each do |nt|
    pool = ["A","C","T","G"]
    pool.delete(nt)
    s = error_rate * 10000
    r = rand(10000)
    if r < s
      nt = pool.sample
    end
    new_string << nt
  end
  return new_string
end

#nt_parserObject



379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/viral_seq/sequence.rb', line 379

def nt_parser
  match = ""
  self.each_char.each do |base|
    base_array = ViralSeq.to_list(base)
    if base_array.size == 1
      match += base_array[0]
    else
      pattern = "[" + base_array.join("|") + "]"
      match += pattern
    end
  end
  Regexp.new match
end

#rcObject

direct function of calling reverse complement on String class



360
361
362
# File 'lib/viral_seq/sequence.rb', line 360

def rc
    self.reverse.tr("ACTG","TGAC")
end