Skip to content

Commit ac4a3f3

Browse files
added random.DNASequence function. (#179)
1 parent 04bd937 commit ac4a3f3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

random/random.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"math/rand"
66
)
77

8-
// ProteinSequence returns a random protein sequence as a string that have size length, starts with aminoacid M (Methionine) and finishes with * (stop codon). The random generator uses the seed provided as parameter.
8+
// ProteinSequence returns a random protein sequence string of a given length and seed.
9+
// All returned sequences start M (Methionine) and end with * (stop codon).
910
func ProteinSequence(length int, seed int64) (string, error) {
1011
//The length needs to be greater than two because the random protein sequenced returned always contain a start and stop codon. You could see more about this stuff here: https://en.wikipedia.org/wiki/Genetic_code#Start_and_stop_codons
1112
if length <= 2 {
@@ -34,3 +35,19 @@ func ProteinSequence(length int, seed int64) (string, error) {
3435

3536
return string(randomSequence), nil
3637
}
38+
39+
// DNASequence returns a random DNA sequence string of a given length and seed.
40+
func DNASequence(length int, seed int64) (string, error) {
41+
42+
var nucleicAcidsAlphabet = []rune("ACTG")
43+
alphabetLength := len(nucleicAcidsAlphabet)
44+
rand.Seed(seed)
45+
46+
randomSequence := make([]rune, length)
47+
for basepair := range randomSequence {
48+
randomIndex := rand.Intn(alphabetLength)
49+
randomSequence[basepair] = nucleicAcidsAlphabet[randomIndex]
50+
}
51+
52+
return string(randomSequence), nil
53+
}

random/random_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ func TestRandomProteinSequenceError(t *testing.T) {
4242
t.Errorf("Random sequence must have sequence size equals 0 'RandomSequence(2, 4)'. Got this: \n%s instead of \n%s", strconv.Itoa(len(sequence)), strconv.Itoa(length))
4343
}
4444
}
45+
func ExampleDNASequence() {
46+
// RandomDNASequence builds a DNA Sequence by only passing through arguments a length and a seed that will be use to generate a randomly the sequence. The length needs to be greater than two because every sequence already have a start and stop codon. Seed makes this test deterministic.
47+
randomDNA, _ := DNASequence(15, 2)
48+
fmt.Println(randomDNA)
49+
50+
// Output: TTAAATTAGATGCAA
51+
}

0 commit comments

Comments
 (0)