hierarchy-preset.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 { PresetProvider } from '../preset-provider';
  7. import { PluginStateObject } from '../../objects';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  9. import { StateObjectRef, StateTransformer } from '../../../mol-state';
  10. import { StateTransforms } from '../../transforms';
  11. import { RootStructureDefinition } from '../../helpers/root-structure';
  12. import { PresetStructureReprentations } from './representation-preset';
  13. import { PluginContext } from '../../../mol-plugin/context';
  14. import { isProductionMode } from '../../../mol-util/debug';
  15. import { Task } from '../../../mol-task';
  16. export interface TrajectoryHierarchyPresetProvider<P = any, S = {}> extends PresetProvider<PluginStateObject.Molecule.Trajectory, P, S> { }
  17. export namespace TrajectoryHierarchyPresetProvider {
  18. export type Params<P extends TrajectoryHierarchyPresetProvider> = P extends TrajectoryHierarchyPresetProvider<infer T> ? T : never;
  19. export type State<P extends TrajectoryHierarchyPresetProvider> = P extends TrajectoryHierarchyPresetProvider<infer _, infer S> ? S : never;
  20. }
  21. export function TrajectoryHierarchyPresetProvider<P, S>(preset: TrajectoryHierarchyPresetProvider<P, S>) { return preset; }
  22. const CommonParams = (a: PluginStateObject.Molecule.Trajectory | undefined, plugin: PluginContext) => ({
  23. modelProperties: PD.Optional(PD.Group(StateTransformer.getParamDefinition(StateTransforms.Model.CustomModelProperties, void 0, plugin))),
  24. structureProperties: PD.Optional(PD.Group(StateTransformer.getParamDefinition(StateTransforms.Model.CustomStructureProperties, void 0, plugin))),
  25. representationPreset: PD.Optional(PD.Text<keyof PresetStructureReprentations>('auto' as const)),
  26. })
  27. const FirstModelParams = (a: PluginStateObject.Molecule.Trajectory | undefined, plugin: PluginContext) => ({
  28. model: PD.Optional(PD.Group(StateTransformer.getParamDefinition(StateTransforms.Model.ModelFromTrajectory, a, plugin))),
  29. showUnitcell: PD.Optional(PD.Boolean(true)),
  30. structure: PD.Optional(RootStructureDefinition.getParams(void 0, 'assembly').type),
  31. ...CommonParams(a, plugin)
  32. });
  33. const firstModel = TrajectoryHierarchyPresetProvider({
  34. id: 'preset-trajectory-first-model',
  35. display: { name: 'First Model', group: 'Preset' },
  36. params: FirstModelParams,
  37. async apply(trajectory, params, plugin) {
  38. const builder = plugin.builders.structure;
  39. const model = await builder.createModel(trajectory, params.model);
  40. const modelProperties = await builder.insertModelProperties(model, params.modelProperties);
  41. const structure = await builder.createStructure(modelProperties || model, params.structure);
  42. const structureProperties = await builder.insertStructureProperties(structure, params.structureProperties);
  43. const unitcell = params.showUnitcell === void 0 || !!params.showUnitcell ? await builder.tryCreateUnitcell(modelProperties, undefined, { isHidden: true }) : void 0;
  44. const representation = await plugin.builders.structure.representation.applyPreset(structureProperties, params.representationPreset || 'auto');
  45. return {
  46. model: modelProperties,
  47. modelRoot: model,
  48. modelProperties,
  49. unitcell,
  50. structure: structureProperties,
  51. structureRoot: structure,
  52. structureProperties,
  53. representation
  54. };
  55. }
  56. });
  57. const allModels = TrajectoryHierarchyPresetProvider({
  58. id: 'preset-trajectory-all-models',
  59. display: { name: 'All Models', group: 'Preset' },
  60. params: CommonParams,
  61. async apply(trajectory, params, plugin) {
  62. const tr = StateObjectRef.resolveAndCheck(plugin.state.data, trajectory)?.obj?.data;
  63. if (!tr) return { };
  64. const builder = plugin.builders.structure;
  65. const models = [], structures = [];
  66. for (let i = 0; i < tr.length; i++) {
  67. const model = await builder.createModel(trajectory, { modelIndex: i }, { isCollapsed: true });
  68. const modelProperties = await builder.insertModelProperties(model, params.modelProperties);
  69. const structure = await builder.createStructure(modelProperties || model, { name: 'deposited', params: {} });
  70. const structureProperties = await builder.insertStructureProperties(structure, params.structureProperties);
  71. models.push(model);
  72. structures.push(structure);
  73. await builder.representation.applyPreset(structureProperties, params.representationPreset || 'auto', { globalThemeName: 'model-index' });
  74. }
  75. return { models, structures };
  76. }
  77. });
  78. export const PresetStructureTrajectoryHierarchy = {
  79. 'first-model': firstModel,
  80. 'all-models': allModels
  81. };
  82. export type PresetStructureTrajectoryHierarchy = typeof PresetStructureTrajectoryHierarchy;
  83. // TODO: should there be a registry like for representations?
  84. export function applyTrajectoryHierarchyPreset<K extends keyof PresetStructureTrajectoryHierarchy>(plugin: PluginContext, parent: StateObjectRef<PluginStateObject.Molecule.Trajectory>, preset: K, params?: Partial<TrajectoryHierarchyPresetProvider.Params<PresetStructureTrajectoryHierarchy[K]>>): Promise<TrajectoryHierarchyPresetProvider.State<PresetStructureTrajectoryHierarchy[K]>> | undefined
  85. export function applyTrajectoryHierarchyPreset<P = any, S = {}>(plugin: PluginContext, parent: StateObjectRef<PluginStateObject.Molecule.Trajectory>, provider: TrajectoryHierarchyPresetProvider<P, S>, params?: P): Promise<S> | undefined
  86. export function applyTrajectoryHierarchyPreset(plugin: PluginContext, parent: StateObjectRef, providerRef: string | TrajectoryHierarchyPresetProvider, params?: any): Promise<any> | undefined {
  87. const provider = typeof providerRef === 'string' ? (PresetStructureTrajectoryHierarchy as any)[providerRef] : providerRef;
  88. if (!provider) return;
  89. const state = plugin.state.data;
  90. const cell = StateObjectRef.resolveAndCheck(state, parent);
  91. if (!cell) {
  92. if (!isProductionMode) console.warn(`Applying hierarchy preset provider to bad cell.`);
  93. return;
  94. }
  95. const prms = { ...PD.getDefaultValues(provider.params(cell.obj!, plugin) as PD.Params), ...params };
  96. const task = Task.create(`${provider.display.name}`, () => provider.apply(cell, prms, plugin) as Promise<any>);
  97. return plugin.runTask(task);
  98. }