index.ts 8.7 KB

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