-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
586 lines (515 loc) · 18.8 KB
/
index.js
File metadata and controls
586 lines (515 loc) · 18.8 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
const fs = require('fs');
const { jsonifyVariant, parseVariant } = require('@bcgsc-pori/graphkb-parser');
const {
loadDelimToJson,
convertRowFields,
} = require('../util');
const {
orderPreferredOntologyTerms, rid,
} = require('../graphkb');
const { logger } = require('../logging');
const _trials = require('../clinicaltrialsgov');
const _pubmed = require('../entrez/pubmed');
const _asco = require('../asco');
const _gene = require('../entrez/gene');
const { cgi: SOURCE_DEFN } = require('../sources');
const HEADER = {
alteration: 'Alteration',
biomarker: 'Biomarker',
cds: 'cDNA',
disease: 'Primary Tumor type full name',
drug: 'Drug',
drugFamily: 'Drug family',
evidence: 'Source',
evidenceLevel: 'Evidence level',
gene: 'Gene',
genomic: 'gDNA',
relevance: 'Association',
reviewData: 'Curation date',
reviewer: 'Curator',
transcript: 'transcript',
variantClass: 'Alteration type',
};
// mappings are given primarily to fix known typos
const relevanceMapping = {
'increased toxicity (myelosupression)': 'increased toxicity (myelosuppression)',
'no responsive': 'no response',
resistant: 'resistance',
responsive: 'response',
};
const diseaseMapping = {
'any cancer type': 'cancer',
'billiary tract': 'Biliary tract cancer',
'cervix squamous cell': 'cervix squamous cell carcinoma',
endometrium: 'endometrial cancer',
'gastrointestinal stromal': 'gastrointestinal stromal tumor',
'head an neck': 'head and neck cancer',
'head an neck squamous': 'head and neck squamous cell carcinoma',
'lung squamous cell': 'lung squamous cell carcinoma',
'malignant peripheral nerve sheat tumor': 'malignant peripheral nerve sheath tumor',
ovary: 'ovarian cancer',
thymic: 'thymic tumor',
};
const therapyMapping = {
'MEK inhibitor (alone or in combination)': 'mek inhibitor',
'egfr tk inhibitor': 'egfr tyrosine kinase inhibitor',
'egfr tk inhibitors': 'egfr tyrosine kinase inhibitor',
flourouracil: 'fluorouracil',
fluvestrant: 'fulvestrant',
'jak inhibitors (alone or in combination)': 'jak inhibitor',
'mek inhibitors (alone or in combination)': 'mek inhibitor',
tensirolimus: 'temsirolimus',
'trastuzumab deruxtecan-nxki': 'fam-trastuzumab deruxtecan-nxki',
};
const parseCategoryVariant = (row) => {
const type = row.biomarker
.slice(row.gene.length)
.trim()
.replace('undexpression', 'underexpression'); // fix typo
const result = { gene: row.gene, type };
if (row.variantClass === 'CNA') {
if (type === 'deletion') {
return { ...result, type: 'copy loss' };
}
return result;
}
return result;
};
const parseEvidence = (row) => {
const evidence = [];
for (const item of row.evidence.split(';').map(i => i.trim())) {
if (item.startsWith('PMID:')) {
evidence.push(item.slice('PMID:'.length));
} else if (item.startsWith('PMC')) {
evidence.push(item);
} else if (/^NCT\d+$/.exec(item)) {
evidence.push(item);
} else if (!['FDA', 'NCCN', 'ASCO', 'AACR'].some(prefix => item.startsWith(prefix))) {
throw new Error(`cannot process non-pubmed/nct/aacr/asco evidence ${item}`);
}
}
return evidence;
};
const parseTherapy = (row) => {
let { drug } = row;
if (drug === '[]' || !drug) {
drug = row.drugFamily;
}
return drug.replace(/^\[/, '').replace(/\]$/, '');
};
/**
* Process variants into a list to deal with concomittent variants
* format each variant like the original row to re-use the processor
*/
const preprocessVariants = (row) => {
const { biomarker, variantClass, protein } = row;
if (biomarker.split('+').length > 2) {
throw new Error('Missing logic to process variant combinations of 3 or more');
}
if (protein.trim()) {
return [[{
...row,
protein: protein.replace(':', ':p.'),
}]];
}
const combinations = [];
for (const variant of biomarker.split(/\s*\+\s*/)) {
let match;
const variants = [];
if (match = /^(\w+) \(([A-Z0-9*,;]+)\)$/.exec(variant)) {
const [, gene, tail] = match;
for (const singleProtein of tail.split(/[,;]/)) {
let hgvsp = `p.${singleProtein}`;
if (match = /^([A-Z])?(\d+)$/.exec(singleProtein)) {
const [, refAA, pos] = match;
hgvsp = `p.${refAA || '?'}${pos}${variantClass.toLowerCase()}`;
} else if (match = /^(\d+)-(\d+)$/.exec(tail)) {
const [, start, end] = match;
hgvsp = `p.(?${start}_?${end})${variantClass.toLowerCase()}`;
}
variants.push({ gene, protein: `${gene}:${hgvsp}` });
}
} else if (match = /^(\w+)\s+(.*)$/.exec(variant)) {
const [, gene, tail] = match;
if (match = /^exon (\d+) (insertion|deletion)s?$/.exec(tail)) {
const [, pos, type] = match;
variants.push({ exonic: `e.${pos}${type.slice(0, 3)}`, gene });
} else {
variants.push(parseCategoryVariant({ biomarker, gene, isCat: true }));
}
} else if (match = /^([A-Za-z0-9.]+)-([A-Za-z0-9.]+) fusion$/.exec(variant)) {
const [, gene1, gene2] = match;
variants.push({
gene: gene1, gene2, isCat: true, type: 'fusion',
});
} else {
throw new Error(`unable to process variant (${variant})`);
}
combinations.push(variants);
}
const result = [];
if (combinations.length > 1) {
// all combinations with 1 from each level
for (let i = 0; i < combinations[0].length; i++) {
for (let j = 0; j < combinations[1].length; j++) {
result.push([combinations[0][i], combinations[1][j]]);
}
}
} else {
result.push(...combinations[0].map(v => [v]));
}
return result;
};
/**
* parse and add the variant records
* returns the variant to be linked to the statement (protein > cds > category)
*/
const processVariants = async ({ conn, row, source }) => {
const {
genomic, protein, transcript, cds, type: variantType, gene, exonic, gene2, isCat = false,
} = row;
let proteinVariant,
cdsVariant,
categoryVariant,
genomicVariant,
exonicVariant,
gene1Record,
gene2Record;
try {
[gene1Record] = await _gene.fetchAndLoadBySymbol(conn, gene);
} catch (err) {
logger.error(err);
}
try {
[gene2Record] = await _gene.fetchAndLoadBySymbol(conn, gene2);
} catch (err) {
logger.error(err);
}
if (genomic && !isCat) {
const parsed = jsonifyVariant(parseVariant(genomic));
const reference1 = await conn.getUniqueRecordBy({
filters: {
AND: [
{ biotype: 'chromosome' },
{
OR: [
{ sourceId: parsed.reference1 },
{ name: parsed.reference1 },
],
},
],
},
sort: orderPreferredOntologyTerms,
target: 'Feature',
});
const type = await conn.getVocabularyTerm(parsed.type);
genomicVariant = await conn.addVariant({
content: { ...parsed, reference1, type },
existsOk: true,
target: 'PositionalVariant',
});
}
if (protein && !isCat) {
const parsed = jsonifyVariant(parseVariant(`${gene}:${protein.split(':')[1]}`));
const type = await conn.getVocabularyTerm(parsed.type);
proteinVariant = await conn.addVariant({
content: { ...parsed, reference1: rid(gene1Record), type },
existsOk: true,
target: 'PositionalVariant',
});
}
if (transcript && cds && !isCat) {
const parsed = jsonifyVariant(parseVariant(`${transcript}:${cds}`));
const reference1 = await conn.getUniqueRecordBy({
filters: { AND: [{ biotype: 'transcript' }, { sourceId: transcript }, { sourceIdVersion: null }] },
sort: orderPreferredOntologyTerms,
target: 'Feature',
});
const type = await conn.getVocabularyTerm(parsed.type);
cdsVariant = await conn.addVariant({
content: { ...parsed, reference1, type },
existsOk: true,
target: 'PositionalVariant',
});
}
if (exonic && !isCat) {
const parsed = jsonifyVariant(parseVariant(`${gene}:${exonic}`));
const type = await conn.getVocabularyTerm(parsed.type);
exonicVariant = await conn.addVariant({
content: { ...parsed, reference1: rid(gene1Record), type },
existsOk: true,
target: 'PositionalVariant',
});
}
try {
const type = rid(await conn.getVocabularyTerm(variantType));
categoryVariant = await conn.addVariant({
content: {
reference1: rid(gene1Record),
reference2: gene2Record
? rid(gene2Record)
: null,
type,
},
existsOk: true,
target: 'CategoryVariant',
});
} catch (err) {
// category variant is optional if any of the positional variants are defined
if (!proteinVariant && !cdsVariant && !genomicVariant) {
throw err;
}
}
// link the defined variants by infers
const combinations = [
// highest level positional infers the vategorical variant
[exonicVariant || proteinVariant || cdsVariant || genomicVariant, categoryVariant],
[proteinVariant || cdsVariant || genomicVariant, exonicVariant],
[cdsVariant || genomicVariant, proteinVariant],
[genomicVariant, cdsVariant || proteinVariant || exonicVariant],
];
for (const [src, tgt] of combinations) {
if (src && tgt) {
await conn.addRecord({
content: {
in: rid(tgt),
out: rid(src),
source: rid(source),
},
existsOk: true,
fetchExisting: false,
target: 'Infers',
});
}
}
return proteinVariant || cdsVariant || genomicVariant || exonicVariant || categoryVariant;
};
const processDisease = async (conn, originalDiseaseName) => {
const diseaseName = diseaseMapping[originalDiseaseName.toLowerCase().trim()] || `${originalDiseaseName}|${originalDiseaseName} cancer`;
// find the exact disease name
let disease;
try {
disease = rid(await conn.getUniqueRecordBy({
filters: { name: diseaseName },
sort: orderPreferredOntologyTerms,
target: 'Disease',
}));
return disease;
} catch (err) {}
// split on the vertical bar that indicates aliases
for (const alias of diseaseName.split('|')) {
try {
disease = rid(await conn.getUniqueRecordBy({
filters: { name: alias },
sort: orderPreferredOntologyTerms,
target: 'Disease',
}));
return disease;
} catch (err) {}
}
if (!disease) {
throw new Error(`missing disease for input: ${originalDiseaseName} (${diseaseName})`);
}
return disease;
};
const fetchAbstract = async (conn, abstractString) => {
const m = /^(ASCO|AACR)\s+(20[0-2][0-9])\s+\((abstr(act)?)?\s*(\w+)\)$/.exec(abstractString);
if (!m) {
throw new Error(`unable to parse abstract from ${abstractString}`);
}
const [, source, year,, abstractNumber] = m;
let abstract;
try {
abstract = await conn.getUniqueRecordBy({
filters: [
{ year },
{ source: { filters: { name: source }, target: 'Source' } },
{ abstractNumber },
],
target: 'Abstract',
});
} catch (err) {
if (source !== 'ASCO') {
throw err;
}
const absList = (await _asco.fetchAndLoadByIds(conn, [abstractNumber])).filter(a => a.year === year);
if (absList.length > 1) {
throw Error(`Cannot uniquely identify the correct ASCO abstract (${abstractNumber}). Found ${absList.length} abstracts`);
}
if (absList.length === 0) {
throw err;
}
[abstract] = absList;
}
return abstract;
};
const processRow = async ({ row, source, conn }) => {
// process the protein notation
// look up the disease by name
const disease = await processDisease(conn, row.disease);
const therapyName = row.therapy.includes(';')
? row.therapy.split(';').map(n => n.toLowerCase().trim()).sort().join(' + ')
: row.therapy;
// look up the drug by name
const drug = rid(await conn.addTherapyCombination(
source, therapyMapping[therapyName.toLowerCase().trim()] || therapyName,
));
const variants = await Promise.all(row.variants.map(
async variant => processVariants({ conn, row: variant, source }),
));
const level = rid(await conn.getUniqueRecordBy({
filters: { AND: [{ name: row.evidenceLevel }, { source: rid(source) }] },
target: 'EvidenceLevel',
}));
const articles = await _pubmed.fetchAndLoadByIds(
conn,
row.evidence.filter(ev => ['NCT', 'ASCO', 'AACR'].every(prefix => !ev.startsWith(prefix))),
);
const trials = await Promise.all(
row.evidence
.filter(ev => ev.startsWith('NCT'))
.map(async evidence => _trials.fetchAndLoadById(conn, evidence)),
);
const abstracts = await Promise.all(
row.evidence.filter(ev => ev.startsWith('AACR') || ev.startsWith('ASCO'))
.map(async ev => fetchAbstract(conn, ev)),
);
// determine the relevance of the statement
let relevance;
try {
relevance = rid(await conn.getVocabularyTerm(
relevanceMapping[row.relevance.toLowerCase()] || row.relevance,
));
} catch (err) {
relevance = rid(await conn.getVocabularyTerm(
relevanceMapping[row.relevance.toLowerCase()] || row.relevance, SOURCE_DEFN.name,
));
}
const evidence = [...articles.map(rid), ...trials.map(rid), ...abstracts.map(rid)];
if (evidence.length === 0) {
evidence.push(rid(source));
}
// create the statement
await conn.addRecord({
content: {
conditions: [...variants.map(rid), disease, drug],
evidence,
evidenceLevel: level,
relevance,
source: rid(source),
sourceId: row.sourceId,
subject: drug,
},
existsOk: true,
fetchExisting: false,
target: 'Statement',
});
};
const uploadFile = async ({
conn, filename, errorLogPrefix, maxRecords,
}) => {
const rows = await loadDelimToJson(filename);
logger.info('creating the source record');
const source = rid(await conn.addSource(SOURCE_DEFN));
const counts = { error: 0, skip: 0, success: 0 }; // tracking errors relative to the number of total statements
const inputCounts = { error: 0, skip: 0, success: 0 }; // tracking errors relative to the input number of records
const perVariantRows = [];
for (let index = 0; index < rows.length; index++) {
let match,
protein;
if (match = /^(\w+) \(([A-Z0-9*,;]+)\)$/.exec(rows[index].Biomarker)) {
const mutations = match[2].split(',');
for (let i = 0; i < mutations.length; i++) {
protein = `${match[1]}:${mutations[i]}`;
perVariantRows.push({
...rows[i],
protein,
sourceId: `${index + 1}:${i + 1}`,
});
}
} else {
perVariantRows.push({
...rows[index],
protein: '',
sourceId: `${index + 1}`,
});
}
}
await _pubmed.preLoadCache(conn);
const errorList = [];
logger.info(`loading ${perVariantRows.length} rows`);
for (let index = 0; index < perVariantRows.length; index++) {
if (maxRecords && index > maxRecords) {
logger.warn(`not loading all content due to max records limit (${maxRecords})`);
break;
}
const rawRow = perVariantRows[index];
logger.info(`processing: ${perVariantRows[index].sourceId} (${index} / ${perVariantRows.length})`);
const row = {
_raw: rawRow,
protein: perVariantRows[index].protein,
sourceId: perVariantRows[index].sourceId,
...convertRowFields(HEADER, perVariantRows[index]),
};
row.therapy = parseTherapy(row);
if (row.evidenceLevel.includes(',')) {
logger.info(`skipping row #${index} due to multiple evidence levels (${row.evidenceLevel})`);
inputCounts.skip++;
counts.skip++;
continue;
}
try {
row.evidence = parseEvidence(row);
} catch (err) {
logger.error(err);
errorList.push({
error: err,
errorMessage: err.toString(),
index,
row,
});
counts.error += row.disease.split(';').length;
inputCounts.error++;
continue;
}
let variants;
try {
variants = preprocessVariants(row);
} catch (err) {
counts.error += row.disease.split(';').length;
inputCounts.error++;
logger.error(err);
continue;
}
let errors = false;
for (const disease of row.disease.split(';')) {
for (const combo of variants) {
try {
await processRow({ conn, row: { ...row, disease, variants: combo }, source });
counts.success++;
} catch (err) {
errorList.push({
error: err,
errorMessage: err.toString(),
index,
row,
});
errors = true;
logger.error(err);
counts.error++;
}
}
}
if (errors) {
inputCounts.error++;
} else {
inputCounts.success++;
}
}
const errorLogFile = `${errorLogPrefix}-cgi.json`;
logger.info(`writing errors to: ${errorLogFile}`);
fs.writeFileSync(errorLogFile, JSON.stringify({ records: errorList }, null, 2));
logger.info(`statements ${JSON.stringify(counts)}`);
logger.info(`inputs ${JSON.stringify(counts)}`);
};
module.exports = { SOURCE_DEFN, uploadFile };