-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
311 lines (249 loc) · 7.39 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
)
type Cgene struct {
StartBP int64
EndBP int64
Strand string
ProteinLength int64
PID string
Name string
Product string
}
type Organism struct {
// ------------- organism info -------------------------------------------------
OrganismName string
// ------------- protein(gene) info --------------------------------------------
PttFilename string
Ngenes int64
Ptt []Cgene
// ------------- DNA info ---------------------------------------
DNAFilename string
Nnucleotides int64
HeaderLine string
DNA string
}
func getDataPath() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
excuteDir := filepath.Dir(ex)
return path.Join(excuteDir, "data")
}
func (organism *Organism) LoadProteinTable(organismName string) bool {
log.Printf("\tAttempt to Load Protein Table (list of genes) for organism:%s\n", organismName)
organism.PttFilename = path.Join(getDataPath(), fmt.Sprintf("%s.ptt", organismName))
log.Printf("\tAttempt to open filename: %s\n", organism.PttFilename)
if strings.TrimSpace(organism.PttFilename) == "" {
log.Fatalln("[LoadProteinTable]:PttFilename can't be null")
return false
}
if _, err := os.Stat(organism.PttFilename); os.IsNotExist(err) {
log.Fatalln("[LoadProteinTable]:PttFilename not exists")
return false
}
pttBytes, err := ioutil.ReadFile(organism.PttFilename)
if err != nil {
log.Fatalln("[LoadProteinTable]:", err)
return false
}
ptt_text := string(pttBytes)
line_texts := strings.Split(strings.TrimSpace(ptt_text), "\n")
organism.Ptt = make([]Cgene, 0)
for idx, line := range line_texts {
if idx == 0 {
// parse header
continue
}
if idx == 1 {
// proteins length
protein_len_texts := strings.Split(line, " ")
if len(protein_len_texts) == 0 {
log.Println("[LoadProteinTable]:parse protein length error")
continue
}
proteion_len, err := strconv.ParseInt(protein_len_texts[0], 10, 64)
if err != nil {
log.Println("[LoadProteinTable]:parse protein length error ", err)
continue
}
organism.Ngenes = proteion_len
log.Printf("\tNumber of lines (excl. header) in %s.ptt is: %d\n", organismName, organism.Ngenes)
continue
}
if idx == 2 {
// column header
continue
}
// log.Println(line)
gene_line_texts := strings.Split(line, "\t")
if len(gene_line_texts) < 9 {
log.Println("[LoadProteinTable]: data error")
continue
}
protein_len, err := strconv.ParseInt(gene_line_texts[2], 10, 64)
if err != nil {
log.Println("[LoadProteinTable]: data error", err)
continue
}
location_texts := strings.Split(gene_line_texts[0], "..")
start_bp, err := strconv.ParseInt(location_texts[0], 10, 64)
end_bp, err := strconv.ParseInt(location_texts[1], 10, 64)
gene := Cgene{
StartBP: start_bp,
EndBP: end_bp,
Strand: gene_line_texts[1],
ProteinLength: protein_len,
PID: gene_line_texts[3],
Name: gene_line_texts[4],
Product: gene_line_texts[8],
}
organism.Ptt = append(organism.Ptt, gene)
}
log.Printf("\tProtein table %s.ptt successfully opened and loaded.\n\n", organismName)
return true
}
func (organism *Organism) printProteinTable2stdout() {
}
func (organism *Organism) PrintProteinTable2stdout(n int64) {
}
func (organism *Organism) LoadDNA(organismName string) bool {
log.Printf("\tAttempt to Load DNA for organism:%s\n", organismName)
organism.DNAFilename = path.Join(getDataPath(), fmt.Sprintf("%s.fna", organismName))
log.Printf("\tAttempt to open filename: %s\n", organism.DNAFilename)
if strings.TrimSpace(organism.DNAFilename) == "" {
log.Fatalln("[LoadDNA]:DNAFilename can't be null")
return false
}
if _, err := os.Stat(organism.DNAFilename); os.IsNotExist(err) {
log.Fatalln("[LoadDNA]:DNAFilename not exists")
return false
}
dnaBytes, err := ioutil.ReadFile(organism.DNAFilename)
if err != nil {
log.Fatalln("[LoadDNA]:", err)
return false
}
dna_text := string(dnaBytes)
dna_line_texts := strings.SplitN(dna_text, "\n", 2)
if len(dna_line_texts) < 2 {
log.Fatalln("[LoadDNA]: parse failed")
return false
}
organism.HeaderLine = dna_line_texts[0]
header_infos := strings.Split(organism.HeaderLine, "|")
if len(header_infos) > 1 {
organism.Nnucleotides, err = strconv.ParseInt(header_infos[1], 10, 64)
log.Printf("\tNumber of lines of DNA (excl. header) in %s.fna is: %d\n", organismName, organism.Nnucleotides)
}
organism.DNA = strings.ReplaceAll(dna_line_texts[1], "\n", "")
log.Printf("\tDNA %s.fna successfully opened and loaded.\n\n", organismName)
return true
}
func (organism *Organism) headDNA2stdout(n int64) {
}
func (organism *Organism) bsearch(search string) {
dna_search_index := int64(strings.Index(organism.DNA, search))
dna_search_end_index := dna_search_index + int64(len(search))
ptts := make([]Cgene, 0)
for ptt_idx, ptt := range organism.Ptt {
if ptt.StartBP <= dna_search_index && ptt.EndBP >= dna_search_end_index {
ptts = append(ptts, ptt)
break
}
if ptt.StartBP > dna_search_end_index {
if ptt_idx > 0 {
last_ptt := organism.Ptt[ptt_idx-1]
if last_ptt.StartBP <= dna_search_index {
ptts = append(ptts, last_ptt)
ptts = append(ptts, ptt)
break
}
}
}
}
if len(ptts) == 0 {
log.Printf("Not Found\n")
} else if len(ptts) == 1 {
ptt := ptts[0]
log.Printf("%s found at bp %d\n", search, dna_search_index)
log.Printf("WITHIN GENE Name: %s Start: %d End: %d PID: %s\n", ptt.Name, ptt.StartBP, ptt.EndBP, ptt.PID)
} else if len(ptts) == 2 {
up := ptts[0]
down := ptts[1]
log.Printf("\t%s found at bp %d\n", search, dna_search_index)
log.Printf("\t\tIn between genes:\n")
log.Printf("\t\t\tUPSTREAM Name: %s Start: %d End: %d PID: %s\n", up.Name, up.StartBP, up.EndBP, up.PID)
log.Printf("\t\t\tand\n")
log.Printf("\t\t\tDOWNSTREAM Name: %s Start: %d End: %d PID: %s\n", down.Name, down.StartBP, down.EndBP, down.PID)
}
}
func printLine() {
log.Println("----------------------------------------------------------")
}
func printDoubleLine() {
log.Println("==========================================================================")
}
func main() {
log.SetFlags(0)
log.SetOutput(new(logWriter))
for {
printDoubleLine()
fmt.Printf("Enter the Organism name (e.g., Ecoli): ")
var orgaismName string
fmt.Scanln(&orgaismName)
if len(orgaismName) == 0 {
continue
}
organism := &Organism{}
organism.LoadProteinTable(orgaismName)
organism.LoadDNA(orgaismName)
for {
fmt.Printf("What motif would you like to find >> ")
var motif string
fmt.Scanln(&motif)
organism.bsearch(motif)
printLine()
var response string
var breakParent bool = false
for len(response) == 0 {
fmt.Printf("Do you want to search for another motif? (Y/n): ")
fmt.Scanln(&response)
printLine()
if len(response) > 0 {
if response[0] == 'N' || response[0] == 'n' {
breakParent = true
break
}
}
}
if breakParent {
break
}
}
fmt.Printf("Do you want to try another organism? (Y/n):")
var response string
fmt.Scanln(&response)
printLine()
if len(response) > 0 {
if response[0] == 'N' || response[0] == 'n' {
break
}
}
}
printDoubleLine()
}
type logWriter struct {
}
func (writer logWriter) Write(bytes []byte) (int, error) {
return fmt.Print(string(bytes))
}