Bioinformatics/Sequence mutation: Difference between revisions

Content added Content deleted
(Changing the representation of a DNA sequence from a sequence of Base to a string, as requested. Updated procedures to work with a string rather than a sequence.)
Line 1,419: Line 1,419:


type
type

# Enumeration type for bases.
# Enumeration type for bases.
Base {.pure.} = enum A, C, G, T, Other = "other"
Base {.pure.} = enum A, C, G, T, Other = "other"


# Sequence of bases.
# Sequence of bases.
DnaSequence = seq[Base]
DnaSequence = string


# Kind of mutation.
# Kind of mutation.
Mutation = enum mutSwap, mutDelete, mutInsert
Mutation = enum mutSwap, mutDelete, mutInsert

const MaxBaseVal = ord(Base.high) - 1 # Maximum base value.

#---------------------------------------------------------------------------------------------------

template toChar(base: Base): char = ($base)[0]


#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
Line 1,434: Line 1,440:
## Create a DNA sequence of given length.
## Create a DNA sequence of given length.


result = newSeqOfCap[Base](length)
result = newStringOfCap(length)
for _ in 1..length:
for _ in 1..length:
result.add(Base(rand(3)))
result.add($Base(rand(MaxBaseVal)))


#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
Line 1,455: Line 1,461:
let newBase = Base(rand(ord(Base.Other) - 1))
let newBase = Base(rand(ord(Base.Other) - 1))
echo fmt"Changing base at position {idx + 1} from {dnaSeq[idx]} to {newBase}"
echo fmt"Changing base at position {idx + 1} from {dnaSeq[idx]} to {newBase}"
dnaSeq[idx] = newBase
dnaSeq[idx] = newBase.toChar


of mutDelete:
of mutDelete:
echo fmt"Deleting base {dnaSeq[idx]} at position {idx + 1}"
echo fmt"Deleting base {dnaSeq[idx]} at position {idx + 1}"
dnaSeq.delete(idx)
dnaSeq.delete(idx, idx)


of mutInsert:
of mutInsert:
let newBase = Base(rand(ord(Base.Other) - 1))
let newBase = Base(rand(ord(Base.Other) - 1))
echo fmt"Inserting base {newBase} at position {idx + 1}"
echo fmt"Inserting base {newBase} at position {idx + 1}"
dnaSeq.insert(newBase, idx)
dnaSeq.insert($newBase, idx)


#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
Line 1,472: Line 1,478:


var counts: array[Base, Natural] # Count of bases.
var counts: array[Base, Natural] # Count of bases.
for base in dnaSeq:
for c in dnaSeq:
inc counts[base]
inc counts[parseEnum[Base]($c, Other)] # Use Other as default value.


# Display the SQ line.
# Display the SQ line.