index.ts 7.3 KB

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