-
Notifications
You must be signed in to change notification settings - Fork 4
/
snp.js
185 lines (162 loc) · 5.66 KB
/
snp.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
const Ajv = require('ajv');
const { variant: { parse: variantParser } } = require('@bcgsc-pori/graphkb-parser');
const { checkSpec } = require('../util');
const {
fetchByIdList, uploadRecord, preLoadCache: preLoadAnyCache, BASE_FETCH_URL,
} = require('./util');
const refseq = require('./refseq');
const entrezGene = require('./gene');
const { rid } = require('../graphkb');
const { logger } = require('../logging');
const ajv = new Ajv();
const { dbSnp: SOURCE_DEFN } = require('../sources');
const DB_NAME = 'snp';
const LINK_URL = 'https://www.ncbi.nlm.nih.gov/snp';
const CACHE = {};
const recordSpec = ajv.compile({
properties: {
clinical_significance: { type: 'string' },
docsum: { type: 'string' },
genes: {
items: {
properties: {
gene_id: { pattern: '^\\d+$', type: 'string' },
name: { type: 'string' },
},
required: ['name', 'gene_id'],
type: 'object',
},
type: 'array',
},
snp_id: { type: 'integer' },
uid: { pattern: '^\\d+$', type: 'string' },
updatedate: { type: 'string' },
},
required: ['uid', 'snp_id'],
type: 'object',
});
const loadFromDocsumHgvs = async (api, hgvsVariants) => {
let cds,
protein;
try {
if (hgvsVariants.cds) {
const parsed = variantParser(hgvsVariants.cds.split('|')[0], true).toJSON();
const [transcript] = await refseq.fetchAndLoadByIds(api, [parsed.reference1]);
const type = await api.getVocabularyTerm(parsed.type);
cds = await api.addVariant({
content: { ...parsed, reference1: rid(transcript), type: rid(type) },
existsOk: true,
target: 'PositionalVariant',
});
}
} catch (err) {
logger.error(`creating HGVSc (${hgvsVariants.cds}): ${err}`);
}
try {
if (hgvsVariants.protein) {
const gene = hgvsVariants.protein.split('|').find(p => p.startsWith('GENE='));
const parsed = variantParser(hgvsVariants.protein.split('|')[0], true).toJSON();
const [reference1] = await refseq.fetchAndLoadByIds(api, [parsed.reference1]);
const type = await api.getVocabularyTerm(parsed.type);
protein = await api.addVariant({
content: { ...parsed, reference1: rid(reference1), type: rid(type) },
existsOk: true,
target: 'PositionalVariant',
});
if (cds) {
await api.addRecord({
content: { in: rid(protein), out: rid(cds) },
existsOk: true,
target: 'Infers',
});
}
if (gene) {
const [geneRec] = await entrezGene.fetchAndLoadByIds(api, gene.split(':')[1]);
const alternateProtein = await api.addVariant({
content: { ...parsed, reference1: rid(geneRec), type: rid(type) },
existsOk: true,
target: 'PositionalVariant',
});
await api.addRecord({
content: { in: rid(alternateProtein), out: rid(protein) },
existsOk: true,
target: 'Infers',
});
}
}
} catch (err) {
logger.error(`creating HGVSp (${hgvsVariants.protein}): ${err}`);
}
return cds || protein;
};
/**
* Given an record record retrieved from pubmed, parse it into its equivalent
* GraphKB representation
*/
const parseRecord = (record) => {
checkSpec(recordSpec, record);
const parsed = {
displayName: `rs${record.snp_id}`,
genes: record.genes.map(gene => gene.gene_id),
hgvs: {},
name: `rs${record.snp_id}`,
sourceId: record.uid,
sourceIdVersion: record.updatedate,
url: `${LINK_URL}/rs${record.snp_id}`,
};
for (const tag of record.docsum.replace(/>/g, '>').split(';')) {
if (tag.startsWith('HGVS=')) {
const notation = tag.replace(/^HGVS=/, '').split(',').sort().reverse();
parsed.hgvs.cds = notation.find(n => /^NM_\d+.*:c\..*/.exec(n));
parsed.hgvs.protein = notation.find(n => /^NP_\d+.*:p\..*/.exec(n));
break;
}
}
return parsed;
};
const loadSnpRecord = async (api, { hgvs, genes, ...content }) => {
const uploaded = await uploadRecord(api, content, {
cache: CACHE,
sourceDefn: SOURCE_DEFN,
target: 'CatalogueVariant',
});
// link to the hgvs cds notation
const hgvsVariant = await loadFromDocsumHgvs(api, hgvs);
if (hgvsVariant) {
await api.addRecord({
content: { in: rid(uploaded), out: rid(hgvsVariant) },
existsOk: true,
target: 'Infers',
});
}
return uploaded;
};
/**
* 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>} idList list of pubmed IDs
*/
const fetchAndLoadByIds = async (api, idListIn) => {
const records = await fetchByIdList(
idListIn,
{
cache: CACHE, db: DB_NAME, parser: parseRecord, url: BASE_FETCH_URL,
},
);
return Promise.all(records.map(async rec => loadSnpRecord(api, rec)));
};
const preLoadCache = async api => preLoadAnyCache(
api,
{
cache: CACHE, sourceDefn: SOURCE_DEFN, target: 'CatalogueVariant',
},
);
module.exports = {
SOURCE_DEFN,
fetchAndLoadByIds,
parseRecord,
preLoadCache,
};