data.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { PluginStateTransform } from '../objects';
  8. import { PluginStateObject as SO } from '../objects';
  9. import { Task } from 'mol-task';
  10. import CIF from 'mol-io/reader/cif'
  11. import { PluginContext } from 'mol-plugin/context';
  12. import { ParamDefinition as PD } from 'mol-util/param-definition';
  13. import { StateTransformer } from 'mol-state';
  14. import { readFromFile } from 'mol-util/data-source';
  15. import * as CCP4 from 'mol-io/reader/ccp4/parser'
  16. import * as DSN6 from 'mol-io/reader/dsn6/parser'
  17. export { Download }
  18. type Download = typeof Download
  19. const Download = PluginStateTransform.BuiltIn({
  20. name: 'download',
  21. display: { name: 'Download', description: 'Download string or binary data from the specified URL' },
  22. from: [SO.Root],
  23. to: [SO.Data.String, SO.Data.Binary],
  24. params: {
  25. 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.' }),
  26. label: PD.makeOptional(PD.Text('')),
  27. isBinary: PD.makeOptional(PD.Boolean(false, { description: 'If true, download data as binary (string otherwise)' }))
  28. }
  29. })({
  30. apply({ params: p }, globalCtx: PluginContext) {
  31. return Task.create('Download', async ctx => {
  32. const data = await globalCtx.fetch({ url: p.url, type: 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 StateTransformer.UpdateResult.Recreate;
  40. if (oldParams.label !== newParams.label) {
  41. (b.label as string) = newParams.label || newParams.url;
  42. return StateTransformer.UpdateResult.Updated;
  43. }
  44. return StateTransformer.UpdateResult.Unchanged;
  45. }
  46. });
  47. export { ReadFile }
  48. type ReadFile = typeof ReadFile
  49. const ReadFile = PluginStateTransform.BuiltIn({
  50. name: 'read-file',
  51. display: { name: 'Read File', description: 'Read string or binary data from the specified file' },
  52. from: SO.Root,
  53. to: [SO.Data.String, SO.Data.Binary],
  54. params: {
  55. file: PD.File(),
  56. label: PD.makeOptional(PD.Text('')),
  57. isBinary: PD.makeOptional(PD.Boolean(false, { description: 'If true, open file as as binary (string otherwise)' }))
  58. }
  59. })({
  60. apply({ params: p }) {
  61. return Task.create('Open File', async ctx => {
  62. const data = await readFromFile(p.file, p.isBinary ? 'binary' : 'string').runInContext(ctx);
  63. return p.isBinary
  64. ? new SO.Data.Binary(data as Uint8Array, { label: p.label ? p.label : p.file.name })
  65. : new SO.Data.String(data as string, { label: p.label ? p.label : p.file.name });
  66. });
  67. },
  68. update({ oldParams, newParams, b }) {
  69. if (oldParams.label !== newParams.label) {
  70. (b.label as string) = newParams.label || oldParams.file.name;
  71. return StateTransformer.UpdateResult.Updated;
  72. }
  73. return StateTransformer.UpdateResult.Unchanged;
  74. },
  75. isSerializable: () => ({ isSerializable: false, reason: 'Cannot serialize user loaded files.' })
  76. });
  77. export { ParseCif }
  78. type ParseCif = typeof ParseCif
  79. const ParseCif = PluginStateTransform.BuiltIn({
  80. name: 'parse-cif',
  81. display: { name: 'Parse CIF', description: 'Parse CIF from String or Binary data' },
  82. from: [SO.Data.String, SO.Data.Binary],
  83. to: SO.Format.Cif
  84. })({
  85. apply({ a }) {
  86. return Task.create('Parse CIF', async ctx => {
  87. const parsed = await (SO.Data.String.is(a) ? CIF.parse(a.data) : CIF.parseBinary(a.data)).runInContext(ctx);
  88. if (parsed.isError) throw new Error(parsed.message);
  89. return new SO.Format.Cif(parsed.result);
  90. });
  91. }
  92. });
  93. export { ParseCcp4 }
  94. type ParseCcp4 = typeof ParseCcp4
  95. const ParseCcp4 = PluginStateTransform.BuiltIn({
  96. name: 'parse-ccp4',
  97. display: { name: 'Parse CCP4/MRC/MAP', description: 'Parse CCP4/MRC/MAP from Binary data' },
  98. from: [SO.Data.Binary],
  99. to: SO.Format.Ccp4
  100. })({
  101. apply({ a }) {
  102. return Task.create('Parse CCP4/MRC/MAP', async ctx => {
  103. const parsed = await CCP4.parse(a.data).runInContext(ctx);
  104. if (parsed.isError) throw new Error(parsed.message);
  105. return new SO.Format.Ccp4(parsed.result);
  106. });
  107. }
  108. });
  109. export { ParseDsn6 }
  110. type ParseDsn6 = typeof ParseDsn6
  111. const ParseDsn6 = PluginStateTransform.BuiltIn({
  112. name: 'parse-dsn6',
  113. display: { name: 'Parse DSN6/BRIX', description: 'Parse CCP4/BRIX from Binary data' },
  114. from: [SO.Data.Binary],
  115. to: SO.Format.Dsn6
  116. })({
  117. apply({ a }) {
  118. return Task.create('Parse DSN6/BRIX', async ctx => {
  119. const parsed = await DSN6.parse(a.data).runInContext(ctx);
  120. if (parsed.isError) throw new Error(parsed.message);
  121. return new SO.Format.Dsn6(parsed.result);
  122. });
  123. }
  124. });