util.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Structure, Model } from '../../../../mol-model/structure';
  8. import { VolumeServerInfo } from './model';
  9. import { PluginContext } from '../../../../mol-plugin/context';
  10. import { RuntimeContext } from '../../../../mol-task';
  11. import { MmcifFormat } from '../../../../mol-model-formats/structure/mmcif';
  12. import { PluginConfig } from '../../../config';
  13. export function getStreamingMethod(s?: Structure, defaultKind: VolumeServerInfo.Kind = 'x-ray'): VolumeServerInfo.Kind {
  14. if (!s) return defaultKind;
  15. const model = s.models[0];
  16. if (!MmcifFormat.is(model.sourceData)) return defaultKind;
  17. // Prefer EMDB entries over structure-factors (SF) e.g. for 'ELECTRON CRYSTALLOGRAPHY' entries
  18. // like 6AXZ or 6KJ3 for which EMDB entries are available but map calculation from SF is hard.
  19. if (Model.hasEmMap(model)) return 'em';
  20. if (Model.hasXrayMap(model)) return 'x-ray';
  21. // Fallbacks based on experimental method
  22. if (Model.isFromEm(model)) return 'em';
  23. if (Model.isFromXray(model)) return 'x-ray';
  24. return defaultKind;
  25. }
  26. /** Returns EMD ID when available, otherwise falls back to PDB ID */
  27. export function getEmIds(model: Model): string[] {
  28. const ids: string[] = [];
  29. if (!MmcifFormat.is(model.sourceData)) return [model.entryId];
  30. const { db_id, db_name, content_type } = model.sourceData.data.db.pdbx_database_related;
  31. if (!db_name.isDefined) return [model.entryId];
  32. for (let i = 0, il = db_name.rowCount; i < il; ++i) {
  33. if (db_name.value(i).toUpperCase() === 'EMDB' && content_type.value(i) === 'associated EM volume') {
  34. ids.push(db_id.value(i));
  35. }
  36. }
  37. return ids;
  38. }
  39. export function getXrayIds(model: Model): string[] {
  40. return [model.entryId];
  41. }
  42. export function getIds(method: VolumeServerInfo.Kind, s?: Structure): string[] {
  43. if (!s || !s.models.length) return [];
  44. const model = s.models[0];
  45. switch (method) {
  46. case 'em': return getEmIds(model);
  47. case 'x-ray': return getXrayIds(model);
  48. }
  49. }
  50. export async function getContourLevel(provider: 'emdb' | 'pdbe', plugin: PluginContext, taskCtx: RuntimeContext, emdbId: string) {
  51. switch (provider) {
  52. case 'emdb': return getContourLevelEmdb(plugin, taskCtx, emdbId);
  53. case 'pdbe': return getContourLevelPdbe(plugin, taskCtx, emdbId);
  54. }
  55. }
  56. export async function getContourLevelEmdb(plugin: PluginContext, taskCtx: RuntimeContext, emdbId: string) {
  57. const emdbHeaderServer = plugin.config.get(PluginConfig.VolumeStreaming.EmdbHeaderServer);
  58. const header = await plugin.fetch({ url: `${emdbHeaderServer}/${emdbId.toUpperCase()}/header/${emdbId.toLowerCase()}.xml`, type: 'xml' }).runInContext(taskCtx);
  59. const map = header.getElementsByTagName('map')[0];
  60. const contourLevel = parseFloat(map.getElementsByTagName('contourLevel')[0].textContent!);
  61. return contourLevel;
  62. }
  63. export async function getContourLevelPdbe(plugin: PluginContext, taskCtx: RuntimeContext, emdbId: string) {
  64. // TODO: parametrize URL in plugin settings?
  65. emdbId = emdbId.toUpperCase();
  66. const header = await plugin.fetch({ url: `https://www.ebi.ac.uk/emdb/api/entry/map/${emdbId}`, type: 'json' }).runInContext(taskCtx);
  67. const contours = header?.map?.contour_list?.contour;
  68. if (!contours || contours.length === 0) {
  69. // try fallback to the old API
  70. return getContourLevelPdbeLegacy(plugin, taskCtx, emdbId);
  71. }
  72. return contours.find((c: any) => c.primary)?.level ?? contours[0].level;
  73. }
  74. async function getContourLevelPdbeLegacy(plugin: PluginContext, taskCtx: RuntimeContext, emdbId: string) {
  75. // TODO: parametrize URL in plugin settings?
  76. emdbId = emdbId.toUpperCase();
  77. const header = await plugin.fetch({ url: `https://www.ebi.ac.uk/pdbe/api/emdb/entry/map/${emdbId}`, type: 'json' }).runInContext(taskCtx);
  78. const emdbEntry = header?.[emdbId];
  79. let contourLevel: number | undefined = void 0;
  80. if (emdbEntry?.[0]?.map?.contour_level?.value !== void 0) {
  81. contourLevel = +emdbEntry[0].map.contour_level.value;
  82. }
  83. return contourLevel;
  84. }
  85. export async function getEmdbIds(plugin: PluginContext, taskCtx: RuntimeContext, pdbId: string) {
  86. // TODO: parametrize to a differnt URL? in plugin settings perhaps
  87. const summary = await plugin.fetch({ url: `https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/${pdbId}`, type: 'json' }).runInContext(taskCtx);
  88. const summaryEntry = summary?.[pdbId];
  89. let emdbIds: string[] = [];
  90. if (summaryEntry?.[0]?.related_structures) {
  91. const emdb = summaryEntry[0].related_structures.filter((s: any) => s.resource === 'EMDB' && s.relationship === 'associated EM volume');
  92. if (!emdb.length) {
  93. throw new Error(`No related EMDB entry found for '${pdbId}'.`);
  94. }
  95. emdbIds.push(...emdb.map((e: { accession: string }) => e.accession));
  96. } else {
  97. throw new Error(`No related EMDB entry found for '${pdbId}'.`);
  98. }
  99. return emdbIds;
  100. }