viewport.tsx 13 KB

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