-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrefseq.js
185 lines (162 loc) · 5.33 KB
/
refseq.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
/**
* @module importer/entrez/refseq
*/
const Ajv = require('ajv');
const _ = require('lodash');
const pLimit = require('p-limit');
const {
fetchByIdList, uploadRecord, preLoadCache: preLoadAnyCache, fetchAndLoadBySearchTerm: search,
} = require('./util');
const { checkSpec } = require('../util');
const { rid } = require('../graphkb');
const { refseq: SOURCE_DEFN } = require('../sources');
const { logger } = require('../logging');
const ajv = new Ajv();
const CONCURENCY_LIMIT = 100;
const DB_NAME = 'nucleotide';
const CACHE = {};
const recordSpec = ajv.compile({
properties: {
accessionversion: { pattern: '^N[A-Z]_\\d+\\.\\d+$', type: 'string' },
biomol: { enum: ['genomic', 'rna', 'peptide', 'mRNA'], type: 'string' },
replacedby: { type: 'string' },
status: { type: 'string' },
subname: { type: 'string' },
title: { type: 'string' },
},
required: ['title', 'biomol', 'accessionversion'],
type: 'object',
});
/**
* Given an record record retrieved from refseq, parse it into its equivalent
* GraphKB representation
*/
const parseRecord = (record) => {
checkSpec(recordSpec, record);
const [sourceId, sourceIdVersion] = record.accessionversion.split('.');
let biotype = 'transcript';
if (record.biomol === 'genomic') {
biotype = 'chromosome';
} else if (record.biomol === 'peptide') {
biotype = 'protein';
}
const parsed = {
biotype,
displayName: record.accessionversion.toUpperCase(),
longName: record.title,
sourceId,
sourceIdVersion: sourceIdVersion || null,
};
if (biotype === 'chromosome') {
parsed.name = record.subname;
}
return parsed;
};
/**
* Given some list of refseq IDs, return if cached,
* If they do not exist, grab from the refseq graphkbConn
* and then upload to GraphKB
*
* @param {ApiConnection} api connection to GraphKB
* @param {Array.<string>} idList list of IDs
*/
const fetchAndLoadByIds = async (api, idListIn) => {
const versionedIds = [];
const unversionedIds = [];
idListIn.forEach((id) => {
if (/\.\d+$/.exec(id)) {
versionedIds.push(id);
} else {
unversionedIds.push(id);
}
});
const records = [];
if (versionedIds.length > 0) {
const fullRecords = await fetchByIdList(
versionedIds,
{
cache: CACHE, db: DB_NAME, parser: parseRecord,
},
);
records.push(...fullRecords);
}
if (unversionedIds.length > 0) {
const fullRecords = await fetchByIdList(
unversionedIds,
{
cache: CACHE, db: DB_NAME, parser: parseRecord,
},
);
fullRecords.forEach((rec) => {
const simplified = _.omit(rec, ['sourceIdVersion', 'longName', 'description']);
simplified.displayName = simplified.sourceId.toUpperCase();
records.push({ ...simplified, sourceIdVersion: null });
});
}
logger.verbose(`uploading ${records.length} records`);
const limit = pLimit(CONCURENCY_LIMIT);
const result = await Promise.all(records.map(rec => limit(() => uploadRecord(api, rec, {
cache: CACHE,
sourceDefn: SOURCE_DEFN,
target: 'Feature',
}))));
// for versioned records link to the unversioned version
await Promise.all(result
.filter(r => (r.sourceIdVersion !== undefined && r.sourceIdVersion !== null))
.map((record) => {
const linkRecords = async () => {
const unversioned = await api.addRecord({
content: {
biotype: record.biotype,
description: record.description,
displayName: record.sourceId.toUpperCase(),
longName: record.longName,
name: record.name,
source: rid(record.source),
sourceId: record.sourceId,
sourceIdVersion: null,
},
existsOk: true,
fetchConditions: {
AND: [
{ name: record.name },
{ source: rid(record.source) },
{ sourceId: record.sourceId },
{ sourceIdVersion: null },
],
},
target: 'Feature',
});
await api.addRecord({
content: { in: rid(record), out: rid(unversioned), source: record.source },
existsOk: true,
target: 'GeneralizationOf',
});
};
return limit(() => linkRecords);
}));
return result;
};
const preLoadCache = async api => preLoadAnyCache(
api,
{
cache: CACHE, sourceDefn: SOURCE_DEFN, target: 'Feature',
},
);
const fetchAndLoadBySearchTerm = (api, term, opt = {}) => search(api, term, {
...opt,
cache: CACHE,
dbName: DB_NAME,
parser: parseRecord,
sourceDefn: SOURCE_DEFN,
target: 'Feature',
});
const cacheHas = key => Boolean(CACHE[key]);
module.exports = {
SOURCE_DEFN,
cacheHas,
fetchAndLoadByIds,
fetchAndLoadBySearchTerm,
parseRecord,
preLoadCache,
};