formats.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Adam Midlik <midlik@gmail.com>
  5. */
  6. import { DataFormatProvider } from '../../../mol-plugin-state/formats/provider';
  7. import { PluginStateObject as SO } from '../../../mol-plugin-state/objects';
  8. import { Download } from '../../../mol-plugin-state/transforms/data';
  9. import { PluginContext } from '../../../mol-plugin/context';
  10. import { StateAction, StateObjectRef } from '../../../mol-state';
  11. import { Task } from '../../../mol-task';
  12. import { Asset } from '../../../mol-util/assets';
  13. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  14. import { loadMVS } from '../load';
  15. import { MVSData } from '../mvs-data';
  16. import { MVSTransform } from './annotation-structure-component';
  17. /** Plugin state object storing `MVSData` */
  18. export class Mvs extends SO.Create<{ mvsData: MVSData, sourceUrl?: string }>({ name: 'MVS Data', typeClass: 'Data' }) { }
  19. /** Transformer for parsing data in MVSJ format */
  20. export const ParseMVSJ = MVSTransform({
  21. name: 'mvs-parse-mvsj',
  22. display: { name: 'MVS Annotation Component', description: 'A molecular structure component defined by MVS annotation data.' },
  23. from: SO.Data.String,
  24. to: Mvs,
  25. })({
  26. apply({ a }, plugin: PluginContext) {
  27. const mvsData = MVSData.fromMVSJ(a.data);
  28. const sourceUrl = tryGetDownloadUrl(a, plugin);
  29. return new Mvs({ mvsData, sourceUrl });
  30. },
  31. });
  32. /** If the PluginStateObject `pso` comes from a Download transform, try to get its `url` parameter. */
  33. function tryGetDownloadUrl(pso: SO.Data.String, plugin: PluginContext): string | undefined {
  34. const theCell = plugin.state.data.selectQ(q => q.ofTransformer(Download)).find(cell => cell.obj === pso);
  35. const urlParam = theCell?.transform.params?.url;
  36. return urlParam ? Asset.getUrl(urlParam) : undefined;
  37. }
  38. /** Params for the `LoadMvsData` action */
  39. const LoadMvsDataParams = {
  40. replaceExisting: PD.Boolean(false, { description: 'If true, the loaded MVS view will replace the current state; if false, the MVS view will be added to the current state.' }),
  41. };
  42. /** State action which loads a MVS view into Mol* */
  43. export const LoadMvsData = StateAction.build({
  44. display: { name: 'Load MVS Data' },
  45. from: Mvs,
  46. params: LoadMvsDataParams,
  47. })(({ a, params }, plugin: PluginContext) => Task.create('Load MVS Data', async () => {
  48. const { mvsData, sourceUrl } = a.data;
  49. await loadMVS(plugin, mvsData, { replaceExisting: params.replaceExisting, sourceUrl: sourceUrl });
  50. }));
  51. /** Data format provider for MVSJ format.
  52. * If Visuals:On, it will load the parsed MVS view;
  53. * otherwise it will just create a plugin state object with parsed data. */
  54. export const MVSJFormatProvider: DataFormatProvider<{}, StateObjectRef<Mvs>, any> = DataFormatProvider({
  55. label: 'MVSJ',
  56. description: 'MVSJ',
  57. category: 'Miscellaneous',
  58. stringExtensions: ['mvsj'],
  59. parse: async (plugin, data) => {
  60. return plugin.state.data.build().to(data).apply(ParseMVSJ).commit();
  61. },
  62. visuals: async (plugin, data) => {
  63. const ref = StateObjectRef.resolveRef(data);
  64. const params = PD.getDefaultValues(LoadMvsDataParams);
  65. return await plugin.state.data.applyAction(LoadMvsData, params, ref).run();
  66. },
  67. });