index.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import '../../mol-util/polyfill';
  8. import { createPlugin, DefaultPluginSpec } from '../../mol-plugin';
  9. import './index.html';
  10. import './embedded.html';
  11. import './favicon.ico';
  12. import { PluginContext } from '../../mol-plugin/context';
  13. import { PluginCommands } from '../../mol-plugin/commands';
  14. import { PluginSpec } from '../../mol-plugin/spec';
  15. import { DownloadStructure, PdbDownloadProvider } from '../../mol-plugin-state/actions/structure';
  16. import { PluginConfig } from '../../mol-plugin/config';
  17. import { CellPack } from '../../extensions/cellpack';
  18. import { RCSBAssemblySymmetry, RCSBValidationReport } from '../../extensions/rcsb';
  19. import { PDBeStructureQualityReport } from '../../extensions/pdbe';
  20. import { Asset } from '../../mol-util/assets';
  21. import { ObjectKeys } from '../../mol-util/type-helpers';
  22. import { PluginState } from '../../mol-plugin/state';
  23. import { DownloadDensity } from '../../mol-plugin-state/actions/volume';
  24. import { PluginLayoutControlsDisplay } from '../../mol-plugin/layout';
  25. import { BuiltInTrajectoryFormat } from '../../mol-plugin-state/formats/trajectory';
  26. import { MembraneOrientationData } from '../../extensions/membrane-orientation/behavior';
  27. import { DnatcoConfalPyramids } from '../../extensions/dnatco';
  28. require('mol-plugin-ui/skin/light.scss');
  29. export { PLUGIN_VERSION as version } from '../../mol-plugin/version';
  30. export { setProductionMode, setDebugMode } from '../../mol-util/debug';
  31. const Extensions = {
  32. 'cellpack': PluginSpec.Behavior(CellPack),
  33. 'dnatco-confal-pyramids': PluginSpec.Behavior(DnatcoConfalPyramids),
  34. 'pdbe-structure-quality-report': PluginSpec.Behavior(PDBeStructureQualityReport),
  35. 'rcsb-assembly-symmetry': PluginSpec.Behavior(RCSBAssemblySymmetry),
  36. 'rcsb-validation-report': PluginSpec.Behavior(RCSBValidationReport),
  37. 'membrane-orientation': PluginSpec.Behavior(MembraneOrientationData)
  38. };
  39. const DefaultViewerOptions = {
  40. extensions: ObjectKeys(Extensions),
  41. layoutIsExpanded: true,
  42. layoutShowControls: true,
  43. layoutShowRemoteState: true,
  44. layoutControlsDisplay: 'reactive' as PluginLayoutControlsDisplay,
  45. layoutShowSequence: true,
  46. layoutShowLog: true,
  47. layoutShowLeftPanel: true,
  48. viewportShowExpand: PluginConfig.Viewport.ShowExpand.defaultValue,
  49. viewportShowSelectionMode: PluginConfig.Viewport.ShowSelectionMode.defaultValue,
  50. viewportShowAnimation: PluginConfig.Viewport.ShowAnimation.defaultValue,
  51. pluginStateServer: PluginConfig.State.DefaultServer.defaultValue,
  52. volumeStreamingServer: PluginConfig.VolumeStreaming.DefaultServer.defaultValue,
  53. pdbProvider: PluginConfig.Download.DefaultPdbProvider.defaultValue,
  54. emdbProvider: PluginConfig.Download.DefaultEmdbProvider.defaultValue,
  55. };
  56. type ViewerOptions = typeof DefaultViewerOptions;
  57. export class Viewer {
  58. plugin: PluginContext
  59. constructor(elementOrId: string | HTMLElement, options: Partial<ViewerOptions> = {}) {
  60. const o = { ...DefaultViewerOptions, ...options };
  61. const spec: PluginSpec = {
  62. actions: [...DefaultPluginSpec.actions],
  63. behaviors: [
  64. ...DefaultPluginSpec.behaviors,
  65. ...o.extensions.map(e => Extensions[e]),
  66. ],
  67. animations: [...DefaultPluginSpec.animations || []],
  68. customParamEditors: DefaultPluginSpec.customParamEditors,
  69. layout: {
  70. initial: {
  71. isExpanded: o.layoutIsExpanded,
  72. showControls: o.layoutShowControls,
  73. controlsDisplay: o.layoutControlsDisplay,
  74. },
  75. controls: {
  76. ...DefaultPluginSpec.layout && DefaultPluginSpec.layout.controls,
  77. top: o.layoutShowSequence ? undefined : 'none',
  78. bottom: o.layoutShowLog ? undefined : 'none',
  79. left: o.layoutShowLeftPanel ? undefined : 'none',
  80. }
  81. },
  82. components: {
  83. ...DefaultPluginSpec.components,
  84. remoteState: o.layoutShowRemoteState ? 'default' : 'none',
  85. },
  86. config: [
  87. [PluginConfig.Viewport.ShowExpand, o.viewportShowExpand],
  88. [PluginConfig.Viewport.ShowSelectionMode, o.viewportShowSelectionMode],
  89. [PluginConfig.Viewport.ShowAnimation, o.viewportShowAnimation],
  90. [PluginConfig.State.DefaultServer, o.pluginStateServer],
  91. [PluginConfig.State.CurrentServer, o.pluginStateServer],
  92. [PluginConfig.VolumeStreaming.DefaultServer, o.volumeStreamingServer],
  93. [PluginConfig.Download.DefaultPdbProvider, o.pdbProvider],
  94. [PluginConfig.Download.DefaultEmdbProvider, o.emdbProvider]
  95. ]
  96. };
  97. const element = typeof elementOrId === 'string'
  98. ? document.getElementById(elementOrId)
  99. : elementOrId;
  100. if (!element) throw new Error(`Could not get element with id '${elementOrId}'`);
  101. this.plugin = createPlugin(element, spec);
  102. }
  103. setRemoteSnapshot(id: string) {
  104. const url = `${this.plugin.config.get(PluginConfig.State.CurrentServer)}/get/${id}`;
  105. return PluginCommands.State.Snapshots.Fetch(this.plugin, { url });
  106. }
  107. loadSnapshotFromUrl(url: string, type: PluginState.SnapshotType) {
  108. return PluginCommands.State.Snapshots.OpenUrl(this.plugin, { url, type });
  109. }
  110. loadStructureFromUrl(url: string, format: BuiltInTrajectoryFormat = 'mmcif', isBinary = false) {
  111. const params = DownloadStructure.createDefaultParams(this.plugin.state.data.root.obj!, this.plugin);
  112. return this.plugin.runTask(this.plugin.state.data.applyAction(DownloadStructure, {
  113. source: {
  114. name: 'url',
  115. params: {
  116. url: Asset.Url(url),
  117. format: format as any,
  118. isBinary,
  119. options: params.source.params.options,
  120. }
  121. }
  122. }));
  123. }
  124. async loadStructureFromData(data: string | number[], format: BuiltInTrajectoryFormat, options?: { dataLabel?: string }) {
  125. const _data = await this.plugin.builders.data.rawData({ data, label: options?.dataLabel });
  126. const trajectory = await this.plugin.builders.structure.parseTrajectory(_data, format);
  127. await this.plugin.builders.structure.hierarchy.applyPreset(trajectory, 'default');
  128. }
  129. loadPdb(pdb: string) {
  130. const params = DownloadStructure.createDefaultParams(this.plugin.state.data.root.obj!, this.plugin);
  131. const provider = this.plugin.config.get(PluginConfig.Download.DefaultPdbProvider)!;
  132. return this.plugin.runTask(this.plugin.state.data.applyAction(DownloadStructure, {
  133. source: {
  134. name: 'pdb' as const,
  135. params: {
  136. provider: {
  137. id: pdb,
  138. server: {
  139. name: provider,
  140. params: PdbDownloadProvider[provider].defaultValue as any
  141. }
  142. },
  143. options: params.source.params.options,
  144. }
  145. }
  146. }));
  147. }
  148. loadPdbDev(pdbDev: string) {
  149. const params = DownloadStructure.createDefaultParams(this.plugin.state.data.root.obj!, this.plugin);
  150. return this.plugin.runTask(this.plugin.state.data.applyAction(DownloadStructure, {
  151. source: {
  152. name: 'pdb-dev' as const,
  153. params: {
  154. provider: {
  155. id: pdbDev,
  156. encoding: 'bcif',
  157. },
  158. options: params.source.params.options,
  159. }
  160. }
  161. }));
  162. }
  163. loadEmdb(emdb: string) {
  164. const provider = this.plugin.config.get(PluginConfig.Download.DefaultEmdbProvider)!;
  165. return this.plugin.runTask(this.plugin.state.data.applyAction(DownloadDensity, {
  166. source: {
  167. name: 'pdb-emd-ds' as const,
  168. params: {
  169. provider: {
  170. id: emdb,
  171. server: provider,
  172. },
  173. detail: 3,
  174. }
  175. }
  176. }));
  177. }
  178. }