title: Mutate DNA sequence with ruby
body: <p>I have a simple class that models random mutations in a DNA string. It introduces a random (A C G T) character into a random location. I would like to introduce a way for specifying a rate of mutation. </p>
  
  <p>Is there a way I can specify how to:</p>
  
  <ul>
  <li>Mutate the sequence given an error rate. (e.g. 1 in 1000) ?</li>
  <li>A way to bias the mutation (choose G&lt;->A or C&lt;->T more frequently)?  </li>
  </ul>
  
  <p>here is my current implementation that takes care of random changes only. </p>
  
  <pre><code>class DNA
    attr_accessor :number_of_mutations
    attr_accessor :seq
  
    NUCLEOTIDES = ['A','C','T','G']
  
    #random positions
    def random_pos
      (1..seq.length).to_a.sample(number_of_mutations)
    end
  
    #pick a random nucleotide
    def random_nucleotide
      NUCLEOTIDES.sample
    end
  
    #generate a random seq
    def random_seq
      (1..number_of_mutations).map{NUCLEOTIDES[rand(NUCLEOTIDES.length)]}
    end
  
    def mutate
      #pick a random position
      positions = random_pos
  
      #pick a random nucleotide
      nucleotides = random_seq
  
      #insert the random nucleotide at the random position in the seq
      positions.zip(nucleotides).map do |pos,nuc|
        seq[pos] = nuc.downcase
      end
      seq
    end
  end
  
  d = DNA.new
  d.number_of_mutations = 1
  d.seq = "AAAAAA"
  puts d.mutate  #will introduce a single random DNA base at a random location 
  </code></pre>