index.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { BehaviorSubject } from 'rxjs';
  7. import { debounceTime, skip } from 'rxjs/operators';
  8. import { AlphaOrbital, Basis } from '../../extensions/alpha-orbitals/data-model';
  9. import { SphericalBasisOrder } from '../../extensions/alpha-orbitals/spherical-functions';
  10. import { BasisAndOrbitals, CreateOrbitalDensityVolume, CreateOrbitalRepresentation3D, CreateOrbitalVolume, StaticBasisAndOrbitals } from '../../extensions/alpha-orbitals/transforms';
  11. import { canComputeGrid3dOnGPU } from '../../mol-gl/compute/grid3d';
  12. import { PluginStateObject } from '../../mol-plugin-state/objects';
  13. import { createPluginUI } from '../../mol-plugin-ui';
  14. import { PluginUIContext } from '../../mol-plugin-ui/context';
  15. import { DefaultPluginUISpec } from '../../mol-plugin-ui/spec';
  16. import { PluginCommands } from '../../mol-plugin/commands';
  17. import { PluginConfig } from '../../mol-plugin/config';
  18. import { StateObjectSelector, StateTransformer } from '../../mol-state';
  19. import { Color } from '../../mol-util/color';
  20. import { ColorNames } from '../../mol-util/color/names';
  21. import { ParamDefinition } from '../../mol-util/param-definition';
  22. import { mountControls } from './controls';
  23. import { DemoMoleculeSDF, DemoOrbitals } from './example-data';
  24. import './index.html';
  25. require('mol-plugin-ui/skin/light.scss');
  26. interface DemoInput {
  27. moleculeSdf: string,
  28. basis: Basis,
  29. order: SphericalBasisOrder,
  30. orbitals: AlphaOrbital[]
  31. }
  32. interface Params {
  33. show: { name: 'orbital', params: { index: number } } | { name: 'density', params: {} },
  34. isoValue: number,
  35. gpuSurface: boolean
  36. }
  37. type Selectors = {
  38. type: 'orbital',
  39. volume: StateObjectSelector<PluginStateObject.Volume.Data, typeof CreateOrbitalVolume>,
  40. positive: StateObjectSelector<PluginStateObject.Volume.Representation3D, typeof CreateOrbitalRepresentation3D>
  41. negative: StateObjectSelector<PluginStateObject.Volume.Representation3D, typeof CreateOrbitalRepresentation3D>
  42. } | {
  43. type: 'density',
  44. volume: StateObjectSelector<PluginStateObject.Volume.Data, typeof CreateOrbitalDensityVolume>,
  45. positive: StateObjectSelector<PluginStateObject.Volume.Representation3D, typeof CreateOrbitalRepresentation3D>
  46. }
  47. export class AlphaOrbitalsExample {
  48. plugin: PluginUIContext;
  49. async init(target: string | HTMLElement) {
  50. const defaultSpec = DefaultPluginUISpec();
  51. this.plugin = await createPluginUI(typeof target === 'string' ? document.getElementById(target)! : target, {
  52. ...defaultSpec,
  53. layout: {
  54. initial: {
  55. isExpanded: false,
  56. showControls: false
  57. },
  58. },
  59. components: {
  60. controls: { left: 'none', right: 'none', top: 'none', bottom: 'none' },
  61. },
  62. canvas3d: {
  63. camera: {
  64. helper: { axes: { name: 'off', params: {} } }
  65. }
  66. },
  67. config: [
  68. [PluginConfig.Viewport.ShowExpand, false],
  69. [PluginConfig.Viewport.ShowControls, false],
  70. [PluginConfig.Viewport.ShowSelectionMode, false],
  71. [PluginConfig.Viewport.ShowAnimation, false],
  72. ]
  73. });
  74. this.plugin.managers.interactivity.setProps({ granularity: 'element' });
  75. if (!canComputeGrid3dOnGPU(this.plugin.canvas3d?.webgl)) {
  76. PluginCommands.Toast.Show(this.plugin, {
  77. title: 'Error',
  78. message: `Browser/device does not support required WebGL extension (OES_texture_float).`
  79. });
  80. return;
  81. }
  82. this.load({
  83. moleculeSdf: DemoMoleculeSDF,
  84. ...DemoOrbitals
  85. });
  86. mountControls(this, document.getElementById('controls')!);
  87. }
  88. readonly params = new BehaviorSubject<ParamDefinition.For<Params>>({} as any);
  89. readonly state = new BehaviorSubject<Params>({ show: { name: 'orbital', params: { index: 32 } }, isoValue: 1, gpuSurface: false });
  90. private selectors?: Selectors = void 0;
  91. private basis?: StateObjectSelector<BasisAndOrbitals> = void 0;
  92. private currentParams: Params = { ...this.state.value };
  93. private clearVolume() {
  94. if (!this.selectors) return;
  95. const v = this.selectors.volume;
  96. this.selectors = void 0;
  97. return this.plugin.build().delete(v).commit();
  98. }
  99. private async syncVolume() {
  100. if (!this.basis?.isOk) return;
  101. const state = this.state.value;
  102. if (state.show.name !== this.selectors?.type) {
  103. await this.clearVolume();
  104. }
  105. const update = this.plugin.build();
  106. if (state.show.name === 'orbital') {
  107. if (!this.selectors) {
  108. const volume = update
  109. .to(this.basis)
  110. .apply(CreateOrbitalVolume, { index: state.show.params.index });
  111. const positive = volume.apply(CreateOrbitalRepresentation3D, this.volumeParams('positive', ColorNames.blue)).selector;
  112. const negative = volume.apply(CreateOrbitalRepresentation3D, this.volumeParams('negative', ColorNames.red)).selector;
  113. this.selectors = { type: 'orbital', volume: volume.selector, positive, negative };
  114. } else {
  115. const index = state.show.params.index;
  116. update.to(this.selectors.volume).update(CreateOrbitalVolume, () => ({ index }));
  117. }
  118. } else {
  119. if (!this.selectors) {
  120. const volume = update
  121. .to(this.basis)
  122. .apply(CreateOrbitalDensityVolume);
  123. const positive = volume.apply(CreateOrbitalRepresentation3D, this.volumeParams('positive', ColorNames.blue)).selector;
  124. this.selectors = { type: 'density', volume: volume.selector, positive };
  125. }
  126. }
  127. await update.commit();
  128. if (this.currentParams.gpuSurface !== this.state.value.gpuSurface) {
  129. await this.setIsovalue();
  130. }
  131. this.currentParams = this.state.value;
  132. }
  133. private setIsovalue() {
  134. if (!this.selectors) return;
  135. this.currentParams = this.state.value;
  136. const update = this.plugin.build();
  137. update.to(this.selectors.positive).update(this.volumeParams('positive', ColorNames.blue));
  138. if (this.selectors?.type === 'orbital') {
  139. update.to(this.selectors.negative).update(this.volumeParams('negative', ColorNames.red));
  140. }
  141. return update.commit();
  142. }
  143. private volumeParams(kind: 'positive' | 'negative', color: Color): StateTransformer.Params<typeof CreateOrbitalRepresentation3D> {
  144. return {
  145. alpha: 0.85,
  146. color,
  147. directVolume: this.state.value.gpuSurface,
  148. kind,
  149. relativeIsovalue: this.state.value.isoValue,
  150. pickable: false,
  151. xrayShaded: true,
  152. tryUseGpu: false
  153. };
  154. }
  155. async load(input: DemoInput) {
  156. await this.plugin.clear();
  157. const data = await this.plugin.builders.data.rawData({ data: input.moleculeSdf }, { state: { isGhost: true } });
  158. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, 'mol');
  159. const model = await this.plugin.builders.structure.createModel(trajectory);
  160. const structure = await this.plugin.builders.structure.createStructure(model);
  161. const all = await this.plugin.builders.structure.tryCreateComponentStatic(structure, 'all');
  162. if (all) await this.plugin.builders.structure.representation.addRepresentation(all, { type: 'ball-and-stick', color: 'element-symbol', colorParams: { carbonColor: { name: 'element-symbol', params: {} } } });
  163. this.basis = await this.plugin.build().toRoot()
  164. .apply(StaticBasisAndOrbitals, { basis: input.basis, order: input.order, orbitals: input.orbitals })
  165. .commit();
  166. await this.syncVolume();
  167. this.params.next({
  168. show: ParamDefinition.MappedStatic('orbital', {
  169. 'orbital': ParamDefinition.Group({
  170. index: ParamDefinition.Numeric(32, { min: 0, max: input.orbitals.length - 1 }, { immediateUpdate: true, isEssential: true }),
  171. }),
  172. 'density': ParamDefinition.EmptyGroup()
  173. }, { cycle: true }),
  174. isoValue: ParamDefinition.Numeric(this.currentParams.isoValue, { min: 0.5, max: 3, step: 0.1 }, { immediateUpdate: true, isEssential: false }),
  175. gpuSurface: ParamDefinition.Boolean(this.currentParams.gpuSurface, { isHidden: true })
  176. });
  177. this.state.pipe(skip(1), debounceTime(1000 / 24)).subscribe(async params => {
  178. if (params.show.name !== this.currentParams.show.name
  179. || (params.show.name === 'orbital' && this.currentParams.show.name === 'orbital' && params.show.params.index !== this.currentParams.show.params.index)) {
  180. this.syncVolume();
  181. } else if (params.isoValue !== this.currentParams.isoValue || params.gpuSurface !== this.currentParams.gpuSurface) {
  182. this.setIsovalue();
  183. }
  184. });
  185. }
  186. }
  187. (window as any).AlphaOrbitalsExample = new AlphaOrbitalsExample();