data.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { PluginStateTransform } from '../objects';
  7. import { PluginStateObject as SO } from '../objects';
  8. import { Task } from 'mol-task';
  9. import CIF from 'mol-io/reader/cif'
  10. import { PluginContext } from 'mol-plugin/context';
  11. import { ParamDefinition as PD } from 'mol-util/param-definition';
  12. import { Transformer } from 'mol-state';
  13. export { Download }
  14. namespace Download { export interface Params { url: string, isBinary?: boolean, label?: string } }
  15. const Download = PluginStateTransform.Create<SO.Root, SO.Data.String | SO.Data.Binary, Download.Params>({
  16. name: 'download',
  17. display: {
  18. name: 'Download',
  19. description: 'Download string or binary data from the specified URL'
  20. },
  21. from: [SO.Root],
  22. to: [SO.Data.String, SO.Data.Binary],
  23. params: {
  24. default: () => ({
  25. url: 'https://www.ebi.ac.uk/pdbe/static/entry/1cbs_updated.cif'
  26. }),
  27. definition: () => ({
  28. url: PD.Text('', { description: 'Resource URL. Must be the same domain or support CORS.' }),
  29. label: PD.Text(''),
  30. isBinary: PD.Boolean(false, { description: 'If true, download data as binary (string otherwise)' })
  31. }),
  32. // validate: p => !p.url || !p.url.trim() ? [['Enter url.', 'url']] : void 0
  33. },
  34. apply({ params: p }, globalCtx: PluginContext) {
  35. return Task.create('Download', async ctx => {
  36. // TODO: track progress
  37. const data = await globalCtx.fetch(p.url, p.isBinary ? 'binary' : 'string');
  38. return p.isBinary
  39. ? new SO.Data.Binary(data as Uint8Array, { label: p.label ? p.label : p.url })
  40. : new SO.Data.String(data as string, { label: p.label ? p.label : p.url });
  41. });
  42. },
  43. update({ oldParams, newParams, b }) {
  44. if (oldParams.url !== newParams.url || oldParams.isBinary !== newParams.isBinary) return Transformer.UpdateResult.Recreate;
  45. if (oldParams.label !== newParams.label) {
  46. (b.label as string) = newParams.label || newParams.url;
  47. return Transformer.UpdateResult.Updated;
  48. }
  49. return Transformer.UpdateResult.Unchanged;
  50. }
  51. });
  52. export { ParseCif }
  53. namespace ParseCif { export interface Params { } }
  54. const ParseCif = PluginStateTransform.Create<SO.Data.String | SO.Data.Binary, SO.Data.Cif, ParseCif.Params>({
  55. name: 'parse-cif',
  56. display: {
  57. name: 'Parse CIF',
  58. description: 'Parse CIF from String or Binary data'
  59. },
  60. from: [SO.Data.String, SO.Data.Binary],
  61. to: [SO.Data.Cif],
  62. apply({ a }) {
  63. return Task.create('Parse CIF', async ctx => {
  64. const parsed = await (SO.Data.String.is(a) ? CIF.parse(a.data) : CIF.parseBinary(a.data)).runInContext(ctx);
  65. if (parsed.isError) throw new Error(parsed.message);
  66. return new SO.Data.Cif(parsed.result);
  67. });
  68. }
  69. });