Jump to content

Bioinformatics/Sequence mutation: Difference between revisions

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.
(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:
 
type
 
# Enumeration type for bases.
Base {.pure.} = enum A, C, G, T, Other = "other"
 
# Sequence of bases.
DnaSequence = seq[Base]string
 
# Kind of mutation.
Mutation = enum mutSwap, mutDelete, mutInsert
 
const MaxBaseVal = ord(Base.high) - 1 # Maximum base value.
 
#---------------------------------------------------------------------------------------------------
 
template toChar(base: Base): char = ($base)[0]
 
#---------------------------------------------------------------------------------------------------
Line 1,434 ⟶ 1,440:
## Create a DNA sequence of given length.
 
result = newSeqOfCap[Base]newStringOfCap(length)
for _ in 1..length:
result.add($Base(rand(3MaxBaseVal)))
 
#---------------------------------------------------------------------------------------------------
Line 1,455 ⟶ 1,461:
let newBase = Base(rand(ord(Base.Other) - 1))
echo fmt"Changing base at position {idx + 1} from {dnaSeq[idx]} to {newBase}"
dnaSeq[idx] = newBase.toChar
 
of mutDelete:
echo fmt"Deleting base {dnaSeq[idx]} at position {idx + 1}"
dnaSeq.delete(idx, idx)
 
of mutInsert:
let newBase = Base(rand(ord(Base.Other) - 1))
echo fmt"Inserting base {newBase} at position {idx + 1}"
dnaSeq.insert($newBase, idx)
 
#---------------------------------------------------------------------------------------------------
Line 1,472 ⟶ 1,478:
 
var counts: array[Base, Natural] # Count of bases.
for basec in dnaSeq:
inc counts[baseparseEnum[Base]($c, Other)] # Use Other as default value.
 
# Display the SQ line.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.