structure-selection-helper.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 { MolScriptBuilder as MS } from '../../mol-script/language/builder';
  7. import { StateSelection } from '../../mol-state';
  8. import { PluginStateObject } from '../state/objects';
  9. import { QueryContext, StructureSelection } from '../../mol-model/structure';
  10. import { compile } from '../../mol-script/runtime/query/compiler';
  11. import { Loci } from '../../mol-model/loci';
  12. import { PluginContext } from '../context';
  13. import Expression from '../../mol-script/language/expression';
  14. const all = MS.struct.generator.all()
  15. const polymers = MS.struct.modifier.union([
  16. MS.struct.generator.atomGroups({
  17. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'polymer'])
  18. })
  19. ])
  20. const backboneTrace = MS.struct.modifier.union([
  21. MS.struct.generator.atomGroups({
  22. 'atom-test': MS.core.logic.or([
  23. MS.core.rel.eq([MS.ammp('label_atom_id'), 'CA']),
  24. MS.core.rel.eq([MS.ammp('label_atom_id'), 'P'])
  25. ])
  26. })
  27. ])
  28. const water = MS.struct.modifier.union([
  29. MS.struct.generator.atomGroups({
  30. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'water'])
  31. })
  32. ])
  33. const branched = MS.struct.modifier.union([
  34. MS.struct.combinator.merge([
  35. MS.struct.modifier.union([
  36. MS.struct.generator.atomGroups({
  37. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'branched'])
  38. })
  39. ]),
  40. MS.struct.modifier.union([
  41. MS.struct.generator.atomGroups({
  42. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'non-polymer']),
  43. 'residue-test': MS.core.str.match([MS.re('saccharide', 'i'), MS.ammp('chemCompType')])
  44. })
  45. ])
  46. ])
  47. ])
  48. const branchedPlusConnected = MS.struct.modifier.union([
  49. MS.struct.modifier.includeConnected({
  50. 0: branched, 'layer-count': 1, 'as-whole-residues': true
  51. })
  52. ])
  53. const branchedConnectedOnly = MS.struct.modifier.union([
  54. MS.struct.modifier.exceptBy({
  55. 0: branchedPlusConnected,
  56. by: branched
  57. })
  58. ])
  59. const ligands = MS.struct.modifier.union([
  60. MS.struct.generator.atomGroups({
  61. 'entity-test': MS.core.logic.and([
  62. MS.core.rel.neq([MS.ammp('entityType'), 'branched']),
  63. MS.core.rel.eq([MS.ammp('entityType'), 'non-polymer'])
  64. ]),
  65. 'residue-test': MS.core.logic.not([
  66. MS.core.str.match([MS.re('saccharide', 'i'), MS.ammp('chemCompType')])
  67. ])
  68. })
  69. ])
  70. const ligandsPlusConnected = MS.struct.modifier.union([
  71. MS.struct.modifier.includeConnected({
  72. 0: ligands, 'layer-count': 1, 'as-whole-residues': true
  73. })
  74. ])
  75. const coarse = MS.struct.modifier.union([
  76. MS.struct.generator.atomGroups({
  77. 'chain-test': MS.core.set.has([
  78. MS.set('sphere', 'gaussian'), MS.ammp('objectPrimitive')
  79. ])
  80. })
  81. ])
  82. export const StructureSelectionQueries = {
  83. all,
  84. polymers,
  85. backboneTrace,
  86. water,
  87. branched,
  88. branchedPlusConnected,
  89. branchedConnectedOnly,
  90. ligands,
  91. ligandsPlusConnected,
  92. coarse,
  93. }
  94. //
  95. type SelectionModifier = 'add' | 'remove' | 'only'
  96. export class StructureSelectionHelper {
  97. private get structures() {
  98. const state = this.plugin.state.dataState
  99. return state.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure))
  100. }
  101. private _set(modifier: SelectionModifier, loci: Loci) {
  102. switch (modifier) {
  103. case 'add':
  104. this.plugin.interactivity.lociSelections.add({ loci })
  105. break
  106. case 'remove':
  107. this.plugin.interactivity.lociSelections.remove({ loci })
  108. break
  109. case 'only':
  110. this.plugin.interactivity.lociSelections.only({ loci })
  111. break
  112. }
  113. }
  114. set(modifier: SelectionModifier, query: Expression) {
  115. const compiled = compile<StructureSelection>(query)
  116. for (const so of this.structures) {
  117. const s = so.obj!.data
  118. const result = compiled(new QueryContext(s))
  119. const loci = StructureSelection.toLoci2(result)
  120. this._set(modifier, loci)
  121. }
  122. }
  123. constructor(private plugin: PluginContext) {
  124. }
  125. }