-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
343 lines (307 loc) · 10.9 KB
/
Copy pathindex.js
File metadata and controls
343 lines (307 loc) · 10.9 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
/**
* @module src/cancerhotspots
*/
const fs = require('fs');
const csv = require('fast-csv');
const { jsonifyVariant, parseVariant } = require('@bcgsc-pori/graphkb-parser');
const {
convertRowFields,
hashRecordToId,
} = require('../util');
const {
rid,
orderPreferredOntologyTerms,
} = require('../graphkb');
const _entrezGene = require('../entrez/gene');
const { logger } = require('../logging');
const {
cancerhotspots: SOURCE_DEFN,
oncotree: { name: oncotreeName },
ensembl: { name: ensemblName },
} = require('../sources');
const HEADER = {
assembly: 'NCBI_Build',
cds: 'HGVSc',
chromosome: 'Chromosome',
clinSig: 'CLIN_SIG',
dbsnp: 'dbSNP_RS',
diseaseId: 'oncotree_detailed',
geneId: 'Entrez_Gene_Id',
impact: 'IMPACT',
protein: 'HGVSp_Short',
refSeq: 'Reference_Allele',
start: 'Start_Position',
stop: 'End_Position',
transcriptId: 'Transcript_ID',
untemplatedSeq: 'Allele',
};
const diseasesCache = {};
const featureCache = {};
const chromosomeCache = {};
/**
* Create and link the variant defuinitions for a single row/record
*/
const processVariants = async ({ conn, record, source }) => {
const {
protein, cds, transcriptId, geneId, chromosome, start, stop,
} = record;
let proteinVariant,
cdsVariant,
genomicVariant;
try {
// get the chromosome
let reference1;
if (chromosomeCache[chromosome] !== undefined) {
reference1 = chromosomeCache[chromosome];
} else {
reference1 = await conn.getUniqueRecordBy({
filters: {
AND: [
{ OR: [{ sourceId: chromosome }, { name: chromosome }] },
{ biotype: 'chromosome' },
],
},
sort: orderPreferredOntologyTerms,
target: 'Feature',
});
chromosomeCache[chromosome] = reference1;
}
// try to create the genomic variant
const refSeq = record.refSeq === '-'
? ''
: record.refSeq;
const untemplatedSeq = record.untemplatedSeq === '-'
? ''
: record.untemplatedSeq;
let notation = `${chromosome}:g.`;
if (refSeq.length && untemplatedSeq.length) {
if (refSeq.length === 1 && untemplatedSeq.length === 1) {
// substitution
notation = `${notation}${start}${refSeq}>${untemplatedSeq}`;
} else {
// indel
notation = `${notation}${start}_${stop}del${refSeq}ins${untemplatedSeq}`;
}
} else if (refSeq.length === 0) {
// insertion
notation = `${notation}${start}_${stop}ins${untemplatedSeq}`;
} else {
// deletion
notation = `${notation}${start}_${stop}del${refSeq}`;
}
const variant = jsonifyVariant(parseVariant(notation));
variant.reference1 = rid(reference1);
variant.type = rid(await conn.getVocabularyTerm(variant.type));
genomicVariant = rid(await conn.addVariant({
content: { ...variant },
existsOk: true,
target: 'PositionalVariant',
}));
} catch (err) {
logger.warn(`failed to create the genomic variant (${chromosome}:${start}-${stop})`);
logger.warn(err);
}
try {
// get the gene
let reference1;
if (featureCache[reference1] !== undefined) {
reference1 = featureCache[reference1];
} else {
[reference1] = await _entrezGene.fetchAndLoadByIds(conn, [geneId]);
featureCache[geneId] = reference1;
}
const variant = jsonifyVariant(parseVariant(
protein.replace(/fs\*\?$/, 'fs'), // ignore uncertain truncations
false,
));
variant.reference1 = rid(reference1);
variant.type = rid(await conn.getVocabularyTerm(variant.type));
proteinVariant = rid(await conn.addVariant({
content: { ...variant },
existsOk: true,
target: 'PositionalVariant',
}));
} catch (err) {
logger.error(`Failed the protein variant (${geneId}:${protein}) ${err}`);
throw err;
}
// create the cds variant
try {
// get the ensembl transcript
let reference1;
if (featureCache[transcriptId] !== undefined) {
reference1 = featureCache[transcriptId];
} else {
reference1 = rid(await conn.getUniqueRecordBy({
filters: {
AND: [
{ sourceId: transcriptId },
{ biotype: 'transcript' },
{ source: { filters: { name: ensemblName }, target: 'Source' } },
],
},
sort: orderPreferredOntologyTerms,
target: 'Feature',
}));
featureCache[transcriptId] = reference1;
}
// parse the cds variant
const variant = jsonifyVariant(parseVariant(cds, false));
variant.reference1 = reference1;
variant.type = rid(await conn.getVocabularyTerm(variant.type));
cdsVariant = rid(await conn.addVariant({
content: { ...variant },
existsOk: true,
target: 'PositionalVariant',
}));
await conn.addRecord({
content: { in: proteinVariant, out: cdsVariant, source: rid(source) },
existsOk: true,
fetchExisting: false,
target: 'Infers',
});
} catch (err) {
logger.error(`Failed the cds variant (${transcriptId}:${cds}) ${err}`);
}
// link the genomic variant
if (genomicVariant && cdsVariant) {
await conn.addRecord({
content: { in: rid(cdsVariant), out: rid(genomicVariant), source: rid(source) },
existsOk: true,
fetchExisting: false,
target: 'Infers',
});
} else if (genomicVariant) {
await conn.addRecord({
content: { in: rid(proteinVariant), out: rid(genomicVariant), source: rid(source) },
existsOk: true,
fetchExisting: false,
target: 'Infers',
});
}
return proteinVariant;
};
const processRecord = async (conn, record, source, relevance) => {
const { diseaseId, sourceId } = record;
// get the protein variant
const variantId = await processVariants({ conn, record, source });
// get the disease by id from oncotree (try cache first)
let disease;
if (diseasesCache[diseaseId]) {
disease = diseasesCache[diseaseId];
} else {
disease = rid(await conn.getUniqueRecordBy({
filters: {
AND: [
{ sourceId: diseaseId },
{ source: { filters: { name: oncotreeName }, target: 'Source' } },
],
},
sort: orderPreferredOntologyTerms,
target: 'Disease',
}));
diseasesCache[diseaseId] = disease;
}
await conn.addRecord({
content: {
conditions: [variantId, disease],
evidence: [source],
relevance,
reviewStatus: 'not required',
source,
sourceId,
subject: disease,
},
existsOk: true,
fetchExisting: false,
target: 'Statement',
});
};
const createRowId = row => hashRecordToId(row);
/**
* Given some TAB delimited file, upload the resulting statements to GraphKB
*
* @param {object} opt options
* @param {string} opt.filename the path to the input tab delimited file
* @param {ApiConnection} opt.conn the API connection object
*/
const uploadFile = async ({ filename, conn, errorLogPrefix }) => {
logger.info(`loading: ${filename}`);
// get the dbID for the source
const source = rid(await conn.addSource(SOURCE_DEFN));
const relevance = rid(await conn.getVocabularyTerm('mutation hotspot'));
const counts = { error: 0, skip: 0, success: 0 };
const errorList = [];
let index = 0;
logger.info('load entrez genes cache');
await _entrezGene.preLoadCache(conn);
const previousLoad = new Set();
logger.info('load previous statements');
const statements = await conn.getRecords({
filters: { source: rid(source) },
returnProperties: ['sourceId'],
target: 'Statement',
});
for (const { sourceId } of statements) {
previousLoad.add(sourceId);
}
logger.info(`${previousLoad.size} loaded statements`);
const parserPromise = new Promise((resolve, reject) => {
const parser = csv
.parseFile(filename, {
comment: '#', delimiter: '\t', headers: true, trim: true,
})
.on('data', (data) => {
const record = convertRowFields(HEADER, data);
const sourceId = createRowId(record);
record.sourceId = sourceId;
index++;
if (
record.impact.toLowerCase() !== 'high'
|| record.clinSig === ''
|| record.clinSig.includes('benign')
) {
counts.skip++;
} else if (previousLoad.has(sourceId)) {
logger.info(`Already loaded ${sourceId}`);
} else if (record.protein.endsWith('=')) {
counts.skip++;
logger.info('skipping synonymous protein variant');
} else if (record.protein.endsWith('_splice')) {
counts.skip++;
logger.info('skipping non-standard splice notation');
} else {
parser.pause();
logger.info(`processing row #${index} ${sourceId}`);
processRecord(conn, record, source, relevance)
.then(() => {
logger.info('created record');
counts.success++;
parser.resume();
}).catch((err) => {
logger.error(err);
errorList.push({ error: err, errorMessage: err.toString(), record });
counts.error++;
parser.resume();
});
}
})
.on('error', (err) => {
console.error(err);
logger.error(err);
reject(err);
})
.on('end', () => {
logger.info('completed stream');
resolve();
});
});
await parserPromise;
const errorJson = `${errorLogPrefix}-cancerhotspots.json`;
logger.info(`writing: ${errorJson}`);
fs.writeFileSync(errorJson, JSON.stringify({ records: errorList }, null, 2));
logger.info(JSON.stringify(counts));
};
module.exports = {
SOURCE_DEFN, uploadFile,
};