structure-focus-representation.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { InteractionsRepresentationProvider } from '../../../../mol-model-props/computed/representations/interactions';
  8. import { InteractionTypeColorThemeProvider } from '../../../../mol-model-props/computed/themes/interaction-type';
  9. import { StructureElement } from '../../../../mol-model/structure';
  10. import { createStructureRepresentationParams } from '../../../../mol-plugin-state/helpers/structure-representation-params';
  11. import { PluginStateObject } from '../../../../mol-plugin-state/objects';
  12. import { StateTransforms } from '../../../../mol-plugin-state/transforms';
  13. import { PluginBehavior } from '../../../behavior';
  14. import { MolScriptBuilder as MS } from '../../../../mol-script/language/builder';
  15. import { StateObjectCell, StateSelection, StateTransform } from '../../../../mol-state';
  16. import { SizeTheme } from '../../../../mol-theme/size';
  17. import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
  18. import { PluginCommands } from '../../../commands';
  19. import { PluginContext } from '../../../context';
  20. const StructureFocusRepresentationParams = (plugin: PluginContext) => {
  21. const reprParams = StateTransforms.Representation.StructureRepresentation3D.definition.params!(void 0, plugin) as PD.Params;
  22. return {
  23. expandRadius: PD.Numeric(5, { min: 1, max: 10, step: 1 }),
  24. targetParams: PD.Group(reprParams, {
  25. label: 'Target',
  26. customDefault: createStructureRepresentationParams(plugin, void 0, { type: 'ball-and-stick', size: 'physical', typeParams: { sizeFactor: 0.26, alpha: 0.51 } })
  27. }),
  28. surroundingsParams: PD.Group(reprParams, {
  29. label: 'Surroundings',
  30. customDefault: createStructureRepresentationParams(plugin, void 0, { type: 'ball-and-stick', color: 'element-symbol', size: 'physical', typeParams: { sizeFactor: 0.16 } })
  31. }),
  32. nciParams: PD.Group(reprParams, {
  33. label: 'Non-covalent Int.',
  34. customDefault: createStructureRepresentationParams(plugin, void 0, {
  35. type: InteractionsRepresentationProvider,
  36. color: InteractionTypeColorThemeProvider,
  37. size: SizeTheme.BuiltIn.uniform
  38. })
  39. }),
  40. components: PD.MultiSelect(FocusComponents, PD.arrayToOptions(FocusComponents))
  41. };
  42. };
  43. const FocusComponents = ['target' as const, 'surroundings' as const, 'interactions' as const];
  44. type StructureFocusRepresentationProps = PD.ValuesFor<ReturnType<typeof StructureFocusRepresentationParams>>
  45. export enum StructureFocusRepresentationTags {
  46. TargetSel = 'structure-focus-target-sel',
  47. TargetRepr = 'structure-focus-target-repr',
  48. SurrSel = 'structure-focus-surr-sel',
  49. SurrRepr = 'structure-focus-surr-repr',
  50. SurrNciRepr = 'structure-focus-surr-nci-repr'
  51. }
  52. const TagSet: Set<StructureFocusRepresentationTags> = new Set([StructureFocusRepresentationTags.TargetSel, StructureFocusRepresentationTags.TargetRepr, StructureFocusRepresentationTags.SurrSel, StructureFocusRepresentationTags.SurrRepr, StructureFocusRepresentationTags.SurrNciRepr]);
  53. export class StructureFocusRepresentationBehavior extends PluginBehavior.WithSubscribers<StructureFocusRepresentationProps> {
  54. private get surrLabel() { return `[Focus] Surroundings (${this.params.expandRadius} Å)`; }
  55. private ensureShape(cell: StateObjectCell<PluginStateObject.Molecule.Structure>) {
  56. const state = this.plugin.state.data, tree = state.tree;
  57. const builder = state.build();
  58. const refs = StateSelection.findUniqueTagsInSubtree(tree, cell.transform.ref, TagSet);
  59. // Selections
  60. if (!refs[StructureFocusRepresentationTags.TargetSel]) {
  61. refs[StructureFocusRepresentationTags.TargetSel] = builder
  62. .to(cell)
  63. .apply(StateTransforms.Model.StructureSelectionFromBundle,
  64. { bundle: StructureElement.Bundle.Empty, label: '[Focus] Target' }, { tags: StructureFocusRepresentationTags.TargetSel }).ref;
  65. }
  66. if (!refs[StructureFocusRepresentationTags.SurrSel]) {
  67. refs[StructureFocusRepresentationTags.SurrSel] = builder
  68. .to(cell)
  69. .apply(StateTransforms.Model.StructureSelectionFromExpression,
  70. { expression: MS.struct.generator.empty(), label: this.surrLabel }, { tags: StructureFocusRepresentationTags.SurrSel }).ref;
  71. }
  72. const components = this.params.components;
  73. // Representations
  74. if (components.indexOf('target') >= 0 && !refs[StructureFocusRepresentationTags.TargetRepr]) {
  75. refs[StructureFocusRepresentationTags.TargetRepr] = builder
  76. .to(refs[StructureFocusRepresentationTags.TargetSel]!)
  77. .apply(StateTransforms.Representation.StructureRepresentation3D, this.params.targetParams, { tags: StructureFocusRepresentationTags.TargetRepr }).ref;
  78. }
  79. if (components.indexOf('surroundings') >= 0 && !refs[StructureFocusRepresentationTags.SurrRepr]) {
  80. refs[StructureFocusRepresentationTags.SurrRepr] = builder
  81. .to(refs[StructureFocusRepresentationTags.SurrSel]!)
  82. .apply(StateTransforms.Representation.StructureRepresentation3D, this.params.surroundingsParams, { tags: StructureFocusRepresentationTags.SurrRepr }).ref;
  83. }
  84. if (components.indexOf('interactions') >= 0 && !refs[StructureFocusRepresentationTags.SurrNciRepr]) {
  85. refs[StructureFocusRepresentationTags.SurrNciRepr] = builder
  86. .to(refs[StructureFocusRepresentationTags.SurrSel]!)
  87. .apply(StateTransforms.Representation.StructureRepresentation3D, this.params.nciParams, { tags: StructureFocusRepresentationTags.SurrNciRepr }).ref;
  88. }
  89. return { state, builder, refs };
  90. }
  91. private clear(root: StateTransform.Ref) {
  92. const state = this.plugin.state.data;
  93. const foci = state.select(StateSelection.Generators.byRef(root).subtree().withTag(StructureFocusRepresentationTags.TargetSel));
  94. const surrs = state.select(StateSelection.Generators.byRef(root).subtree().withTag(StructureFocusRepresentationTags.SurrSel));
  95. if (foci.length === 0 && surrs.length === 0) return;
  96. const update = state.build();
  97. const bundle = StructureElement.Bundle.Empty;
  98. for (const f of foci) {
  99. update.to(f).update(StateTransforms.Model.StructureSelectionFromBundle, old => ({ ...old, bundle }));
  100. }
  101. const expression = MS.struct.generator.empty();
  102. for (const s of surrs) {
  103. update.to(s).update(StateTransforms.Model.StructureSelectionFromExpression, old => ({ ...old, expression }));
  104. }
  105. return PluginCommands.State.Update(this.plugin, { state, tree: update, options: { doNotLogTiming: true, doNotUpdateCurrent: true } });
  106. }
  107. private async focus(sourceLoci: StructureElement.Loci) {
  108. const parent = this.plugin.helpers.substructureParent.get(sourceLoci.structure);
  109. if (!parent || !parent.obj) return;
  110. const loci = StructureElement.Loci.remap(sourceLoci, parent.obj!.data);
  111. const residueLoci = StructureElement.Loci.extendToWholeResidues(loci);
  112. const residueBundle = StructureElement.Bundle.fromLoci(residueLoci);
  113. const surroundings = MS.struct.modifier.includeSurroundings({
  114. 0: StructureElement.Bundle.toExpression(residueBundle),
  115. radius: this.params.expandRadius,
  116. 'as-whole-residues': true
  117. });
  118. const { state, builder, refs } = this.ensureShape(parent);
  119. builder.to(refs[StructureFocusRepresentationTags.TargetSel]!).update(StateTransforms.Model.StructureSelectionFromBundle, old => ({ ...old, bundle: residueBundle }));
  120. builder.to(refs[StructureFocusRepresentationTags.SurrSel]!).update(StateTransforms.Model.StructureSelectionFromExpression, old => ({ ...old, expression: surroundings, label: this.surrLabel }));
  121. await PluginCommands.State.Update(this.plugin, { state, tree: builder, options: { doNotLogTiming: true, doNotUpdateCurrent: true } });
  122. }
  123. register(ref: string): void {
  124. this.subscribeObservable(this.plugin.managers.structure.focus.behaviors.current, (entry) => {
  125. if (entry) this.focus(entry.loci);
  126. else this.clear(StateTransform.RootRef);
  127. });
  128. }
  129. async update(params: StructureFocusRepresentationProps) {
  130. const old = this.params;
  131. this.params = params;
  132. const state = this.plugin.state.data;
  133. const builder = state.build();
  134. const all = StateSelection.Generators.root.subtree();
  135. const components = this.params.components;
  136. // TODO: create component if previously didnt exist
  137. let hasComponent = components.indexOf('target') >= 0;
  138. for (const repr of state.select(all.withTag(StructureFocusRepresentationTags.TargetRepr))) {
  139. if (!hasComponent) builder.delete(repr.transform.ref);
  140. else builder.to(repr).update(this.params.targetParams);
  141. }
  142. hasComponent = components.indexOf('surroundings') >= 0;
  143. for (const repr of state.select(all.withTag(StructureFocusRepresentationTags.SurrRepr))) {
  144. if (!hasComponent) builder.delete(repr.transform.ref);
  145. else builder.to(repr).update(this.params.surroundingsParams);
  146. }
  147. hasComponent = components.indexOf('interactions') >= 0;
  148. for (const repr of state.select(all.withTag(StructureFocusRepresentationTags.SurrNciRepr))) {
  149. if (!hasComponent) builder.delete(repr.transform.ref);
  150. else builder.to(repr).update(this.params.nciParams);
  151. }
  152. await PluginCommands.State.Update(this.plugin, { state, tree: builder, options: { doNotLogTiming: true, doNotUpdateCurrent: true } });
  153. // TODO: update properly
  154. if (params.expandRadius !== old.expandRadius) await this.clear(StateTransform.RootRef);
  155. return true;
  156. }
  157. }
  158. export const StructureFocusRepresentation = PluginBehavior.create({
  159. name: 'create-structure-focus-representation',
  160. display: { name: 'Structure Focus Representation' },
  161. category: 'interaction',
  162. ctor: StructureFocusRepresentationBehavior,
  163. params: (_, plugin) => StructureFocusRepresentationParams(plugin)
  164. });