-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
93 lines (74 loc) · 2.58 KB
/
index.js
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
const Ajv = require('ajv');
const _entrezGene = require('../entrez/gene');
const _chembl = require('../chembl');
const { logger } = require('../logging');
const { checkSpec, request } = require('../util');
const { rid } = require('../graphkb');
const { dgidb: SOURCE_DEFN } = require('../sources');
const spec = require('./spec.json');
const ajv = new Ajv();
const recordSpec = ajv.compile(spec);
const BASE_URL = 'https://dgidb.org/api/v2';
const processRecord = async ({ conn, record, source }) => {
checkSpec(recordSpec, record);
const {
entrez_id: entrezId,
concept_id: chemblId,
interaction_types: interactionTypes,
id,
} = record;
const [gene] = await _entrezGene.fetchAndLoadByIds(conn, [entrezId]);
const drug = await _chembl.fetchAndLoadById(conn, chemblId.replace('chembl:', ''));
const interactionType = interactionTypes.map(i => i.toLowerCase().trim()).sort().join(';');
await conn.addRecord({
content: {
actionType: interactionType,
in: rid(drug),
out: rid(gene),
source: rid(source),
uuid: id, // use the input uuid as the uuid rather than generating one
},
existsOk: true,
fetchExisting: false,
target: 'TargetOf',
});
};
const upload = async ({ conn, url = BASE_URL }) => {
logger.info('creating the source record');
const source = rid(await conn.addSource(SOURCE_DEFN));
const limit = 100;
let page = `${url}/interactions?count=${limit}&page=1`;
const counts = { error: 0, skip: 0, success: 0 };
// pre-cache the entrez genes
logger.info('pre-loading the entrez gene list');
await _entrezGene.preLoadCache(conn);
logger.info('pre-loading the chembl drug list');
await _chembl.preLoadCache(conn);
while (page) {
logger.info(`loading: ${page}`);
const resp = await request({
json: true,
method: 'GET',
uri: page,
});
const { _meta: { links: { next } }, records } = resp;
page = next;
// process this batch of records
for (const record of records) {
logger.info(`processing ${record.id}`);
try {
await processRecord({ conn, record, source });
counts.success++;
} catch (err) {
logger.error(err);
counts.error++;
}
}
}
logger.info(JSON.stringify(counts));
};
module.exports = {
SOURCE_DEFN,
dependencies: [_entrezGene.SOURCE_DEFN.name, _chembl.SOURCE_DEFN.name],
upload,
};