hierarchy.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 { PluginContext } from '../../../mol-plugin/context';
  7. import { StructureHierarchy, buildStructureHierarchy, ModelRef, StructureComponentRef, StructureRef, HierarchyRef, TrajectoryRef } from './hierarchy-state';
  8. import { PluginComponent } from '../../component';
  9. import { SetUtils } from '../../../mol-util/set';
  10. interface StructureHierarchyManagerState {
  11. hierarchy: StructureHierarchy,
  12. current: {
  13. trajectories: ReadonlyArray<TrajectoryRef>,
  14. models: ReadonlyArray<ModelRef>,
  15. structures: ReadonlyArray<StructureRef>
  16. }
  17. }
  18. export class StructureHierarchyManager extends PluginComponent<StructureHierarchyManagerState> {
  19. readonly behaviors = {
  20. current: this.ev.behavior({
  21. hierarchy: this.state.hierarchy,
  22. trajectories: this.state.current.trajectories,
  23. models: this.state.current.models,
  24. structures: this.state.current.structures
  25. })
  26. }
  27. private get dataState() {
  28. return this.plugin.state.dataState;
  29. }
  30. private _currentComponentGroups: ReturnType<typeof StructureHierarchyManager['getComponentGroups']> | undefined = void 0;
  31. get currentComponentGroups() {
  32. if (this._currentComponentGroups) return this._currentComponentGroups;
  33. this._currentComponentGroups = StructureHierarchyManager.getComponentGroups(this.state.current.structures);
  34. return this._currentComponentGroups;
  35. }
  36. private _currentSelectionSet: Set<string> | undefined = void 0;
  37. get currentSeletionSet() {
  38. if (this._currentSelectionSet) return this._currentSelectionSet;
  39. this._currentSelectionSet = new Set();
  40. for (const r of this.state.current.trajectories) this._currentSelectionSet.add(r.cell.transform.ref);
  41. for (const r of this.state.current.models) this._currentSelectionSet.add(r.cell.transform.ref);
  42. for (const r of this.state.current.structures) this._currentSelectionSet.add(r.cell.transform.ref);
  43. return this._currentSelectionSet;
  44. }
  45. private syncCurrent<T extends HierarchyRef>(hierarchy: StructureHierarchy, current: ReadonlyArray<T>, all: ReadonlyArray<T>): T[] {
  46. if (current.length === 0) return all.length > 0 ? [all[0]] : [];
  47. const newCurrent: T[] = [];
  48. for (const c of current) {
  49. const ref = hierarchy.refs.get(c.cell.transform.ref) as T;
  50. if (ref) newCurrent.push(ref);
  51. }
  52. if (newCurrent.length === 0) return all.length > 0 ? [all[0]] : [];
  53. return newCurrent;
  54. }
  55. private sync() {
  56. const update = buildStructureHierarchy(this.plugin.state.dataState, this.state.hierarchy);
  57. if (update.added.length === 0 && update.updated.length === 0 && update.removed.length === 0) {
  58. return;
  59. }
  60. this._currentComponentGroups = void 0;
  61. this._currentSelectionSet = void 0;
  62. const { hierarchy } = update;
  63. const trajectories = this.syncCurrent(hierarchy, this.state.current.trajectories, hierarchy.trajectories);
  64. const models = this.syncCurrent(hierarchy, this.state.current.models, hierarchy.models);
  65. const structures = this.syncCurrent(hierarchy, this.state.current.structures, hierarchy.structures);
  66. this.updateState({ hierarchy, current: { trajectories, models, structures }});
  67. this.behaviors.current.next({ hierarchy, trajectories, models, structures });
  68. }
  69. updateCurrent(refs: HierarchyRef[], action: 'add' | 'remove') {
  70. const hierarchy = this.state.hierarchy;
  71. const set = action === 'add'
  72. ? SetUtils.union(this.currentSeletionSet, new Set(refs.map(r => r.cell.transform.ref)))
  73. : SetUtils.difference(this.currentSeletionSet, new Set(refs.map(r => r.cell.transform.ref)));
  74. const trajectories = [];
  75. const models = [];
  76. const structures = [];
  77. for (const t of hierarchy.trajectories) {
  78. if (set.has(t.cell.transform.ref)) trajectories.push(t);
  79. for (const m of t.models) {
  80. if (set.has(m.cell.transform.ref)) models.push(m);
  81. for (const s of m.structures) {
  82. if (set.has(s.cell.transform.ref)) structures.push(s);
  83. }
  84. }
  85. }
  86. this._currentComponentGroups = void 0;
  87. this._currentSelectionSet = void 0;
  88. this.updateState({ current: { trajectories, models, structures }});
  89. this.behaviors.current.next({ hierarchy, trajectories, models, structures });
  90. }
  91. remove(refs: HierarchyRef[]) {
  92. if (refs.length === 0) return;
  93. const deletes = this.plugin.state.dataState.build();
  94. for (const r of refs) deletes.delete(r.cell.transform.ref);
  95. return this.plugin.runTask(this.plugin.state.dataState.updateTree(deletes));
  96. }
  97. createAllModels(trajectory: TrajectoryRef) {
  98. return this.plugin.dataTransaction(async () => {
  99. if (trajectory.models.length > 0) {
  100. await this.clearTrajectory(trajectory);
  101. }
  102. const tr = trajectory.cell.obj?.data!;
  103. for (let i = 0; i < tr.length; i++) {
  104. const model = await this.plugin.builders.structure.createModel(trajectory.cell, { modelIndex: i });
  105. const structure = await this.plugin.builders.structure.createStructure(model, { name: 'deposited', params: { } });
  106. await this.plugin.builders.structure.representation.structurePreset(structure, 'auto', { globalThemeName: 'model-index' });
  107. }
  108. })
  109. }
  110. private clearTrajectory(trajectory: TrajectoryRef) {
  111. const builder = this.dataState.build();
  112. for (const m of trajectory.models) {
  113. builder.delete(m.cell);
  114. }
  115. return this.plugin.runTask(this.dataState.updateTree(builder));
  116. }
  117. constructor(private plugin: PluginContext) {
  118. super({
  119. hierarchy: StructureHierarchy(),
  120. current: { trajectories: [], models: [], structures: [] }
  121. });
  122. plugin.state.dataState.events.changed.subscribe(e => {
  123. if (e.inTransaction || plugin.behaviors.state.isAnimating.value) return;
  124. this.sync();
  125. });
  126. plugin.behaviors.state.isAnimating.subscribe(isAnimating => {
  127. if (!isAnimating && !plugin.behaviors.state.isUpdating.value) this.sync();
  128. });
  129. }
  130. }
  131. export namespace StructureHierarchyManager {
  132. export function getComponentGroups(structures: ReadonlyArray<StructureRef>): StructureComponentRef[][] {
  133. if (!structures.length) return [];
  134. if (structures.length === 1) return structures[0].components.map(c => [c]);
  135. const groups: StructureComponentRef[][] = [];
  136. const map = new Map<string, StructureComponentRef[]>();
  137. for (const s of structures) {
  138. for (const c of s.components) {
  139. const key = c.key;
  140. if (!key) continue;
  141. let component = map.get(key);
  142. if (!component) {
  143. component = [];
  144. map.set(key, component);
  145. groups.push(component);
  146. }
  147. component.push(c);
  148. }
  149. }
  150. return groups;
  151. }
  152. }