index.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { createPlugin, DefaultPluginSpec } from '../../mol-plugin';
  7. import './index.html'
  8. import { PluginContext } from '../../mol-plugin/context';
  9. import { PluginCommands } from '../../mol-plugin/command';
  10. import { StateTransforms } from '../../mol-plugin/state/transforms';
  11. import { StructureRepresentation3DHelpers } from '../../mol-plugin/state/transforms/representation';
  12. import { Color } from '../../mol-util/color';
  13. import { PluginStateObject as PSO } from '../../mol-plugin/state/objects';
  14. import { AnimateModelIndex } from '../../mol-plugin/state/animation/built-in';
  15. import { StateBuilder, StateTransform } from '../../mol-state';
  16. import { StripedResidues } from './coloring';
  17. // import { BasicWrapperControls } from './controls';
  18. import { StaticSuperpositionTestData, buildStaticSuperposition, dynamicSuperpositionTest } from './superposition';
  19. require('mol-plugin/skin/light.scss')
  20. type SupportedFormats = 'cif' | 'pdb'
  21. type LoadParams = { url: string, format?: SupportedFormats, assemblyId?: string }
  22. class BasicWrapper {
  23. plugin: PluginContext;
  24. init(target: string | HTMLElement) {
  25. this.plugin = createPlugin(typeof target === 'string' ? document.getElementById(target)! : target, {
  26. ...DefaultPluginSpec,
  27. layout: {
  28. initial: {
  29. isExpanded: false,
  30. showControls: false
  31. },
  32. controls: {
  33. // left: 'none',
  34. // right: BasicWrapperControls
  35. }
  36. }
  37. });
  38. this.plugin.structureRepresentation.themeCtx.colorThemeRegistry.add(StripedResidues.Descriptor.name, StripedResidues.colorTheme!);
  39. this.plugin.lociLabels.addProvider(StripedResidues.labelProvider);
  40. this.plugin.customModelProperties.register(StripedResidues.propertyProvider);
  41. }
  42. private download(b: StateBuilder.To<PSO.Root>, url: string) {
  43. return b.apply(StateTransforms.Data.Download, { url, isBinary: false })
  44. }
  45. private parse(b: StateBuilder.To<PSO.Data.Binary | PSO.Data.String>, format: SupportedFormats, assemblyId: string) {
  46. const parsed = format === 'cif'
  47. ? b.apply(StateTransforms.Data.ParseCif).apply(StateTransforms.Model.TrajectoryFromMmCif)
  48. : b.apply(StateTransforms.Model.TrajectoryFromPDB);
  49. return parsed
  50. .apply(StateTransforms.Model.ModelFromTrajectory, { modelIndex: 0 })
  51. .apply(StateTransforms.Model.CustomModelProperties, { properties: [StripedResidues.Descriptor.name] }, { ref: 'props', state: { isGhost: false } })
  52. .apply(StateTransforms.Model.StructureAssemblyFromModel, { id: assemblyId || 'deposited' }, { ref: 'asm' });
  53. }
  54. private visual(visualRoot: StateBuilder.To<PSO.Molecule.Structure>) {
  55. visualRoot.apply(StateTransforms.Model.StructureComplexElement, { type: 'atomic-sequence' })
  56. .apply(StateTransforms.Representation.StructureRepresentation3D,
  57. StructureRepresentation3DHelpers.getDefaultParamsStatic(this.plugin, 'cartoon'), { ref: 'seq-visual' });
  58. visualRoot.apply(StateTransforms.Model.StructureComplexElement, { type: 'atomic-het' })
  59. .apply(StateTransforms.Representation.StructureRepresentation3D,
  60. StructureRepresentation3DHelpers.getDefaultParamsStatic(this.plugin, 'ball-and-stick'), { ref: 'het-visual' });
  61. visualRoot.apply(StateTransforms.Model.StructureComplexElement, { type: 'water' })
  62. .apply(StateTransforms.Representation.StructureRepresentation3D,
  63. StructureRepresentation3DHelpers.getDefaultParamsStatic(this.plugin, 'ball-and-stick', { alpha: 0.51 }), { ref: 'water-visual' });
  64. visualRoot.apply(StateTransforms.Model.StructureComplexElement, { type: 'spheres' })
  65. .apply(StateTransforms.Representation.StructureRepresentation3D,
  66. StructureRepresentation3DHelpers.getDefaultParamsStatic(this.plugin, 'spacefill'), { ref: 'ihm-visual' });
  67. return visualRoot;
  68. }
  69. private loadedParams: LoadParams = { url: '', format: 'cif', assemblyId: '' };
  70. async load({ url, format = 'cif', assemblyId = '' }: LoadParams) {
  71. let loadType: 'full' | 'update' = 'full';
  72. const state = this.plugin.state.dataState;
  73. if (this.loadedParams.url !== url || this.loadedParams.format !== format) {
  74. loadType = 'full';
  75. } else if (this.loadedParams.url === url) {
  76. if (state.select('asm').length > 0) loadType = 'update';
  77. }
  78. let tree: StateBuilder.Root;
  79. if (loadType === 'full') {
  80. await PluginCommands.State.RemoveObject.dispatch(this.plugin, { state, ref: state.tree.root.ref });
  81. tree = state.build();
  82. this.visual(this.parse(this.download(tree.toRoot(), url), format, assemblyId));
  83. } else {
  84. tree = state.build();
  85. tree.to('asm').update(StateTransforms.Model.StructureAssemblyFromModel, p => ({ ...p, id: assemblyId || 'deposited' }));
  86. }
  87. await PluginCommands.State.Update.dispatch(this.plugin, { state: this.plugin.state.dataState, tree });
  88. this.loadedParams = { url, format, assemblyId };
  89. PluginCommands.Camera.Reset.dispatch(this.plugin, { });
  90. }
  91. setBackground(color: number) {
  92. const renderer = this.plugin.canvas3d.props.renderer;
  93. PluginCommands.Canvas3D.SetSettings.dispatch(this.plugin, { settings: { renderer: { ...renderer, backgroundColor: Color(color) } } });
  94. }
  95. toggleSpin() {
  96. const trackball = this.plugin.canvas3d.props.trackball;
  97. const spinning = trackball.spin;
  98. PluginCommands.Canvas3D.SetSettings.dispatch(this.plugin, { settings: { trackball: { ...trackball, spin: !trackball.spin } } });
  99. if (!spinning) PluginCommands.Camera.Reset.dispatch(this.plugin, { });
  100. }
  101. animate = {
  102. modelIndex: {
  103. maxFPS: 8,
  104. onceForward: () => { this.plugin.state.animation.play(AnimateModelIndex, { maxFPS: Math.max(0.5, this.animate.modelIndex.maxFPS | 0), mode: { name: 'once', params: { direction: 'forward' } } }) },
  105. onceBackward: () => { this.plugin.state.animation.play(AnimateModelIndex, { maxFPS: Math.max(0.5, this.animate.modelIndex.maxFPS | 0), mode: { name: 'once', params: { direction: 'backward' } } }) },
  106. palindrome: () => { this.plugin.state.animation.play(AnimateModelIndex, { maxFPS: Math.max(0.5, this.animate.modelIndex.maxFPS | 0), mode: { name: 'palindrome', params: {} } }) },
  107. loop: () => { this.plugin.state.animation.play(AnimateModelIndex, { maxFPS: Math.max(0.5, this.animate.modelIndex.maxFPS | 0), mode: { name: 'loop', params: {} } }) },
  108. stop: () => this.plugin.state.animation.stop()
  109. }
  110. }
  111. coloring = {
  112. applyStripes: async () => {
  113. const state = this.plugin.state.dataState;
  114. const visuals = state.selectQ(q => q.ofTransformer(StateTransforms.Representation.StructureRepresentation3D));
  115. const tree = state.build();
  116. const colorTheme = { name: StripedResidues.Descriptor.name, params: this.plugin.structureRepresentation.themeCtx.colorThemeRegistry.get(StripedResidues.Descriptor.name).defaultValues };
  117. for (const v of visuals) {
  118. tree.to(v).update(old => ({ ...old, colorTheme }));
  119. }
  120. await PluginCommands.State.Update.dispatch(this.plugin, { state, tree });
  121. }
  122. }
  123. tests = {
  124. staticSuperposition: async () => {
  125. const state = this.plugin.state.dataState;
  126. const tree = buildStaticSuperposition(this.plugin, StaticSuperpositionTestData);
  127. await PluginCommands.State.RemoveObject.dispatch(this.plugin, { state, ref: StateTransform.RootRef });
  128. await PluginCommands.State.Update.dispatch(this.plugin, { state, tree });
  129. },
  130. dynamicSuperposition: async () => {
  131. await PluginCommands.State.RemoveObject.dispatch(this.plugin, { state: this.plugin.state.dataState, ref: StateTransform.RootRef });
  132. await dynamicSuperpositionTest(this.plugin, ['1tqn', '2hhb', '4hhb'], 'HEM');
  133. }
  134. }
  135. }
  136. (window as any).BasicMolStarWrapper = new BasicWrapper();