-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi-lapis.ts
422 lines (379 loc) · 13.4 KB
/
api-lapis.ts
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
import { LapisInformation, LapisResponse } from './LapisResponse';
import { DateCountSampleEntry } from './sample/DateCountSampleEntry';
import { AgeCountSampleEntry } from './sample/AgeCountSampleEntry';
import { DivisionCountSampleEntry } from './sample/DivisionCountSampleEntry';
import { addLocationSelectorToUrlSearchParams, LocationSelector } from './LocationSelector';
import {
addDateRangeSelectorToUrlSearchParams,
addSubmittedDateRangeSelectorToUrlParams,
} from './DateRangeSelector';
import { CountryDateCountSampleEntry } from './sample/CountryDateCountSampleEntry';
import { PangoCountSampleEntry } from './sample/PangoCountSampleEntry';
import {
FullSampleAggEntry,
FullSampleAggEntryRaw,
parseFullSampleAggEntry,
} from './sample/FullSampleAggEntry';
import { SequenceType } from './SequenceType';
import { MutationProportionEntry } from './MutationProportionEntry';
import dayjs from 'dayjs';
import { LocationService } from '../services/LocationService';
import { OrderAndLimitConfig } from './OrderAndLimitConfig';
import { addSamplingStrategyToUrlSearchParams } from './SamplingStrategy';
import { DatelessCountrylessCountSampleEntry } from './sample/DatelessCountrylessCountSampleEntry';
import { HospDiedAgeSampleEntry } from './sample/HospDiedAgeSampleEntry';
import { LapisSelector } from './LapisSelector';
import { addHostSelectorToUrlSearchParams } from './HostSelector';
import { addQcSelectorToUrlSearchParams } from './QcSelector';
import { HostCountSampleEntry } from './sample/HostCountSampleEntry';
import { InsertionCountEntry } from './InsertionCountEntry';
import { NextcladeDatasetInfo } from './NextcladeDatasetInfo';
import { mapFilterToLapisV2 } from './api-lapis-v2';
import { addVariantSelectorToUrlSearchParamsForApi } from './VariantSelector';
const HOST = process.env.REACT_APP_LAPIS_HOST;
const ACCESS_KEY = process.env.REACT_APP_LAPIS_ACCESS_KEY;
let currentLapisDataVersion: number | undefined = undefined;
const getRaw = async (
endpoint: string,
signal?: AbortSignal,
options: { skipMaintenanceCheck?: boolean } = {}
) => {
let url = `${HOST}/sample${endpoint}`;
const requestInit =
signal === undefined
? {
method: 'GET',
}
: {
method: 'GET',
signal: signal,
};
const response = await fetch(url, requestInit);
if (!(options.skipMaintenanceCheck === true) && response.status === 503) {
window.location.reload();
}
return response;
};
const get = async (
endpoint: string,
signal?: AbortSignal,
options: { skipMaintenanceCheck?: boolean } = {}
) => {
const response = await getRaw(endpoint, signal, options);
if (!response.ok) {
if (response.body !== null) {
let body;
try {
body = await response.json();
} catch (e) {
throw new Error(`Failed to fetch data from LAPIS: ${response.status}`);
}
if (body.error?.detail !== undefined) {
throw new Error(`Failed to fetch data from LAPIS: ${body.error?.detail}`);
}
}
throw new Error(`Failed to fetch data from LAPIS: ${response.status}`);
}
return response;
};
export type SiloAvailability =
| { isAvailable: true }
| { isAvailable: false; retryAfterInSeconds: number | null };
export async function checkSiloAvailability(signal?: AbortSignal): Promise<SiloAvailability> {
let url = '/aggregated';
if (ACCESS_KEY) {
url += '?accessKey=' + ACCESS_KEY;
}
const response = await getRaw(url, signal, { skipMaintenanceCheck: true });
if (response.status !== 503) {
return { isAvailable: true as const };
}
const retryAfterInSeconds = response.headers.get('Retry-After');
if (retryAfterInSeconds === null) {
return { isAvailable: false, retryAfterInSeconds: null };
}
return { isAvailable: false, retryAfterInSeconds: Number(retryAfterInSeconds) };
}
export async function fetchLapisDataVersion(signal?: AbortSignal): Promise<number> {
let url = '/info';
if (ACCESS_KEY) {
url += '?accessKey=' + ACCESS_KEY;
}
const response = await get(url, signal, { skipMaintenanceCheck: true });
if (!response.ok) {
throw new Error('Error fetching info');
}
const info = (await response.json()) as LapisInformation;
return Number(info.dataVersion);
}
export async function fetchNextcladeDatasetInfo(signal?: AbortSignal): Promise<NextcladeDatasetInfo> {
let url = '/aggregated?fields=nextcladeDatasetVersion';
if (ACCESS_KEY) {
url += '&accessKey=' + ACCESS_KEY;
}
const response = await get(url, signal, { skipMaintenanceCheck: true });
const nexcladeDatasetInfo = (await response.json()) as LapisResponse<{ nextcladeDatasetVersion: string }[]>;
return {
name: 'nextclade-dataset',
tag: nexcladeDatasetInfo.data[0].nextcladeDatasetVersion,
};
}
export async function fetchAllHosts(): Promise<string[]> {
let url = '/aggregated?fields=host';
if (ACCESS_KEY) {
url += '&accessKey=' + ACCESS_KEY;
}
const res = await get(url, undefined, { skipMaintenanceCheck: true });
const body = (await res.json()) as LapisResponse<{ host: string; count: number }[]>;
return _extractLapisData(body)
.map(entry => entry.host)
.map(host => (host === null ? 'Unknown' : host));
}
export async function fetchDateCountSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<DateCountSampleEntry[]> {
return _fetchAggSamples(selector, ['date'], signal);
}
export async function fetchAgeCountSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<AgeCountSampleEntry[]> {
return _fetchAggSamples(selector, ['age'], signal);
}
export async function fetchDivisionCountSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<DivisionCountSampleEntry[]> {
return _fetchAggSamples(selector, ['division', 'country', 'region'], signal);
}
export async function fetchCountryDateCountSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<CountryDateCountSampleEntry[]> {
return _fetchAggSamples(selector, ['date', 'country'], signal);
}
export async function fetchDatelessCountrylessCountSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<DatelessCountrylessCountSampleEntry[]> {
return _fetchAggSamples(selector, ['division', 'age', 'sex', 'hospitalized', 'died'], signal);
}
export async function fetchHospDiedAgeSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<HospDiedAgeSampleEntry[]> {
return _fetchAggSamples(selector, ['age', 'hospitalized', 'died'], signal);
}
export async function fetchSamplesCount(selector: LapisSelector, signal?: AbortSignal): Promise<number> {
return _fetchAggSamples(selector, [], signal).then(entries => entries[0].count);
}
export async function fetchPangoLineageCountSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<PangoCountSampleEntry[]> {
return _fetchAggSamples(selector, ['pangoLineage'], signal);
}
export async function fetchHostCountSamples(
selector: LapisSelector,
signal?: AbortSignal
): Promise<HostCountSampleEntry[]> {
return _fetchAggSamples(selector, ['host'], signal);
}
export async function fetchNumberSubmittedSamplesInPastTenDays(
selector: LapisSelector,
signal?: AbortSignal
): Promise<number> {
const additionalParams = new URLSearchParams();
additionalParams.set('dateSubmittedFrom', dayjs().subtract(10, 'days').toISOString().substring(0, 10));
const res = await _fetchAggSamples(selector, [], signal, additionalParams);
return res[0].count;
}
export async function fetchMutationProportions(
selector: LapisSelector,
sequenceType: SequenceType,
signal?: AbortSignal,
minProportion = 0.001
): Promise<MutationProportionEntry[]> {
const url = await getLinkTo(
getMutationEndpoint(sequenceType),
selector,
undefined,
undefined,
undefined,
true,
minProportion.toString()
);
const res = await get(url, signal);
const body = (await res.json()) as LapisResponse<MutationProportionEntry[]>;
return _extractLapisData(body);
}
function getMutationEndpoint(sequenceType: SequenceType): string {
switch (sequenceType) {
case 'nuc':
return 'nucleotideMutations';
case 'aa':
return 'aminoAcidMutations';
default:
throw new Error(`Unknown mutation type: ${sequenceType}`);
}
}
export async function fetchInsertionCounts(
selector: LapisSelector,
sequenceType: SequenceType,
signal?: AbortSignal
): Promise<InsertionCountEntry[]> {
const url = await getLinkTo(
getInsertionEndpoint(sequenceType),
selector,
undefined,
undefined,
undefined,
true
);
const res = await get(url, signal);
const body = (await res.json()) as LapisResponse<InsertionCountEntry[]>;
return _extractLapisData(body);
}
function getInsertionEndpoint(sequenceType: SequenceType): string {
switch (sequenceType) {
case 'nuc':
return 'nucleotideInsertions';
case 'aa':
return 'aminoAcidInsertions';
default:
throw new Error(`Unknown mutation type: ${sequenceType}`);
}
}
export async function getLinkToListOfPrimaryKeys(
primaryKey: string,
selector: LapisSelector,
orderAndLimit?: OrderAndLimitConfig
): Promise<string> {
const dataFormat = 'CSV-WITHOUT-HEADERS';
const linkToDetails = new URL(await getLinkTo('details', selector, orderAndLimit, undefined, dataFormat));
linkToDetails.searchParams.set('fields', primaryKey);
return linkToDetails.toString();
}
export async function getCsvLinkToDetails(selector: LapisSelector): Promise<string> {
return getLinkTo('details', selector, undefined, true, 'csv');
}
export async function getLinkToFasta(
aligned: boolean,
selector: LapisSelector,
orderAndLimit?: OrderAndLimitConfig
): Promise<string> {
return getLinkTo(
aligned ? 'alignedNucleotideSequences' : 'unalignedNucleotideSequences',
selector,
orderAndLimit,
true
);
}
export async function getLinkTo(
endpoint: string,
selector: LapisSelector,
orderAndLimit?: OrderAndLimitConfig,
downloadAsFile?: boolean,
dataFormat?: string,
omitHost = false,
minProportion?: string
): Promise<string> {
const params = new URLSearchParams();
_addOrderAndLimitToSearchParams(params, orderAndLimit);
selector = await _mapCountryName(selector);
addLocationSelectorToUrlSearchParams(selector.location, params);
if (selector.dateRange) {
addDateRangeSelectorToUrlSearchParams(selector.dateRange, params);
}
if (selector.variant) {
addVariantSelectorToUrlSearchParamsForApi(selector.variant, params);
}
if (selector.samplingStrategy) {
addSamplingStrategyToUrlSearchParams(selector.samplingStrategy, params);
}
if (selector.host) {
addHostSelectorToUrlSearchParams(selector.host, params);
}
if (selector.submissionDate) {
addSubmittedDateRangeSelectorToUrlParams(params, selector.submissionDate, true);
}
addQcSelectorToUrlSearchParams(selector.qc, params);
if (downloadAsFile) {
params.set('downloadAsFile', 'true');
}
if (dataFormat) {
params.set('dataFormat', dataFormat);
}
if (minProportion) {
params.set('minProportion', minProportion);
}
if (ACCESS_KEY) {
params.set('accessKey', ACCESS_KEY);
}
if (omitHost) {
return `/${endpoint}?${params.toString()}`;
} else {
return `${HOST}/sample/${endpoint}?${params.toString()}`;
}
}
export async function _fetchAggSamples(
selector: LapisSelector,
fields: string[],
signal?: AbortSignal,
additionalParams?: URLSearchParams
): Promise<FullSampleAggEntry[]> {
const linkPrefix = await getLinkTo('aggregated', selector, undefined, undefined, undefined, true);
const _additionalParams = new URLSearchParams(additionalParams);
_additionalParams.set('fields', fields.map(mapFilterToLapisV2).join(','));
const response = await get(`${linkPrefix}&${_additionalParams}`, signal);
const body = (await response.json()) as LapisResponse<FullSampleAggEntryRaw[]>;
const parsed = _extractLapisData(body).map(raw => parseFullSampleAggEntry(raw));
if (fields.includes('country')) {
const gisaidToCovSpectrumNameMap = await LocationService.getGisaidToCovSpectrumNameMap();
return parsed.map(e => ({
...e,
country: e.country ? (gisaidToCovSpectrumNameMap.get(e.country) ?? null) : null,
}));
}
return parsed;
}
function _addOrderAndLimitToSearchParams(params: URLSearchParams, orderAndLimitConfig?: OrderAndLimitConfig) {
if (orderAndLimitConfig) {
const { orderBy, limit } = orderAndLimitConfig;
if (orderBy) {
params.set('orderBy', orderBy);
}
if (limit) {
params.set('limit', limit.toString());
}
}
}
function _extractLapisData<T>(response: LapisResponse<T>): T {
if (currentLapisDataVersion === undefined) {
currentLapisDataVersion = Number(response.info.dataVersion);
} else if (currentLapisDataVersion !== Number(response.info.dataVersion)) {
console.log(
`LAPIS has new data. Old version: ${currentLapisDataVersion}, new version: ${response.info.dataVersion}. ` +
`The website will be reloaded.`
);
window.location.reload();
throw new Error(
`LAPIS has new data. Old version: ${currentLapisDataVersion}, new version: ${response.info.dataVersion}. ` +
`The website will be reloaded.`
);
}
return response.data;
}
async function _mapCountryName<T extends { location: LocationSelector }>(selector: T): Promise<T> {
if (selector.location.country) {
selector = {
...selector,
location: {
...selector.location,
country: await LocationService.getGisaidName(selector.location.country),
},
};
}
return selector;
}