structure-selection-helper.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { MolScriptBuilder as MS } from '../../mol-script/language/builder';
  8. import { StateSelection, StateBuilder } from '../../mol-state';
  9. import { PluginStateObject } from '../state/objects';
  10. import { QueryContext, StructureSelection, StructureQuery, StructureElement } from '../../mol-model/structure';
  11. import { compile } from '../../mol-script/runtime/query/compiler';
  12. import { Loci } from '../../mol-model/loci';
  13. import { PluginContext } from '../context';
  14. import Expression from '../../mol-script/language/expression';
  15. import { LinkType } from '../../mol-model/structure/model/types';
  16. import { StateTransforms } from '../state/transforms';
  17. export interface StructureSelectionQuery {
  18. label: string
  19. query: StructureQuery
  20. expression: Expression
  21. description: string
  22. }
  23. export function StructureSelectionQuery(label: string, expression: Expression, description = ''): StructureSelectionQuery {
  24. return { label, expression, query: compile<StructureSelection>(expression), description }
  25. }
  26. const all = StructureSelectionQuery('All', MS.struct.generator.all())
  27. const polymer = StructureSelectionQuery('Polymer', MS.struct.modifier.union([
  28. MS.struct.generator.atomGroups({
  29. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'polymer'])
  30. })
  31. ]))
  32. const trace = StructureSelectionQuery('Trace', MS.struct.modifier.union([
  33. MS.struct.combinator.merge([
  34. MS.struct.modifier.union([
  35. MS.struct.generator.atomGroups({
  36. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'polymer']),
  37. 'chain-test': MS.core.set.has([
  38. MS.set('sphere', 'gaussian'), MS.ammp('objectPrimitive')
  39. ])
  40. })
  41. ]),
  42. MS.struct.modifier.union([
  43. MS.struct.generator.atomGroups({
  44. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'polymer']),
  45. 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
  46. 'atom-test': MS.core.set.has([MS.set('CA', 'P'), MS.ammp('label_atom_id')])
  47. })
  48. ])
  49. ])
  50. ]))
  51. const protein = StructureSelectionQuery('Protein', MS.struct.modifier.union([
  52. MS.struct.generator.atomGroups({
  53. 'entity-test': MS.core.logic.and([
  54. MS.core.rel.eq([MS.ammp('entityType'), 'polymer']),
  55. MS.core.str.match([
  56. MS.re('(polypeptide|cyclic-pseudo-peptide)', 'i'),
  57. MS.ammp('entitySubtype')
  58. ])
  59. ])
  60. })
  61. ]))
  62. const nucleic = StructureSelectionQuery('Nucleic', MS.struct.modifier.union([
  63. MS.struct.generator.atomGroups({
  64. 'entity-test': MS.core.logic.and([
  65. MS.core.rel.eq([MS.ammp('entityType'), 'polymer']),
  66. MS.core.str.match([
  67. MS.re('(nucleotide|peptide nucleic acid)', 'i'),
  68. MS.ammp('entitySubtype')
  69. ])
  70. ])
  71. })
  72. ]))
  73. const proteinOrNucleic = StructureSelectionQuery('Protein or Nucleic', MS.struct.modifier.union([
  74. MS.struct.generator.atomGroups({
  75. 'entity-test': MS.core.logic.and([
  76. MS.core.rel.eq([MS.ammp('entityType'), 'polymer']),
  77. MS.core.str.match([
  78. MS.re('(polypeptide|cyclic-pseudo-peptide|nucleotide|peptide nucleic acid)', 'i'),
  79. MS.ammp('entitySubtype')
  80. ])
  81. ])
  82. })
  83. ]))
  84. const water = StructureSelectionQuery('Water', MS.struct.modifier.union([
  85. MS.struct.generator.atomGroups({
  86. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'water'])
  87. })
  88. ]))
  89. const branched = StructureSelectionQuery('Carbohydrate', MS.struct.modifier.union([
  90. MS.struct.generator.atomGroups({
  91. 'entity-test': MS.core.logic.or([
  92. MS.core.rel.eq([MS.ammp('entityType'), 'branched']),
  93. MS.core.logic.and([
  94. MS.core.rel.eq([MS.ammp('entityType'), 'non-polymer']),
  95. MS.core.str.match([
  96. MS.re('oligosaccharide', 'i'),
  97. MS.ammp('entitySubtype')
  98. ])
  99. ])
  100. ])
  101. })
  102. ]))
  103. const branchedPlusConnected = StructureSelectionQuery('Carbohydrate with Connected', MS.struct.modifier.union([
  104. MS.struct.modifier.includeConnected({
  105. 0: branched.expression, 'layer-count': 1, 'as-whole-residues': true
  106. })
  107. ]))
  108. const branchedConnectedOnly = StructureSelectionQuery('Connected to Carbohydrate', MS.struct.modifier.union([
  109. MS.struct.modifier.exceptBy({
  110. 0: branchedPlusConnected.expression,
  111. by: branched.expression
  112. })
  113. ]))
  114. const ligand = StructureSelectionQuery('Ligand', MS.struct.modifier.union([
  115. MS.struct.combinator.merge([
  116. MS.struct.modifier.union([
  117. MS.struct.generator.atomGroups({
  118. 'entity-test': MS.core.logic.and([
  119. MS.core.rel.eq([MS.ammp('entityType'), 'non-polymer']),
  120. MS.core.logic.not([MS.core.str.match([
  121. MS.re('oligosaccharide', 'i'),
  122. MS.ammp('entitySubtype')
  123. ])])
  124. ]),
  125. 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
  126. 'residue-test': MS.core.logic.not([
  127. MS.core.str.match([MS.re('saccharide', 'i'), MS.ammp('chemCompType')])
  128. ])
  129. })
  130. ]),
  131. // this is to get non-polymer and peptide terminus components in polymer entities,
  132. // - non-polymer, e.g. PXZ in 4HIV or generally ACE
  133. // - carboxy terminus, e.g. FC0 in 4BP9, or ETA in 6DDE
  134. // - amino terminus, e.g. ARF in 3K4V, or 4MM in 3EGV
  135. MS.struct.modifier.union([
  136. MS.struct.generator.atomGroups({
  137. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'polymer']),
  138. 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
  139. 'residue-test': MS.core.str.match([
  140. MS.re('non-polymer|(amino|carboxy) terminus', 'i'),
  141. MS.ammp('chemCompType')
  142. ])
  143. })
  144. ])
  145. ]),
  146. ]))
  147. // don't include branched entities as they have their own link representation
  148. const ligandPlusConnected = StructureSelectionQuery('Ligand with Connected', MS.struct.modifier.union([
  149. MS.struct.modifier.exceptBy({
  150. 0: MS.struct.modifier.union([
  151. MS.struct.modifier.includeConnected({
  152. 0: ligand.expression,
  153. 'layer-count': 1,
  154. 'as-whole-residues': true,
  155. 'link-test': MS.core.flags.hasAny([
  156. MS.struct.linkProperty.flags(),
  157. MS.core.type.bitflags([
  158. LinkType.Flag.Covalent | LinkType.Flag.MetallicCoordination
  159. ])
  160. ])
  161. })
  162. ]),
  163. by: branched.expression
  164. })
  165. ]))
  166. const ligandConnectedOnly = StructureSelectionQuery('Connected to Ligand', MS.struct.modifier.union([
  167. MS.struct.modifier.exceptBy({
  168. 0: ligandPlusConnected.expression,
  169. by: ligand.expression
  170. })
  171. ]))
  172. // residues connected to ligands or branched entities
  173. const connectedOnly = StructureSelectionQuery('Connected to Ligand or Carbohydrate', MS.struct.modifier.union([
  174. MS.struct.combinator.merge([
  175. branchedConnectedOnly.expression,
  176. ligandConnectedOnly.expression
  177. ]),
  178. ]))
  179. const disulfideBridges = StructureSelectionQuery('Disulfide Bridges', MS.struct.modifier.union([
  180. MS.struct.modifier.wholeResidues([
  181. MS.struct.modifier.union([
  182. MS.struct.generator.linkedAtomicPairs({
  183. 0: MS.core.flags.hasAny([
  184. MS.struct.linkProperty.flags(),
  185. MS.core.type.bitflags([LinkType.Flag.Sulfide])
  186. ])
  187. })
  188. ])
  189. ])
  190. ]))
  191. const modified = StructureSelectionQuery('Modified Residues', MS.struct.modifier.union([
  192. MS.struct.generator.atomGroups({
  193. 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
  194. 'residue-test': MS.ammp('isModified')
  195. })
  196. ]))
  197. const nonStandardPolymer = StructureSelectionQuery('Non-standard Residues in Polymers', MS.struct.modifier.union([
  198. MS.struct.generator.atomGroups({
  199. 'entity-test': MS.core.rel.eq([MS.ammp('entityType'), 'polymer']),
  200. 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
  201. 'residue-test': MS.ammp('isNonStandard')
  202. })
  203. ]))
  204. const coarse = StructureSelectionQuery('Coarse Elements', MS.struct.modifier.union([
  205. MS.struct.generator.atomGroups({
  206. 'chain-test': MS.core.set.has([
  207. MS.set('sphere', 'gaussian'), MS.ammp('objectPrimitive')
  208. ])
  209. })
  210. ]))
  211. const surroundings = StructureSelectionQuery('Surrounding Residues (5 \u212B) of Selection', MS.struct.modifier.union([
  212. MS.struct.modifier.exceptBy({
  213. 0: MS.struct.modifier.includeSurroundings({
  214. 0: MS.internal.generator.current(),
  215. radius: 5,
  216. 'as-whole-residues': true
  217. }),
  218. by: MS.internal.generator.current()
  219. })
  220. ]), 'Select residues within 5 \u212B of the current selection.')
  221. const complement = StructureSelectionQuery('Inverse / Complement of Selection', MS.struct.modifier.union([
  222. MS.struct.modifier.exceptBy({
  223. 0: MS.struct.generator.all(),
  224. by: MS.internal.generator.current()
  225. })
  226. ]), 'Select everything not in the current selection.')
  227. const bonded = StructureSelectionQuery('Residues Bonded to Selection', MS.struct.modifier.union([
  228. MS.struct.modifier.includeConnected({
  229. 0: MS.internal.generator.current(), 'layer-count': 1, 'as-whole-residues': true
  230. })
  231. ]), 'Select residues covalently bonded to current selection.')
  232. export const StructureSelectionQueries = {
  233. all,
  234. polymer,
  235. trace,
  236. protein,
  237. nucleic,
  238. proteinOrNucleic,
  239. water,
  240. branched,
  241. branchedPlusConnected,
  242. branchedConnectedOnly,
  243. ligand,
  244. ligandPlusConnected,
  245. ligandConnectedOnly,
  246. connectedOnly,
  247. disulfideBridges,
  248. modified,
  249. nonStandardPolymer,
  250. coarse,
  251. surroundings,
  252. complement,
  253. bonded,
  254. }
  255. export function applyBuiltInSelection(to: StateBuilder.To<PluginStateObject.Molecule.Structure>, query: keyof typeof StructureSelectionQueries, customTag?: string) {
  256. return to.apply(StateTransforms.Model.StructureSelectionFromExpression,
  257. { expression: StructureSelectionQueries[query].expression, label: StructureSelectionQueries[query].label },
  258. { tags: customTag ? [query, customTag] : [query] });
  259. }
  260. //
  261. export type SelectionModifier = 'add' | 'remove' | 'only'
  262. export class StructureSelectionHelper {
  263. private get structures() {
  264. return this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure)).map(s => s.obj!.data)
  265. }
  266. private _set(modifier: SelectionModifier, loci: Loci, applyGranularity = true) {
  267. switch (modifier) {
  268. case 'add':
  269. this.plugin.interactivity.lociSelects.select({ loci }, applyGranularity)
  270. break
  271. case 'remove':
  272. this.plugin.interactivity.lociSelects.deselect({ loci }, applyGranularity)
  273. break
  274. case 'only':
  275. this.plugin.interactivity.lociSelects.selectOnly({ loci }, applyGranularity)
  276. break
  277. }
  278. }
  279. set(modifier: SelectionModifier, query: StructureQuery, applyGranularity = true) {
  280. for (const s of this.structures) {
  281. const current = this.plugin.helpers.structureSelectionManager.get(s)
  282. const currentSelection = Loci.isEmpty(current)
  283. ? StructureSelection.Empty(s)
  284. : StructureSelection.Singletons(s, StructureElement.Loci.toStructure(current))
  285. const result = query(new QueryContext(s, { currentSelection }))
  286. const loci = StructureSelection.toLociWithSourceUnits(result)
  287. this._set(modifier, loci, applyGranularity)
  288. }
  289. }
  290. constructor(private plugin: PluginContext) {
  291. }
  292. }