Skip to content

Basic sequence

Open this tutorial as a notebook

Sequences are series of characters that can represent RNA, DNA or chains of amino acids. Within BioKotlin, they are represented by the seq package. Below, we demonstrate the basics of creating and manipulating sequences.

Start by importing the BioKotlin module, and the seq package.

%use biokotlin
import biokotlin.seq.*

Next, create a nucleotide sequence and output its complement. DNA is inferred (instead of RNA) because of the "T" within the sequence.

val dna = NucSeq("GCAGAT")
dna.complement()
CGTCTA

Ensure that a sequence is a specific type when there is no thymine or uracil for inference with the NUC enum.

val rna = NucSeq("AGCG", NUC.RNA)
rna.complement() //Note that this outputs uracil because the sequence is RNA!
UCGC

Now, translate it to a protein sequence (will print both the codon table ID, defaulting to 1, and the translated sequence).

dna.translate()
1
AD

Now, transcribe it to and RNA sequence

dna.transcribe()
GCAGAU

Create a protein sequence, count its number of glysines, and print it in a String template. Here, the AminoAcid enum is used, which stores useful properties of the amino acids, such as their three letter encoding.

val protein = ProteinSeq("GCAGAT")
val gCount = protein.count(AminoAcid.G)
println("${AminoAcid.G.name3letter} count is $gCount")
Gly count is 2

The + and * operators can be used to concatenate two sequences or the same sequence with itself multiple times, respectively.

val otherProtein = ProteinSeq("ARSQRS")
val concatenatedProtein = protein + otherProtein
println("A concatenated protein: $concatenatedProtein")
println("A repeated DNA sequence: ${dna * 3}")
A concatenated protein: GCAGATARSQRS
A repeated DNA sequence: GCAGATGCAGATGCAGAT

Finally, let's find the mass of our concatenated protein in daltons. The brackets return an AminoAcid when a single integer is used as the index. Each AminoAcid stores its mass in the weight property.

var mass = 0.0
for (index in 0 until concatenatedProtein.size()) {
    mass += concatenatedProtein[index].weight
}
println("The mass of $concatenatedProtein is $mass daltons")
The mass of GCAGATARSQRS is 1362.4218999999998 daltons