index.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 { PDBeStructureQualityReport } from '../../extensions/pdbe';
  7. import { EmptyLoci } from '../../mol-model/loci';
  8. import { StructureSelection } from '../../mol-model/structure';
  9. import { AnimateModelIndex } from '../../mol-plugin-state/animation/built-in/model-index';
  10. import { BuiltInTrajectoryFormat } from '../../mol-plugin-state/formats/trajectory';
  11. import { createPluginUI } from '../../mol-plugin-ui';
  12. import { PluginUIContext } from '../../mol-plugin-ui/context';
  13. import { DefaultPluginUISpec } from '../../mol-plugin-ui/spec';
  14. import { PluginCommands } from '../../mol-plugin/commands';
  15. import { Script } from '../../mol-script/script';
  16. import { Asset } from '../../mol-util/assets';
  17. import { Color } from '../../mol-util/color';
  18. import { StripedResidues } from './coloring';
  19. import { CustomToastMessage } from './controls';
  20. import { CustomColorThemeProvider } from './custom-theme';
  21. import './index.html';
  22. import { buildStaticSuperposition, dynamicSuperpositionTest, StaticSuperpositionTestData } from './superposition';
  23. require('mol-plugin-ui/skin/light.scss');
  24. type LoadParams = { url: string, format?: BuiltInTrajectoryFormat, isBinary?: boolean, assemblyId?: string }
  25. class BasicWrapper {
  26. plugin: PluginUIContext;
  27. async init(target: string | HTMLElement) {
  28. this.plugin = await createPluginUI(typeof target === 'string' ? document.getElementById(target)! : target, {
  29. ...DefaultPluginUISpec(),
  30. layout: {
  31. initial: {
  32. isExpanded: false,
  33. showControls: false
  34. }
  35. },
  36. components: {
  37. remoteState: 'none'
  38. }
  39. });
  40. this.plugin.representation.structure.themes.colorThemeRegistry.add(StripedResidues.colorThemeProvider!);
  41. this.plugin.representation.structure.themes.colorThemeRegistry.add(CustomColorThemeProvider);
  42. this.plugin.managers.lociLabels.addProvider(StripedResidues.labelProvider!);
  43. this.plugin.customModelProperties.register(StripedResidues.propertyProvider, true);
  44. }
  45. async load({ url, format = 'mmcif', isBinary = false, assemblyId = '' }: LoadParams) {
  46. await this.plugin.clear();
  47. const data = await this.plugin.builders.data.download({ url: Asset.Url(url), isBinary }, { state: { isGhost: true } });
  48. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format);
  49. await this.plugin.builders.structure.hierarchy.applyPreset(trajectory, 'default', {
  50. structure: assemblyId ? {
  51. name: 'assembly',
  52. params: { id: assemblyId }
  53. } : {
  54. name: 'model',
  55. params: {}
  56. },
  57. showUnitcell: false,
  58. representationPreset: 'auto'
  59. });
  60. }
  61. setBackground(color: number) {
  62. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: props => { props.renderer.backgroundColor = Color(color); } });
  63. }
  64. toggleSpin() {
  65. if (!this.plugin.canvas3d) return;
  66. const trackball = this.plugin.canvas3d.props.trackball;
  67. PluginCommands.Canvas3D.SetSettings(this.plugin, {
  68. settings: {
  69. trackball: {
  70. ...trackball,
  71. animate: trackball.animate.name === 'spin'
  72. ? { name: 'off', params: {} }
  73. : { name: 'spin', params: { speed: 1 } }
  74. }
  75. }
  76. });
  77. if (this.plugin.canvas3d.props.trackball.animate.name !== 'spin') {
  78. PluginCommands.Camera.Reset(this.plugin, {});
  79. }
  80. }
  81. private animateModelIndexTargetFps() {
  82. return Math.max(1, this.animate.modelIndex.targetFps | 0);
  83. }
  84. animate = {
  85. modelIndex: {
  86. targetFps: 8,
  87. onceForward: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'once', params: { direction: 'forward' } } }); },
  88. onceBackward: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'once', params: { direction: 'backward' } } }); },
  89. palindrome: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'palindrome', params: {} } }); },
  90. loop: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'loop', params: { direction: 'forward' } } }); },
  91. stop: () => this.plugin.managers.animation.stop()
  92. }
  93. };
  94. coloring = {
  95. applyStripes: async () => {
  96. this.plugin.dataTransaction(async () => {
  97. for (const s of this.plugin.managers.structure.hierarchy.current.structures) {
  98. await this.plugin.managers.structure.component.updateRepresentationsTheme(s.components, { color: StripedResidues.propertyProvider.descriptor.name as any });
  99. }
  100. });
  101. },
  102. applyCustomTheme: async () => {
  103. this.plugin.dataTransaction(async () => {
  104. for (const s of this.plugin.managers.structure.hierarchy.current.structures) {
  105. await this.plugin.managers.structure.component.updateRepresentationsTheme(s.components, { color: CustomColorThemeProvider.name as any });
  106. }
  107. });
  108. },
  109. applyDefault: async () => {
  110. this.plugin.dataTransaction(async () => {
  111. for (const s of this.plugin.managers.structure.hierarchy.current.structures) {
  112. await this.plugin.managers.structure.component.updateRepresentationsTheme(s.components, { color: 'default' });
  113. }
  114. });
  115. }
  116. };
  117. interactivity = {
  118. highlightOn: () => {
  119. const data = this.plugin.managers.structure.hierarchy.current.structures[0]?.cell.obj?.data;
  120. if (!data) return;
  121. const seq_id = 7;
  122. const sel = Script.getStructureSelection(Q => Q.struct.generator.atomGroups({
  123. 'residue-test': Q.core.rel.eq([Q.struct.atomProperty.macromolecular.label_seq_id(), seq_id]),
  124. 'group-by': Q.struct.atomProperty.macromolecular.residueKey()
  125. }), data);
  126. const loci = StructureSelection.toLociWithSourceUnits(sel);
  127. this.plugin.managers.interactivity.lociHighlights.highlightOnly({ loci });
  128. },
  129. clearHighlight: () => {
  130. this.plugin.managers.interactivity.lociHighlights.highlightOnly({ loci: EmptyLoci });
  131. }
  132. };
  133. tests = {
  134. staticSuperposition: async () => {
  135. await this.plugin.clear();
  136. return buildStaticSuperposition(this.plugin, StaticSuperpositionTestData);
  137. },
  138. dynamicSuperposition: async () => {
  139. await this.plugin.clear();
  140. return dynamicSuperpositionTest(this.plugin, ['1tqn', '2hhb', '4hhb'], 'HEM');
  141. },
  142. toggleValidationTooltip: () => {
  143. return this.plugin.state.updateBehavior(PDBeStructureQualityReport, params => { params.showTooltip = !params.showTooltip; });
  144. },
  145. showToasts: () => {
  146. PluginCommands.Toast.Show(this.plugin, {
  147. title: 'Toast 1',
  148. message: 'This is an example text, timeout 3s',
  149. key: 'toast-1',
  150. timeoutMs: 3000
  151. });
  152. PluginCommands.Toast.Show(this.plugin, {
  153. title: 'Toast 2',
  154. message: CustomToastMessage,
  155. key: 'toast-2'
  156. });
  157. },
  158. hideToasts: () => {
  159. PluginCommands.Toast.Hide(this.plugin, { key: 'toast-1' });
  160. PluginCommands.Toast.Hide(this.plugin, { key: 'toast-2' });
  161. }
  162. };
  163. }
  164. (window as any).BasicMolStarWrapper = new BasicWrapper();