index.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 { Structure } from '../../mol-model/structure';
  8. import { BuiltInTrajectoryFormat } from '../../mol-plugin-state/formats/trajectory';
  9. import { PluginStateObject as PSO, PluginStateTransform } from '../../mol-plugin-state/objects';
  10. import { createPluginUI } from '../../mol-plugin-ui';
  11. import { PluginUIContext } from '../../mol-plugin-ui/context';
  12. import { PluginLayoutControlsDisplay } from '../../mol-plugin/layout';
  13. import { DefaultPluginUISpec, PluginUISpec } from '../../mol-plugin-ui/spec';
  14. import { PluginBehaviors } from '../../mol-plugin/behavior';
  15. import { PluginCommands } from '../../mol-plugin/commands';
  16. import { PluginConfig } from '../../mol-plugin/config';
  17. import { PluginSpec } from '../../mol-plugin/spec';
  18. import { StateObject } from '../../mol-state';
  19. import { Task } from '../../mol-task';
  20. import { Color } from '../../mol-util/color';
  21. import { ColorNames } from '../../mol-util/color/names';
  22. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  23. import '../../mol-util/polyfill';
  24. import { ObjectKeys } from '../../mol-util/type-helpers';
  25. import './index.html';
  26. import { ShowButtons, StructurePreset, ViewportComponent } from './viewport';
  27. require('mol-plugin-ui/skin/light.scss');
  28. export { PLUGIN_VERSION as version } from '../../mol-plugin/version';
  29. export { setDebugMode, setProductionMode } from '../../mol-util/debug';
  30. export { Viewer as DockingViewer };
  31. const DefaultViewerOptions = {
  32. extensions: ObjectKeys({}),
  33. layoutIsExpanded: true,
  34. layoutShowControls: true,
  35. layoutShowRemoteState: true,
  36. layoutControlsDisplay: 'reactive' as PluginLayoutControlsDisplay,
  37. layoutShowSequence: true,
  38. layoutShowLog: true,
  39. layoutShowLeftPanel: true,
  40. viewportShowExpand: PluginConfig.Viewport.ShowExpand.defaultValue,
  41. viewportShowControls: PluginConfig.Viewport.ShowControls.defaultValue,
  42. viewportShowSettings: PluginConfig.Viewport.ShowSettings.defaultValue,
  43. viewportShowSelectionMode: PluginConfig.Viewport.ShowSelectionMode.defaultValue,
  44. viewportShowAnimation: PluginConfig.Viewport.ShowAnimation.defaultValue,
  45. pluginStateServer: PluginConfig.State.DefaultServer.defaultValue,
  46. volumeStreamingServer: PluginConfig.VolumeStreaming.DefaultServer.defaultValue,
  47. pdbProvider: PluginConfig.Download.DefaultPdbProvider.defaultValue,
  48. emdbProvider: PluginConfig.Download.DefaultEmdbProvider.defaultValue,
  49. };
  50. class Viewer {
  51. constructor(public plugin: PluginUIContext) {
  52. }
  53. static async create(elementOrId: string | HTMLElement, colors = [Color(0x992211), Color(0xDDDDDD)], showButtons = true) {
  54. const o = { ...DefaultViewerOptions, ...{
  55. layoutIsExpanded: false,
  56. layoutShowControls: false,
  57. layoutShowRemoteState: false,
  58. layoutShowSequence: true,
  59. layoutShowLog: false,
  60. layoutShowLeftPanel: true,
  61. viewportShowExpand: true,
  62. viewportShowControls: false,
  63. viewportShowSettings: false,
  64. viewportShowSelectionMode: false,
  65. viewportShowAnimation: false,
  66. } };
  67. const defaultSpec = DefaultPluginUISpec();
  68. const spec: PluginUISpec = {
  69. actions: defaultSpec.actions,
  70. behaviors: [
  71. PluginSpec.Behavior(PluginBehaviors.Representation.HighlightLoci, { mark: false }),
  72. PluginSpec.Behavior(PluginBehaviors.Representation.DefaultLociLabelProvider),
  73. PluginSpec.Behavior(PluginBehaviors.Camera.FocusLoci),
  74. PluginSpec.Behavior(PluginBehaviors.CustomProps.StructureInfo),
  75. PluginSpec.Behavior(PluginBehaviors.CustomProps.Interactions),
  76. PluginSpec.Behavior(PluginBehaviors.CustomProps.SecondaryStructure),
  77. ],
  78. animations: defaultSpec.animations,
  79. customParamEditors: defaultSpec.customParamEditors,
  80. layout: {
  81. initial: {
  82. isExpanded: o.layoutIsExpanded,
  83. showControls: o.layoutShowControls,
  84. controlsDisplay: o.layoutControlsDisplay,
  85. },
  86. },
  87. components: {
  88. ...defaultSpec.components,
  89. controls: {
  90. ...defaultSpec.components?.controls,
  91. top: o.layoutShowSequence ? undefined : 'none',
  92. bottom: o.layoutShowLog ? undefined : 'none',
  93. left: o.layoutShowLeftPanel ? undefined : 'none',
  94. },
  95. remoteState: o.layoutShowRemoteState ? 'default' : 'none',
  96. viewport: {
  97. view: ViewportComponent
  98. }
  99. },
  100. config: [
  101. [PluginConfig.Viewport.ShowExpand, o.viewportShowExpand],
  102. [PluginConfig.Viewport.ShowControls, o.viewportShowControls],
  103. [PluginConfig.Viewport.ShowSettings, o.viewportShowSettings],
  104. [PluginConfig.Viewport.ShowSelectionMode, o.viewportShowSelectionMode],
  105. [PluginConfig.Viewport.ShowAnimation, o.viewportShowAnimation],
  106. [PluginConfig.State.DefaultServer, o.pluginStateServer],
  107. [PluginConfig.State.CurrentServer, o.pluginStateServer],
  108. [PluginConfig.VolumeStreaming.DefaultServer, o.volumeStreamingServer],
  109. [PluginConfig.Download.DefaultPdbProvider, o.pdbProvider],
  110. [PluginConfig.Download.DefaultEmdbProvider, o.emdbProvider],
  111. [ShowButtons, showButtons]
  112. ]
  113. };
  114. const element = typeof elementOrId === 'string'
  115. ? document.getElementById(elementOrId)
  116. : elementOrId;
  117. if (!element) throw new Error(`Could not get element with id '${elementOrId}'`);
  118. const plugin = await createPluginUI(element, spec);
  119. (plugin.customState as any) = {
  120. colorPalette: {
  121. name: 'colors',
  122. params: { list: { colors } }
  123. }
  124. };
  125. plugin.behaviors.canvas3d.initialized.subscribe(v => {
  126. if (v) {
  127. PluginCommands.Canvas3D.SetSettings(plugin, { settings: {
  128. renderer: {
  129. ...plugin.canvas3d!.props.renderer,
  130. backgroundColor: ColorNames.white,
  131. },
  132. camera: {
  133. ...plugin.canvas3d!.props.camera,
  134. helper: { axes: { name: 'off', params: {} } }
  135. }
  136. } });
  137. }
  138. });
  139. return new Viewer(plugin);
  140. }
  141. async loadStructuresFromUrlsAndMerge(sources: { url: string, format: BuiltInTrajectoryFormat, isBinary?: boolean }[]) {
  142. const structures: { ref: string }[] = [];
  143. for (const { url, format, isBinary } of sources) {
  144. const data = await this.plugin.builders.data.download({ url, isBinary });
  145. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format);
  146. const model = await this.plugin.builders.structure.createModel(trajectory);
  147. const modelProperties = await this.plugin.builders.structure.insertModelProperties(model);
  148. const structure = await this.plugin.builders.structure.createStructure(modelProperties || model);
  149. const structureProperties = await this.plugin.builders.structure.insertStructureProperties(structure);
  150. structures.push({ ref: structureProperties?.ref || structure.ref });
  151. }
  152. // remove current structuresfrom hierarchy as they will be merged
  153. // TODO only works with using loadStructuresFromUrlsAndMerge once
  154. // need some more API metho to work with the hierarchy
  155. this.plugin.managers.structure.hierarchy.updateCurrent(this.plugin.managers.structure.hierarchy.current.structures, 'remove');
  156. const dependsOn = structures.map(({ ref }) => ref);
  157. const data = this.plugin.state.data.build().toRoot().apply(MergeStructures, { structures }, { dependsOn });
  158. const structure = await data.commit();
  159. const structureProperties = await this.plugin.builders.structure.insertStructureProperties(structure);
  160. this.plugin.behaviors.canvas3d.initialized.subscribe(async v => {
  161. await this.plugin.builders.structure.representation.applyPreset(structureProperties || structure, StructurePreset);
  162. });
  163. }
  164. }
  165. type MergeStructures = typeof MergeStructures
  166. const MergeStructures = PluginStateTransform.BuiltIn({
  167. name: 'merge-structures',
  168. display: { name: 'Merge Structures', description: 'Merge Structure' },
  169. from: PSO.Root,
  170. to: PSO.Molecule.Structure,
  171. params: {
  172. structures: PD.ObjectList({
  173. ref: PD.Text('')
  174. }, ({ ref }) => ref, { isHidden: true })
  175. }
  176. })({
  177. apply({ params, dependencies }) {
  178. return Task.create('Merge Structures', async ctx => {
  179. if (params.structures.length === 0) return StateObject.Null;
  180. const first = dependencies![params.structures[0].ref].data as Structure;
  181. const builder = Structure.Builder({ masterModel: first.models[0] });
  182. for (const { ref } of params.structures) {
  183. const s = dependencies![ref].data as Structure;
  184. for (const unit of s.units) {
  185. // TODO invariantId
  186. builder.addUnit(unit.kind, unit.model, unit.conformation.operator, unit.elements, unit.traits);
  187. }
  188. }
  189. const structure = builder.getStructure();
  190. return new PSO.Molecule.Structure(structure, { label: 'Merged Structure' });
  191. });
  192. }
  193. });
  194. (window as any).DockingViewer = Viewer;