model.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. export class ModelLoader {
  14. async load(load: LoadParams, props?: PresetProps, matrix?: Mat4) {
  15. const { fileOrUrl, format, isBinary } = load;
  16. const data = fileOrUrl instanceof File
  17. ? (await this.plugin.builders.data.readFile({ file: Asset.File(fileOrUrl), isBinary })).data
  18. : await this.plugin.builders.data.download({ url: fileOrUrl, isBinary });
  19. await this.handleTrajectory(data, format, props, matrix);
  20. }
  21. async parse(parse: ParseParams, props?: PresetProps & { dataLabel?: string }, matrix?: Mat4) {
  22. const { data, format } = parse;
  23. const _data = await this.plugin.builders.data.rawData({ data, label: props?.dataLabel });
  24. await this.handleTrajectory(_data, format, props, matrix);
  25. }
  26. async handleTrajectory(data: any, format: BuiltInTrajectoryFormat, props?: PresetProps, matrix?: Mat4) {
  27. const trajectory = await this.plugin.builders.structure.parseTrajectory(data, format);
  28. const selector = await this.plugin.builders.structure.hierarchy.applyPreset(trajectory, RcsbPreset, {
  29. preset: props || { kind: 'standard', assemblyId: '' }
  30. });
  31. if (matrix && selector) {
  32. const params = {
  33. transform: {
  34. name: 'matrix' as const,
  35. params: { data: matrix, transpose: false }
  36. }
  37. };
  38. const b = this.plugin.state.data.build().to(selector.structureProperties)
  39. .insert(StateTransforms.Model.TransformStructureConformation, params);
  40. await this.plugin.runTask(this.plugin.state.data.updateTree(b));
  41. }
  42. }
  43. constructor(private plugin: PluginContext) {
  44. }
  45. }