data.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. import { readFromFile } from 'mol-util/data-source';
  14. export { Download }
  15. type Download = typeof Download
  16. const Download = PluginStateTransform.BuiltIn({
  17. name: 'download',
  18. from: [SO.Root],
  19. to: [SO.Data.String, SO.Data.Binary],
  20. params: {
  21. url: PD.Text('https://www.ebi.ac.uk/pdbe/static/entry/1cbs_updated.cif', { description: 'Resource URL. Must be the same domain or support CORS.' }),
  22. label: PD.makeOptional(PD.Text('')),
  23. isBinary: PD.makeOptional(PD.Boolean(false, { description: 'If true, download data as binary (string otherwise)' }))
  24. }
  25. })({
  26. display: {
  27. name: 'Download',
  28. description: 'Download string or binary data from the specified URL'
  29. },
  30. apply({ params: p }, globalCtx: PluginContext) {
  31. return Task.create('Download', async ctx => {
  32. const data = await globalCtx.fetch(p.url, p.isBinary ? 'binary' : 'string').runInContext(ctx);
  33. return p.isBinary
  34. ? new SO.Data.Binary(data as Uint8Array, { label: p.label ? p.label : p.url })
  35. : new SO.Data.String(data as string, { label: p.label ? p.label : p.url });
  36. });
  37. },
  38. update({ oldParams, newParams, b }) {
  39. if (oldParams.url !== newParams.url || oldParams.isBinary !== newParams.isBinary) return Transformer.UpdateResult.Recreate;
  40. if (oldParams.label !== newParams.label) {
  41. (b.label as string) = newParams.label || newParams.url;
  42. return Transformer.UpdateResult.Updated;
  43. }
  44. return Transformer.UpdateResult.Unchanged;
  45. }
  46. });
  47. export { ReadFile }
  48. type ReadFile = typeof ReadFile
  49. const ReadFile = PluginStateTransform.BuiltIn({
  50. name: 'read-file',
  51. from: SO.Root,
  52. to: [SO.Data.String, SO.Data.Binary],
  53. params: {
  54. file: PD.File(),
  55. label: PD.makeOptional(PD.Text('')),
  56. isBinary: PD.makeOptional(PD.Boolean(false, { description: 'If true, open file as as binary (string otherwise)' }))
  57. },
  58. })({
  59. display: {
  60. name: 'Read File',
  61. description: 'Read string or binary data from the specified file'
  62. },
  63. apply({ params: p }) {
  64. return Task.create('Open File', async ctx => {
  65. const data = await readFromFile(p.file, p.isBinary ? 'binary' : 'string').runInContext(ctx);
  66. return p.isBinary
  67. ? new SO.Data.Binary(data as Uint8Array, { label: p.label ? p.label : p.file.name })
  68. : new SO.Data.String(data as string, { label: p.label ? p.label : p.file.name });
  69. });
  70. },
  71. update({ oldParams, newParams, b }) {
  72. if (oldParams.label !== newParams.label) {
  73. (b.label as string) = newParams.label || oldParams.file.name;
  74. return Transformer.UpdateResult.Updated;
  75. }
  76. return Transformer.UpdateResult.Unchanged;
  77. },
  78. isSerializable: () => ({ isSerializable: false, reason: 'Cannot serialize user loaded files.' })
  79. });
  80. export { ParseCif }
  81. type ParseCif = typeof ParseCif
  82. const ParseCif = PluginStateTransform.BuiltIn({
  83. name: 'parse-cif',
  84. from: [SO.Data.String, SO.Data.Binary],
  85. to: SO.Format.Cif
  86. })({
  87. display: {
  88. name: 'Parse CIF',
  89. description: 'Parse CIF from String or Binary data'
  90. },
  91. apply({ a }) {
  92. return Task.create('Parse CIF', async ctx => {
  93. const parsed = await (SO.Data.String.is(a) ? CIF.parse(a.data) : CIF.parseBinary(a.data)).runInContext(ctx);
  94. if (parsed.isError) throw new Error(parsed.message);
  95. return new SO.Format.Cif(parsed.result);
  96. });
  97. }
  98. });