index.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. backgroundColor: ColorNames.white,
  66. showWelcomeToast: true
  67. };
  68. type ViewerProps = typeof DefaultViewerProps
  69. export class Viewer {
  70. private readonly plugin: PluginContext;
  71. private readonly modelUrlProviders: ModelUrlProvider[];
  72. private get customState() {
  73. return this.plugin.customState as ViewerState
  74. }
  75. constructor(target: string | HTMLElement, props: Partial<ViewerProps> = {}) {
  76. target = typeof target === 'string' ? document.getElementById(target)! : target
  77. const o = { ...DefaultViewerProps, ...props }
  78. const spec: PluginSpec = {
  79. actions: [...DefaultPluginSpec.actions],
  80. behaviors: [
  81. ...DefaultPluginSpec.behaviors,
  82. ...o.extensions.map(e => Extensions[e]),
  83. ],
  84. animations: [...DefaultPluginSpec.animations || []],
  85. customParamEditors: DefaultPluginSpec.customParamEditors,
  86. layout: {
  87. initial: {
  88. isExpanded: o.layoutIsExpanded,
  89. showControls: o.layoutShowControls,
  90. controlsDisplay: o.layoutControlsDisplay,
  91. },
  92. controls: {
  93. ...DefaultPluginSpec.layout && DefaultPluginSpec.layout.controls,
  94. top: o.layoutShowSequence ? undefined : 'none',
  95. bottom: o.layoutShowLog ? undefined : 'none',
  96. left: 'none',
  97. right: ControlsWrapper,
  98. }
  99. },
  100. components: {
  101. ...DefaultPluginSpec.components,
  102. remoteState: 'none',
  103. },
  104. config: [
  105. [PluginConfig.Viewport.ShowExpand, o.viewportShowExpand],
  106. [PluginConfig.Viewport.ShowSelectionMode, o.viewportShowSelectionMode],
  107. [PluginConfig.Viewport.ShowAnimation, false],
  108. [PluginConfig.VolumeStreaming.DefaultServer, o.volumeStreamingServer],
  109. [PluginConfig.Download.DefaultPdbProvider, 'rcsb'],
  110. [PluginConfig.Download.DefaultEmdbProvider, 'rcsb']
  111. ]
  112. };
  113. this.plugin = new PluginContext(spec);
  114. this.modelUrlProviders = o.modelUrlProviders;
  115. (this.plugin.customState as ViewerState) = {
  116. showImportControls: o.showImportControls,
  117. showSessionControls: o.showSessionControls,
  118. modelLoader: new ModelLoader(this.plugin),
  119. collapsed: new BehaviorSubject<CollapsedState>({
  120. selection: true,
  121. measurements: true,
  122. superposition: true,
  123. component: false,
  124. volume: true,
  125. custom: true,
  126. }),
  127. }
  128. ReactDOM.render(React.createElement(Plugin, { plugin: this.plugin }), target)
  129. const renderer = this.plugin.canvas3d!.props.renderer;
  130. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { renderer: { ...renderer, backgroundColor: o.backgroundColor } } });
  131. if (o.showWelcomeToast) {
  132. PluginCommands.Toast.Show(this.plugin, {
  133. title: 'Welcome',
  134. message: `RCSB PDB Mol* Viewer ${RCSB_MOLSTAR_VERSION} [${BUILD_DATE.toLocaleString()}]`,
  135. key: 'toast-welcome',
  136. timeoutMs: 5000
  137. })
  138. }
  139. }
  140. //
  141. resetCamera(durationMs?: number) {
  142. this.plugin.managers.camera.reset(undefined, durationMs);
  143. }
  144. clear() {
  145. const state = this.plugin.state.data;
  146. return PluginCommands.State.RemoveObject(this.plugin, { state, ref: state.tree.root.ref })
  147. }
  148. async loadPdbId(pdbId: string, props?: PresetProps, matrix?: Mat4) {
  149. for (const provider of this.modelUrlProviders) {
  150. try {
  151. const p = provider(pdbId)
  152. await this.customState.modelLoader.load({ fileOrUrl: p.url, format: p.format, isBinary: p.isBinary }, props, matrix)
  153. break
  154. } catch (e) {
  155. console.warn(`loading '${pdbId}' failed with '${e}', trying next model-loader-provider`)
  156. }
  157. }
  158. }
  159. async loadPdbIds(args: { pdbId: string, props?: PresetProps, matrix?: Mat4 }[]) {
  160. for (const { pdbId, props, matrix } of args) {
  161. await this.loadPdbId(pdbId, props, matrix);
  162. }
  163. this.resetCamera(0);
  164. }
  165. loadStructureFromUrl(url: string, format: BuiltInTrajectoryFormat, isBinary: boolean, props?: PresetProps, matrix?: Mat4) {
  166. return this.customState.modelLoader.load({ fileOrUrl: url, format, isBinary }, props, matrix)
  167. }
  168. loadSnapshotFromUrl(url: string, type: PluginState.SnapshotType) {
  169. return PluginCommands.State.Snapshots.OpenUrl(this.plugin, { url, type });
  170. }
  171. async loadStructureFromData(data: string | number[], format: BuiltInTrajectoryFormat, isBinary: boolean, props?: PresetProps & { dataLabel?: string }, matrix?: Mat4) {
  172. return this.customState.modelLoader.parse({ data, format, isBinary }, props, matrix);
  173. }
  174. }