viewport.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { InteractionsRepresentationProvider } from '../../mol-model-props/computed/representations/interactions';
  7. import { InteractionTypeColorThemeProvider } from '../../mol-model-props/computed/themes/interaction-type';
  8. import { presetStaticComponent, StructureRepresentationPresetProvider } from '../../mol-plugin-state/builder/structure/representation-preset';
  9. import { StructureSelectionQueries, StructureSelectionQuery } from '../../mol-plugin-state/helpers/structure-selection-query';
  10. import { StructureRef } from '../../mol-plugin-state/manager/structure/hierarchy-state';
  11. import { PluginUIComponent } from '../../mol-plugin-ui/base';
  12. import { LociLabels } from '../../mol-plugin-ui/controls';
  13. import { Button } from '../../mol-plugin-ui/controls/common';
  14. import { BackgroundTaskProgress } from '../../mol-plugin-ui/task';
  15. import { Toasts } from '../../mol-plugin-ui/toast';
  16. import { Viewport, ViewportControls } from '../../mol-plugin-ui/viewport';
  17. import { PluginCommands } from '../../mol-plugin/commands';
  18. import { PluginConfig } from '../../mol-plugin/config';
  19. import { PluginContext } from '../../mol-plugin/context';
  20. import { MolScriptBuilder as MS } from '../../mol-script/language/builder';
  21. import { StateObjectRef } from '../../mol-state';
  22. import { Color } from '../../mol-util/color';
  23. import { Material } from '../../mol-util/material';
  24. function shinyStyle(plugin: PluginContext) {
  25. return PluginCommands.Canvas3D.SetSettings(plugin, { settings: {
  26. renderer: {
  27. ...plugin.canvas3d!.props.renderer,
  28. },
  29. postprocessing: {
  30. ...plugin.canvas3d!.props.postprocessing,
  31. occlusion: { name: 'off', params: {} },
  32. shadow: { name: 'off', params: {} },
  33. outline: { name: 'off', params: {} },
  34. }
  35. } });
  36. }
  37. function occlusionStyle(plugin: PluginContext) {
  38. return PluginCommands.Canvas3D.SetSettings(plugin, { settings: {
  39. renderer: {
  40. ...plugin.canvas3d!.props.renderer,
  41. },
  42. postprocessing: {
  43. ...plugin.canvas3d!.props.postprocessing,
  44. occlusion: { name: 'on', params: {
  45. bias: 0.8,
  46. blurKernelSize: 15,
  47. radius: 5,
  48. samples: 32,
  49. resolutionScale: 1,
  50. } },
  51. outline: { name: 'on', params: {
  52. scale: 1.0,
  53. threshold: 0.33,
  54. color: Color(0x0000),
  55. } },
  56. shadow: { name: 'off', params: {} },
  57. }
  58. } });
  59. }
  60. const ligandPlusSurroundings = StructureSelectionQuery('Surrounding Residues (5 \u212B) of Ligand plus Ligand itself', MS.struct.modifier.union([
  61. MS.struct.modifier.includeSurroundings({
  62. 0: StructureSelectionQueries.ligand.expression,
  63. radius: 5,
  64. 'as-whole-residues': true
  65. })
  66. ]));
  67. const ligandSurroundings = StructureSelectionQuery('Surrounding Residues (5 \u212B) of Ligand', MS.struct.modifier.union([
  68. MS.struct.modifier.exceptBy({
  69. 0: ligandPlusSurroundings.expression,
  70. by: StructureSelectionQueries.ligand.expression
  71. })
  72. ]));
  73. const PresetParams = {
  74. ...StructureRepresentationPresetProvider.CommonParams,
  75. };
  76. const CustomMaterial = Material({ roughness: 0.2, metalness: 0 });
  77. export const StructurePreset = StructureRepresentationPresetProvider({
  78. id: 'preset-structure',
  79. display: { name: 'Structure' },
  80. params: () => PresetParams,
  81. async apply(ref, params, plugin) {
  82. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  83. if (!structureCell) return {};
  84. const components = {
  85. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  86. polymer: await presetStaticComponent(plugin, structureCell, 'polymer'),
  87. };
  88. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  89. const representations = {
  90. ligand: builder.buildRepresentation(update, components.ligand, { type: 'ball-and-stick', typeParams: { ...typeParams, material: CustomMaterial, sizeFactor: 0.35 }, color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } }, { tag: 'ligand' }),
  91. polymer: builder.buildRepresentation(update, components.polymer, { type: 'cartoon', typeParams: { ...typeParams, material: CustomMaterial }, color: 'chain-id', colorParams: { palette: (plugin.customState as any).colorPalette } }, { tag: 'polymer' }),
  92. };
  93. await update.commit({ revertOnError: true });
  94. await shinyStyle(plugin);
  95. plugin.managers.interactivity.setProps({ granularity: 'residue' });
  96. return { components, representations };
  97. }
  98. });
  99. export const IllustrativePreset = StructureRepresentationPresetProvider({
  100. id: 'preset-illustrative',
  101. display: { name: 'Illustrative' },
  102. params: () => PresetParams,
  103. async apply(ref, params, plugin) {
  104. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  105. if (!structureCell) return {};
  106. const components = {
  107. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  108. polymer: await presetStaticComponent(plugin, structureCell, 'polymer'),
  109. };
  110. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  111. const representations = {
  112. ligand: builder.buildRepresentation(update, components.ligand, { type: 'spacefill', typeParams: { ...typeParams, ignoreLight: true }, color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } }, { tag: 'ligand' }),
  113. polymer: builder.buildRepresentation(update, components.polymer, { type: 'spacefill', typeParams: { ...typeParams, ignoreLight: true }, color: 'illustrative', colorParams: { palette: (plugin.customState as any).colorPalette } }, { tag: 'polymer' }),
  114. };
  115. await update.commit({ revertOnError: true });
  116. await occlusionStyle(plugin);
  117. plugin.managers.interactivity.setProps({ granularity: 'residue' });
  118. return { components, representations };
  119. }
  120. });
  121. const SurfacePreset = StructureRepresentationPresetProvider({
  122. id: 'preset-surface',
  123. display: { name: 'Surface' },
  124. params: () => PresetParams,
  125. async apply(ref, params, plugin) {
  126. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  127. const structure = structureCell?.obj?.data;
  128. if (!structureCell || !structure) return {};
  129. const components = {
  130. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  131. polymer: await presetStaticComponent(plugin, structureCell, 'polymer'),
  132. };
  133. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  134. const representations = {
  135. ligand: builder.buildRepresentation(update, components.ligand, { type: 'ball-and-stick', typeParams: { ...typeParams, material: CustomMaterial, sizeFactor: 0.26 }, color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } }, { tag: 'ligand' }),
  136. polymer: builder.buildRepresentation(update, components.polymer, { type: 'molecular-surface', typeParams: { ...typeParams, material: CustomMaterial, quality: 'custom', resolution: 0.5, doubleSided: true }, color: 'partial-charge' }, { tag: 'polymer' }),
  137. };
  138. await update.commit({ revertOnError: true });
  139. await shinyStyle(plugin);
  140. plugin.managers.interactivity.setProps({ granularity: 'residue' });
  141. return { components, representations };
  142. }
  143. });
  144. const PocketPreset = StructureRepresentationPresetProvider({
  145. id: 'preset-pocket',
  146. display: { name: 'Pocket' },
  147. params: () => PresetParams,
  148. async apply(ref, params, plugin) {
  149. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  150. const structure = structureCell?.obj?.data;
  151. if (!structureCell || !structure) return {};
  152. const components = {
  153. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  154. surroundings: await plugin.builders.structure.tryCreateComponentFromSelection(structureCell, ligandSurroundings, `surroundings`),
  155. };
  156. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  157. const representations = {
  158. ligand: builder.buildRepresentation(update, components.ligand, { type: 'ball-and-stick', typeParams: { ...typeParams, material: CustomMaterial, sizeFactor: 0.26 }, color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } }, { tag: 'ligand' }),
  159. surroundings: builder.buildRepresentation(update, components.surroundings, { type: 'molecular-surface', typeParams: { ...typeParams, material: CustomMaterial, includeParent: true, quality: 'custom', resolution: 0.2, doubleSided: true }, color: 'partial-charge' }, { tag: 'surroundings' }),
  160. };
  161. await update.commit({ revertOnError: true });
  162. await shinyStyle(plugin);
  163. plugin.managers.interactivity.setProps({ granularity: 'element' });
  164. return { components, representations };
  165. }
  166. });
  167. const InteractionsPreset = StructureRepresentationPresetProvider({
  168. id: 'preset-interactions',
  169. display: { name: 'Interactions' },
  170. params: () => PresetParams,
  171. async apply(ref, params, plugin) {
  172. const structureCell = StateObjectRef.resolveAndCheck(plugin.state.data, ref);
  173. const structure = structureCell?.obj?.data;
  174. if (!structureCell || !structure) return {};
  175. const components = {
  176. ligand: await presetStaticComponent(plugin, structureCell, 'ligand'),
  177. surroundings: await plugin.builders.structure.tryCreateComponentFromSelection(structureCell, ligandSurroundings, `surroundings`),
  178. interactions: await presetStaticComponent(plugin, structureCell, 'ligand'),
  179. };
  180. const { update, builder, typeParams } = StructureRepresentationPresetProvider.reprBuilder(plugin, params);
  181. const representations = {
  182. ligand: builder.buildRepresentation(update, components.ligand, { type: 'ball-and-stick', typeParams: { ...typeParams, material: CustomMaterial, sizeFactor: 0.3 }, color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } }, { tag: 'ligand' }),
  183. ballAndStick: builder.buildRepresentation(update, components.surroundings, { type: 'ball-and-stick', typeParams: { ...typeParams, material: CustomMaterial, sizeFactor: 0.1, sizeAspectRatio: 1 }, color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } }, { tag: 'ball-and-stick' }),
  184. interactions: builder.buildRepresentation(update, components.interactions, { type: InteractionsRepresentationProvider, typeParams: { ...typeParams, material: CustomMaterial, includeParent: true, parentDisplay: 'between' }, color: InteractionTypeColorThemeProvider }, { tag: 'interactions' }),
  185. label: builder.buildRepresentation(update, components.surroundings, { type: 'label', typeParams: { ...typeParams, material: CustomMaterial, background: false, borderWidth: 0.1 }, color: 'uniform', colorParams: { value: Color(0x000000) } }, { tag: 'label' }),
  186. };
  187. await update.commit({ revertOnError: true });
  188. await shinyStyle(plugin);
  189. plugin.managers.interactivity.setProps({ granularity: 'element' });
  190. return { components, representations };
  191. }
  192. });
  193. export const ShowButtons = PluginConfig.item('showButtons', true);
  194. export class ViewportComponent extends PluginUIComponent {
  195. async _set(structures: readonly StructureRef[], preset: StructureRepresentationPresetProvider) {
  196. await this.plugin.managers.structure.component.clear(structures);
  197. await this.plugin.managers.structure.component.applyPreset(structures, preset);
  198. }
  199. set = async (preset: StructureRepresentationPresetProvider) => {
  200. await this._set(this.plugin.managers.structure.hierarchy.selection.structures, preset);
  201. };
  202. structurePreset = () => this.set(StructurePreset);
  203. illustrativePreset = () => this.set(IllustrativePreset);
  204. surfacePreset = () => this.set(SurfacePreset);
  205. pocketPreset = () => this.set(PocketPreset);
  206. interactionsPreset = () => this.set(InteractionsPreset);
  207. get showButtons() {
  208. return this.plugin.config.get(ShowButtons);
  209. }
  210. render() {
  211. const VPControls = this.plugin.spec.components?.viewport?.controls || ViewportControls;
  212. return <>
  213. <Viewport />
  214. {this.showButtons && <div className='msp-viewport-top-left-controls'>
  215. <div style={{ marginBottom: '4px' }}>
  216. <Button onClick={this.structurePreset} >Structure</Button>
  217. </div>
  218. <div style={{ marginBottom: '4px' }}>
  219. <Button onClick={this.illustrativePreset}>Illustrative</Button>
  220. </div>
  221. <div style={{ marginBottom: '4px' }}>
  222. <Button onClick={this.surfacePreset}>Surface</Button>
  223. </div>
  224. {/* <div style={{ marginBottom: '4px' }}>
  225. <Button onClick={this.pocketPreset}>Pocket</Button>
  226. </div> */}
  227. <div style={{ marginBottom: '4px' }}>
  228. <Button onClick={this.interactionsPreset}>Interactions</Button>
  229. </div>
  230. </div>}
  231. <VPControls />
  232. <BackgroundTaskProgress />
  233. <div className='msp-highlight-toast-wrapper'>
  234. <LociLabels />
  235. <Toasts />
  236. </div>
  237. </>;
  238. }
  239. }