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.
string dna = repeat(' ',200+rand(300))
for i=1 to length(dna) do dna[i] = "ACGT"[rand(4)] end for
 
procedure show()
    sequence acgt = repeat(0,5)
    for i=1 to length(dna) do
        acgt[find(dna[i],"ACGT")] += 1
    end for
    acgt[$] = sum(acgt)
    sequence s = split(trim(join_by(split(join_by(dna,1,10,""),"\n"),1,5," ")),"\n")
    for i=1 to length(s) do
        printf(1,"%3d: %s\n",{(i-1)*50+1,s[i]})
    end for
    printf(1,"\nBase counts: A:%d, C:%d, G:%d, T:%d, total:%d\n",acgt)
end procedure
 
procedure mutate()
    printf(1,"\n")
    for i=1 to 10 do
        integer p = rand(length(dna)),
                sdi = "SDI"[rand(3)],
                rep = "ACGT"[rand(4)],
                was = dna[p]
        switch sdi do
            case 'S':dna[p] = rep           printf(1,"swapped %c at %d for %c\n",{was,p,rep})
            case 'D':dna[p..p] = ""         printf(1,"deleted %c at %d\n",{was,p})
            case 'I':dna[p..p-1] = ""&rep   printf(1,"inserted %c at %d, before %c\n",{rep,p,was})
        end switch
    end for
    printf(1,"\n")
end procedure
 
show()
mutate()
show()
