hierarchy.ts 9.3 KB

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