hierarchy-state.ts 12 KB

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