-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
504 lines (441 loc) · 18.7 KB
/
Copy pathindex.js
File metadata and controls
504 lines (441 loc) · 18.7 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/**
* @module importer/civic
*/
const fs = require('fs');
const { rid } = require('../graphkb');
const { logger } = require('../logging');
const { civic: SOURCE_DEFN } = require('../sources');
const { getDisease } = require('./disease');
const { getRelevance } = require('./relevance');
const { getEvidenceLevel } = require('./evidenceLevel');
const { getPublication, loadPubmedCache } = require('./publication');
const {
downloadEvidenceItems,
processCombination,
processEvidenceItem,
} = require('./evidenceItem');
const {
contentMatching,
createStatement,
deleteStatements,
getStatements,
needsUpdate,
updateStatement,
} = require('./statement');
const BASE_URL = 'https://civicdb.org/api/graphql';
/**
* Increment counter on GraphKB Statement CRUD operations
*
* @param {object} initial the counter
* @param {object} updates the increment to apply
* @returns {object} the incremented counter
*/
const incrementCounts = (initial, updates) => {
if (!initial) {
return updates;
}
// deep copy
const updated = JSON.parse(JSON.stringify(initial));
for (const level1 of Object.keys(updated)) {
for (const level2 of Object.keys(updated[level1])) {
updated[level1][level2] += updates[level1][level2];
}
}
return updated;
};
/**
* Access the CIVic API, parse content, transform and load into GraphKB
*
* @param {object} param0
* @param {ApiConnection} param0.conn the api connection object for GraphKB
* @param {?boolean} param0.deleteDeprecated delete GraphKB Statements if deprecated evidence(s)
* @param {string} param0.errorLogPrefix prefix to the generated error json file
* @param {number} param0.maxRecords limit of EvidenceItem records to be processed and upload
* @param {?boolean} param0.noDeleteOnUnmatched don't delete GraphKB St. if unmatched combination(s)
* @param {?boolean} param0.noUpdate no update of existing GraphKB Statements
* @param {string[]} param0.trustedCurators a list of curator IDs for submitted-only EvidenceItems
* @param {?string} param0.url url to use as the base for accessing the civic ApiConnection
*/
const upload = async ({
conn,
deleteDeprecated = false, // Won't delete deprecated sourceIds by default
errorLogPrefix,
maxRecords,
noDeleteOnUnmatched = false,
noUpdate = false,
trustedCurators,
url = BASE_URL,
}) => {
const countsEI = {
error: 0,
partialSuccess: 0,
skip: 0,
success: 0,
};
let countsST;
// Adding CIViC as source if not already in GraphKB
const source = await conn.addSource(SOURCE_DEFN);
const sourceRid = rid(source);
/*
1. DOWNLOAD & PREPROCESSING
*/
// GETTING CIVIC EVIDENCEITEMS FROM CIVIC API
// Evidences accepted, or submitted from a trusted curator
logger.info(`loading evidenceItems from ${url}`);
const {
errors: downloadEvidenceItemsErr,
records: evidenceItems,
} = await downloadEvidenceItems(url, trustedCurators);
// Validation errors
const validationErrorList = [];
if (downloadEvidenceItemsErr.length > 0) {
countsEI.error += downloadEvidenceItemsErr.length;
validationErrorList.push(...downloadEvidenceItemsErr);
}
// GETTING CIVIC STATEMENTS FROM GRAPHKB API
// Note: One or more GKB Statement can come from the same CIVIC id (sourceId)
logger.info('loading related statements from GraphKB');
const statements = await conn.getRecords({
filters: { source: sourceRid },
returnProperties: [
'@rid',
'conditions',
'description',
'evidence',
'evidenceLevel',
'relevance',
'reviewStatus',
'source',
'sourceId',
'subject',
],
target: 'Statement',
});
const sourceIdsFromGKB = new Set(statements.map(r => r.sourceId));
logger.info(`${sourceIdsFromGKB.size} distinct ${SOURCE_DEFN.name} sourceId in GraphKB statements`);
logger.info(`${statements.length} total statements previously added to GraphKB from ${SOURCE_DEFN.name}`);
// REFACTORING GRAPHKB STATEMENTS INTO STATEMENTSBYSOURCEID
// where each sourceId is a key associated with an array
// of one or more GKB Statement records
const statementsBySourceId = {};
for (const record of statements) {
if (!statementsBySourceId[record.sourceId]) {
statementsBySourceId[record.sourceId] = [];
}
// Sorting conditions for downstream object comparison
record.conditions.sort();
statementsBySourceId[record.sourceId].push(record);
}
// REFACTORING CIVIC EVIDENCEITEMS INTO EVIDENCEITEMSBYID
// where each id is a key associated with one CIViC EvidenceItem as value
logger.info(`Pre-pocessing ${evidenceItems.length} records`);
const evidenceItemsById = {};
// Performing some checks. Skipping some records if needed
// eslint-disable-next-line guard-for-in
for (const i in evidenceItems) {
// Check if max records limit has been reached
if (maxRecords && Object.keys(evidenceItemsById).length >= maxRecords) {
logger.warn(`Not loading all content due to max records limit (${maxRecords})`);
countsEI.skip += (evidenceItems.length - i);
break;
}
// Check if record id is unique
if (evidenceItemsById[evidenceItems[i].id]) {
logger.error(`Multiple Evidence Items with the same id: ${evidenceItems[i].id}. Violates assumptions. Only the 1st one was kept.`);
countsEI.skip++;
continue;
}
// Adding EvidenceItem to object for upload
evidenceItemsById[evidenceItems[i].id] = evidenceItems[i];
}
const noRecords = Object.keys(evidenceItemsById).length;
logger.info(`${noRecords}/${evidenceItems.length} Evidence Items to process`);
/*
2. PROCESSING EACH CIVIC EVIDENCEITEM INTO ONE OR MORE GKB STATEMENTS
*/
// PubMed caching
logger.info('Caching Pubmed publication');
await loadPubmedCache(conn);
// Keeping track of EvidenceItem sourceIds who raised errors during processing
const errorSourceIds = {
disease: new Map(),
evidence: new Map(),
evidenceLevel: new Map(),
individualCombinationProcessing: new Map(),
processingIntoCombinations: new Map(),
relevance: new Map(),
};
const statementsToReviewUnmatchedProcessingError = new Map();
const statementsToReviewUnmatched = new Map();
logger.info(`\n\n${'#'.repeat(80)}\n## PROCESSING RECORDS\n${'#'.repeat(80)}\n`);
let recordNumber = 0;
// MAIN LOOP
// Looping through Evidence Items
for (const [id, evidenceItem] of Object.entries(evidenceItemsById)) {
/* PROCESSING EVIDENCEITEMS */
recordNumber++;
logger.info();
logger.info(`***** ${recordNumber}/${noRecords} : processing id ${id} *****`);
const numberOfStatements = statementsBySourceId[id]
? statementsBySourceId[id].length
: 0;
logger.info(`${numberOfStatements} related statement(s)`);
// Base object (properties order matters)
// Common content will be deep copied downstream for each combination
evidenceItem.content = {
conditions: [],
description: evidenceItem.description || '',
evidence: [],
evidenceLevel: [],
relevance: undefined,
reviewStatus: (evidenceItem.status === 'ACCEPTED'
? 'not required'
: 'pending'
),
source: sourceRid,
sourceId: id,
subject: undefined,
};
// PROCESSING DATA COMMON TO ALL COMBINATIONS
// Removing extra spaces in description. Needed before content comparison
evidenceItem.content.description = evidenceItem.content.description.replace(/\s+/g, ' ').trim();
// Get evidence (publication) rid
try {
evidenceItem.content.evidence.push(rid(
await getPublication(conn, evidenceItem),
));
} catch (err) {
logger.error(err);
countsEI.error++;
errorSourceIds.evidence.set(id, err);
continue;
}
// Get evidenceLevel rid
try {
evidenceItem.content.evidenceLevel.push(rid(
await getEvidenceLevel(conn, {
rawRecord: evidenceItem,
source: sourceRid,
sourceDisplayName: SOURCE_DEFN.displayName,
}),
));
} catch (err) {
logger.error(err);
countsEI.error++;
errorSourceIds.evidenceLevel.set(id, err);
continue;
}
// Get relevance rid
try {
evidenceItem.content.relevance = rid(
await getRelevance(conn, { rawRecord: evidenceItem }),
);
} catch (err) {
logger.error(err);
countsEI.error++;
errorSourceIds.relevance.set(id, err);
continue;
}
// Get disease rid
try {
// Will be removed downstream after being used as content's subject and/or condition
evidenceItem.content.disease = rid(
await getDisease(conn, { rawRecord: evidenceItem }),
true, // nullOk=true since some EvidenceItems aren't related to any specific disease
);
} catch (err) {
logger.error(err);
countsEI.error++;
errorSourceIds.disease.set(id, err);
continue;
}
// PROCESSING INDIVIDUAL EVIDENCEITEM INTO AN ARRAY OF COMBINATIONS
// (One combination per expected GraphKB statement)
const combinations = [];
try {
combinations.push(...await processEvidenceItem(evidenceItem));
} catch (err) {
logger.error(err);
countsEI.error++;
errorSourceIds.processingIntoCombinations.set(id, err);
continue;
}
logger.info(`${combinations.length} combination(s)`);
// PROCESSING INDIVIDUAL COMBINATION
// Formatting each combination's content for GraphKB statement requirements
const contents = [];
let processCombinationErrors = 0;
for (const combination of combinations) {
try {
contents.push(
await processCombination(conn, {
record: combination,
sourceRid,
}),
);
} catch (err) {
logger.error(err);
processCombinationErrors++;
if (!errorSourceIds.individualCombinationProcessing.get(id)) {
errorSourceIds.individualCombinationProcessing.set(id, []);
}
const v = errorSourceIds.individualCombinationProcessing.get(id);
errorSourceIds.individualCombinationProcessing.set(id, [...v, err]);
}
}
const successRatio = `${combinations.length - processCombinationErrors}/${combinations.length}`;
const processCombinationsMsg = `Processed ${successRatio} combination(s)`;
// If at least some combinations succeeds, then it's a success
if (processCombinationErrors === 0) {
countsEI.success++;
logger.info(processCombinationsMsg);
} else if (processCombinationErrors < combinations.length) {
countsEI.partialSuccess++;
logger.warn(processCombinationsMsg);
} else {
countsEI.error++;
logger.error(processCombinationsMsg);
}
/* MATCHING EVIDENCEITEMS WITH STATEMENTS */
// Content matching between CIViC and GraphKB records
// so we know which CRUD operation to perform on each statement
const { toCreate, toDelete, toUpdate } = contentMatching({
allFromCivic: contents,
allFromGkb: statementsBySourceId[id] || [],
});
/* CREATE/UPDATE/DELETE STATEMENTS */
const loaclCountsST = {
create: { err: 0, success: 0 },
delete: { err: 0, success: 0 },
noUpdateNeeded: { success: 0 },
update: { err: 0, success: 0 },
};
// UPDATE
if (!noUpdate && toUpdate.length > 0) {
for (let i = 0; i < toUpdate.length; i++) {
const { fromCivic, fromGkb } = toUpdate[i];
// Check if an update is needed to avoid unnecessary API calls
if (needsUpdate({ fromCivic, fromGkb })) {
const updatedCount = await updateStatement(conn, { fromCivic, fromGkb });
loaclCountsST.update.err += updatedCount.err;
loaclCountsST.update.success += updatedCount.success;
} else {
loaclCountsST.noUpdateNeeded.success++;
}
}
}
// DELETE
if (toDelete.length > 0) {
const rids = toDelete.map(el => el['@rid']);
if (processCombinationErrors > 0) {
// Do not delete any statements if some combinations have processing errors
logger.warn(`${toDelete.length} unmatched statement(s). To be reviewed since some processing errors occured`);
statementsToReviewUnmatchedProcessingError.set(id, rids);
} else if (noDeleteOnUnmatched) {
// Do not delete any statements if noDeleteOnUnmatched flag
logger.warn(`${toDelete.length} unmatched statement(s). To be reviewed since the noDeleteOnUnmatched flag is being used`);
statementsToReviewUnmatched.set(id, rids);
} else {
loaclCountsST.delete = await deleteStatements(conn, { rids });
}
}
// CREATE
if (toCreate.length > 0) {
for (let i = 0; i < toCreate.length; i++) {
const createdCount = await createStatement(conn, { fromCivic: toCreate[i] });
loaclCountsST.create.err += createdCount.err;
loaclCountsST.create.success += createdCount.success;
}
}
// logging
for (const level of Object.keys(loaclCountsST)) {
if (loaclCountsST[level].err > 0 || loaclCountsST[level].success > 0) {
logger.info(`${level}: ${JSON.stringify(loaclCountsST[level])}`);
}
}
countsST = incrementCounts(countsST, loaclCountsST);
// END OF MAIN LOOP
}
logger.info(`\n\n${'#'.repeat(80)}\n## END OF RECORD PROCESSING\n${'#'.repeat(80)}\n`);
// Logging EvidenceItem processing counts
logger.info();
logger.info('***** CIViC EvidenceItem records processing report: *****');
logger.info(JSON.stringify(countsEI));
// Logging detailed EvidenceItem processing counts
logger.info('Processing errors report:');
for (const [key, value] of Object.entries(errorSourceIds)) {
logger.info(`${key}: ${value.size}`);
// Also formatting Maps into objects for saving to file downstream
errorSourceIds[key] = Object.fromEntries(errorSourceIds[key]);
}
// DELETING UNWANTED GRAPHKB STATEMENTS
// sourceIds no longer in CIViC (not accepted/submitted-by-trustedCurators) but still in GraphKB
const allIdsFromCivic = new Set(evidenceItems.map(r => r.id.toString()));
const sourceIdstoDeleteStatementsFrom = Array.from(
new Set([...sourceIdsFromGKB].filter(x => !allIdsFromCivic.has(x))),
);
logger.info();
logger.info('***** Deprecated items *****');
logger.warn(`${sourceIdstoDeleteStatementsFrom.length} deprecated ${SOURCE_DEFN.name} Evidence Items still in GraphKB Statement`);
if (sourceIdstoDeleteStatementsFrom.length > 0) {
logger.info(`sourceIds: ${sourceIdstoDeleteStatementsFrom}`);
}
// GraphKB Statements Soft-deletion
if (sourceIdstoDeleteStatementsFrom.length > 0) {
if (!deleteDeprecated) {
// Do not delete any statements if no deleteDeprecated flag
const deprecatedStatementRids = await getStatements(
conn,
{ source: sourceRid, sourceIds: sourceIdstoDeleteStatementsFrom },
);
logger.warn(`${deprecatedStatementRids.length} corresponding deprecated statement(s). To be reviewed since no deleteDeprecated flag`);
const deprecatedStatementsFilepath = `${errorLogPrefix}-civic-deprecatedStatements.json`;
logger.info(`writing ${deprecatedStatementsFilepath}`);
fs.writeFileSync(
deprecatedStatementsFilepath,
JSON.stringify(deprecatedStatementRids, null, 2),
);
} else {
const deletedCount = await deleteStatements(conn, {
source: sourceRid,
sourceIds: sourceIdstoDeleteStatementsFrom,
});
const attempts = deletedCount.success + deletedCount.err;
logger.info(`${deletedCount.success}/${attempts} soft-deleted statements`);
if (countsST) {
countsST.delete.err += deletedCount.err;
countsST.delete.success += deletedCount.success;
} else {
countsST = { delete: { err: deletedCount.err, success: deletedCount.success } };
}
}
}
// Logging processing error cases to be reviewed,
// so a reviewer can decide if corresponding statements need to be deleted or not
logger.info();
logger.info('***** Unmatched cases to be reviewed for deletion *****');
logger.warn(`${statementsToReviewUnmatchedProcessingError.size} Evidence Item(s) with processing errors leading to unmatched Statement(s)`);
statementsToReviewUnmatchedProcessingError.forEach((v, k) => logger.info(`${k} -> ${JSON.stringify(v)}`));
logger.warn(`${statementsToReviewUnmatched.size} Evidence Item(s) with unmatched Statement(s) with no processing error involved`);
statementsToReviewUnmatched.forEach((v, k) => logger.info(`${k} -> ${JSON.stringify(v)}`));
// Logging Statement CRUD operations counts
if (countsST) {
logger.info();
logger.info('***** GraphKB Statement records CRUD operations report: *****');
for (const op of Object.keys(countsST)) {
logger.info(`${op}: ${JSON.stringify(countsST[op])}`);
}
}
// SAVING LOGGED ERRORS TO FILE
const errorFileContent = {
...errorSourceIds,
validationErrors: validationErrorList,
};
const errorJson = `${errorLogPrefix}-civic.json`;
logger.info();
logger.info(`***** Global report: *****\nwriting ${errorJson}`);
fs.writeFileSync(errorJson, JSON.stringify(errorFileContent, null, 2));
};
module.exports = {
upload,
};