viewport.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. */
  6. import * as React from 'react';
  7. import { PluginUIComponent } from '../../mol-plugin-ui/base';
  8. import { Viewport, ViewportControls } from '../../mol-plugin-ui/viewport';
  9. import { BackgroundTaskProgress } from '../../mol-plugin-ui/task';
  10. import { LociLabels } from '../../mol-plugin-ui/controls';
  11. import { Toasts } from '../../mol-plugin-ui/toast';
  12. import { Button } from '../../mol-plugin-ui/controls/common';
  13. import { StructureRepresentationPresetProvider, presetStaticComponent } from '../../mol-plugin-state/builder/structure/representation-preset';
  14. import { StateObjectRef } from '../../mol-state';
  15. import { StructureSelectionQueries, StructureSelectionQuery } from '../../mol-plugin-state/helpers/structure-selection-query';
  16. import { MolScriptBuilder as MS } from '../../mol-script/language/builder';
  17. import { InteractionsRepresentationProvider } from '../../mol-model-props/computed/representations/interactions';
  18. import { InteractionTypeColorThemeProvider } from '../../mol-model-props/computed/themes/interaction-type';
  19. import { compile } from '../../mol-script/runtime/query/compiler';
  20. import { StructureSelection, QueryContext, Structure } from '../../mol-model/structure';
  21. import { PluginCommands } from '../../mol-plugin/commands';
  22. import { PluginContext } from '../../mol-plugin/context';
  23. function shinyStyle(plugin: PluginContext) {
  24. return PluginCommands.Canvas3D.SetSettings(plugin, { settings: {
  25. renderer: {
  26. ...plugin.canvas3d!.props.renderer,
  27. style: { name: 'plastic', params: {} },
  28. },
  29. postprocessing: {
  30. ...plugin.canvas3d!.props.postprocessing,
  31. occlusion: { name: 'off', params: {} },
  32. outline: { name: 'off', params: {} }
  33. }
  34. } });
  35. }
  36. function occlusionStyle(plugin: PluginContext) {
  37. return PluginCommands.Canvas3D.SetSettings(plugin, { settings: {
  38. renderer: {
  39. ...plugin.canvas3d!.props.renderer,
  40. style: { name: 'flat', params: {} }
  41. },
  42. postprocessing: {
  43. ...plugin.canvas3d!.props.postprocessing,
  44. occlusion: { name: 'on', params: {
  45. kernelSize: 8,
  46. bias: 0.8,
  47. radius: 64
  48. } },
  49. outline: { name: 'on', params: {
  50. scale: 1.0,
  51. threshold: 0.8
  52. } }
  53. }
  54. } });
  55. }
  56. const ligandPlusSurroundings = StructureSelectionQuery('Surrounding Residues (5 \u212B) of Ligand plus Ligand itself', MS.struct.modifier.union([
  57. MS.struct.modifier.includeSurroundings({
  58. 0: StructureSelectionQueries.ligand.expression,
  59. radius: 5,
  60. 'as-whole-residues': true
  61. })
  62. ]));
  63. const ligandSurroundings = StructureSelectionQuery('Surrounding Residues (5 \u212B) of Ligand', MS.struct.modifier.union([
  64. MS.struct.modifier.exceptBy({
  65. 0: ligandPlusSurroundings.expression,
  66. by: StructureSelectionQueries.ligand.expression
  67. })
  68. ]));
  69. const PresetParams = {
  70. ...StructureRepresentationPresetProvider.CommonParams,
  71. };
  72. export const StructurePreset = StructureRepresentationPresetProvider({
  73. id: 'preset-structure',
  74. display: { name: 'Structure' },
  75. params: () => PresetParams,
  76. async apply(ref, params, plugin) {
  77. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  78. if (!structureCell) return {};
  79. const components = {
  80. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  81. polymer: await presetStaticComponent(plugin, structureCell, 'polymer'),
  82. };
  83. const { update, builder, typeParams, color } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  84. const representations = {
  85. ligand: builder.buildRepresentation(update, components.ligand, { type: 'ball-and-stick', typeParams: { ...typeParams, sizeFactor: 0.26 }, color }, { tag: 'ligand' }),
  86. polymer: builder.buildRepresentation(update, components.polymer, { type: 'cartoon', typeParams: { ...typeParams }, color }, { tag: 'polymer' }),
  87. };
  88. await update.commit({ revertOnError: true });
  89. await shinyStyle(plugin);
  90. plugin.managers.interactivity.setProps({ granularity: 'residue' });
  91. return { components, representations };
  92. }
  93. });
  94. export const IllustrativePreset = StructureRepresentationPresetProvider({
  95. id: 'preset-illustrative',
  96. display: { name: 'Illustrative' },
  97. params: () => PresetParams,
  98. async apply(ref, params, plugin) {
  99. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  100. if (!structureCell) return {};
  101. const components = {
  102. all: await presetStaticComponent(plugin, structureCell, 'all')
  103. };
  104. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  105. const representations = {
  106. all: builder.buildRepresentation(update, components.all, { type: 'spacefill', typeParams: { ...typeParams }, color: 'illustrative' }, { tag: 'all' }),
  107. };
  108. await update.commit({ revertOnError: true });
  109. await occlusionStyle(plugin);
  110. plugin.managers.interactivity.setProps({ granularity: 'residue' });
  111. return { components, representations };
  112. }
  113. });
  114. const PocketPreset = StructureRepresentationPresetProvider({
  115. id: 'preset-pocket',
  116. display: { name: 'Pocket' },
  117. params: () => PresetParams,
  118. async apply(ref, params, plugin) {
  119. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  120. const structure = structureCell?.obj?.data;
  121. if (!structureCell || !structure) return {};
  122. const components = {
  123. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  124. surroundings: await plugin.builders.structure.tryCreateComponentFromSelection(structureCell, ligandSurroundings, `surroundings`),
  125. };
  126. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  127. const representations = {
  128. ligand: builder.buildRepresentation(update, components.ligand, { type: 'ball-and-stick', typeParams: { ...typeParams, sizeFactor: 0.26 }, color: 'partial-charge' }, { tag: 'ligand' }),
  129. surroundings: builder.buildRepresentation(update, components.surroundings, { type: 'molecular-surface', typeParams: { ...typeParams, includeParent: true, quality: 'custom', resolution: 0.2, doubleSided: true }, color: 'partial-charge' }, { tag: 'surroundings' }),
  130. };
  131. await update.commit({ revertOnError: true });
  132. await shinyStyle(plugin);
  133. plugin.managers.interactivity.setProps({ granularity: 'element' });
  134. const compiled = compile<StructureSelection>(StructureSelectionQueries.ligand.expression);
  135. const result = compiled(new QueryContext(structure));
  136. const selection = StructureSelection.unionStructure(result);
  137. plugin.managers.camera.focusLoci(Structure.toStructureElementLoci(selection));
  138. return { components, representations };
  139. }
  140. });
  141. const InteractionsPreset = StructureRepresentationPresetProvider({
  142. id: 'preset-interactions',
  143. display: { name: 'Interactions' },
  144. params: () => PresetParams,
  145. async apply(ref, params, plugin) {
  146. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  147. const structure = structureCell?.obj?.data;
  148. if (!structureCell || !structure) return {};
  149. const components = {
  150. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  151. selection: await plugin.builders.structure.tryCreateComponentFromSelection(structureCell, ligandPlusSurroundings, `selection`)
  152. };
  153. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  154. const representations = {
  155. ligand: builder.buildRepresentation(update, components.ligand, { type: 'ball-and-stick', typeParams: { ...typeParams, sizeFactor: 0.26 }, color: 'partial-charge' }, { tag: 'ligand' }),
  156. ballAndStick: builder.buildRepresentation(update, components.selection, { type: 'ball-and-stick', typeParams: { ...typeParams, sizeFactor: 0.1, sizeAspectRatio: 1 }, color: 'partial-charge' }, { tag: 'ball-and-stick' }),
  157. interactions: builder.buildRepresentation(update, components.selection, { type: InteractionsRepresentationProvider, typeParams: { ...typeParams }, color: InteractionTypeColorThemeProvider }, { tag: 'interactions' }),
  158. };
  159. await update.commit({ revertOnError: true });
  160. await shinyStyle(plugin);
  161. plugin.managers.interactivity.setProps({ granularity: 'element' });
  162. const compiled = compile<StructureSelection>(StructureSelectionQueries.ligand.expression);
  163. const result = compiled(new QueryContext(structure));
  164. const selection = StructureSelection.unionStructure(result);
  165. plugin.managers.camera.focusLoci(Structure.toStructureElementLoci(selection));
  166. return { components, representations };
  167. }
  168. });
  169. export class ViewportComponent extends PluginUIComponent {
  170. structurePreset = () => {
  171. this.plugin.managers.structure.component.applyPreset(
  172. this.plugin.managers.structure.hierarchy.selection.structures,
  173. StructurePreset
  174. );
  175. }
  176. illustrativePreset = () => {
  177. this.plugin.managers.structure.component.applyPreset(
  178. this.plugin.managers.structure.hierarchy.selection.structures,
  179. IllustrativePreset
  180. );
  181. }
  182. pocketPreset = () => {
  183. this.plugin.managers.structure.component.applyPreset(
  184. this.plugin.managers.structure.hierarchy.selection.structures,
  185. PocketPreset
  186. );
  187. }
  188. interactionsPreset = () => {
  189. this.plugin.managers.structure.component.applyPreset(
  190. this.plugin.managers.structure.hierarchy.selection.structures,
  191. InteractionsPreset
  192. );
  193. }
  194. render() {
  195. const VPControls = this.plugin.spec.components?.viewport?.controls || ViewportControls;
  196. return <>
  197. <Viewport />
  198. <div className='msp-viewport-top-left-controls'>
  199. <div style={{ marginBottom: '4px' }}>
  200. <Button onClick={this.structurePreset} >Structure</Button>
  201. </div>
  202. <div style={{ marginBottom: '4px' }}>
  203. <Button onClick={this.illustrativePreset}>Illustrative</Button>
  204. </div>
  205. <div style={{ marginBottom: '4px' }}>
  206. <Button onClick={this.pocketPreset}>Pocket</Button>
  207. </div>
  208. <div style={{ marginBottom: '4px' }}>
  209. <Button onClick={this.interactionsPreset}>Interactions</Button>
  210. </div>
  211. </div>
  212. <VPControls />
  213. <BackgroundTaskProgress />
  214. <div className='msp-highlight-toast-wrapper'>
  215. <LociLabels />
  216. <Toasts />
  217. </div>
  218. </>;
  219. }
  220. }