index.ts 8.1 KB

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