Skip to content

Instantly share code, notes, and snippets.

@mduvall
Created March 30, 2014 05:48
Show Gist options
  • Save mduvall/9868221 to your computer and use it in GitHub Desktop.
Save mduvall/9868221 to your computer and use it in GitHub Desktop.
Quick example of using FASTA file to compute consensus strings
package main
import (
"fasta"
"fmt"
)
func main() {
fastaFile := fasta.NewFastaFile("cons.txt")
var dnaLength int
for _, dna := range fastaFile.DnaSeqs {
dnaLength = len(dna.Sequence)
break
}
profileMatrix := map[byte][]float64{
'A': make([]float64, dnaLength),
'T': make([]float64, dnaLength),
'C': make([]float64, dnaLength),
'G': make([]float64, dnaLength),
}
for _, dna := range fastaFile.DnaSeqs {
for index, nucleobase := range dna.Sequence {
profileMatrix[nucleobase][index] += 1.0
}
}
fmt.Println("A: ", profileMatrix['A'])
fmt.Println("C: ", profileMatrix['C'])
fmt.Println("G: ", profileMatrix['G'])
fmt.Println("T: ", profileMatrix['T'])
consensusString := make([]byte, dnaLength)
var (
dominantKeyAT byte
dominantKeyGC byte
dominantKey byte
)
for i, _ := range profileMatrix['A'] {
if profileMatrix['A'][i] > profileMatrix['T'][i] {
dominantKeyAT = 'A'
} else {
dominantKeyAT = 'T'
}
if profileMatrix['G'][i] > profileMatrix['C'][i] {
dominantKeyGC = 'G'
} else {
dominantKeyGC = 'C'
}
if profileMatrix[dominantKeyAT][i] > profileMatrix[dominantKeyGC][i] {
dominantKey = dominantKeyAT
} else {
dominantKey = dominantKeyGC
}
consensusString[i] = dominantKey
}
fmt.Println("Consensus string: ", string(consensusString))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment