Task

Given a string of characters A, C, G, and T representing a DNA sequence write a routine to mutate the sequence, (string) by:

 Choosing a random base position in the sequence.
 Mutate the sequence by doing one of either:
 Swap the base at that position by changing it to one of A, C, G, or T. (which has a chance of swapping the base for the same base)
 Delete the chosen base at the position.
 Insert another base randomly chosen from A,C, G, or T into the sequence at that position.
 Randomly generate a test DNA sequence of at least 200 bases
 "Pretty print" the sequence and a count of its size, and the count of each base in the sequence
 Mutate the sequence ten times.
 "Pretty print" the sequence after all mutations, and a count of its size, and the count of each base in the sequence.
Extra credit

 Give more information on the individual mutations applied.
 Allow mutations to be weighted and/or chosen.
UInt32 seed = 0
F nonrandom(n)
   :seed = 1664525 * :seed + 1013904223
   R Int(:seed >> 16) % n
F nonrandom_choice(lst)
   R lst[nonrandom(lst.len)]
 
F basecount(dna)
   DefaultDict[Char, Int] d
   L(c) dna
      d[c]++
   R sorted(d.items())
 
F seq_split(dna, n = 50)
   R (0 .< dna.len).step(n).map(i -> @dna[i .< i + @n])
 
F seq_pp(dna, n = 50)
   L(part) seq_split(dna, n)
      print(‘#5: #.’.format(L.index * n, part))
   print("\n  BASECOUNT:")
   V tot = 0
   L(base, count) basecount(dna)
      print(‘    #3: #.’.format(base, count))
      tot += count
   V (base, count) = (‘TOT’, tot)
   print(‘    #3= #.’.format(base, count))
 
F seq_mutate(String =dna; count = 1, kinds = ‘IDSSSS’, choice = ‘ATCG’)
   [(String, Int)] mutation
   V k2txt = [‘I’ = ‘Insert’, ‘D’ = ‘Delete’, ‘S’ = ‘Substitute’]
   L 0 .< count
      V kind = nonrandom_choice(kinds)
      V index = nonrandom(dna.len + 1)
      I kind == ‘I’
         dna = dna[0 .< index]‘’nonrandom_choice(choice)‘’dna[index..]
      E I kind == ‘D’ & !dna.empty
         dna = dna[0 .< index]‘’dna[index+1..]
      E I kind == ‘S’ & !dna.empty
         dna = dna[0 .< index]‘’nonrandom_choice(choice)‘’dna[index+1..]
      mutation.append((k2txt[kind], index))
   R (dna, mutation)
 
print(‘SEQUENCE:’)
V sequence = ‘TCAATCATTAATCGATTAATACATTCAATTTGAACATCTCCAGGAGAAGGCAGGGTAATCTCGTGTAGCCGTGCTTGGGGCCTCCGATATGGCCGGGGAATTTCAAAGTATAGTGTGCATCCCCTCATAATACATAGATCTATAGGTAAGTATATGGGTTGACGTTGTTAGATGCGATACACGTGCACACTTTATGAATTTTACGTTCCTCTGCCTAGAGTGCCAAGTTTCAATTTGCTACGGTTCCTCA’
seq_pp(sequence)
print("\n\nMUTATIONS:")
V (mseq, m) = seq_mutate(sequence, 10)
L(kind, index) m
   print(‘ #10 @#.’.format(kind, index))
print()
seq_pp(mseq)