-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
110 lines (97 loc) · 3.35 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const {
loadDelimToJson, convertRowFields,
} = require('../util');
const { rid, orderPreferredOntologyTerms } = require('../graphkb');
const { SOURCE_DEFN: { name: ncitSourceName } } = require('../ncit');
const { logger } = require('../logging');
const { fdaSrs: SOURCE_DEFN } = require('../sources');
const HEADER = {
id: 'UNII',
name: 'PT',
ncit: 'NCIT',
pubchem: 'PUBCHEM',
};
/**
* Given the TAB delimited UNII records file. Load therapy records and NCIT links
*
* @param {object} opt options
* @param {string} opt.filename the path to the input file
* @param {ApiConnection} opt.conn the api connection object
*/
const uploadFile = async ({ filename, conn: graphkbConn, maxRecords }) => {
const jsonList = await loadDelimToJson(filename);
const source = await graphkbConn.addSource(SOURCE_DEFN);
// only load FDA records if we have already loaded NCIT
try {
await graphkbConn.getUniqueRecordBy({
filters: { name: ncitSourceName },
target: 'Source',
});
} catch (err) {
logger.error('Cannot link to NCIT, Unable to find source record');
throw err;
}
const counts = { error: 0, skip: 0, success: 0 };
logger.info(`loading ${jsonList.length} records`);
const intervalSize = 1000;
for (let i = 0; i < jsonList.length; i++) {
if (maxRecords && i > maxRecords) {
logger.warn(`not loading all content due to max records limit (${maxRecords})`);
break;
}
const {
id, ncit, name,
} = convertRowFields(HEADER, jsonList[i]);
if (!name || !id) {
// only load records with at min these 3 values filled out
counts.skip++;
continue;
}
let ncitRec;
if (i % intervalSize === 0) {
logger.info(`processing ${id} (${i} / ${jsonList.length})`);
} else {
logger.verbose(`processing ${id} (${i} / ${jsonList.length})`);
}
if (ncit) {
try {
ncitRec = await graphkbConn.getUniqueRecordBy({
filters: {
AND: [
{ source: { filters: { name: ncitSourceName }, target: 'Source' } },
{ sourceId: ncit },
],
},
sort: orderPreferredOntologyTerms,
target: 'Therapy',
});
} catch (err) {
logger.error(err);
counts.error++;
}
}
let drug;
try {
drug = await graphkbConn.addRecord({
content: { name, source: rid(source), sourceId: id },
existsOk: true,
target: 'Therapy',
});
if (ncitRec) {
await graphkbConn.addRecord({
content: { in: rid(ncitRec), out: rid(drug), source: rid(source) },
existsOk: true,
fetchExisting: false,
target: 'CrossReferenceOf',
});
}
counts.success++;
} catch (err) {
counts.error++;
logger.error(err);
continue;
}
}
logger.info(JSON.stringify(counts));
};
module.exports = { SOURCE_DEFN, dependencies: [ncitSourceName], uploadFile };