-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRANb.go
47 lines (42 loc) · 829 Bytes
/
TRANb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"bytes"
"fmt"
"io/ioutil"
)
func main() {
data := readBytes(`data\rosalind_tran.txt`)
dna := parseFasta(data)
fmt.Printf("%v\n", tratio(dna[1], dna[3])) // 1.7065217391304348
}
func tratio(s, t []byte) float64 {
transitions := 0.0
transversions := 0.0
for i := range s {
if s[i] != t[i] {
if s[i]+t[i] == 'A'+'G' || s[i]+t[i] == 'C'+'T' {
transitions++
} else {
transversions++
}
}
}
return transitions / transversions
}
func parseFasta(data [][]byte) [][]byte {
var buf bytes.Buffer
for _, line := range data {
if line[0] == '>' {
buf.WriteRune('\n')
buf.Write(line)
buf.WriteRune('\n')
} else {
buf.Write(line)
}
}
return bytes.Fields(buf.Bytes())
}
func readBytes(name string) [][]byte {
b, _ := ioutil.ReadFile(name)
return bytes.Fields(b)
}