index.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { BehaviorSubject } from 'rxjs';
  7. import { DefaultPluginSpec } from 'molstar/lib/mol-plugin';
  8. import { Plugin } from 'molstar/lib/mol-plugin-ui/plugin'
  9. import './index.html'
  10. import './favicon.ico'
  11. import { PluginContext } from 'molstar/lib/mol-plugin/context';
  12. import { PluginCommands } from 'molstar/lib/mol-plugin/commands';
  13. import { ViewerState as ViewerState, CollapsedState, ModelUrlProvider } from './types';
  14. import { PluginSpec } from 'molstar/lib/mol-plugin/spec';
  15. import { ColorNames } from 'molstar/lib/mol-util/color/names';
  16. import ReactDOM = require('react-dom');
  17. import React = require('react');
  18. import { ModelLoader } from './helpers/model';
  19. import { PresetProps } from './helpers/preset';
  20. import { ControlsWrapper } from './ui/controls';
  21. import { PluginConfig } from 'molstar/lib/mol-plugin/config';
  22. import { RCSBAssemblySymmetry } from 'molstar/lib/extensions/rcsb/assembly-symmetry/behavior';
  23. import { RCSBValidationReport } from 'molstar/lib/extensions/rcsb/validation-report/behavior';
  24. import { Mat4 } from 'molstar/lib/mol-math/linear-algebra';
  25. import { PluginState } from 'molstar/lib/mol-plugin/state';
  26. import { BuiltInTrajectoryFormat } from 'molstar/lib/mol-plugin-state/formats/trajectory';
  27. import { ObjectKeys } from 'molstar/lib/mol-util/type-helpers';
  28. import { PluginLayoutControlsDisplay } from 'molstar/lib/mol-plugin/layout';
  29. require('./skin/rcsb.scss')
  30. /** package version, filled in at bundle build time */
  31. declare const __RCSB_MOLSTAR_VERSION__: string
  32. export const RCSB_MOLSTAR_VERSION = __RCSB_MOLSTAR_VERSION__;
  33. /** unix time stamp, to be filled in at bundle build time */
  34. declare const __BUILD_TIMESTAMP__: number
  35. export const BUILD_TIMESTAMP = __BUILD_TIMESTAMP__;
  36. export const BUILD_DATE = new Date(BUILD_TIMESTAMP);
  37. const Extensions = {
  38. 'rcsb-assembly-symmetry': PluginSpec.Behavior(RCSBAssemblySymmetry),
  39. 'rcsb-validation-report': PluginSpec.Behavior(RCSBValidationReport)
  40. };
  41. const DefaultViewerProps = {
  42. showImportControls: false,
  43. showSessionControls: false,
  44. modelUrlProviders: [
  45. (pdbId: string) => ({
  46. url: `//models.rcsb.org/${pdbId.toLowerCase()}.bcif`,
  47. format: 'mmcif',
  48. isBinary: true
  49. }),
  50. (pdbId: string) => ({
  51. url: `//files.rcsb.org/download/${pdbId.toLowerCase()}.cif`,
  52. format: 'mmcif',
  53. isBinary: false
  54. })
  55. ] as ModelUrlProvider[],
  56. extensions: ObjectKeys(Extensions),
  57. layoutIsExpanded: false,
  58. layoutShowControls: true,
  59. layoutControlsDisplay: 'reactive' as PluginLayoutControlsDisplay,
  60. layoutShowSequence: true,
  61. layoutShowLog: false,
  62. viewportShowExpand: true,
  63. viewportShowSelectionMode: true,
  64. volumeStreamingServer: '//maps.rcsb.org/',
  65. };
  66. type ViewerProps = typeof DefaultViewerProps
  67. export class Viewer {
  68. private readonly plugin: PluginContext;
  69. private readonly modelUrlProviders: ModelUrlProvider[];
  70. private get customState() {
  71. return this.plugin.customState as ViewerState
  72. }
  73. constructor(target: string | HTMLElement, props: Partial<ViewerProps> = {}) {
  74. target = typeof target === 'string' ? document.getElementById(target)! : target
  75. const o = { ...DefaultViewerProps, ...props }
  76. const spec: PluginSpec = {
  77. actions: [...DefaultPluginSpec.actions],
  78. behaviors: [
  79. ...DefaultPluginSpec.behaviors,
  80. ...o.extensions.map(e => Extensions[e]),
  81. ],
  82. animations: [...DefaultPluginSpec.animations || []],
  83. customParamEditors: DefaultPluginSpec.customParamEditors,
  84. layout: {
  85. initial: {
  86. isExpanded: o.layoutIsExpanded,
  87. showControls: o.layoutShowControls,
  88. controlsDisplay: o.layoutControlsDisplay,
  89. },
  90. controls: {
  91. ...DefaultPluginSpec.layout && DefaultPluginSpec.layout.controls,
  92. top: o.layoutShowSequence ? undefined : 'none',
  93. bottom: o.layoutShowLog ? undefined : 'none',
  94. left: 'none',
  95. right: ControlsWrapper,
  96. }
  97. },
  98. components: {
  99. ...DefaultPluginSpec.components,
  100. remoteState: 'none',
  101. },
  102. config: [
  103. [PluginConfig.Viewport.ShowExpand, o.viewportShowExpand],
  104. [PluginConfig.Viewport.ShowSelectionMode, o.viewportShowSelectionMode],
  105. [PluginConfig.Viewport.ShowAnimation, false],
  106. [PluginConfig.VolumeStreaming.DefaultServer, o.volumeStreamingServer],
  107. [PluginConfig.Download.DefaultPdbProvider, 'rcsb'],
  108. [PluginConfig.Download.DefaultEmdbProvider, 'rcsb']
  109. ]
  110. };
  111. this.plugin = new PluginContext(spec);
  112. this.modelUrlProviders = o.modelUrlProviders;
  113. (this.plugin.customState as ViewerState) = {
  114. showImportControls: o.showImportControls,
  115. showSessionControls: o.showSessionControls,
  116. modelLoader: new ModelLoader(this.plugin),
  117. collapsed: new BehaviorSubject<CollapsedState>({
  118. selection: true,
  119. measurements: true,
  120. superposition: true,
  121. component: false,
  122. volume: true,
  123. custom: true,
  124. }),
  125. }
  126. ReactDOM.render(React.createElement(Plugin, { plugin: this.plugin }), target)
  127. const renderer = this.plugin.canvas3d!.props.renderer;
  128. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { renderer: { ...renderer, backgroundColor: ColorNames.white } } });
  129. PluginCommands.Toast.Show(this.plugin, {
  130. title: 'Welcome',
  131. message: `RCSB PDB Mol* Viewer ${RCSB_MOLSTAR_VERSION} [${BUILD_DATE.toLocaleString()}]`,
  132. key: 'toast-welcome',
  133. timeoutMs: 5000
  134. })
  135. }
  136. //
  137. resetCamera(durationMs?: number) {
  138. this.plugin.managers.camera.reset(undefined, durationMs);
  139. }
  140. clear() {
  141. const state = this.plugin.state.data;
  142. return PluginCommands.State.RemoveObject(this.plugin, { state, ref: state.tree.root.ref })
  143. }
  144. async loadPdbId(pdbId: string, props?: PresetProps, matrix?: Mat4) {
  145. for (const provider of this.modelUrlProviders) {
  146. try {
  147. const p = provider(pdbId)
  148. await this.customState.modelLoader.load({ fileOrUrl: p.url, format: p.format, isBinary: p.isBinary }, props, matrix)
  149. break
  150. } catch (e) {
  151. console.warn(`loading '${pdbId}' failed with '${e}', trying next model-loader-provider`)
  152. }
  153. }
  154. }
  155. async loadPdbIds(args: { pdbId: string, props?: PresetProps, matrix?: Mat4 }[]) {
  156. for (const { pdbId, props, matrix } of args) {
  157. await this.loadPdbId(pdbId, props, matrix);
  158. }
  159. this.resetCamera(0);
  160. }
  161. loadStructureFromUrl(url: string, format: BuiltInTrajectoryFormat, isBinary: boolean, props?: PresetProps, matrix?: Mat4) {
  162. return this.customState.modelLoader.load({ fileOrUrl: url, format, isBinary }, props, matrix)
  163. }
  164. loadSnapshotFromUrl(url: string, type: PluginState.SnapshotType) {
  165. return PluginCommands.State.Snapshots.OpenUrl(this.plugin, { url, type });
  166. }
  167. }