-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.js
344 lines (300 loc) · 9.75 KB
/
util.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* @module importer/entrez/util
*/
const {
requestWithRetry, orderPreferredOntologyTerms,
} = require('../util');
const { rid, generateCacheKey } = require('../graphkb');
const { logger } = require('../logging');
const DEFAULT_QS = {
retmode: 'json',
rettype: 'docsum',
};
const BASE_URL = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi';
const BASE_SEARCH_URL = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi';
const BASE_FETCH_URL = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi';
const MAX_CONSEC_IDS = 150;
/**
* pull records from a cache where stored, return leftoever id's otherwise
* @param {Array.<string>} rawIdList array of sourceIds to pull records from a cache by
* @param {object} cache the cache to pull from
*/
const pullFromCacheById = (rawIdList, cache) => {
const idList = Array.from(new Set(Array.from(rawIdList, id => `${id}`.toLowerCase().trim())));
const cached = [];
const remaining = [];
for (const id of idList) {
if (cache[id]) {
cached.push(cache[id]);
} else {
remaining.push(id);
}
}
return { cached, remaining };
};
/**
* Given some list of pumbed Ids, fetch the minimal parsed aricle summaries
* @param {Array.<string>} pmidListIn list of pubmed ids
* @param {object} opt
* @param {string} [opt.url=BASE_URL] the base url for the pubmed api
* @param {string} [opt.db='pubmed'] the entrez database name
* @param {function} opt.parser the parser function to transform the entrez record to a graphkb record
* @param {object} [opt.cache={}] the cache associated with calls to this db
* @param {string} [opt.dbfrom=null] if querying by a linked ID must include the db you wish to retrieve from
*/
const fetchByIdList = async (rawIdList, opt) => {
const {
url = BASE_URL, db = 'pubmed', parser, cache = {}, dbfrom = null,
} = opt;
if (rawIdList.length === 0) {
return [];
}
rawIdList = Array.from(new Set(rawIdList));
const { cached: allRecords, remaining: idList } = pullFromCacheById(rawIdList, cache);
for (let startIndex = 0; startIndex < idList.length; startIndex += MAX_CONSEC_IDS) {
const idListString = idList
.slice(startIndex, startIndex + MAX_CONSEC_IDS)
.map(id => id.toString())
.join(',');
const queryParams = { ...DEFAULT_QS, db, id: idListString };
if (dbfrom) {
queryParams.dbfrom = dbfrom;
}
logger.debug(`loading: ${url}?db=${db}`);
const { result, error } = await requestWithRetry({
headers: { Accept: 'application/json' },
json: true,
method: 'GET',
qs: queryParams,
uri: url,
});
if (error) {
logger.error(error);
}
const records = [];
Object.keys(result).filter(k => k !== 'uids').forEach((key) => {
const rec = result[key];
try {
records.push(parser(rec));
} catch (err) {
logger.error(err);
}
});
allRecords.push(...records);
}
return allRecords;
};
/**
* Given some pubmed ID, get the corresponding record from GraphKB
*/
const fetchRecord = async (api, {
sourceId, sourceIdVersion = null, db = 'pubmed', target = 'Publication', cache = {},
}) => {
const cacheKey = generateCacheKey({ sourceId, sourceIdVersion });
if (cache[cacheKey]) {
return cache[cacheKey];
}
const record = await api.getUniqueRecordBy({
filters: {
AND: [
{ sourceId },
{ sourceIdVersion },
{ source: { filters: { name: db }, target: 'Source' } },
],
},
sort: orderPreferredOntologyTerms,
target,
});
cache[cacheKey] = record;
return record;
};
/**
* Given the parsed content of some recrod, upload to the api
* @param {object} content the record contents to be uploaded
* @param {object} opt
* @param {boolean} opt.cache add the GraphKB record to the cache
* @param {boolean} opt.fetchFirst attempt to get the record by source Id before uploading it
* @param {string} opt.target
* @param {object} opt.sourceDefn
* @param {boolean} opt.upsert update the record if already exists
* @param {function} opt.createDisplayName
*/
const uploadRecord = async (api, content, opt = {}) => {
const {
cache = false,
fetchFirst = true,
target = 'Publication',
sourceDefn,
upsert = false,
createDisplayName,
} = opt;
const { sourceId, sourceIdVersion } = content;
const cacheKey = generateCacheKey({ sourceId, sourceIdVersion });
if (cache && cache[cacheKey]) {
return cache[cacheKey];
}
let source = cache.__source;
if (!source) {
source = await api.addSource(sourceDefn);
if (cache) {
cache.__source = source;
}
}
if (fetchFirst) {
try {
const record = await api.getUniqueRecordBy({
filters: {
AND: [
{ sourceId },
{ source: rid(source) },
{ sourceIdVersion: sourceIdVersion || null },
],
},
target,
});
if (cache) {
cache[cacheKey] = record;
}
return record;
} catch (err) { }
}
const formattedContent = {
...content,
source: rid(source),
};
if (createDisplayName) {
formattedContent.displayName = createDisplayName(content.sourceId);
}
const result = await api.addRecord({
content: formattedContent,
existsOk: true,
fetchConditions: {
AND: [
{ sourceId },
{ source: rid(source) },
{ sourceIdVersion: sourceIdVersion || null },
],
},
target,
upsert,
});
if (cache) {
cache[cacheKey] = result;
}
return result;
};
const preLoadCache = async (api, { sourceDefn, cache, target }) => {
const records = await api.getRecords({
filters: {
AND: [
{ source: { filters: { name: sourceDefn.name }, target: 'Source' } },
{ dependency: null },
{ deprecated: false },
],
},
neighbors: 0,
target,
});
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`);
};
/**
* Given some list of pubmed IDs, return if cached,
* If they do not exist, grab from the pubmed api
* and then upload to GraphKB
*
* @param {ApiConnection} api connection to GraphKB
* @param {Array.<string>} idListIn list of pubmed IDs
* @param {Object} opt
* @param {string} opt.dbName name of the entrez db to pull from ex. gene
* @param {boolean} opt.fetchFirst override uploadRecord() fetchFirst
* @param {function} opt.parser function to convert records from the api to the graphkb format
* @param {object} opt.cache
* @param {number} opt.MAX_CONSEC maximum consecutive records to upload at once
* @param {string} opt.target the graphkb api target to upload to
* @param {object} opt.sourceDefn the object with the source information
* @param {boolean} opt.upsert override uploadRecord() upsert
*/
const fetchAndLoadByIds = async (api, idListIn, {
dbName,
fetchFirst,
parser,
cache,
MAX_CONSEC = 100,
target,
sourceDefn,
upsert,
}) => {
const records = await fetchByIdList(
idListIn,
{
cache, db: dbName, parser,
},
);
const result = [];
let queue = records;
while (queue.length > 0) {
const current = queue.slice(0, MAX_CONSEC);
queue = queue.slice(MAX_CONSEC);
const newRecords = await Promise.all(current.map(
async record => uploadRecord(api, record, {
cache,
fetchFirst,
sourceDefn,
target,
upsert,
}),
));
result.push(...newRecords);
}
return result;
};
/**
* ex. https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?retmode=json&db=gene&rettype=docsum&term=kras[sym]
*
* @param {ApiConnection} api graphkb api connection
* @param {string} term the search term ex. kras[sym]
* @param {Object} opt
* @param {string} opt.dbName name of the entrez db to pull from ex. gene
* @param {function} opt.parser function to convert records from the api to the graphkb format
* @param {object} opt.cache
* @param {number} opt.MAX_CONSEC maximum consecutive records to upload at once
* @param {string} opt.target the graphkb api target to upload to
* @param {object} opt.sourceDefn the object with the source information
*/
const fetchAndLoadBySearchTerm = async (api, term, opt) => {
const { dbName } = opt;
// get the list of ids
logger.info(`searching ${BASE_SEARCH_URL}?db=${dbName}&term=${term}`);
const { esearchresult: { idlist } } = await requestWithRetry({
headers: { Accept: 'application/json' },
json: true,
method: 'GET',
qs: { ...DEFAULT_QS, db: dbName, term },
uri: BASE_SEARCH_URL,
});
return fetchAndLoadByIds(api, idlist, opt);
};
module.exports = {
BASE_FETCH_URL,
BASE_URL,
DEFAULT_QS,
fetchAndLoadByIds,
fetchAndLoadBySearchTerm,
fetchByIdList,
fetchRecord,
preLoadCache,
pullFromCacheById,
uploadRecord,
};