This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLiteInterface.go
81 lines (69 loc) · 2.32 KB
/
SQLiteInterface.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
package HPO
import (
"database/sql"
"fmt"
"strconv"
"github.com/artonge/Tamalou/Models"
// Import SQLite drivers
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
// Init DB connection to HPO SQLite DB
func init() {
var err error
db, err = sql.Open("sqlite3", "datas/hpo/hpo_annotations.sqlite")
if err != nil {
fmt.Println("Error in HPO SQLite init: ", err)
}
}
// SQLiteQuery - Make a request on HPO SQLite database
// @param query : The query to make
// Note that the query will be prepend with "SELECT ... FROM ... WHERE "
// You can use the following fields names :
// 'disease_db', 'disease_id', 'disease_label', 'sign_id'
// @return results : Array fill with HPOSQLiteStruct
// @return error : Contains an error if one occurs
func SQLiteQuery(diseasesIDs []string) ([]*Models.Disease, error) {
fullQuery := "SELECT disease_db, disease_id, disease_label, sign_id FROM phenotype_annotation WHERE "
for _, id := range diseasesIDs {
fullQuery += "sign_id='" + id + "' OR "
}
// Remove the last " OR "
fullQuery = fullQuery[:len(fullQuery)-len(" OR ")]
// Make the query
rows, err := db.Query(fullQuery)
if err != nil {
return nil, fmt.Errorf("Error in SQLiteQuery when querying: %v", err)
}
// Prepare the resuts array
diseasesArray := make([]*Models.Disease, 0, 100)
// Parse rows
for rows.Next() {
tmpHPOSQLiteStruct := new(hpoSQLiteStruct)
tmpDisease := new(Models.Disease)
err = rows.Scan(
&tmpHPOSQLiteStruct.DiseaseDB,
&tmpHPOSQLiteStruct.DiseaseID,
&tmpHPOSQLiteStruct.DiseaseLabel,
&tmpHPOSQLiteStruct.SignID,
)
if err != nil {
return nil, fmt.Errorf("Error in SQLiteQuery while scanning a row \n ==> %v\n ==> Error : %v", tmpHPOSQLiteStruct, err)
}
tmpDisease.Name = tmpHPOSQLiteStruct.DiseaseLabel
tmpDisease.Sources = append(tmpDisease.Sources, "HPO")
switch tmpHPOSQLiteStruct.DiseaseDB {
case "ORPHA":
value, err := strconv.ParseFloat(tmpHPOSQLiteStruct.DiseaseID, 64)
if err != nil {
return nil, fmt.Errorf("Error in SQLiteQuery while parsing float\n ==> %v\n ==> Error : %v", tmpHPOSQLiteStruct.DiseaseID, err)
}
tmpDisease.OrphaID = value
case "OMIM":
tmpDisease.OMIMID = tmpHPOSQLiteStruct.DiseaseID
}
// Append
diseasesArray = append(diseasesArray, tmpDisease)
}
return diseasesArray, nil
}