From charlesreid1

ba1a.go

package main

import "fmt"

// Rosalind: Problem BA1A: Most Frequent k-mers

// Describe the problem
func BA1ADescription() {
    description := []string{
        "-----------------------------------------",
        "Rosalind: Problem BA1A:",
        "Most Frequest k-mers",
        "",
        "Given an input string and a length k,",
        "report the k-mer or k-mers that occur",
        "most frequently.",
        "",
        "URL: http://rosalind.info/problems/ba1a/",
        "",
    }
    for _, line := range description {
        fmt.Println(line)
    }
}

// Count occurrences of a substring pattern 
// in a string input
func PatternCount(input string, pattern string) int {

    // Number of substring overlaps
    var overlap = len(input) - len(pattern) + 1

    // If overlap < 1, we are looking 
    // for a pattern longer than our input
    if overlap<1 {
        return 0
    }

    // Count of occurrences
    count:=0

    // Loop over each substring overlap
    for i:=0; i<overlap; i++ {
        // Grab a slice of the full input
        start:=i
        end:=i+len(pattern)
        var slice = input[start:end]
        if slice==pattern {
            count += 1
        }
    }
    return count
}

// Describe the problem, and call the function
func BA1A() {
    BA1ADescription()
    res := PatternCount("GCGCG","GCG")
    fmt.Println("PatternCount(GCGCG,GCG) yields:",res)
}


ba1a_test.go

package main

import (
    "fmt"
    "testing"
)

// To run this test:
//
// $ go test -v -run TestPatternCount

// Run a single test of the PatternCount function
func TestPatternCount(t *testing.T) {
    // Call the PatternCount function
    input := "GCGCG"
    pattern := "GCG"
    result := PatternCount(input,pattern)
    gold := 2
    if result != gold {
        err := fmt.Sprintf("Error testing PatternCount(): input = %s, pattern = %s, result = %d (should be %d)",
            input, pattern, result, gold)
        t.Error(err)
    }
}

// Run a test matrix of the PatternCount function
func TestPatternCounts(t *testing.T) {
    // Construct a test matrix
    var tests = []struct {
		input    string
        pattern  string
		gold int
	}{
		{"GCGCG",        "GCG",      2},
		{"GAGGGGGGGAG",  "AGG",      1},
		{"GCACGCACGCAC", "GCAC",     3},
        {"",             "GC",       0},
        {"GCG",          "GTACTCTC", 0},
        {"ACGTACGTACGT", "CG",       3},
        {"AAAGAGTGTCTGATAGCAGCTTCTGAACTGGTTACCTGCCGTGAGTAAATTAAATTTTATTGACTTAGGTCACTAAATACTTTAACCAATATAGGCATAGCGCACAGACAGATAATAATTACAGAGTACACAACATCCA",
                         "AAA",      4},
        {"AGCGTGCCGAAATATGCCGCCAGACCTGCTGCGGTGGCCTCGCCGACTTCACGGATGCCAAGTGCATAGAGGAAGCGAGCAAAGGTGGTTTCTTTCGCTTTATCCAGCGCGTTAACCACGTTCTGTGCCGACTTT",
                         "TTT",      4},
        {"GGACTTACTGACGTACG","ACT",  2},
        {"ATCCGATCCCATGCCCATG","CC", 5},
        {"CTGTTTTTGATCCATGATATGTTATCTCTCCGTCATCAGAAGAACAGTGACGGATCGCCCTCTCTCTTGGTCAGGCGACCGTTTGCCATAATGCCCATGCTTTCCAGCCAGCTCTCAAACTCCGGTGACTCGCGCAGGTTGAGT",
                         "CTC",      9},
	}
    for _, test := range tests {
        result := PatternCount(test.input, test.pattern)
        if result != test.gold {
            err := fmt.Sprintf("Error testing PatternCount(): input = %s, pattern = %s, result = %d (should be %d)",
                        test.input, test.pattern, result, test.gold)
            t.Error(err)
        }
    }
}

Flags