-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
102 lines (86 loc) · 1.74 KB
/
file.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
package main
import (
"encoding/gob"
"fmt"
"glycodist/pdb"
"glycodist/uniprot"
"os"
)
const (
dataDir = "data/"
unpDir = dataDir + "uniprot/"
pdbDir = dataDir + "pdb/"
fileExt = ".data"
)
func makeDirs() {
os.MkdirAll(unpDir, os.ModePerm)
os.MkdirAll(pdbDir, os.ModePerm)
}
func loadPDB(pdbID string) (*pdb.PDB, error) {
path := pdbDir + pdbID + fileExt
_, err := os.Stat(path)
if os.IsNotExist(err) {
p, err := pdb.NewPDBFromID(pdbID)
if err != nil {
return nil, err
}
err = write(path, &p)
if err != nil {
return nil, fmt.Errorf("write PDB: %v", err)
}
return &p, nil
}
return readPDB(pdbID)
}
func readPDB(pdbID string) (*pdb.PDB, error) {
path := pdbDir + pdbID + fileExt
p := new(pdb.PDB)
err := read(path, &p)
if err != nil {
return nil, fmt.Errorf("load file: %v", err)
}
err = p.Parse()
return p, err
}
func loadUniProt(unpID string) (*uniprot.UniProt, error) {
path := unpDir + unpID + fileExt
_, err := os.Stat(path)
if os.IsNotExist(err) {
u, err := uniprot.NewUniProt(unpID)
if err != nil {
return nil, err
}
err = write(path, &u)
if err != nil {
return nil, fmt.Errorf("write UniProt: %v", err)
}
return u, nil
}
u := new(uniprot.UniProt)
err = read(path, &u)
if err != nil {
return nil, fmt.Errorf("load file: %v", err)
}
return u, nil
}
func write(filePath string, object interface{}) error {
file, err := os.Create(filePath)
if err == nil {
encoder := gob.NewEncoder(file)
err = encoder.Encode(object)
if err != nil {
return err
}
}
file.Close()
return err
}
func read(filePath string, object interface{}) error {
file, err := os.Open(filePath)
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(object)
}
file.Close()
return err
}