state.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright (c) 2019 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. // export const DefaultCellPackBaseUrl = 'https://cdn.jsdelivr.net/gh/mesoscope/cellPACK_data@master/cellPACK_database_1.1.0/'
  12. export const DefaultCellPackBaseUrl = 'https://mgldev.scripps.edu/projects/autoPACK/web/cellpackproject/'
  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. };
  52. }
  53. const options = a.data.packings.map((d, i) => [i, d.name] as [number, string])
  54. return {
  55. packing: PD.Select(0, options),
  56. baseUrl: PD.Text(DefaultCellPackBaseUrl)
  57. }
  58. }
  59. })({
  60. apply({ a, params }) {
  61. return Task.create('Structure from CellPack', async ctx => {
  62. const packing = a.data.packings[params.packing]
  63. const structure = await createStructureFromCellPack(packing, params.baseUrl).runInContext(ctx)
  64. return new PSO.Molecule.Structure(structure, { label: packing.name })
  65. });
  66. }
  67. });