state.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { PluginStateObject as PSO, PluginStateTransform } from '../../../../mol-plugin-state/objects';
  7. import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
  8. import { Task } from '../../../../mol-task';
  9. import { CellPack as _CellPack, Cell, CellPacking } from './data';
  10. import { createStructureFromCellPack } from './model';
  11. import { IngredientFiles } from './util';
  12. export const DefaultCellPackBaseUrl = 'https://mesoscope.scripps.edu/data/cellPACK_data/cellPACK_database_1.1.0/'
  13. export class CellPack extends PSO.Create<_CellPack>({ name: 'CellPack', typeClass: 'Object' }) { }
  14. export { ParseCellPack }
  15. type ParseCellPack = typeof ParseCellPack
  16. const ParseCellPack = PluginStateTransform.BuiltIn({
  17. name: 'parse-cellpack',
  18. display: { name: 'Parse CellPack', description: 'Parse CellPack from JSON data' },
  19. from: PSO.Format.Json,
  20. to: CellPack
  21. })({
  22. apply({ a }) {
  23. return Task.create('Parse CellPack', async ctx => {
  24. const cell = a.data as Cell
  25. const packings: CellPacking[] = []
  26. const { compartments, cytoplasme } = cell
  27. if (compartments) {
  28. for (const name in compartments) {
  29. const { surface, interior } = compartments[name]
  30. if (surface) packings.push({ name, location: 'surface', ingredients: surface.ingredients })
  31. if (interior) packings.push({ name, location: 'interior', ingredients: interior.ingredients })
  32. }
  33. }
  34. if (cytoplasme) packings.push({ name: 'Cytoplasme', location: 'cytoplasme', ingredients: cytoplasme.ingredients })
  35. return new CellPack({ cell, packings });
  36. });
  37. }
  38. });
  39. export { StructureFromCellpack }
  40. type StructureFromCellpack = typeof ParseCellPack
  41. const StructureFromCellpack = PluginStateTransform.BuiltIn({
  42. name: 'structure-from-cellpack',
  43. display: { name: 'Structure from CellPack', description: 'Create Structure from CellPack Packing' },
  44. from: CellPack,
  45. to: PSO.Molecule.Structure,
  46. params: a => {
  47. if (!a) {
  48. return {
  49. packing: PD.Numeric(0, {}, { description: 'Packing Index' }),
  50. baseUrl: PD.Text(DefaultCellPackBaseUrl),
  51. ingredientFiles: PD.FileList({ accept: '.cif,.pdb' })
  52. };
  53. }
  54. const options = a.data.packings.map((d, i) => [i, d.name] as [number, string])
  55. return {
  56. packing: PD.Select(0, options),
  57. baseUrl: PD.Text(DefaultCellPackBaseUrl),
  58. ingredientFiles: PD.FileList({ accept: '.cif,.pdb' })
  59. }
  60. }
  61. })({
  62. apply({ a, params }) {
  63. return Task.create('Structure from CellPack', async ctx => {
  64. const packing = a.data.packings[params.packing]
  65. const ingredientFiles: IngredientFiles = {}
  66. if (params.ingredientFiles !== null) {
  67. for (let i = 0, il = params.ingredientFiles.length; i < il; ++i) {
  68. const file = params.ingredientFiles.item(i)
  69. if (file) ingredientFiles[file.name] = file
  70. }
  71. }
  72. const structure = await createStructureFromCellPack(packing, params.baseUrl, ingredientFiles).runInContext(ctx)
  73. return new PSO.Molecule.Structure(structure, { label: packing.name })
  74. });
  75. }
  76. });