/
compound-id-download.go
156 lines (131 loc) · 3.26 KB
/
compound-id-download.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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
)
// CompoundID represents a subset of mappings from:
// https://www.ebi.ac.uk/unichem/rest/src_compound_id/{compound_id}/{source_id}
//
// Sources described here:
// https://www.ebi.ac.uk/unichem/ucquery/listSources
func getCompoundIDs(chemblID string, srcMap map[string]string) (map[string]string, error) {
compound := map[string]string{"chembl": chemblID}
urlTmpl := "https://www.ebi.ac.uk/unichem/rest/src_compound_id/%s/1"
respMap, err := httpGet(fmt.Sprintf(urlTmpl, chemblID))
if err != nil {
return compound, err
}
for _, v := range respMap {
compound[srcMap[v["src_id"]]] = v["src_compound_id"]
}
urlTmpl = "https://www.ebi.ac.uk/unichem/rest/structure/%s/1"
respMap, err = httpGet(fmt.Sprintf(urlTmpl, chemblID))
if err != nil {
return compound, err
}
if len(respMap) != 1 {
return compound, fmt.Errorf("unexpected response from %s; %v", fmt.Sprintf(urlTmpl, chemblID), respMap)
}
compound["standardinchi"] = respMap[0]["standardinchi"]
compound["standardinchikey"] = respMap[0]["standardinchikey"]
return compound, nil
}
func makeSourceMap() (map[string]string, error) {
// src_id -> name
srcMap := map[string]string{}
srcURL := "https://www.ebi.ac.uk/unichem/rest/src_ids/"
srcInfoURLTmpl := "https://www.ebi.ac.uk/unichem/rest/sources/%s"
respMap, err := httpGet(srcURL)
if err != nil {
return nil, err
}
for _, src := range respMap {
srcInfo, err := httpGet(fmt.Sprintf(srcInfoURLTmpl, src["src_id"]))
if err != nil {
return nil, err
}
srcMap[src["src_id"]] = srcInfo[0]["name"]
}
return srcMap, nil
}
func httpGet(url string) ([]map[string]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("[STATUS CODE - %d]\t%s", resp.StatusCode, body)
}
respMap := []map[string]string{}
err = json.Unmarshal(body, &respMap)
if err != nil {
return nil, err
}
return respMap, nil
}
func main() {
inputFile := ""
outputFile := ""
flag.StringVar(&inputFile, "input", inputFile, "input file containing a ChEMBL ID per line")
flag.StringVar(&outputFile, "output", outputFile, "output file path")
flag.Parse()
if inputFile == "" {
fmt.Println("interactions file must be provided")
os.Exit(1)
}
file, err := os.Open(inputFile)
if err != nil {
panic(err)
}
defer file.Close()
var out io.WriteCloser
if outputFile != "" {
outputFile, err = filepath.Abs(outputFile)
if err != nil {
panic(err)
}
d := filepath.Dir(outputFile)
err = os.MkdirAll(d, 0755)
if err != nil {
panic(err)
}
out, err = os.Create(outputFile)
if err != nil {
panic(err)
}
} else {
out = os.Stdout
}
defer out.Close()
srcMap, err := makeSourceMap()
if err != nil {
panic(err)
}
logger := log.New(os.Stderr, "logger: ", log.Lshortfile)
writer := json.NewEncoder(out)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
chemblID := scanner.Text()
cid, err := getCompoundIDs(chemblID, srcMap)
if err != nil {
logger.Print(err)
}
err = writer.Encode(cid)
if err != nil {
logger.Print(err)
}
}
}