Skip to content

Instantly share code, notes, and snippets.

@dahankzter
Created August 6, 2014 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dahankzter/ce09d94a58e00e268797 to your computer and use it in GitHub Desktop.
Save dahankzter/ce09d94a58e00e268797 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"log"
"os"
)
const (
G = rune('G')
C = rune('C')
A = rune('A')
T = rune('T')
)
type Pair struct {
gc int
at int
}
func main() {
file, err := os.Open("./Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa")
if err != nil {
log.Fatal(err)
}
gc_frac := countFile(false, file)
fmt.Printf("GC count: %v\n", gc_frac)
}
func countFile(async bool, file *os.File) float64 {
at := 0
gc := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
tmpAt, tmpGc := count(scanner.Text())
at += tmpAt
gc += tmpGc
}
return float64(gc) / float64((gc + at))
}
func count(line string) (int, int) {
at := 0
gc := 0
for _, c := range line {
if c == G || c == C {
gc++
}
if c == A || c == T {
at++
}
}
return at, gc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment