preset.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { StateTransforms } from '../../transforms';
  7. import { StructureRepresentation3DHelpers } from '../../transforms/representation';
  8. import { StructureSelectionQueries as Q } from '../../../mol-plugin/util/structure-selection-helper';
  9. import { BuiltInStructureRepresentations } from '../../../mol-repr/structure/registry';
  10. import { StructureRepresentationProvider, RepresentationProviderTags } from './provider';
  11. import { StateObjectRef } from '../../../mol-state';
  12. import { PluginStateObject } from '../../objects';
  13. import { StaticStructureComponentType } from '../../helpers/structure-component';
  14. import { PluginContext } from '../../../mol-plugin/context';
  15. const auto = StructureRepresentationProvider({
  16. id: 'preset-structure-representation-auto',
  17. display: { name: 'Automaic', group: 'Preset' },
  18. apply(ctx, state, structureCell, _, plugin) {
  19. const s = structureCell.obj!.data;
  20. // TODO: a way to improve this?
  21. if (s.elementCount < 50000) {
  22. return defaultPreset.apply(ctx, state, structureCell, void 0, plugin);
  23. } else if (s.elementCount < 200000) {
  24. return proteinAndNucleic.apply(ctx, state, structureCell, void 0, plugin);
  25. } else {
  26. if (s.unitSymmetryGroups[0].units.length > 10) {
  27. return capsid.apply(ctx, state, structureCell, void 0, plugin);
  28. } else {
  29. return coarseCapsid.apply(ctx, state, structureCell, void 0, plugin);
  30. }
  31. }
  32. }
  33. });
  34. const defaultPreset = StructureRepresentationProvider({
  35. id: 'preset-structure-representation-default',
  36. display: { name: 'Default', group: 'Preset' },
  37. async apply(ctx, state, structureCell, _, plugin) {
  38. const structure = structureCell.obj?.data!;
  39. const reprTags = [this.id, RepresentationProviderTags.Representation];
  40. const components = {
  41. proteinOrNucleic: await staticComponent(plugin, structureCell, 'protein-or-nucleic'),
  42. ligand: await staticComponent(plugin, structureCell, 'ligand'),
  43. nonStandard: await staticComponent(plugin, structureCell, 'non-standard'),
  44. branched: await staticComponent(plugin, structureCell, 'branched'),
  45. water: await staticComponent(plugin, structureCell, 'water'),
  46. coarse: await staticComponent(plugin, structureCell, 'coarse')
  47. };
  48. const builder = state.build();
  49. const representations = {
  50. proteinOrNucleic: components.proteinOrNucleic && builder
  51. .to(components.proteinOrNucleic)
  52. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParams(plugin, 'cartoon', structure)).selector,
  53. ligand: components.ligand && builder
  54. .to(components.ligand)
  55. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParams(plugin, 'ball-and-stick', structure)).selector,
  56. nonStandard: components.nonStandard && builder
  57. .to(components.nonStandard)
  58. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParamsWithTheme(plugin, 'ball-and-stick', 'polymer-id', structure, void 0)).selector,
  59. branched: components.branched && {
  60. ballAndStick: builder.to(components.branched).applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParams(plugin, 'ball-and-stick', structure, { alpha: 0.15 })).selector,
  61. snfg3d: builder.to(components.branched).applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParams(plugin, 'carbohydrate', structure)).selector
  62. },
  63. water: components.water && builder
  64. .to(components.water)
  65. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParams(plugin, 'ball-and-stick', structure, { alpha: 0.51 })).selector,
  66. coarse: components.coarse && builder
  67. .to(components.coarse)
  68. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParamsWithTheme(plugin, 'spacefill', 'polymer-id', structure, {}))
  69. };
  70. await state.updateTree(builder, { revertOnError: false }).runInContext(ctx);
  71. return { components, representations };
  72. }
  73. });
  74. const proteinAndNucleic = StructureRepresentationProvider({
  75. id: 'preset-structure-representation-protein-and-nucleic',
  76. display: { name: 'Protein & Nucleic', group: 'Preset' },
  77. async apply(ctx, state, structureCell, _, plugin) {
  78. const structure = structureCell.obj!.data;
  79. const reprTags = [this.id, RepresentationProviderTags.Representation];
  80. const components = {
  81. protein: await selectionComponent(plugin, structureCell, 'protein'),
  82. nucleic: await selectionComponent(plugin, structureCell, 'nucleic'),
  83. };
  84. const builder = state.build();
  85. const representations = {
  86. protein: components.protein && builder
  87. .to(components.protein)
  88. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParams(plugin, 'cartoon', structure)).selector,
  89. nucleic: components.nucleic && builder
  90. .to(components.nucleic)
  91. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, StructureRepresentation3DHelpers.getDefaultParams(plugin, 'gaussian-surface', structure)).selector,
  92. };
  93. await state.updateTree(builder, { revertOnError: true }).runInContext(ctx);
  94. return { components, representations };
  95. }
  96. });
  97. const capsid = StructureRepresentationProvider({
  98. id: 'preset-structure-representation-capsid',
  99. display: { name: 'Capsid', group: 'Preset' },
  100. async apply(ctx, state, structureCell, _, plugin) {
  101. const structure = structureCell.obj!.data;
  102. const params = StructureRepresentation3DHelpers.createParams(plugin, structure, {
  103. repr: [BuiltInStructureRepresentations['gaussian-surface'], () => ({ smoothness: 1 })]
  104. });
  105. const reprTags = [this.id, RepresentationProviderTags.Representation];
  106. const components = {
  107. polymer: await selectionComponent(plugin, structureCell, 'polymer')
  108. };
  109. const builder = state.build();
  110. const representations = {
  111. polymer: components.polymer && builder
  112. .to(components.polymer)
  113. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, params).selector,
  114. };
  115. await state.updateTree(builder, { revertOnError: true }).runInContext(ctx);
  116. return { components, representations };
  117. }
  118. });
  119. const coarseCapsid = StructureRepresentationProvider({
  120. id: 'preset-structure-representation-coarse-capsid',
  121. display: { name: 'Coarse Capsid', group: 'Preset' },
  122. async apply(ctx, state, structureCell, _, plugin) {
  123. const structure = structureCell.obj!.data;
  124. const params = StructureRepresentation3DHelpers.createParams(plugin, structure, {
  125. repr: [
  126. BuiltInStructureRepresentations['gaussian-surface'],
  127. () => ({ smoothness: 0.5, radiusOffset: 1, /* visuals: ['gaussian-surface-mesh']*/ })
  128. ]
  129. });
  130. const reprTags = [this.id, RepresentationProviderTags.Representation];
  131. const components = {
  132. trace: await selectionComponent(plugin, structureCell, 'trace')
  133. };
  134. const builder = state.build();
  135. const representations = {
  136. trace: components.trace && builder
  137. .to(components.trace)
  138. .applyOrUpdateTagged(reprTags, StateTransforms.Representation.StructureRepresentation3D, params).selector,
  139. };
  140. await state.updateTree(builder, { revertOnError: true }).runInContext(ctx);
  141. return { components, representations };
  142. }
  143. });
  144. function staticComponent(plugin: PluginContext, structure: StateObjectRef<PluginStateObject.Molecule.Structure>, type: StaticStructureComponentType) {
  145. return plugin.builders.structure.tryCreateComponent(structure, {
  146. type: { name: 'static', params: type },
  147. nullIfEmpty: true,
  148. label: ''
  149. }, `static-${type}`);
  150. }
  151. function selectionComponent(plugin: PluginContext, structure: StateObjectRef<PluginStateObject.Molecule.Structure>, query: keyof typeof Q) {
  152. return plugin.builders.structure.tryCreateComponent(structure, {
  153. type: { name: 'expression', params: Q[query].expression },
  154. nullIfEmpty: true,
  155. label: Q[query].label
  156. }, `selection-${query}`);
  157. }
  158. export const PresetStructureReprentations = {
  159. auto,
  160. default: defaultPreset,
  161. proteinAndNucleic,
  162. capsid,
  163. coarseCapsid
  164. };
  165. export type PresetStructureReprentations = typeof PresetStructureReprentations;