hierarchy.ts 8.6 KB

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