index.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. PluginCommands.Canvas3D.SetSettings(this.plugin, {
  67. settings: props => {
  68. props.trackball.spin = !props.trackball.spin;
  69. }
  70. });
  71. if (!this.plugin.canvas3d.props.trackball.spin) PluginCommands.Camera.Reset(this.plugin, {});
  72. }
  73. private animateModelIndexTargetFps() {
  74. return Math.max(1, this.animate.modelIndex.targetFps | 0);
  75. }
  76. animate = {
  77. modelIndex: {
  78. targetFps: 8,
  79. onceForward: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'once', params: { direction: 'forward' } } }); },
  80. onceBackward: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'once', params: { direction: 'backward' } } }); },
  81. palindrome: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'palindrome', params: {} } }); },
  82. loop: () => { this.plugin.managers.animation.play(AnimateModelIndex, { duration: { name: 'computed', params: { targetFps: this.animateModelIndexTargetFps() } }, mode: { name: 'loop', params: { direction: 'forward' } } }); },
  83. stop: () => this.plugin.managers.animation.stop()
  84. }
  85. };
  86. coloring = {
  87. applyStripes: async () => {
  88. this.plugin.dataTransaction(async () => {
  89. for (const s of this.plugin.managers.structure.hierarchy.current.structures) {
  90. await this.plugin.managers.structure.component.updateRepresentationsTheme(s.components, { color: StripedResidues.propertyProvider.descriptor.name as any });
  91. }
  92. });
  93. },
  94. applyCustomTheme: async () => {
  95. this.plugin.dataTransaction(async () => {
  96. for (const s of this.plugin.managers.structure.hierarchy.current.structures) {
  97. await this.plugin.managers.structure.component.updateRepresentationsTheme(s.components, { color: CustomColorThemeProvider.name as any });
  98. }
  99. });
  100. },
  101. applyDefault: async () => {
  102. this.plugin.dataTransaction(async () => {
  103. for (const s of this.plugin.managers.structure.hierarchy.current.structures) {
  104. await this.plugin.managers.structure.component.updateRepresentationsTheme(s.components, { color: 'default' });
  105. }
  106. });
  107. }
  108. };
  109. interactivity = {
  110. highlightOn: () => {
  111. const data = this.plugin.managers.structure.hierarchy.current.structures[0]?.cell.obj?.data;
  112. if (!data) return;
  113. const seq_id = 7;
  114. const sel = Script.getStructureSelection(Q => Q.struct.generator.atomGroups({
  115. 'residue-test': Q.core.rel.eq([Q.struct.atomProperty.macromolecular.label_seq_id(), seq_id]),
  116. 'group-by': Q.struct.atomProperty.macromolecular.residueKey()
  117. }), data);
  118. const loci = StructureSelection.toLociWithSourceUnits(sel);
  119. this.plugin.managers.interactivity.lociHighlights.highlightOnly({ loci });
  120. },
  121. clearHighlight: () => {
  122. this.plugin.managers.interactivity.lociHighlights.highlightOnly({ loci: EmptyLoci });
  123. }
  124. };
  125. tests = {
  126. staticSuperposition: async () => {
  127. await this.plugin.clear();
  128. return buildStaticSuperposition(this.plugin, StaticSuperpositionTestData);
  129. },
  130. dynamicSuperposition: async () => {
  131. await this.plugin.clear();
  132. return dynamicSuperpositionTest(this.plugin, ['1tqn', '2hhb', '4hhb'], 'HEM');
  133. },
  134. toggleValidationTooltip: () => {
  135. return this.plugin.state.updateBehavior(PDBeStructureQualityReport, params => { params.showTooltip = !params.showTooltip; });
  136. },
  137. showToasts: () => {
  138. PluginCommands.Toast.Show(this.plugin, {
  139. title: 'Toast 1',
  140. message: 'This is an example text, timeout 3s',
  141. key: 'toast-1',
  142. timeoutMs: 3000
  143. });
  144. PluginCommands.Toast.Show(this.plugin, {
  145. title: 'Toast 2',
  146. message: CustomToastMessage,
  147. key: 'toast-2'
  148. });
  149. },
  150. hideToasts: () => {
  151. PluginCommands.Toast.Hide(this.plugin, { key: 'toast-1' });
  152. PluginCommands.Toast.Hide(this.plugin, { key: 'toast-2' });
  153. }
  154. };
  155. }
  156. (window as any).BasicMolStarWrapper = new BasicWrapper();