model.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { LoadParams, ParseParams } from '../types';
  7. import { PluginContext } from 'molstar/lib/mol-plugin/context';
  8. import { PresetProps, RcsbPreset } from './preset';
  9. import { Asset } from 'molstar/lib/mol-util/assets';
  10. import { Mat4 } from 'molstar/lib/mol-math/linear-algebra';
  11. import { StateTransforms } from 'molstar/lib/mol-plugin-state/transforms';
  12. import { BuiltInTrajectoryFormat } from 'molstar/lib/mol-plugin-state/formats/trajectory';
  13. import { TrajectoryHierarchyPresetProvider } from 'molstar/lib/mol-plugin-state/builder/structure/hierarchy-preset';
  14. import { StateObjectRef } from 'molstar/lib/mol-state';
  15. import { ModelExport } from 'molstar/lib/extensions/model-export/export';
  16. export class ModelLoader {
  17. async load<P = any, S = {}>(load: LoadParams, props?: PresetProps & { dataLabel?: string }, matrix?: Mat4, reprProvider?: TrajectoryHierarchyPresetProvider<P, S>, params?: P) {
  18. const { fileOrUrl, format, isBinary } = load;
  19. const data = fileOrUrl instanceof File
  20. ? (await this.plugin.builders.data.readFile({ file: Asset.File(fileOrUrl), isBinary, label: props?.dataLabel })).data
  21. : await this.plugin.builders.data.download({ url: fileOrUrl, isBinary, label: props?.dataLabel });
  22. const hierarchy = await this.handleTrajectory<P, S>(data, format, props, matrix, reprProvider, params) as any;
  23. const structureCell = StateObjectRef.resolveAndCheck(this.plugin.state.data, hierarchy.structure);
  24. ModelExport.setStructureName(structureCell?.obj?.data, props?.dataLabel || '');
  25. return hierarchy;
  26. }
  27. async parse<P = any, S = {}>(parse: ParseParams, props?: PresetProps & { dataLabel?: string }, matrix?: Mat4, reprProvider?: TrajectoryHierarchyPresetProvider<P, S>, params?: P) {
  28. const { data, format } = parse;
  29. const _data = await this.plugin.builders.data.rawData({ data, label: props?.dataLabel });
  30. return await this.handleTrajectory(_data, format, props, matrix, reprProvider, params);
  31. }
  32. private async handleTrajectory<P = any, S = {}>(
  33. data: any,
  34. format: BuiltInTrajectoryFormat,
  35. props?: PresetProps,
  36. matrix?: Mat4,
  37. reprProvider?: TrajectoryHierarchyPresetProvider<P, S>,
  38. params?: P
  39. ): Promise<S | ReturnType<typeof RcsbPreset.apply> | undefined> {
  40. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format);
  41. if (reprProvider) {
  42. return this.plugin.builders.structure.hierarchy.applyPreset(trajectory, reprProvider, params);
  43. } else {
  44. const selector = await this.plugin.builders.structure.hierarchy.applyPreset(trajectory, RcsbPreset, {
  45. preset: props || { kind: 'standard', assemblyId: '' }
  46. });
  47. if (matrix && selector?.structureProperties) {
  48. const params = {
  49. transform: {
  50. name: 'matrix' as const,
  51. params: { data: matrix, transpose: false }
  52. }
  53. };
  54. const b = this.plugin.state.data.build().to(selector.structureProperties)
  55. .insert(StateTransforms.Model.TransformStructureConformation, params);
  56. await this.plugin.runTask(this.plugin.state.data.updateTree(b));
  57. }
  58. return selector;
  59. }
  60. }
  61. constructor(private plugin: PluginContext) {
  62. }
  63. }