Created
November 9, 2012 14:12
-
-
Save lukad/4045891 to your computer and use it in GitHub Desktop.
rosalind.info problem #1 parallel solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"runtime" | |
) | |
func baseCountWork(data []byte, baseChan chan [4]int64) { | |
var bases [4]int64 | |
for _, ch := range data { | |
switch ch { | |
case 'A', 'a': | |
bases[0]++ | |
case 'G', 'g': | |
bases[1]++ | |
case 'C', 'c': | |
bases[2]++ | |
case 'T', 't': | |
bases[3]++ | |
} | |
} | |
baseChan<- bases | |
} | |
func baseCount(data []byte) (a, c, g, t int64) { | |
baseChan := make(chan [4]int64) | |
defer close(baseChan) | |
n := runtime.GOMAXPROCS(0) | |
sliceLen := len(data) / n | |
for i := 0; i < n; i++ { | |
if i-1 == n { | |
go baseCountWork(data[sliceLen*i:], baseChan) | |
} else { | |
go baseCountWork(data[sliceLen*i:sliceLen*(i+1)], baseChan) | |
} | |
} | |
for i := 0; i < n; i++ { | |
bases := <-baseChan | |
a += bases[0] | |
c += bases[1] | |
g += bases[2] | |
t += bases[3] | |
} | |
return | |
} | |
func init() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
} | |
func main() { | |
input, err := ioutil.ReadFile("rosalind_dna.txt") | |
if err != nil { | |
fmt.Println(err.Error()) | |
return | |
} | |
fmt.Println(baseCount(input)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment