valence-model.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 { PluginBehavior } from '../../../behavior';
  7. import { ParamDefinition as PD } from '../../../../../mol-util/param-definition';
  8. import { ValenceModelProvider } from '../../../../../mol-model-props/computed/valence-model';
  9. import { Loci } from '../../../../../mol-model/loci';
  10. import { PluginStateObject } from '../../../../state/objects';
  11. import { StateSelection } from '../../../../../mol-state';
  12. import { Structure, StructureElement } from '../../../../../mol-model/structure';
  13. import { OrderedSet } from '../../../../../mol-data/int';
  14. import { geometryLabel } from '../../../../../mol-model-props/computed/chemistry/geometry';
  15. export const ValenceModel = PluginBehavior.create<{ autoAttach: boolean, showTooltip: boolean }>({
  16. name: 'computed-valence-model-prop',
  17. category: 'custom-props',
  18. display: { name: 'Valence Model' },
  19. ctor: class extends PluginBehavior.Handler<{ autoAttach: boolean, showTooltip: boolean }> {
  20. private provider = ValenceModelProvider
  21. private getStructures(structure: Structure) {
  22. const structures: Structure[] = []
  23. const root = this.ctx.helpers.substructureParent.get(structure)
  24. if (root) {
  25. const state = this.ctx.state.dataState
  26. const selections = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure, root.transform.ref));
  27. for (const s of selections) {
  28. if (s.obj) structures.push(s.obj.data)
  29. }
  30. }
  31. return structures
  32. }
  33. private label = (loci: Loci): string | undefined => {
  34. if (!this.params.showTooltip) return void 0;
  35. switch (loci.kind) {
  36. case 'element-loci':
  37. if (loci.elements.length === 0) return void 0;
  38. const labels: string[] = []
  39. const structures = this.getStructures(loci.structure)
  40. for (const s of structures) {
  41. const valenceModel = this.provider.getValue(s).value
  42. if (!valenceModel) continue;
  43. const l = StructureElement.Loci.remap(loci, s)
  44. if (l.elements.length !== 1) continue
  45. const e = l.elements[0]
  46. if (OrderedSet.size(e.indices) !== 1) continue
  47. const vm = valenceModel.get(e.unit.id)
  48. if (!vm) continue;
  49. const idx = OrderedSet.start(e.indices)
  50. const charge = vm.charge[idx]
  51. const idealGeometry = vm.idealGeometry[idx]
  52. const implicitH = vm.implicitH[idx]
  53. const totalH = vm.totalH[idx]
  54. labels.push(`Valence Model: <small>Charge</small> ${charge} | <small>Ideal Geometry</small> ${geometryLabel(idealGeometry)} | <small>Implicit H</small> ${implicitH} | <small>Total H</small> ${totalH}`)
  55. }
  56. return labels.length ? labels.join('<br/>') : undefined;
  57. default: return void 0;
  58. }
  59. }
  60. update(p: { autoAttach: boolean, showTooltip: boolean }) {
  61. let updated = (
  62. this.params.autoAttach !== p.autoAttach ||
  63. this.params.showTooltip !== p.showTooltip
  64. )
  65. this.params.autoAttach = p.autoAttach;
  66. this.params.showTooltip = p.showTooltip;
  67. this.ctx.customStructureProperties.setDefaultAutoAttach(this.provider.descriptor.name, this.params.autoAttach);
  68. return updated;
  69. }
  70. register(): void {
  71. this.ctx.customStructureProperties.register(this.provider, this.params.autoAttach);
  72. this.ctx.lociLabels.addProvider(this.label);
  73. }
  74. unregister() {
  75. this.ctx.customStructureProperties.unregister(this.provider.descriptor.name);
  76. this.ctx.lociLabels.removeProvider(this.label);
  77. }
  78. },
  79. params: () => ({
  80. autoAttach: PD.Boolean(false),
  81. showTooltip: PD.Boolean(true)
  82. })
  83. });