-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (112 loc) · 3.43 KB
/
Copy pathindex.js
File metadata and controls
136 lines (112 loc) · 3.43 KB
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
/**
* Load therapy recrods from CHEMBL
*/
const Ajv = require('ajv');
const {
checkSpec, requestWithRetry,
} = require('../util');
const {
rid, generateCacheKey,
} = require('../graphkb');
const { logger } = require('../logging');
const { chembl: SOURCE_DEFN } = require('../sources');
const spec = require('./spec.json');
const ajv = new Ajv();
const recordSpec = ajv.compile(spec);
const API = 'https://www.ebi.ac.uk/chembl/api/data/molecule';
const CACHE = {};
/**
* fetch drug by chemblId and load it into GraphKB
* @param {ApiConnection} conn
* @param {string} drugId
*/
const fetchAndLoadById = async (conn, drugId) => {
const cacheKey = generateCacheKey({ sourceId: drugId });
if (CACHE[cacheKey]) {
return CACHE[cacheKey];
}
logger.info(`loading: ${API}/${drugId}`);
const chemblRecord = await requestWithRetry({
json: true,
uri: `${API}/${drugId}`,
});
checkSpec(recordSpec, chemblRecord);
if (!CACHE.SOURCE) {
CACHE.SOURCE = await conn.addSource(SOURCE_DEFN);
}
const source = rid(CACHE.SOURCE);
const content = {
name: chemblRecord.pref_name || chemblRecord.molecule_properties.full_molformula,
source,
sourceId: chemblRecord.molecule_chembl_id,
};
if (content.name) {
content.displayName = `${content.name} [${content.sourceId.toUpperCase()}]`;
} else {
content.displayName = content.sourceId.toUpperCase();
}
if (chemblRecord.molecule_properties && chemblRecord.molecule_properties.full_molformula) {
content.molecularFormula = chemblRecord.molecule_properties.full_molformula;
}
const record = await conn.addRecord({
content,
existsOk: true,
fetchConditions: { AND: [{ name: content.name }, { source }, { sourceId: content.sourceId }] },
target: 'Therapy',
});
CACHE[cacheKey] = record;
if (chemblRecord.usan_stem_definition) {
try {
const parent = await conn.addRecord({
content: {
comment: 'usan stem definition',
name: chemblRecord.usan_stem_definition,
source,
sourceId: chemblRecord.usan_stem_definition,
},
existsOk: true,
target: 'Therapy',
});
await conn.addRecord({
content: {
in: rid(parent),
out: rid(record),
source,
},
existsOk: true,
target: 'SubClassOf',
});
} catch (err) {}
}
return record;
};
const preLoadCache = async (api) => {
const records = await api.getRecords({
filters: {
AND: [
{ source: { filters: { name: SOURCE_DEFN.name }, target: 'Source' } },
{ dependency: null },
{ deprecated: false },
],
},
target: 'Therapy',
});
const dups = new Set();
for (const record of records) {
const cacheKey = generateCacheKey(record);
if (CACHE[cacheKey]) {
// duplicate
dups.add(cacheKey);
}
CACHE[cacheKey] = record;
}
Array(dups).forEach((key) => {
delete CACHE[key];
});
logger.info(`cache contains ${Object.keys(CACHE).length} keys`);
};
module.exports = {
SOURCE_DEFN,
fetchAndLoadById,
preLoadCache,
};