hierarchy-state.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { PluginStateObject as SO } from '../../objects';
  7. import { StateObject, StateTransform, State, StateObjectCell, StateTree, StateTransformer } from '../../../mol-state';
  8. import { StructureBuilderTags } from '../../builder/structure';
  9. import { RepresentationProviderTags } from '../../builder/structure/provider';
  10. import { StructureRepresentationInteractionTags } from '../../../mol-plugin/behavior/dynamic/selection/structure-representation-interaction';
  11. import { StateTransforms } from '../../transforms';
  12. import { VolumeStreaming } from '../../../mol-plugin/behavior/dynamic/volume-streaming/behavior';
  13. import { CreateVolumeStreamingBehavior } from '../../../mol-plugin/behavior/dynamic/volume-streaming/transformers';
  14. export function buildStructureHierarchy(state: State, previous?: StructureHierarchy) {
  15. const build = BuildState(state, previous || StructureHierarchy());
  16. // StateTree.doPreOrder(state.tree, state.tree.root, build, visitCell);
  17. doPreOrder(state.tree, build);
  18. if (previous) previous.refs.forEach(isRemoved, build);
  19. return { hierarchy: build.hierarchy, added: build.added, updated: build.updated, removed: build.removed };
  20. }
  21. export interface StructureHierarchy {
  22. trajectories: TrajectoryRef[],
  23. models: ModelRef[],
  24. structures: StructureRef[],
  25. refs: Map<StateTransform.Ref, HierarchyRef>
  26. }
  27. export function StructureHierarchy(): StructureHierarchy {
  28. return { trajectories: [], models: [], structures: [], refs: new Map() }
  29. }
  30. interface RefBase<K extends string = string, O extends StateObject = StateObject, T extends StateTransformer = StateTransformer> {
  31. kind: K,
  32. cell: StateObjectCell<O, StateTransform<T>>,
  33. version: StateTransform['version']
  34. }
  35. export type HierarchyRef =
  36. | TrajectoryRef
  37. | ModelRef | ModelPropertiesRef
  38. | StructureRef | StructurePropertiesRef | StructureVolumeStreamingRef | StructureComponentRef | StructureRepresentationRef
  39. | GenericRepresentationRef
  40. export interface TrajectoryRef extends RefBase<'trajectory', SO.Molecule.Trajectory> {
  41. models: ModelRef[]
  42. }
  43. function TrajectoryRef(cell: StateObjectCell<SO.Molecule.Trajectory>): TrajectoryRef {
  44. return { kind: 'trajectory', cell, version: cell.transform.version, models: [] };
  45. }
  46. export interface ModelRef extends RefBase<'model', SO.Molecule.Model> {
  47. trajectory?: TrajectoryRef,
  48. properties?: ModelPropertiesRef,
  49. structures: StructureRef[],
  50. genericRepresentations?: GenericRepresentationRef[],
  51. }
  52. function ModelRef(cell: StateObjectCell<SO.Molecule.Model>, trajectory?: TrajectoryRef): ModelRef {
  53. return { kind: 'model', cell, version: cell.transform.version, trajectory, structures: [] };
  54. }
  55. export interface ModelPropertiesRef extends RefBase<'model-properties', SO.Molecule.Model, StateTransforms['Model']['CustomModelProperties']> {
  56. model: ModelRef
  57. }
  58. function ModelPropertiesRef(cell: StateObjectCell<SO.Molecule.Model>, model: ModelRef): ModelPropertiesRef {
  59. return { kind: 'model-properties', cell, version: cell.transform.version, model };
  60. }
  61. export interface StructureRef extends RefBase<'structure', SO.Molecule.Structure> {
  62. model?: ModelRef,
  63. properties?: StructurePropertiesRef,
  64. components: StructureComponentRef[],
  65. currentFocus?: {
  66. focus?: StructureComponentRef,
  67. surroundings?: StructureComponentRef,
  68. },
  69. genericRepresentations?: GenericRepresentationRef[],
  70. volumeStreaming?: StructureVolumeStreamingRef
  71. }
  72. function StructureRef(cell: StateObjectCell<SO.Molecule.Structure>, model?: ModelRef): StructureRef {
  73. return { kind: 'structure', cell, version: cell.transform.version, model, components: [] };
  74. }
  75. export interface StructurePropertiesRef extends RefBase<'structure-properties', SO.Molecule.Structure, StateTransforms['Model']['CustomStructureProperties']> {
  76. structure: StructureRef
  77. }
  78. function StructurePropertiesRef(cell: StateObjectCell<SO.Molecule.Structure>, structure: StructureRef): StructurePropertiesRef {
  79. return { kind: 'structure-properties', cell, version: cell.transform.version, structure };
  80. }
  81. export interface StructureVolumeStreamingRef extends RefBase<'structure-volume-streaming', VolumeStreaming, CreateVolumeStreamingBehavior> {
  82. structure: StructureRef
  83. }
  84. function StructureVolumeStreamingRef(cell: StateObjectCell<VolumeStreaming>, structure: StructureRef): StructureVolumeStreamingRef {
  85. return { kind: 'structure-volume-streaming', cell, version: cell.transform.version, structure };
  86. }
  87. export interface StructureComponentRef extends RefBase<'structure-component', SO.Molecule.Structure, StateTransforms['Model']['StructureComponent']> {
  88. structure: StructureRef,
  89. key?: string,
  90. representations: StructureRepresentationRef[],
  91. genericRepresentations?: GenericRepresentationRef[]
  92. }
  93. function componentKey(cell: StateObjectCell<SO.Molecule.Structure>) {
  94. if (!cell.transform.tags) return;
  95. return [...cell.transform.tags].sort().join();
  96. }
  97. function StructureComponentRef(cell: StateObjectCell<SO.Molecule.Structure>, structure: StructureRef): StructureComponentRef {
  98. return { kind: 'structure-component', cell, version: cell.transform.version, structure, key: componentKey(cell), representations: [] };
  99. }
  100. export interface StructureRepresentationRef extends RefBase<'structure-representation', SO.Molecule.Structure.Representation3D, StateTransforms['Representation']['StructureRepresentation3D']> {
  101. component: StructureComponentRef
  102. }
  103. function StructureRepresentationRef(cell: StateObjectCell<SO.Molecule.Structure.Representation3D>, component: StructureComponentRef): StructureRepresentationRef {
  104. return { kind: 'structure-representation', cell, version: cell.transform.version, component };
  105. }
  106. export interface GenericRepresentationRef extends RefBase<'generic-representation', SO.Any> {
  107. parent: HierarchyRef
  108. }
  109. function GenericRepresentationRef(cell: StateObjectCell<SO.Molecule.Structure.Representation3D>, parent: HierarchyRef): GenericRepresentationRef {
  110. return { kind: 'generic-representation', cell, version: cell.transform.version, parent };
  111. }
  112. interface BuildState {
  113. state: State,
  114. oldHierarchy: StructureHierarchy,
  115. hierarchy: StructureHierarchy,
  116. currentTrajectory?: TrajectoryRef,
  117. currentModel?: ModelRef,
  118. currentStructure?: StructureRef,
  119. currentComponent?: StructureComponentRef,
  120. updated: HierarchyRef[],
  121. added: HierarchyRef[],
  122. removed: HierarchyRef[]
  123. }
  124. function BuildState(state: State, oldHierarchy: StructureHierarchy): BuildState {
  125. return { state, oldHierarchy, hierarchy: StructureHierarchy(), updated: [], added: [], removed: [] };
  126. }
  127. function createOrUpdateRefList<R extends HierarchyRef, C extends any[]>(state: BuildState, cell: StateObjectCell, list: R[], ctor: (...args: C) => R, ...args: C) {
  128. const ref: R = ctor(...args);
  129. list.push(ref);
  130. state.hierarchy.refs.set(cell.transform.ref, ref);
  131. const old = state.oldHierarchy.refs.get(cell.transform.ref);
  132. if (old) {
  133. if (old.version !== cell.transform.version) state.updated.push(ref);
  134. } else {
  135. state.added.push(ref);
  136. }
  137. return ref;
  138. }
  139. function createOrUpdateRef<R extends HierarchyRef, C extends any[]>(state: BuildState, cell: StateObjectCell, ctor: (...args: C) => R, ...args: C) {
  140. const ref: R = ctor(...args);
  141. state.hierarchy.refs.set(cell.transform.ref, ref);
  142. const old = state.oldHierarchy.refs.get(cell.transform.ref);
  143. if (old) {
  144. if (old.version !== cell.transform.version) state.updated.push(ref);
  145. } else {
  146. state.added.push(ref);
  147. }
  148. return ref;
  149. }
  150. const tagMap: [string, (state: BuildState, cell: StateObjectCell) => boolean | void, (state: BuildState) => any][] = [
  151. [StructureBuilderTags.Trajectory, (state, cell) => {
  152. state.currentTrajectory = createOrUpdateRefList(state, cell, state.hierarchy.trajectories, TrajectoryRef, cell);
  153. }, state => state.currentTrajectory = void 0],
  154. [StructureBuilderTags.Model, (state, cell) => {
  155. if (state.currentTrajectory) {
  156. state.currentModel = createOrUpdateRefList(state, cell, state.currentTrajectory.models, ModelRef, cell, state.currentTrajectory);
  157. } else {
  158. state.currentModel = createOrUpdateRef(state, cell, ModelRef, cell);
  159. }
  160. state.hierarchy.models.push(state.currentModel);
  161. }, state => state.currentModel = void 0],
  162. [StructureBuilderTags.ModelProperties, (state, cell) => {
  163. if (!state.currentModel) return false;
  164. state.currentModel.properties = createOrUpdateRef(state, cell, ModelPropertiesRef, cell, state.currentModel);
  165. }, state => { }],
  166. [StructureBuilderTags.Structure, (state, cell) => {
  167. if (state.currentModel) {
  168. state.currentStructure = createOrUpdateRefList(state, cell, state.currentModel.structures, StructureRef, cell, state.currentModel);
  169. } else {
  170. state.currentStructure = createOrUpdateRef(state, cell, StructureRef, cell);
  171. }
  172. state.hierarchy.structures.push(state.currentStructure);
  173. }, state => state.currentStructure = void 0],
  174. [StructureBuilderTags.StructureProperties, (state, cell) => {
  175. if (!state.currentStructure) return false;
  176. state.currentStructure.properties = createOrUpdateRef(state, cell, StructurePropertiesRef, cell, state.currentStructure);
  177. }, state => { }],
  178. [StructureBuilderTags.Component, (state, cell) => {
  179. if (!state.currentStructure) return false;
  180. state.currentComponent = createOrUpdateRefList(state, cell, state.currentStructure.components, StructureComponentRef, cell, state.currentStructure);
  181. }, state => state.currentComponent = void 0],
  182. [RepresentationProviderTags.Representation, (state, cell) => {
  183. if (!state.currentComponent) return false;
  184. createOrUpdateRefList(state, cell, state.currentComponent.representations, StructureRepresentationRef, cell, state.currentComponent);
  185. }, state => { }],
  186. [StructureRepresentationInteractionTags.ResidueSel, (state, cell) => {
  187. if (!state.currentStructure) return false;
  188. if (!state.currentStructure.currentFocus) state.currentStructure.currentFocus = { };
  189. state.currentStructure.currentFocus.focus = createOrUpdateRef(state, cell, StructureComponentRef, cell, state.currentStructure);
  190. state.currentComponent = state.currentStructure.currentFocus.focus;
  191. }, state => state.currentComponent = void 0],
  192. [StructureRepresentationInteractionTags.SurrSel, (state, cell) => {
  193. if (!state.currentStructure) return false;
  194. if (!state.currentStructure.currentFocus) state.currentStructure.currentFocus = { };
  195. state.currentStructure.currentFocus.surroundings = createOrUpdateRef(state, cell, StructureComponentRef, cell, state.currentStructure);
  196. state.currentComponent = state.currentStructure.currentFocus.surroundings;
  197. }, state => state.currentComponent = void 0]
  198. ]
  199. function isValidCell(cell?: StateObjectCell): cell is StateObjectCell {
  200. if (!cell || !cell.parent.cells.has(cell.transform.ref)) return false;
  201. const { obj } = cell;
  202. if (!obj || obj === StateObject.Null || (cell.status !== 'ok' && cell.status !== 'error')) return false;
  203. return true;
  204. }
  205. function isRemoved(this: BuildState, ref: HierarchyRef) {
  206. const { cell } = ref;
  207. if (isValidCell(cell)) return;
  208. this.removed.push(ref);
  209. }
  210. type VisitorCtx = { tree: StateTree, state: BuildState };
  211. function _preOrderFunc(this: VisitorCtx, c: StateTransform.Ref | undefined) { _doPreOrder(this, this.tree.transforms.get(c!)!); }
  212. function _doPreOrder(ctx: VisitorCtx, root: StateTransform) {
  213. const { state } = ctx;
  214. const cell = state.state.cells.get(root.ref);
  215. if (!isValidCell(cell)) return;
  216. let onLeave: undefined | ((state: BuildState) => any) = void 0;
  217. for (const [t, f, l] of tagMap) {
  218. if (StateObject.hasTag(cell.obj!, t)) {
  219. const stop = f(state, cell);
  220. if (stop === false) {
  221. return;
  222. }
  223. onLeave = l;
  224. break;
  225. }
  226. }
  227. if (!onLeave && !cell.state.isGhost && state.currentComponent && SO.Molecule.Structure.Representation3D.is(cell.obj)) {
  228. createOrUpdateRefList(state, cell, state.currentComponent.representations, StructureRepresentationRef, cell, state.currentComponent);
  229. } else if (!cell.state.isGhost && SO.isRepresentation3D(cell.obj)) {
  230. const genericTarget = state.currentComponent || state.currentModel || state.currentStructure;
  231. if (genericTarget) {
  232. if (!genericTarget.genericRepresentations) genericTarget.genericRepresentations = [];
  233. genericTarget.genericRepresentations.push(createOrUpdateRef(state, cell, GenericRepresentationRef, cell, genericTarget));
  234. }
  235. } else if (state.currentStructure && VolumeStreaming.is(cell.obj)) {
  236. state.currentStructure.volumeStreaming = createOrUpdateRef(state, cell, StructureVolumeStreamingRef, cell, state.currentStructure);
  237. return;
  238. }
  239. const children = ctx.tree.children.get(root.ref);
  240. if (children && children.size) {
  241. children.forEach(_preOrderFunc, ctx);
  242. }
  243. if (onLeave) onLeave(state);
  244. }
  245. function doPreOrder(tree: StateTree, state: BuildState): BuildState {
  246. const ctx: VisitorCtx = { tree, state };
  247. _doPreOrder(ctx, tree.root);
  248. return ctx.state;
  249. }