-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
133 lines (129 loc) · 3.19 KB
/
action.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
package action
import (
"fmt"
"os"
"strconv"
"github.com/dictyBase/go-obograph/graph"
araobo "github.com/dictyBase/go-obograph/storage/arangodb"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// LoadOntologies load ontologies into arangodb
func LoadOntologies(c *cli.Context) error {
arPort, _ := strconv.Atoi(c.String("arangodb-port"))
cp := &araobo.ConnectParams{
User: c.String("arangodb-user"),
Pass: c.String("arangodb-pass"),
Host: c.String("arangodb-host"),
Database: c.String("arangodb-database"),
Port: arPort,
Istls: c.Bool("is-secure"),
}
clp := &araobo.CollectionParams{
Term: c.String("term-collection"),
Relationship: c.String("rel-collection"),
GraphInfo: c.String("cv-collection"),
OboGraph: c.String("obograph"),
}
ds, err := araobo.NewDataSource(cp, clp)
if err != nil {
return cli.NewExitError(err.Error(), 2)
}
logger := getLogger(c)
for _, v := range c.StringSlice("obojson") {
r, err := os.Open(v)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error in opening file %s %s", v, err),
2,
)
}
defer r.Close()
g, err := graph.BuildGraph(r)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error in building graph from %s %s", v, err),
2,
)
}
if !ds.ExistsOboGraph(g) {
logger.Infof("obograph %s does not exist, have to be loaded", v)
err := ds.SaveOboGraphInfo(g)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error in saving graph %s", err),
2,
)
}
nt, err := ds.SaveTerms(g)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error in saving terms %s", err),
2,
)
}
logger.Infof("saved %d terms", nt)
nr, err := ds.SaveRelationships(g)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error in saving relationships %s", err),
2,
)
}
logger.Infof("saved %d relationships", nr)
continue
}
logger.Infof("obograph %s exist, have to be updated", v)
if err := ds.UpdateOboGraphInfo(g); err != nil {
return cli.NewExitError(
fmt.Sprintf("error in updating graph information %s", err),
2,
)
}
it, ut, err := ds.SaveOrUpdateTerms(g)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error in updating terms %s", err),
2,
)
}
logger.Infof("saved: %d and updated: %d terms", it, ut)
ur, err := ds.SaveNewRelationships(g)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error in saving relationships %s", err),
2,
)
}
logger.Infof("updated %d relationships", ur)
}
return nil
}
func getLogger(c *cli.Context) *logrus.Entry {
log := logrus.New()
log.Out = os.Stderr
switch c.GlobalString("log-format") {
case "text":
log.Formatter = &logrus.TextFormatter{
TimestampFormat: "02/Jan/2006:15:04:05",
}
case "json":
log.Formatter = &logrus.JSONFormatter{
TimestampFormat: "02/Jan/2006:15:04:05",
}
}
l := c.GlobalString("log-level")
switch l {
case "debug":
log.Level = logrus.DebugLevel
case "warn":
log.Level = logrus.WarnLevel
case "error":
log.Level = logrus.ErrorLevel
case "fatal":
log.Level = logrus.FatalLevel
case "panic":
log.Level = logrus.PanicLevel
}
return logrus.NewEntry(log)
}