structure-selection-helper.ts 14 KB

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