Mutations in a DNA sequence of codons.

Another bit of coding suggested by Rosetta Code.

Life on Earth is coded by sequences of four basic units, ACG and T, twisted into our double-helix DNA. It is copied for reproduction with surprosing fidelity, but errors can creep in, as in this sentence. These may be harmless, lethal- or VERY occasionally lead to offspring with superior ability to thrive in a particular environment.

My code allots different probability to a deletion, insertion, or in-place change. You could add further possibilities, including checking if the initiation and termination codons havebecome in-valid, only allow transition/transversion changes, etc.

RC only asked for a teletype-style printed output. I much prefer the easily LB produced colourful graphic!



    global current$, bases$, V

    bases$  ="ACGT"
    V       =15

    for i =1 to 120  '   generate a random genome of 160 terms
        current$ =current$ +mid$( bases$, 1 +int( 4 *rnd( 1)))
    next i

    WindowWidth  =1300
    WindowHeight = 600

    open "Mutations" for graphics_nsb as #wg

    #wg "down ; size 2 ; down ; fill 50 50 50"
    #wg "trapclose quit"

    for i =1 to 170
        print current$
        call show
        V   =V +3

        R =rnd( 1)

        select case
            case R <=0.6
                call swap   '   60% of the time
            case R <=0.85
                call remove '   15%
            case else
                call insert '   25%
        end select

    next i



    sub quit h$
        close #h$
    end sub

    sub swap

    end sub

    sub remove
        L   =len( current$)
        p   =int( 1 +L *rnd( 1))
        current$ =left$( current$, p -1) +mid$( current$, p +1)
    end sub

    sub insert
        L   =len( current$)
        p   =int( 1 +L *rnd( 1))
        current$ =left$( current$, p) +mid$( bases$, 1 +int( 4 *rnd( 1))) +mid$( current$, p +1)
    end sub

    sub show
        L   =len( current$)
        for k =1 to L
            select case mid$( current$, k, 1)
                case "C"
                    col$ ="red"
                case "G"
                    col$ ="green"
                case "T"
                    col$ ="blue"
                case "A"
                    col$ ="yellow"
            end select

            #wg "color "; col$
            #wg "set "; 10 +3 *k; " "; V
        next k
    end sub