-
Notifications
You must be signed in to change notification settings - Fork 0
/
9_mers.go
198 lines (134 loc) · 3.41 KB
/
9_mers.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Implementation of Position weight matrix on a DnaA 9-mers.
package main
import (
"fmt"
"os"
"bufio"
"strconv"
"math"
"strings"
"io/ioutil"
)
// declare functions
func readLines(path string) ([]string, error){
// Returns array of the string and error
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines[4:], scanner.Err()
}
func readRaw(path string) string{
bs, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
str := string(bs)
return str
}
func writeRaw(lines string, path string)error{
// Reads from string and write to .txt
file, err := os.Create(path)
if err != nil {
panic(err)
}
defer file.Close()
file.WriteString(lines)
return err
}
func writeLines(lines []string, path string) error {
// Reads from array and write to .txt
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines{
var new_line []string = strings.Split(line[67:76], ":") // & extract 9 characters from each line from position 67 to 75
fmt.Fprintln(w, new_line[(len(new_line)) - 1])
}
return w.Flush()
}
func Round(x, unit float64) float64 {
// math extension
return math.Round(x * math.Pow(10, unit)) / math.Pow(10, unit)
}
// declare variables
var (
// name string
test_path string = "data/temp.txt"
file_path string = "data/EI_true.seq"
ei_nine_path string = "outputs/EI_nine.txt"
score_file string = "outputs/EI_nine_pwm.txt"
output_file string = "outputs/EI_nine_output.txt"
)
func main() {
splitted_content, _ := readLines(file_path)
writeLines(splitted_content, ei_nine_path)
// New txt file for position matrix EI_nine
// initialize the PWM with four rows and nine columns
var a, c, g, t [9]float64
input_file, _ := readLines(ei_nine_path)
var num_lines float64 = float64(len(input_file))
// read line by line and update the PWM with the frequencies of each base at
// the 9 position
for _, line := range input_file{
for i:=0; i < 9; i++ {
switch string(line[i]) {
case "A": a[i] += 1.0
case "C": c[i] += 1.0
case "G": g[i] += 1.0
case "T": t[i] += 1.0
}
}
}
for i:=0; i < 9; i++ {
a[i] = Round(math.Log2((a[i] + 0.1) / (num_lines + 0.4) / 0.25), 3)
c[i] = Round(math.Log2((c[i] + 0.1) / (num_lines + 0.4) / 0.25), 3)
g[i] = Round(math.Log2((g[i] + 0.1) / (num_lines + 0.4) / 0.25), 3)
t[i] = Round(math.Log2((t[i] + 0.1) / (num_lines + 0.4) / 0.25), 3)
}
// write the position weight matrix
genes := map[string][9]float64{
"A":a,
"C":c,
"G": g,
"T": t,
}
var text string
for key, gene := range genes {
text += key + "\t" + "\t"
for i:=0; i < 9; i++ {
text += strconv.FormatFloat((gene[i]), 'f', 3, 64) + "\t"
}
text += "\n"
}
writeRaw(text, score_file)
// Final, write to output
var score float64
var temp_str string
for _, line := range input_file{
score = 0.0
for i:=0; i < 9; i++ {
switch string(line[i]) {
case "A": score += a[i]
case "C": score += c[i]
case "G": score += g[i]
case "T": score += t[i]
}
temp_str += line + "\t" + strconv.FormatFloat(score, 'f', 3, 64) + "\n"
}
writeRaw(temp_str, output_file)
}
fmt.Println()
fmt.Println("Done...")
// n := readRaw(output_file)
// fmt.Println(n)
}