-
Notifications
You must be signed in to change notification settings - Fork 0
/
CONS.go
85 lines (77 loc) · 1.49 KB
/
CONS.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
) //comment
func main() {
data := readData(`data\rosalind_cons.txt`)
dna := parseFasta(data)
mat := dnaMatrix(dna)
pro := profile(mat)
con := consensus(pro)
fmt.Println(con)
fmt.Println(proPrint(pro))
}
func proPrint(pro [][]int) string {
var b bytes.Buffer
for i, chr := range "ACGT" {
row := strings.Trim(fmt.Sprintf("%v", pro[i]), "[]")
b.WriteRune(chr)
b.WriteRune(':')
b.WriteString(" " + row + "\n")
}
return b.String()
}
func consensus(pro [][]int) string {
con := ""
for col := range pro[0] {
max, loc := 0, 0
for row := range pro {
if pro[row][col] > max {
max = pro[row][col]
loc = row
}
}
con += string("ACGT"[loc])
}
return con
}
func profile(mat [][]rune) [][]int {
lenDNA := len(mat[0])
pro := make([][]int, 4)
for i, chr := range "ACGT" {
pro[i] = make([]int, lenDNA)
for col := range pro[i] {
for row := range mat {
if mat[row][col] == chr {
pro[i][col]++
}
}
}
}
return pro
}
func dnaMatrix(fasta []string) [][]rune {
var mat [][]rune
for i := 1; i < len(fasta); i += 2 {
mat = append(mat, []rune(fasta[i]))
}
return mat
}
func parseFasta(data []string) []string {
var buf bytes.Buffer
for _, line := range data {
if line[0] == '>' {
buf.WriteString("\n" + line + "\n")
} else {
buf.WriteString(line)
}
}
return strings.Fields(buf.String())
}
func readData(fi string) []string {
b, _ := ioutil.ReadFile(fi)
return strings.Fields(string(b))
}