helpers.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { ModelSymmetry } from '../../mol-model-formats/structure/property/symmetry';
  7. import { Model, ResidueIndex } from '../../mol-model/structure';
  8. import { PluginContext } from '../../mol-plugin/context';
  9. import { StructureRepresentationRegistry } from '../../mol-repr/structure/registry';
  10. import { ColorTheme } from '../../mol-theme/color';
  11. export interface ModelInfo {
  12. hetResidues: { name: string, indices: ResidueIndex[] }[],
  13. assemblies: { id: string, details: string, isPreferred: boolean }[],
  14. preferredAssemblyId: string | undefined
  15. }
  16. export namespace ModelInfo {
  17. async function getPreferredAssembly(ctx: PluginContext, model: Model) {
  18. if (model.entryId.length <= 3) return void 0;
  19. try {
  20. const id = model.entryId.toLowerCase();
  21. const src = await ctx.runTask(ctx.fetch({ url: `https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/${id}` })) as string;
  22. const json = JSON.parse(src);
  23. const data = json && json[id];
  24. const assemblies = data[0] && data[0].assemblies;
  25. if (!assemblies || !assemblies.length) return void 0;
  26. for (const asm of assemblies) {
  27. if (asm.preferred) {
  28. return asm.assembly_id;
  29. }
  30. }
  31. return void 0;
  32. } catch (e) {
  33. console.warn('getPreferredAssembly', e);
  34. }
  35. }
  36. export async function get(ctx: PluginContext, model: Model, checkPreferred: boolean): Promise<ModelInfo> {
  37. const { _rowCount: residueCount } = model.atomicHierarchy.residues;
  38. const { offsets: residueOffsets } = model.atomicHierarchy.residueAtomSegments;
  39. const chainIndex = model.atomicHierarchy.chainAtomSegments.index;
  40. // const resn = SP.residue.label_comp_id, entType = SP.entity.type;
  41. const pref = checkPreferred
  42. ? getPreferredAssembly(ctx, model)
  43. : void 0;
  44. const hetResidues: ModelInfo['hetResidues'] = [];
  45. const hetMap = new Map<string, ModelInfo['hetResidues'][0]>();
  46. for (let rI = 0 as ResidueIndex; rI < residueCount; rI++) {
  47. const cI = chainIndex[residueOffsets[rI]];
  48. const eI = model.atomicHierarchy.index.getEntityFromChain(cI);
  49. const entityType = model.entities.data.type.value(eI);
  50. if (entityType !== 'non-polymer' && entityType !== 'branched') continue;
  51. const comp_id = model.atomicHierarchy.residues.label_comp_id.value(rI);
  52. let lig = hetMap.get(comp_id);
  53. if (!lig) {
  54. lig = { name: comp_id, indices: [] };
  55. hetResidues.push(lig);
  56. hetMap.set(comp_id, lig);
  57. }
  58. lig.indices.push(rI);
  59. }
  60. const preferredAssemblyId = await pref;
  61. const symmetry = ModelSymmetry.Provider.get(model);
  62. return {
  63. hetResidues: hetResidues,
  64. assemblies: symmetry ? symmetry.assemblies.map(a => ({ id: a.id, details: a.details, isPreferred: a.id === preferredAssemblyId })) : [],
  65. preferredAssemblyId
  66. };
  67. }
  68. }
  69. export type SupportedFormats = 'cif' | 'pdb'
  70. export interface LoadParams {
  71. url: string,
  72. format?: SupportedFormats,
  73. assemblyId?: string,
  74. representationStyle?: RepresentationStyle
  75. }
  76. export interface RepresentationStyle {
  77. sequence?: RepresentationStyle.Entry,
  78. hetGroups?: RepresentationStyle.Entry,
  79. snfg3d?: { hide?: boolean },
  80. water?: RepresentationStyle.Entry
  81. }
  82. export namespace RepresentationStyle {
  83. export type Entry = { hide?: boolean, kind?: StructureRepresentationRegistry.BuiltIn, coloring?: ColorTheme.BuiltIn }
  84. }
  85. export enum StateElements {
  86. Model = 'model',
  87. ModelProps = 'model-props',
  88. Assembly = 'assembly',
  89. VolumeStreaming = 'volume-streaming',
  90. Sequence = 'sequence',
  91. SequenceVisual = 'sequence-visual',
  92. Het = 'het',
  93. HetVisual = 'het-visual',
  94. Het3DSNFG = 'het-3dsnfg',
  95. Water = 'water',
  96. WaterVisual = 'water-visual',
  97. HetGroupFocus = 'het-group-focus',
  98. HetGroupFocusGroup = 'het-group-focus-group'
  99. }