-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
381 lines (327 loc) · 10.8 KB
/
index.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
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
/**
* Module to import clinical trials data exported from clinicaltrials.gov
* @module importer/clinicaltrialsgov
*/
const Ajv = require('ajv');
const {
checkSpec,
requestWithRetry,
} = require('../util');
const {
orderPreferredOntologyTerms,
rid,
} = require('../graphkb');
const { logger } = require('../logging');
const { clinicalTrialsGov: SOURCE_DEFN } = require('../sources');
const { studies: studiesSpecs } = require('./specs.json');
const BASE_URL = 'https://clinicaltrials.gov/api/v2/studies';
const CACHE = {};
const ajv = new Ajv();
const validateAPITrialRecord = ajv.compile(studiesSpecs);
/**
* Given some records from the API, convert its form to a standard represention
*/
const convertAPIRecord = (rawRecord) => {
checkSpec(validateAPITrialRecord, rawRecord, rec => rec.protocolSection.identificationModule.nctId);
const { protocolSection: record } = rawRecord;
let startDate,
completionDate;
try {
startDate = record.statusModule.startDateStruct.date;
} catch (err) {}
try {
completionDate = record.statusModule.completionDateStruct.date;
} catch (err) {}
const title = record.identificationModule.officialTitle || record.identificationModule.briefTitle;
const { nctId } = record.identificationModule;
const url = `${BASE_URL}/${nctId}`;
const content = {
completionDate,
diseases: record.conditionsModule.conditions,
displayName: title,
drugs: [],
locations: [],
name: title,
recruitmentStatus: record.statusModule.overallStatus,
sourceId: nctId,
sourceIdVersion: record.statusModule.lastUpdatePostDateStruct.date,
startDate,
url,
};
if (record.designModule.phases) {
content.phases = record.designModule.phases;
}
for (const { name, type } of record.armsInterventionsModule.interventions || []) {
if (type.toLowerCase() === 'drug' || type.toLowerCase() === 'biological') {
content.drugs.push(name);
}
}
if (record.contactsLocationsModule) {
for (const { country, city } of record.contactsLocationsModule.locations || []) {
if (city && country) {
content.locations.push({ city: city.toLowerCase(), country: country.toLowerCase() });
}
if (city && !country) {
content.locations.push({ city: city.toLowerCase() });
}
if (!city && country) {
content.locations.push({ country: country.toLowerCase() });
}
}
}
return content;
};
const processPhases = (phaseList) => {
const phases = [];
for (const raw of phaseList || []) {
const cleanedPhaseList = raw.trim().toLowerCase().replace(/\bn\/a\b/, '').split(/[,/]/);
for (const phase of cleanedPhaseList) {
if (phase !== '' && phase !== 'na' && phase !== 'ph') {
const match = /^(early_)?phase(\d+)$/.exec(phase);
if (!match) {
throw new Error(`unrecognized phase description (${phase})`);
}
phases.push(match[2]);
}
}
}
return phases.sort().join('/');
};
/**
* Process the record. Attempt to link the drug and/or disease information
*
* @param {object} opt
* @param {ApiConnection} opt.conn the GraphKB connection object
* @param {object} opt.record the record (pre-parsed into JSON)
* @param {object|string} opt.source the 'source' record for clinicaltrials.gov
*
* @todo: handle updates to existing clinical trial records
*/
const processRecord = async ({
conn, record, source, upsert = false,
}) => {
const content = {
displayName: record.displayName,
name: record.name,
recruitmentStatus: record.recruitmentStatus.replace(/_/g, ' '),
source: rid(source),
sourceId: record.sourceId,
sourceIdVersion: record.sourceIdVersion,
url: record.url,
};
// temperory mapping to avoid schema change
if (content.recruitmentStatus && content.recruitmentStatus.toLowerCase() === 'active not recruiting') {
content.recruitmentStatus = 'active, not recruiting';
}
if (content.recruitmentStatus && content.recruitmentStatus.toLowerCase() === 'unknown status') {
content.recruitmentStatus = 'unknown';
}
const phase = processPhases(record.phases);
if (phase) {
content.phase = phase;
}
if (record.startDate) {
content.startDate = record.startDate;
}
if (record.completionDate) {
content.completionDate = record.completionDate;
}
// check if single location or at least single country
let consensusCountry,
consensusCity;
for (const { city, country } of record.locations) {
if (country && consensusCountry) {
if (consensusCountry !== country.toLowerCase()) {
consensusCountry = null;
consensusCity = null;
break;
}
} else if (country) {
consensusCountry = country.toLowerCase();
}
if (city && consensusCity) {
if (consensusCity !== city.toLowerCase()) {
consensusCity = null;
}
} else if (city) {
consensusCity = city.toLowerCase();
}
}
if (consensusCountry) {
content.country = consensusCountry;
if (consensusCity) {
content.city = consensusCity;
}
}
const links = [];
const missingLinks = [];
for (const drug of record.drugs) {
try {
const intervention = await conn.getUniqueRecordBy({
filters: { name: drug },
sort: orderPreferredOntologyTerms,
target: 'Therapy',
});
links.push(intervention);
} catch (err) {
logger.warn(`[${record.sourceId}] failed to find drug by name`);
logger.warn(err);
missingLinks.push(`Therapy(${drug})`);
}
}
for (const diseaseName of record.diseases) {
try {
const disease = await conn.getUniqueRecordBy({
filters: { name: diseaseName },
sort: orderPreferredOntologyTerms,
target: 'Disease',
});
links.push(disease);
} catch (err) {
logger.warn(`[${record.sourceId}] failed to find disease by name`);
logger.warn(err);
missingLinks.push(`Disease(${diseaseName})`);
}
}
if (missingLinks.length) {
content.comment = `Missing: ${missingLinks.join('; ')}`;
}
// create the clinical trial record
const trialRecord = await conn.addRecord({
content,
existsOk: true,
fetchConditions: { AND: [{ source: rid(source) }, { sourceId: record.sourceId }] },
fetchFirst: true,
target: 'ClinicalTrial',
upsert,
});
// link to the drugs and diseases
for (const link of links) {
await conn.addRecord({
content: { in: rid(trialRecord), out: rid(link), source: rid(source) },
existsOk: true,
fetchExisting: false,
target: 'ElementOf',
});
}
return trialRecord;
};
/**
* Given some NCT ID, fetch and load the corresponding clinical trial information
*
* https://clinicaltrials.gov/api/v2/studies/NCT03478891
*/
const fetchAndLoadById = async (conn, nctID, { upsert = false } = {}) => {
const url = `${BASE_URL}/${nctID}`;
if (CACHE[nctID.toLowerCase()]) {
return CACHE[nctID.toLowerCase()];
}
// try to get the record from the gkb db first
try {
const trial = await conn.getUniqueRecordBy({
filters: {
AND: [
{ source: { filters: { name: SOURCE_DEFN.name }, target: 'Source' } },
{ sourceId: nctID },
],
},
sort: orderPreferredOntologyTerms,
target: 'ClinicalTrial',
});
CACHE[trial.sourceId] = trial;
return trial;
} catch (err) {}
logger.info(`loading: ${url}`);
// fetch from the external api
const result = await requestWithRetry({
json: true,
method: 'GET',
uri: url,
});
// get or add the source
if (!CACHE.source) {
CACHE.source = rid(await conn.addSource(SOURCE_DEFN));
}
const trial = await processRecord({
conn,
record: convertAPIRecord(result),
source: CACHE.source,
upsert,
});
CACHE[trial.sourceId] = trial;
return trial;
};
const formatDate = (date) => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
/**
* Loading all clinical trials related to cancer
*/
const upload = async ({ conn, maxRecords, days }) => {
const source = await conn.addSource(SOURCE_DEFN);
let options,
optionsWithToken;
if (days) {
const startDate = new Date(Date.now() - days * 24 * 60 * 60 * 1000);
options = { 'query.term': `AREA[LastUpdatePostDate]RANGE[${formatDate(startDate)},MAX]` };
logger.info(`loading records updated from ${formatDate(startDate)} to ${formatDate(new Date())}`);
}
const counts = {
error: 0, success: 0,
};
let processCount = 1,
next = true,
nextToken,
total = maxRecords;
while (next) {
if (nextToken) {
optionsWithToken = { pageToken: nextToken, ...options };
} else {
optionsWithToken = options;
}
const trials = await requestWithRetry({
json: true,
method: 'GET',
qs: {
aggFilters: 'studyType:int',
countTotal: true,
pageSize: 1000,
'query.cond': 'cancer',
sort: 'LastUpdatePostDate',
...optionsWithToken,
},
uri: BASE_URL,
});
if (!total) {
total = trials.totalCount;
}
if (processCount > total) {
break;
}
for (const trial of trials.studies) {
if (processCount > total) {
break;
}
try {
const record = convertAPIRecord(trial);
logger.info(`processing (${processCount}/${total}) record: ${record.sourceId}`);
processCount++;
await processRecord({
conn, record, source, upsert: true,
});
counts.success++;
} catch (err) {
counts.error++;
logger.error(`[${trial}] ${err}`);
}
}
nextToken = trials.nextPageToken;
next = nextToken !== undefined;
}
logger.info(JSON.stringify(counts));
};
module.exports = {
SOURCE_DEFN,
convertAPIRecord,
fetchAndLoadById,
kb: true,
upload,
};