volume.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright (c) 2018-2020 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 { PluginContext } from '../../mol-plugin/context';
  8. import { StateAction, StateTransformer, StateSelection } from '../../mol-state';
  9. import { Task } from '../../mol-task';
  10. import { getFileInfo } from '../../mol-util/file-info';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. import { PluginStateObject } from '../objects';
  13. import { Download } from '../transforms/data';
  14. import { DataFormatProvider } from '../formats/provider';
  15. import { Asset } from '../../mol-util/assets';
  16. import { StateTransforms } from '../transforms';
  17. export type EmdbDownloadProvider = 'pdbe' | 'rcsb'
  18. export { DownloadDensity };
  19. type DownloadDensity = typeof DownloadDensity
  20. const DownloadDensity = StateAction.build({
  21. from: PluginStateObject.Root,
  22. display: { name: 'Download Density', description: 'Load a density from the provided source and create its default visual.' },
  23. params: (a, ctx: PluginContext) => {
  24. const { options } = ctx.dataFormats;
  25. return {
  26. source: PD.MappedStatic('pdb-xray', {
  27. 'pdb-xray': PD.Group({
  28. provider: PD.Group({
  29. id: PD.Text('1tqn', { label: 'Id' }),
  30. server: PD.Select('rcsb', [['pdbe', 'PDBe'], ['rcsb', 'RCSB PDB']]),
  31. }, { pivot: 'id' }),
  32. type: PD.Select('2fofc', [['2fofc', '2Fo-Fc'], ['fofc', 'Fo-Fc']]),
  33. }, { isFlat: true }),
  34. 'pdb-xray-ds': PD.Group({
  35. provider: PD.Group({
  36. id: PD.Text('1tqn', { label: 'Id' }),
  37. server: PD.Select('pdbe', [['pdbe', 'PDBe'], ['rcsb', 'RCSB PDB']]),
  38. }, { pivot: 'id' }),
  39. detail: PD.Numeric(3, { min: 0, max: 10, step: 1 }, { label: 'Detail' }),
  40. }, { isFlat: true }),
  41. 'pdb-emd-ds': PD.Group({
  42. provider: PD.Group({
  43. id: PD.Text('emd-8004', { label: 'Id' }),
  44. server: PD.Select<EmdbDownloadProvider>('pdbe', [['pdbe', 'PDBe'], ['rcsb', 'RCSB PDB']]),
  45. }, { pivot: 'id' }),
  46. detail: PD.Numeric(3, { min: 0, max: 10, step: 1 }, { label: 'Detail' }),
  47. }, { isFlat: true }),
  48. 'url': PD.Group({
  49. url: PD.Url(''),
  50. isBinary: PD.Boolean(false),
  51. format: PD.Select('auto', options),
  52. }, { isFlat: true })
  53. }, {
  54. options: [
  55. ['pdb-xray', 'PDB X-ray maps'],
  56. ['pdb-emd-ds', 'PDB EMD Density Server'],
  57. ['pdb-xray-ds', 'PDB X-ray Density Server'],
  58. ['url', 'URL']
  59. ]
  60. })
  61. };
  62. }
  63. })(({ params }, plugin: PluginContext) => Task.create('Download Density', async taskCtx => {
  64. const src = params.source;
  65. let downloadParams: StateTransformer.Params<Download>;
  66. let provider: DataFormatProvider | undefined;
  67. switch (src.name) {
  68. case 'url':
  69. downloadParams = src.params;
  70. break;
  71. case 'pdb-xray':
  72. downloadParams = src.params.provider.server === 'pdbe' ? {
  73. url: Asset.Url(src.params.type === '2fofc'
  74. ? `http://www.ebi.ac.uk/pdbe/coordinates/files/${src.params.provider.id.toLowerCase()}.ccp4`
  75. : `http://www.ebi.ac.uk/pdbe/coordinates/files/${src.params.provider.id.toLowerCase()}_diff.ccp4`),
  76. isBinary: true,
  77. label: `PDBe X-ray map: ${src.params.provider.id}`
  78. } : {
  79. url: Asset.Url(src.params.type === '2fofc'
  80. ? `https://edmaps.rcsb.org/maps/${src.params.provider.id.toLowerCase()}_2fofc.dsn6`
  81. : `https://edmaps.rcsb.org/maps/${src.params.provider.id.toLowerCase()}_fofc.dsn6`),
  82. isBinary: true,
  83. label: `RCSB X-ray map: ${src.params.provider.id}`
  84. };
  85. break;
  86. case 'pdb-emd-ds':
  87. downloadParams = src.params.provider.server === 'pdbe' ? {
  88. url: Asset.Url(`https://www.ebi.ac.uk/pdbe/densities/emd/${src.params.provider.id.toLowerCase()}/cell?detail=${src.params.detail}`),
  89. isBinary: true,
  90. label: `PDBe EMD Density Server: ${src.params.provider.id}`
  91. } : {
  92. url: Asset.Url(`https://maps.rcsb.org/em/${src.params.provider.id.toLowerCase()}/cell?detail=${src.params.detail}`),
  93. isBinary: true,
  94. label: `RCSB PDB EMD Density Server: ${src.params.provider.id}`
  95. };
  96. break;
  97. case 'pdb-xray-ds':
  98. downloadParams = src.params.provider.server === 'pdbe' ? {
  99. url: Asset.Url(`https://www.ebi.ac.uk/pdbe/densities/x-ray/${src.params.provider.id.toLowerCase()}/cell?detail=${src.params.detail}`),
  100. isBinary: true,
  101. label: `PDBe X-ray Density Server: ${src.params.provider.id}`
  102. } : {
  103. url: Asset.Url(`https://maps.rcsb.org/x-ray/${src.params.provider.id.toLowerCase()}/cell?detail=${src.params.detail}`),
  104. isBinary: true,
  105. label: `RCSB PDB X-ray Density Server: ${src.params.provider.id}`
  106. };
  107. break;
  108. default: throw new Error(`${(src as any).name} not supported.`);
  109. }
  110. const data = await plugin.builders.data.download(downloadParams);
  111. switch (src.name) {
  112. case 'url':
  113. downloadParams = src.params;
  114. provider = src.params.format === 'auto' ? plugin.dataFormats.auto(getFileInfo(Asset.getUrl(downloadParams.url)), data.cell?.obj!) : plugin.dataFormats.get(src.params.format);
  115. break;
  116. case 'pdb-xray':
  117. provider = src.params.provider.server === 'pdbe'
  118. ? plugin.dataFormats.get('ccp4')
  119. : plugin.dataFormats.get('dsn6');
  120. break;
  121. case 'pdb-emd-ds':
  122. case 'pdb-xray-ds':
  123. provider = plugin.dataFormats.get('dscif');
  124. break;
  125. default: throw new Error(`${(src as any).name} not supported.`);
  126. }
  127. if (!provider) {
  128. plugin.log.warn('DownloadDensity: Format provider not found.');
  129. return;
  130. }
  131. const volumes = await provider.parse(plugin, data);
  132. await provider.visuals?.(plugin, volumes);
  133. }));
  134. export const AssignColorVolume = StateAction.build({
  135. display: { name: 'Assign Volume Colors', description: 'Assigns another volume to be available for coloring.' },
  136. from: PluginStateObject.Volume.Data,
  137. isApplicable(a) { return !a.data.colorVolume; },
  138. params(a, plugin: PluginContext) {
  139. const cells = plugin.state.data.select(StateSelection.Generators.root.subtree().ofType(PluginStateObject.Volume.Data).filter(cell => !!cell.obj && !cell.obj?.data.colorVolume && cell.obj !== a));
  140. if (cells.length === 0) return { ref: PD.Text('', { isHidden: true, label: 'Volume' }) };
  141. return { ref: PD.Select(cells[0].transform.ref, cells.map(c => [c.transform.ref, c.obj!.label]), { label: 'Volume' }) };
  142. }
  143. })(({ ref, params, state }, plugin: PluginContext) => {
  144. return plugin.build().to(ref).apply(StateTransforms.Volume.AssignColorVolume, { ref: params.ref }, { dependsOn: [ params.ref ] }).commit();
  145. });