external-api.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Adam Midlik <midlik@gmail.com>
  5. */
  6. import { splitEntryId } from './helpers';
  7. /** Try to get author-defined contour value for isosurface from EMDB API. Return relative value 1.0, if not applicable or fails. */
  8. export async function tryGetIsovalue(entryId: string): Promise<{ kind: 'absolute' | 'relative', value: number } | undefined> {
  9. const split = splitEntryId(entryId);
  10. if (split.source === 'emdb') {
  11. try {
  12. const response = await fetch(`https://www.ebi.ac.uk/emdb/api/entry/map/${split.entryNumber}`);
  13. const json = await response.json();
  14. const contours: any[] = json?.map?.contour_list?.contour;
  15. if (contours && contours.length > 0) {
  16. const theContour = contours.find(c => c.primary) || contours[0];
  17. if (theContour.level === undefined) throw new Error('EMDB API response missing contour level.');
  18. return { kind: 'absolute', value: theContour.level };
  19. }
  20. } catch {
  21. // do nothing
  22. }
  23. }
  24. return undefined;
  25. }
  26. export async function getPdbIdsForEmdbEntry(entryId: string): Promise<string[]> {
  27. const split = splitEntryId(entryId);
  28. const result = [];
  29. if (split.source === 'emdb') {
  30. entryId = entryId.toUpperCase();
  31. const apiUrl = `https://www.ebi.ac.uk/pdbe/api/emdb/entry/fitted/${entryId}`;
  32. try {
  33. const response = await fetch(apiUrl);
  34. if (response.ok) {
  35. const json = await response.json();
  36. const jsonEntry = json[entryId] ?? [];
  37. for (const record of jsonEntry) {
  38. const pdbs = record?.fitted_emdb_id_list?.pdb_id ?? [];
  39. result.push(...pdbs);
  40. }
  41. }
  42. } catch (ex) {
  43. // do nothing
  44. }
  45. }
  46. return result;
  47. }