behavior.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Sebastian Bittrich <sebastian.bittrich@rcsb.org>
  6. */
  7. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  8. import { StructureRepresentationPresetProvider, PresetStructureRepresentations } from '../../mol-plugin-state/builder/structure/representation-preset';
  9. import { MembraneOrientationProvider, MembraneOrientation } from './prop';
  10. import { StateObjectRef, StateTransformer, StateTransform } from '../../mol-state';
  11. import { Task } from '../../mol-task';
  12. import { PluginBehavior } from '../../mol-plugin/behavior';
  13. import { MembraneOrientationRepresentationProvider, MembraneOrientationParams, MembraneOrientationRepresentation } from './representation';
  14. import { HydrophobicityColorThemeProvider } from '../../mol-theme/color/hydrophobicity';
  15. import { PluginStateObject, PluginStateTransform } from '../../mol-plugin-state/objects';
  16. import { PluginContext } from '../../mol-plugin/context';
  17. import { DefaultQueryRuntimeTable } from '../../mol-script/runtime/query/compiler';
  18. import { StructureSelectionQuery, StructureSelectionCategory } from '../../mol-plugin-state/helpers/structure-selection-query';
  19. import { MolScriptBuilder as MS } from '../../mol-script/language/builder';
  20. import { GenericRepresentationRef } from '../../mol-plugin-state/manager/structure/hierarchy-state';
  21. const Tag = MembraneOrientation.Tag;
  22. export const ANVILMembraneOrientation = PluginBehavior.create<{ autoAttach: boolean }>({
  23. name: 'anvil-membrane-orientation-prop',
  24. category: 'custom-props',
  25. display: {
  26. name: 'Membrane Orientation',
  27. description: 'Data calculated with ANVIL algorithm.'
  28. },
  29. ctor: class extends PluginBehavior.Handler<{ autoAttach: boolean }> {
  30. private provider = MembraneOrientationProvider
  31. register(): void {
  32. DefaultQueryRuntimeTable.addCustomProp(this.provider.descriptor);
  33. this.ctx.customStructureProperties.register(this.provider, this.params.autoAttach);
  34. this.ctx.representation.structure.registry.add(MembraneOrientationRepresentationProvider);
  35. this.ctx.query.structure.registry.add(isTransmembrane);
  36. this.ctx.genericRepresentationControls.set(Tag.Representation, selection => {
  37. const refs: GenericRepresentationRef[] = [];
  38. selection.structures.forEach(structure => {
  39. const memRepr = structure.genericRepresentations?.filter(r => r.cell.transform.transformer.id === MembraneOrientation3D.id)[0];
  40. if (memRepr) refs.push(memRepr);
  41. });
  42. return [refs, 'Membrane Orientation'];
  43. });
  44. this.ctx.builders.structure.representation.registerPreset(MembraneOrientationPreset);
  45. }
  46. update(p: { autoAttach: boolean }) {
  47. let updated = this.params.autoAttach !== p.autoAttach;
  48. this.params.autoAttach = p.autoAttach;
  49. this.ctx.customStructureProperties.setDefaultAutoAttach(this.provider.descriptor.name, this.params.autoAttach);
  50. return updated;
  51. }
  52. unregister() {
  53. DefaultQueryRuntimeTable.removeCustomProp(this.provider.descriptor);
  54. this.ctx.customStructureProperties.unregister(this.provider.descriptor.name);
  55. this.ctx.representation.structure.registry.remove(MembraneOrientationRepresentationProvider);
  56. this.ctx.query.structure.registry.remove(isTransmembrane);
  57. this.ctx.genericRepresentationControls.delete(Tag.Representation);
  58. this.ctx.builders.structure.representation.unregisterPreset(MembraneOrientationPreset);
  59. }
  60. },
  61. params: () => ({
  62. autoAttach: PD.Boolean(false)
  63. })
  64. });
  65. //
  66. export const isTransmembrane = StructureSelectionQuery('Residues Embedded in Membrane', MS.struct.modifier.union([
  67. MS.struct.modifier.wholeResidues([
  68. MS.struct.modifier.union([
  69. MS.struct.generator.atomGroups({
  70. 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
  71. 'atom-test': MembraneOrientation.symbols.isTransmembrane.symbol(),
  72. })
  73. ])
  74. ])
  75. ]), {
  76. description: 'Select residues that are embedded between the membrane layers.',
  77. category: StructureSelectionCategory.Residue,
  78. ensureCustomProperties: (ctx, structure) => {
  79. return MembraneOrientationProvider.attach(ctx, structure);
  80. }
  81. });
  82. //
  83. export { MembraneOrientation3D };
  84. type MembraneOrientation3D = typeof MembraneOrientation3D
  85. const MembraneOrientation3D = PluginStateTransform.BuiltIn({
  86. name: 'membrane-orientation-3d',
  87. display: {
  88. name: 'Membrane Orientation',
  89. description: 'Membrane Orientation planes and rims. Data calculated with ANVIL algorithm.'
  90. },
  91. from: PluginStateObject.Molecule.Structure,
  92. to: PluginStateObject.Shape.Representation3D,
  93. params: (a) => {
  94. return {
  95. ...MembraneOrientationParams,
  96. };
  97. }
  98. })({
  99. canAutoUpdate({ oldParams, newParams }) {
  100. return true;
  101. },
  102. apply({ a, params }, plugin: PluginContext) {
  103. return Task.create('Membrane Orientation', async ctx => {
  104. await MembraneOrientationProvider.attach({ runtime: ctx, assetManager: plugin.managers.asset }, a.data);
  105. const repr = MembraneOrientationRepresentation({ webgl: plugin.canvas3d?.webgl, ...plugin.representation.structure.themes }, () => MembraneOrientationParams);
  106. await repr.createOrUpdate(params, a.data).runInContext(ctx);
  107. return new PluginStateObject.Shape.Representation3D({ repr, sourceData: a.data }, { label: 'Membrane Orientation' });
  108. });
  109. },
  110. update({ a, b, newParams }, plugin: PluginContext) {
  111. return Task.create('Membrane Orientation', async ctx => {
  112. await MembraneOrientationProvider.attach({ runtime: ctx, assetManager: plugin.managers.asset }, a.data);
  113. const props = { ...b.data.repr.props, ...newParams };
  114. await b.data.repr.createOrUpdate(props, a.data).runInContext(ctx);
  115. b.data.sourceData = a.data;
  116. return StateTransformer.UpdateResult.Updated;
  117. });
  118. },
  119. isApplicable(a) {
  120. return MembraneOrientationProvider.isApplicable(a.data);
  121. }
  122. });
  123. export const MembraneOrientationPreset = StructureRepresentationPresetProvider({
  124. id: 'preset-membrane-orientation',
  125. display: {
  126. name: 'Membrane Orientation', group: 'Annotation',
  127. description: 'Shows orientation of membrane layers. Data calculated with ANVIL algorithm.' // TODO add ' or obtained via RCSB PDB'
  128. },
  129. isApplicable(a) {
  130. return MembraneOrientationProvider.isApplicable(a.data);
  131. },
  132. params: () => StructureRepresentationPresetProvider.CommonParams,
  133. async apply(ref, params, plugin) {
  134. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  135. const structure = structureCell?.obj?.data;
  136. if (!structureCell || !structure) return {};
  137. if (!MembraneOrientationProvider.get(structure).value) {
  138. await plugin.runTask(Task.create('Membrane Orientation', async runtime => {
  139. await MembraneOrientationProvider.attach({ runtime, assetManager: plugin.managers.asset }, structure);
  140. }));
  141. }
  142. const membraneOrientation = await tryCreateMembraneOrientation(plugin, structureCell);
  143. const colorTheme = HydrophobicityColorThemeProvider.name as any;
  144. const preset = await PresetStructureRepresentations.auto.apply(ref, { ...params, theme: { globalName: colorTheme, focus: { name: colorTheme } } }, plugin);
  145. return { components: preset.components, representations: { ...preset.representations, membraneOrientation } };
  146. }
  147. });
  148. export function tryCreateMembraneOrientation(plugin: PluginContext, structure: StateObjectRef<PluginStateObject.Molecule.Structure>, params?: StateTransformer.Params<MembraneOrientation3D>, initialState?: Partial<StateTransform.State>) {
  149. const state = plugin.state.data;
  150. const membraneOrientation = state.build().to(structure)
  151. .applyOrUpdateTagged('membrane-orientation-3d', MembraneOrientation3D, params, { state: initialState });
  152. return membraneOrientation.commit({ revertOnError: true });
  153. }