structure-query.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * Copyright (c) 2018-2022 Mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Type } from '../type';
  8. import * as Core from './core';
  9. import { Arguments, Argument } from '../symbol';
  10. import { symbol } from '../helpers';
  11. export namespace Types {
  12. export const ElementSymbol = Type.Value('Structure', 'ElementSymbol');
  13. export const AtomName = Type.Value('Structure', 'AtomName');
  14. export const BondFlag = Type.OneOf('Structure', 'BondFlag', Type.Str, ['covalent', 'metallic', 'ion', 'hydrogen', 'sulfide', 'computed', 'aromatic']);
  15. export const BondFlags = Core.Types.Flags(BondFlag, 'BondFlags');
  16. export const SecondaryStructureFlag = Type.OneOf('Structure', 'SecondaryStructureFlag', Type.Str, ['alpha', 'beta', '3-10', 'pi', 'sheet', 'strand', 'helix', 'turn', 'none']);
  17. export const SecondaryStructureFlags = Core.Types.Flags(SecondaryStructureFlag, 'SecondaryStructureFlag');
  18. export const RingFingerprint = Type.Value('Structure', 'RingFingerprint');
  19. export const EntityType = Type.OneOf('Structure', 'EntityType', Type.Str, ['polymer', 'non-polymer', 'water', 'branched']);
  20. export const EntitySubtype = Type.OneOf('Structure', 'EntitySubtype', Type.Str, ['other', 'polypeptide(D)', 'polypeptide(L)', 'polydeoxyribonucleotide', 'polyribonucleotide', 'polydeoxyribonucleotide/polyribonucleotide hybrid', 'cyclic-pseudo-peptide', 'peptide nucleic acid', 'oligosaccharide']);
  21. export const ObjectPrimitive = Type.OneOf('Structure', 'ObjectPrimitive', Type.Str, ['atomistic', 'sphere', 'gaussian', 'other']);
  22. export const ResidueId = Type.Value('Structure', 'ResidueId');
  23. export const ElementSet = Type.Value('Structure', 'ElementSet');
  24. export const ElementSelection = Type.Value('Structure', 'ElementSelection');
  25. export const ElementReference = Type.Value('Structure', 'ElementReference');
  26. export const ElementSelectionQuery = Core.Types.Fn(ElementSelection, 'ElementSelectionQuery');
  27. }
  28. const type = {
  29. '@header': 'Types',
  30. elementSymbol: symbol(
  31. Arguments.Dictionary({ 0: Argument(Type.Str) }),
  32. Types.ElementSymbol, 'Create element symbol representation from a string value.'),
  33. atomName: symbol(
  34. Arguments.Dictionary({ 0: Argument(Type.AnyValue) }), Types.AtomName, 'Convert a value to an atom name.'),
  35. entityType: symbol(
  36. Arguments.Dictionary({ 0: Argument(Types.EntityType) }),
  37. Types.EntityType,
  38. `Create normalized representation of entity type: ${Type.oneOfValues(Types.EntityType).join(', ')}.`),
  39. bondFlags: symbol(
  40. Arguments.List(Types.BondFlag),
  41. Types.BondFlags,
  42. `Create bond flags representation from a list of strings. Allowed flags: ${Type.oneOfValues(Types.BondFlag).join(', ')}.`),
  43. ringFingerprint: symbol(
  44. Arguments.List(Types.ElementSymbol, { nonEmpty: true }),
  45. Types.RingFingerprint,
  46. 'Create ring fingerprint from the supplied atom element list.'),
  47. secondaryStructureFlags: symbol(
  48. Arguments.List(Types.SecondaryStructureFlag),
  49. Types.SecondaryStructureFlags,
  50. `Create secondary structure flags representation from a list of strings. Allowed flags: ${Type.oneOfValues(Types.SecondaryStructureFlag).join(', ')}.`),
  51. authResidueId: symbol(Arguments.Dictionary({
  52. 0: Argument(Type.Str, { description: 'auth_asym_id' }),
  53. 1: Argument(Type.Num, { description: 'auth_seq_id' }),
  54. 2: Argument(Type.Str, { description: 'pdbx_PDB_ins_code', isOptional: true })
  55. }), Types.ResidueId, `Residue identifier based on "auth_" annotation.`),
  56. labelResidueId: symbol(Arguments.Dictionary({
  57. 0: Argument(Type.Str, { description: 'label_entity_id' }),
  58. 1: Argument(Type.Str, { description: 'label_asym_id' }),
  59. 2: Argument(Type.Num, { description: 'label_seq_id' }),
  60. 3: Argument(Type.Str, { description: 'pdbx_PDB_ins_code', isOptional: true })
  61. }), Types.ResidueId, `Residue identifier based on mmCIF's "label_" annotation.`)
  62. };
  63. const slot = {
  64. '@header': 'Iteration Slots',
  65. element: symbol(Arguments.None, Types.ElementReference, 'A reference to the current element.'),
  66. elementSetReduce: symbol(Arguments.None, Type.Variable('a', Type.AnyValue, true), 'Current value of the element set reducer.')
  67. };
  68. const generator = {
  69. '@header': 'Generators',
  70. all: symbol(Arguments.None, Types.ElementSelectionQuery, 'The entire structure.'),
  71. atomGroups: symbol(Arguments.Dictionary({
  72. 'entity-test': Argument(Type.Bool, { isOptional: true, defaultValue: true, description: 'Test for the 1st atom of every entity' }),
  73. 'chain-test': Argument(Type.Bool, { isOptional: true, defaultValue: true, description: 'Test for the 1st atom of every chain' }),
  74. 'residue-test': Argument(Type.Bool, { isOptional: true, defaultValue: true, description: 'Test for the 1st atom every residue' }),
  75. 'atom-test': Argument(Type.Bool, { isOptional: true, defaultValue: true }),
  76. 'group-by': Argument(Type.Any, { isOptional: true, defaultValue: `atom-key`, description: 'Group atoms to sets based on this property. Default: each atom has its own set' }),
  77. }), Types.ElementSelectionQuery, 'Return all atoms for which the tests are satisfied, grouped into sets.'),
  78. bondedAtomicPairs: symbol(Arguments.Dictionary({
  79. 0: Argument(Type.Bool, { isOptional: true, defaultValue: 'true for covalent bonds' as any, description: 'Test each bond with this predicate. Each bond is visited twice with swapped atom order.' }),
  80. // TODO: shoud we support this or just use queryEach to get similar behavior
  81. // 'group-by': Argument(Type.Any, { isOptional: true, defaultValue: ``, description: 'Group the bonds using the privided value' }),
  82. }), Types.ElementSelectionQuery, 'Return all pairs of atoms for which the test is satisfied.'),
  83. rings: symbol(Arguments.Dictionary({
  84. 'fingerprint': Argument(Types.RingFingerprint, { isOptional: true }),
  85. 'only-aromatic': Argument(Type.Bool, { isOptional: true, defaultValue: false }),
  86. }), Types.ElementSelectionQuery, 'Return all rings or those with the specified fingerprint and/or only aromatic rings.'),
  87. queryInSelection: symbol(Arguments.Dictionary({
  88. 0: Argument(Types.ElementSelectionQuery),
  89. query: Argument(Types.ElementSelectionQuery),
  90. 'in-complement': Argument(Type.Bool, { isOptional: true, defaultValue: false })
  91. }), Types.ElementSelectionQuery, 'Executes query only on atoms that are in the source selection.'),
  92. empty: symbol(Arguments.None, Types.ElementSelectionQuery, 'Nada.'),
  93. };
  94. const modifier = {
  95. '@header': 'Selection Modifications',
  96. queryEach: symbol(Arguments.Dictionary({
  97. 0: Argument(Types.ElementSelectionQuery),
  98. query: Argument(Types.ElementSelectionQuery)
  99. }), Types.ElementSelectionQuery, 'Query every atom set in the input selection separately.'),
  100. intersectBy: symbol(Arguments.Dictionary({
  101. 0: Argument(Types.ElementSelectionQuery),
  102. by: Argument(Types.ElementSelectionQuery)
  103. }), Types.ElementSelectionQuery, 'Intersect each atom set from the first sequence from atoms in the second one.'),
  104. exceptBy: symbol(Arguments.Dictionary({
  105. 0: Argument(Types.ElementSelectionQuery),
  106. by: Argument(Types.ElementSelectionQuery)
  107. }), Types.ElementSelectionQuery, `Remove all atoms from 'selection' that occur in 'by'.`),
  108. unionBy: symbol(Arguments.Dictionary({
  109. 0: Argument(Types.ElementSelectionQuery),
  110. by: Argument(Types.ElementSelectionQuery)
  111. }), Types.ElementSelectionQuery, 'For each atom set A in the orginal sequence, combine all atoms sets in the target selection that intersect with A.'),
  112. union: symbol(Arguments.Dictionary({
  113. 0: Argument(Types.ElementSelectionQuery)
  114. }), Types.ElementSelectionQuery, 'Collects all atom sets in the sequence into a single atom set.'),
  115. cluster: symbol(Arguments.Dictionary({
  116. 0: Argument(Types.ElementSelectionQuery),
  117. 'min-distance': Argument(Type.Num, { isOptional: true, defaultValue: 0 }),
  118. 'max-distance': Argument(Type.Num),
  119. 'min-size': Argument(Type.Num, { description: 'Minimal number of sets to merge, must be at least 2', isOptional: true, defaultValue: 2 }),
  120. 'max-size': Argument(Type.Num, { description: 'Maximal number of sets to merge, if not set, no limit', isOptional: true }),
  121. }), Types.ElementSelectionQuery, 'Combines atom sets that have mutual distance in the interval [min-radius, max-radius]. Minimum/maximum size determines how many atom sets can be combined.'),
  122. includeSurroundings: symbol(Arguments.Dictionary({
  123. 0: Argument(Types.ElementSelectionQuery),
  124. radius: Argument(Type.Num),
  125. 'atom-radius': Argument(Type.Num, { isOptional: true, defaultValue: 0, description: 'Value added to each atom before the distance check, for example VDW radius. Using this argument is computationally demanding.' }),
  126. 'as-whole-residues': Argument(Type.Bool, { isOptional: true })
  127. }), Types.ElementSelectionQuery, 'For each atom set in the selection, include all surrouding atoms/residues that are within the specified radius.'),
  128. surroundingLigands: symbol(Arguments.Dictionary({
  129. 0: Argument(Types.ElementSelectionQuery),
  130. radius: Argument(Type.Num),
  131. 'include-water': Argument(Type.Bool, { isOptional: true, defaultValue: true })
  132. }), Types.ElementSelectionQuery, 'Find all ligands components around the source query.'),
  133. includeConnected: symbol(Arguments.Dictionary({
  134. 0: Argument(Types.ElementSelectionQuery),
  135. 'bond-test': Argument(Type.Bool, { isOptional: true, defaultValue: 'true for covalent bonds' as any }),
  136. 'layer-count': Argument(Type.Num, { isOptional: true, defaultValue: 1, description: 'Number of bonded layers to include.' }),
  137. 'fixed-point': Argument(Type.Bool, { isOptional: true, defaultValue: false, description: 'Continue adding layers as long as new connections exist.' }),
  138. 'as-whole-residues': Argument(Type.Bool, { isOptional: true })
  139. }), Types.ElementSelectionQuery, 'Pick all atom sets that are connected to the target.'),
  140. wholeResidues: symbol(Arguments.Dictionary({
  141. 0: Argument(Types.ElementSelectionQuery),
  142. }), Types.ElementSelectionQuery, 'Expand the selection to whole residues.'),
  143. expandProperty: symbol(Arguments.Dictionary({
  144. 0: Argument(Types.ElementSelectionQuery),
  145. property: Argument(Type.AnyValue)
  146. }), Types.ElementSelectionQuery, 'To each atom set in the selection, add all atoms that have the same property value that was already present in the set.')
  147. };
  148. const filter = {
  149. '@header': 'Selection Filters',
  150. pick: symbol(Arguments.Dictionary({
  151. 0: Argument(Types.ElementSelectionQuery),
  152. test: Argument(Type.Bool)
  153. }), Types.ElementSelectionQuery, 'Pick all atom sets that satisfy the test.'),
  154. first: symbol(Arguments.Dictionary({
  155. 0: Argument(Types.ElementSelectionQuery)
  156. }), Types.ElementSelectionQuery, 'Take the 1st atom set in the sequence.'),
  157. withSameAtomProperties: symbol(Arguments.Dictionary({
  158. 0: Argument(Types.ElementSelectionQuery),
  159. source: Argument(Types.ElementSelectionQuery),
  160. property: Argument(Type.Any)
  161. }), Types.ElementSelectionQuery, 'Pick all atom sets for which the set of given atom properties is a subset of the source properties.'),
  162. intersectedBy: symbol(Arguments.Dictionary({
  163. 0: Argument(Types.ElementSelectionQuery),
  164. by: Argument(Types.ElementSelectionQuery)
  165. }), Types.ElementSelectionQuery, 'Pick all atom sets that have non-zero intersection with the target.'),
  166. within: symbol(Arguments.Dictionary({
  167. 0: Argument(Types.ElementSelectionQuery),
  168. target: Argument(Types.ElementSelectionQuery),
  169. 'min-radius': Argument(Type.Num, { isOptional: true, defaultValue: 0 }),
  170. 'max-radius': Argument(Type.Num),
  171. 'atom-radius': Argument(Type.Num, { isOptional: true, defaultValue: 0, description: 'Value added to each atom before the distance check, for example VDW radius. Using this argument is computationally demanding.' }),
  172. invert: Argument(Type.Bool, { isOptional: true, defaultValue: false, description: 'If true, pick only atom sets that are further than the specified radius.' }),
  173. }), Types.ElementSelectionQuery, 'Pick all atom sets from selection that have any atom within the radius of any atom from target.'),
  174. isConnectedTo: symbol(Arguments.Dictionary({
  175. 0: Argument(Types.ElementSelectionQuery),
  176. target: Argument(Types.ElementSelectionQuery),
  177. 'bond-test': Argument(Type.Bool, { isOptional: true, defaultValue: 'true for covalent bonds' as any }),
  178. disjunct: Argument(Type.Bool, { isOptional: true, defaultValue: true, description: 'If true, there must exist a bond to an atom that lies outside the given atom set to pass test.' }),
  179. invert: Argument(Type.Bool, { isOptional: true, defaultValue: false, description: 'If true, return atom sets that are not connected.' })
  180. }), Types.ElementSelectionQuery, 'Pick all atom sets that are connected to the target.'),
  181. };
  182. const combinator = {
  183. '@header': 'Selection Combinators',
  184. intersect: symbol(Arguments.List(Types.ElementSelectionQuery), Types.ElementSelectionQuery, 'Return all unique atom sets that appear in all of the source selections.'),
  185. merge: symbol(Arguments.List(Types.ElementSelectionQuery), Types.ElementSelectionQuery, 'Merges multiple selections into a single one. Only unique atom sets are kept.'),
  186. distanceCluster: symbol(Arguments.Dictionary({
  187. matrix: Argument(Core.Types.List(Core.Types.List(Type.Num)), { description: 'Distance matrix, represented as list of rows (num[][])). Lower triangle is min distance, upper triangle is max distance.' }),
  188. selections: Argument(Core.Types.List(Types.ElementSelectionQuery), { description: 'A list of held selections.' })
  189. }), Types.ElementSelectionQuery, 'Pick combinations of atom sets from the source sequences that are mutually within distances specified by a matrix.')
  190. };
  191. const atomSet = {
  192. '@header': 'Atom Sets',
  193. atomCount: symbol(Arguments.None, Type.Num),
  194. countQuery: symbol(Arguments.Dictionary({
  195. 0: Argument(Types.ElementSelectionQuery)
  196. }), Type.Num, 'Counts the number of occurences of a specific query inside the current atom set.'),
  197. reduce: symbol(Arguments.Dictionary({
  198. initial: Argument(Type.Variable('a', Type.AnyValue, true), { description: 'Initial value assigned to slot.atom-set-reduce. Current atom is set to the 1st atom of the current set for this.' }),
  199. value: Argument(Type.Variable('a', Type.AnyValue, true), { description: 'Expression executed for each atom in the set' })
  200. }), Type.Variable('a', Type.AnyValue, true), 'Execute the value expression for each atom in the current atom set and return the result. Works the same way as Array.reduce in JavaScript (``result = value(value(...value(initial)))``)'),
  201. propertySet: symbol(Arguments.Dictionary({
  202. 0: Argument(Core.Types.ConstrainedVar),
  203. }), Core.Types.Set(Core.Types.ConstrainedVar), 'Returns a set with all values of the given property in the current atom set.'),
  204. };
  205. const atomProperty = {
  206. '@header': 'Atom Properties',
  207. core: {
  208. '@header': 'Core Properties',
  209. elementSymbol: atomProp(Types.ElementSymbol),
  210. vdw: atomProp(Type.Num, 'Van der Waals radius'),
  211. mass: atomProp(Type.Num, 'Atomic weight'),
  212. atomicNumber: atomProp(Type.Num, 'Atomic number'),
  213. x: atomProp(Type.Num, 'Cartesian X coordinate'),
  214. y: atomProp(Type.Num, 'Cartesian Y coordinate'),
  215. z: atomProp(Type.Num, 'Cartesian Z coordinate'),
  216. atomKey: atomProp(Type.AnyValue, 'Unique value for each atom. Main use case is grouping of atoms.'),
  217. bondCount: symbol(Arguments.Dictionary({
  218. 0: Argument(Types.ElementReference, { isOptional: true, defaultValue: 'slot.current-atom' }),
  219. flags: Argument(Types.BondFlags, { isOptional: true, defaultValue: 'covalent' as any }),
  220. }), Type.Num, 'Number of bonds (by default only covalent bonds are counted).'),
  221. sourceIndex: atomProp(Type.Num, 'Index of the atom/element in the input file.'),
  222. operatorName: atomProp(Type.Str, 'Name of the symmetry operator applied to this element.'),
  223. operatorKey: atomProp(Type.Num, 'Key of the symmetry operator applied to this element.'),
  224. modelIndex: atomProp(Type.Num, 'Index of the model in the input file.'),
  225. modelLabel: atomProp(Type.Str, 'Label/header of the model in the input file.')
  226. },
  227. topology: {
  228. connectedComponentKey: atomProp(Type.AnyValue, 'Unique value for each connected component.')
  229. },
  230. macromolecular: {
  231. '@header': 'Macromolecular Properties (derived from the mmCIF format)',
  232. authResidueId: atomProp(Types.ResidueId, `type.auth-residue-id symbol executed on current atom's residue`),
  233. labelResidueId: atomProp(Types.ResidueId, `type.label-residue-id symbol executed on current atom's residue`),
  234. residueKey: atomProp(Type.AnyValue, 'Unique value for each tuple ``(label_entity_id,auth_asym_id, auth_seq_id, pdbx_PDB_ins_code)``, main use case is grouping of atoms'),
  235. chainKey: atomProp(Type.AnyValue, 'Unique value for each tuple ``(label_entity_id, auth_asym_id)``, main use case is grouping of atoms'),
  236. entityKey: atomProp(Type.AnyValue, 'Unique value for each tuple ``label_entity_id``, main use case is grouping of atoms'),
  237. isHet: atomProp(Type.Bool, 'Equivalent to atom_site.group_PDB !== ATOM'),
  238. id: atomProp(Type.Num, '_atom_site.id'),
  239. label_atom_id: atomProp(Types.AtomName),
  240. label_alt_id: atomProp(Type.Str),
  241. label_comp_id: atomProp(Type.Str),
  242. label_asym_id: atomProp(Type.Str),
  243. label_entity_id: atomProp(Type.Str),
  244. label_seq_id: atomProp(Type.Num),
  245. auth_atom_id: atomProp(Types.AtomName),
  246. auth_comp_id: atomProp(Type.Str),
  247. auth_asym_id: atomProp(Type.Str),
  248. auth_seq_id: atomProp(Type.Num),
  249. pdbx_PDB_ins_code: atomProp(Type.Str),
  250. pdbx_formal_charge: atomProp(Type.Num),
  251. occupancy: atomProp(Type.Num),
  252. B_iso_or_equiv: atomProp(Type.Num),
  253. entityType: atomProp(Types.EntityType, 'Type of the entity as defined in mmCIF (polymer, non-polymer, branched, water)'),
  254. entitySubtype: atomProp(Types.EntitySubtype, 'Subtype of the entity as defined in mmCIF _entity_poly.type and _pdbx_entity_branch.type (other, polypeptide(D), polypeptide(L), polydeoxyribonucleotide, polyribonucleotide, polydeoxyribonucleotide/polyribonucleotide hybrid, cyclic-pseudo-peptide, peptide nucleic acid, oligosaccharide)'),
  255. entityPrdId: atomProp(Type.Str, `The PRD ID of the entity.`),
  256. entityDescription: atomProp(Core.Types.List(Type.Str)),
  257. objectPrimitive: atomProp(Types.ObjectPrimitive, 'Type of the primitive object used to model this segment as defined in mmCIF/IHM (atomistic, sphere, gaussian, other)'),
  258. secondaryStructureKey: atomProp(Type.AnyValue, 'Unique value for each secondary structure element.'),
  259. secondaryStructureFlags: atomProp(Types.SecondaryStructureFlags),
  260. isModified: atomProp(Type.Bool, 'True if the atom belongs to modification of a standard residue.'),
  261. modifiedParentName: atomProp(Type.Str, `'3-letter' code of the modifed parent residue.`),
  262. isNonStandard: atomProp(Type.Bool, 'True if this is a non-standard residue.'),
  263. chemCompType: atomProp(Type.Str, `Type of the chemical component as defined in mmCIF.`),
  264. }
  265. };
  266. const bondProperty = {
  267. '@header': 'Bond Properties',
  268. flags: bondProp(Types.BondFlags),
  269. order: bondProp(Type.Num),
  270. key: bondProp(Type.Num),
  271. length: bondProp(Type.Num),
  272. atomA: bondProp(Types.ElementReference),
  273. atomB: bondProp(Types.ElementReference)
  274. };
  275. function atomProp(type: Type, description?: string) {
  276. return symbol(Arguments.Dictionary({ 0: Argument(Types.ElementReference, { isOptional: true, defaultValue: 'slot.current-atom' }) }), type, description);
  277. }
  278. function bondProp(type: Type, description?: string) {
  279. return symbol(Arguments.None, type, description);
  280. }
  281. export const structureQuery = {
  282. '@header': 'Structure Queries',
  283. type,
  284. slot,
  285. generator,
  286. modifier,
  287. filter,
  288. combinator,
  289. atomSet,
  290. atomProperty,
  291. bondProperty
  292. };