Rebol3 Code Examplex


Bioinformatics/Sequence mutation

Given a string of characters A, C, G, and T representing a DNA sequence write a routine to mutate the sequence.

Rebol [
    title: "Rosetta code: Bioinformatics/Sequence mutation"
    file:  %Bioinformatics-Sequence_mutation.r3
    url:   https://rosettacode.org/wiki/Bioinformatics/Sequence_mutation
]

bases: "ATGC"

pretty-print: function [dna][
    count: #[] foreach base bases [count/:base: 0]
    i: 1
    forskip dna 50 [
        line: copy/part dna 50
        prin format [-4 ":"][i * 50 - 50 + min 50 length? dna]
        forskip line 10 [
            prin ["" copy/part line 10]
        ]
        print ""
        foreach key line [
            count/:key: 1 + count/:key
        ]
        i: i + 1
    ]
    print ["Total count:" mold count]
]

perform-random-modifications: function [dna times] [
    result: copy dna
    loop times [
        index: random length? result
        switch random 3 [
            1 [
                prev: result/:index
                until [prev != base: random/only bases]
                result/:index: base
                print [" changing base" prev "at position" pad index 3 "to" base]
            ]
            2 [
                insert at result index base: random/only bases
                print ["inserting base" base "at position" index]
            ]
            3 [
                base: take at result index
                print [" deleting base" base "at position" index]
            ]
        ]
    ]
    result
]

dna: "" loop 200 [append dna random/only bases]
print "------------------------------"
print " Initial sequence"
print "------------------------------"
pretty-print dna
print ""

print "------------------------------"
print " Modifying sequence"
print "------------------------------"
dna: perform-random-modifications dna 10
print ""

print "------------------------------"
print " Final sequence"
print "------------------------------"
pretty-print dna
print ""

Output:

------------------------------
 Initial sequence
------------------------------
  50: CGAGGAATCG TAGTAGTATC CTCGGGCCGG AGGCTAATAT CGCCTTCAGT
 100: CAAAGCTTGA CATTTTATCG ATCCTCCATG GATCATCCAT CTTAGTACCT
 150: GGCTGGTGCC TGTGGACATG GATGTTCCGC CACCTATGTA TTGCCTCGCG
 200: TGACCGACTA TACATAGCTA CTTTGCGGCG TCTAGAGCTT AGTCCCACAG
Total count: #[
    #"A" 42
    #"T" 56
    #"G" 48
    #"C" 54
]

------------------------------
 Modifying sequence
------------------------------
 changing base C at position 158 to T
 changing base G at position 138 to C
inserting base C at position 47
inserting base C at position 48
 deleting base A at position 118
 changing base A at position 119 to C
inserting base G at position 142
 deleting base C at position 134
 deleting base A at position 171
 deleting base C at position 48

------------------------------
 Final sequence
------------------------------
  50: CGAGGAATCG TAGTAGTATC CTCGGGCCGG AGGCTAATAT CGCCTTCCAG
 100: TCAAAGCTTG ACATTTTATC GATCCTCCAT GGATCATCCA TCTTAGTACC
 150: TGGCTGGTGC CTGTGGCCTG GATGTTCCGC CACTATCTAG TTGCCTCGCG
 199: TGACCGATTA TACATAGCTC TTTGCGGCGT CTAGAGCTTA GTCCCACAG
Total count: #[
    #"A" 39
    #"T" 57
    #"G" 48
    #"C" 55
]