trajectory.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 { StateTransforms } from '../transforms';
  8. import { guessCifVariant, DataFormatProvider } from './provider';
  9. import { StateTransformer, StateObjectRef } from '../../mol-state';
  10. import { PluginStateObject } from '../objects';
  11. import { PluginContext } from '../../mol-plugin/context';
  12. export interface TrajectoryFormatProvider<P extends { trajectoryTags?: string | string[] } = { trajectoryTags?: string | string[] }, R extends { trajectory: StateObjectRef<PluginStateObject.Molecule.Trajectory> } = { trajectory: StateObjectRef<PluginStateObject.Molecule.Trajectory> }>
  13. extends DataFormatProvider<P, R> {
  14. }
  15. const Category = 'Trajectory';
  16. function defaultVisuals(plugin: PluginContext, data: { trajectory: StateObjectRef<PluginStateObject.Molecule.Trajectory> }) {
  17. return plugin.builders.structure.hierarchy.applyPreset(data.trajectory, 'default');
  18. }
  19. export const MmcifProvider: TrajectoryFormatProvider = {
  20. label: 'mmCIF',
  21. description: 'mmCIF',
  22. category: Category,
  23. stringExtensions: ['cif', 'mmcif', 'mcif'],
  24. binaryExtensions: ['bcif'],
  25. isApplicable: (info, data) => {
  26. if (info.ext === 'mmcif' || info.ext === 'mcif') return true
  27. // assume undetermined cif/bcif files are mmCIF
  28. if (info.ext === 'cif' || info.ext === 'bcif') return guessCifVariant(info, data) === -1
  29. return false
  30. },
  31. parse: async (plugin, data, params) => {
  32. const state = plugin.state.data;
  33. const cif = state.build().to(data)
  34. .apply(StateTransforms.Data.ParseCif, void 0, { state: { isGhost: true } })
  35. const trajectory = await cif
  36. .apply(StateTransforms.Model.TrajectoryFromMmCif, void 0, { tags: params?.trajectoryTags })
  37. .commit({ revertOnError: true });
  38. if ((cif.selector.cell?.obj?.data.blocks.length || 0) > 1) {
  39. plugin.state.data.updateCellState(cif.ref, { isGhost: false });
  40. }
  41. return { trajectory };
  42. },
  43. visuals: defaultVisuals
  44. }
  45. export const CifCoreProvider: TrajectoryFormatProvider = {
  46. label: 'cifCore',
  47. description: 'CIF Core',
  48. category: Category,
  49. stringExtensions: ['cif'],
  50. isApplicable: (info, data) => {
  51. if (info.ext === 'cif') return guessCifVariant(info, data) === 'coreCif'
  52. return false
  53. },
  54. parse: async (plugin, data, params) => {
  55. const state = plugin.state.data;
  56. const cif = state.build().to(data)
  57. .apply(StateTransforms.Data.ParseCif, void 0, { state: { isGhost: true } })
  58. const trajectory = await cif
  59. .apply(StateTransforms.Model.TrajectoryFromCifCore, void 0, { tags: params?.trajectoryTags })
  60. .commit({ revertOnError: true });
  61. if ((cif.selector.cell?.obj?.data.blocks.length || 0) > 1) {
  62. plugin.state.data.updateCellState(cif.ref, { isGhost: false });
  63. }
  64. return { trajectory };
  65. },
  66. visuals: defaultVisuals
  67. }
  68. function directTrajectory(transformer: StateTransformer<PluginStateObject.Data.String | PluginStateObject.Data.Binary, PluginStateObject.Molecule.Trajectory>): TrajectoryFormatProvider['parse'] {
  69. return async (plugin, data, params) => {
  70. const state = plugin.state.data;
  71. const trajectory = await state.build().to(data)
  72. .apply(transformer, void 0, { tags: params?.trajectoryTags })
  73. .commit({ revertOnError: true });
  74. return { trajectory };
  75. }
  76. }
  77. export const PdbProvider: TrajectoryFormatProvider = {
  78. label: 'PDB',
  79. description: 'PDB',
  80. category: Category,
  81. stringExtensions: ['pdb', 'ent'],
  82. parse: directTrajectory(StateTransforms.Model.TrajectoryFromPDB),
  83. visuals: defaultVisuals
  84. }
  85. export const GroProvider: TrajectoryFormatProvider = {
  86. label: 'GRO',
  87. description: 'GRO',
  88. category: Category,
  89. stringExtensions: ['gro'],
  90. binaryExtensions: [],
  91. parse: directTrajectory(StateTransforms.Model.TrajectoryFromGRO),
  92. visuals: defaultVisuals
  93. }
  94. export const Provider3dg: TrajectoryFormatProvider = {
  95. label: '3DG',
  96. description: '3DG',
  97. category: Category,
  98. stringExtensions: ['3dg'],
  99. parse: directTrajectory(StateTransforms.Model.TrajectoryFrom3DG),
  100. visuals: defaultVisuals
  101. }
  102. export const MolProvider: TrajectoryFormatProvider = {
  103. label: 'MOL',
  104. description: 'MOL',
  105. category: Category,
  106. stringExtensions: ['mol', 'sdf'],
  107. parse: directTrajectory(StateTransforms.Model.TrajectoryFromMOL),
  108. visuals: defaultVisuals
  109. }
  110. export const BuiltInTrajectoryFormats = [
  111. ['mmcif', MmcifProvider] as const,
  112. ['cifCore', CifCoreProvider] as const,
  113. ['pdb', PdbProvider] as const,
  114. ['gro', GroProvider] as const,
  115. ['3dg', Provider3dg] as const,
  116. ['mol', MolProvider] as const
  117. ] as const
  118. export type BuiltInTrajectoryFormat = (typeof BuiltInTrajectoryFormats)[number][0]