table.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { MolScriptSymbolTable as MolScript } from '../../language/symbol-table';
  7. import { DefaultQueryRuntimeTable, QuerySymbolRuntime, QueryRuntimeArguments } from './compiler';
  8. import { Queries, StructureProperties, StructureElement, QueryContext } from 'mol-model/structure';
  9. import { ElementSymbol } from 'mol-model/structure/model/types';
  10. import { SetUtils } from 'mol-util/set';
  11. import toUpperCase from 'mol-util/upper-case';
  12. import { VdwRadius, AtomWeight, AtomNumber } from 'mol-model/structure/model/properties/atomic';
  13. import { cantorPairing } from 'mol-data/util';
  14. import C = QuerySymbolRuntime.Const
  15. import D = QuerySymbolRuntime.Dynamic
  16. const symbols = [
  17. // ============= TYPES =============
  18. C(MolScript.core.type.bool, (ctx, v) => !!v[0](ctx)),
  19. C(MolScript.core.type.num, (ctx, v) => +v[0](ctx)),
  20. C(MolScript.core.type.str, (ctx, v) => '' + v[0](ctx)),
  21. C(MolScript.core.type.list, (ctx, xs) => QueryRuntimeArguments.forEachEval(xs, ctx, (v, i, list) => list[i] = v, [] as any[])),
  22. C(MolScript.core.type.set, (ctx, xs) => QueryRuntimeArguments.forEachEval(xs, ctx, (v, i, set) => set.add(v), new Set<any>())),
  23. C(MolScript.core.type.regex, (ctx, v) => new RegExp(v[0](ctx), (v[1] && v[1](ctx)) || '')),
  24. C(MolScript.core.type.bitflags, (ctx, v) => +v[0](ctx)),
  25. C(MolScript.core.type.compositeKey, (ctx, xs) => QueryRuntimeArguments.forEachEval(xs, ctx, (v, i, list) => list[i] = '' + v, [] as string[]).join('-')),
  26. // ============= LOGIC ================
  27. C(MolScript.core.logic.not, (ctx, v) => !v[0](ctx)),
  28. C(MolScript.core.logic.and, (ctx, xs) => {
  29. if (typeof xs.length === 'number') {
  30. for (let i = 0, _i = xs.length; i < _i; i++) if (!xs[i](ctx)) return false;
  31. } else {
  32. for (const k of Object.keys(xs)) if (!xs[k](ctx)) return false;
  33. }
  34. return true;
  35. }),
  36. C(MolScript.core.logic.or, (ctx, xs) => {
  37. if (typeof xs.length === 'number') {
  38. for (let i = 0, _i = xs.length; i < _i; i++) if (xs[i](ctx)) return true;
  39. } else {
  40. for (const k of Object.keys(xs)) if (xs[k](ctx)) return true;
  41. }
  42. return false;
  43. }),
  44. // ============= RELATIONAL ================
  45. C(MolScript.core.rel.eq, (ctx, v) => v[0](ctx) === v[1](ctx)),
  46. C(MolScript.core.rel.neq, (ctx, v) => v[0](ctx) !== v[1](ctx)),
  47. C(MolScript.core.rel.lt, (ctx, v) => v[0](ctx) < v[1](ctx)),
  48. C(MolScript.core.rel.lte, (ctx, v) => v[0](ctx) <= v[1](ctx)),
  49. C(MolScript.core.rel.gr, (ctx, v) => v[0](ctx) > v[1](ctx)),
  50. C(MolScript.core.rel.gre, (ctx, v) => v[0](ctx) >= v[1](ctx)),
  51. C(MolScript.core.rel.inRange, (ctx, v) => {
  52. const x = v[0](ctx);
  53. return x >= v[1](ctx) && x <= v[2](ctx);
  54. }),
  55. // ============= ARITHMETIC ================
  56. C(MolScript.core.math.add, (ctx, xs) => {
  57. let ret = 0;
  58. if (typeof xs.length === 'number') {
  59. for (let i = 0, _i = xs.length; i < _i; i++) ret += xs[i](ctx);
  60. } else {
  61. for (const k of Object.keys(xs)) ret += xs[k](ctx);
  62. }
  63. return ret;
  64. }),
  65. C(MolScript.core.math.sub, (ctx, xs) => {
  66. let ret = 0;
  67. if (typeof xs.length === 'number') {
  68. if (xs.length === 1) return -xs[0](ctx);
  69. ret = xs[0](ctx) || 0;
  70. for (let i = 1, _i = xs.length; i < _i; i++) ret -= xs[i](ctx);
  71. } else {
  72. const keys = Object.keys(xs);
  73. if (keys.length === 1)
  74. ret = xs[keys[0]](ctx) || 0;
  75. for (let i = 1, _i = keys.length; i < _i; i++) ret -= xs[keys[i]](ctx);
  76. }
  77. return ret;
  78. }),
  79. C(MolScript.core.math.mult, (ctx, xs) => {
  80. let ret = 1;
  81. if (typeof xs.length === 'number') {
  82. for (let i = 0, _i = xs.length; i < _i; i++) ret *= xs[i](ctx);
  83. } else {
  84. for (const k of Object.keys(xs)) ret *= xs[k](ctx);
  85. }
  86. return ret;
  87. }),
  88. C(MolScript.core.math.div, (ctx, v) => v[0](ctx) / v[1](ctx)),
  89. C(MolScript.core.math.pow, (ctx, v) => Math.pow(v[0](ctx), v[1](ctx))),
  90. C(MolScript.core.math.mod, (ctx, v) => v[0](ctx) % v[1](ctx)),
  91. C(MolScript.core.math.min, (ctx, xs) => {
  92. let ret = Number.POSITIVE_INFINITY;
  93. if (typeof xs.length === 'number') {
  94. for (let i = 0, _i = xs.length; i < _i; i++) ret = Math.min(xs[i](ctx), ret);
  95. } else {
  96. for (const k of Object.keys(xs)) ret = Math.min(xs[k](ctx), ret)
  97. }
  98. return ret;
  99. }),
  100. C(MolScript.core.math.max, (ctx, xs) => {
  101. let ret = Number.NEGATIVE_INFINITY;
  102. if (typeof xs.length === 'number') {
  103. for (let i = 0, _i = xs.length; i < _i; i++) ret = Math.max(xs[i](ctx), ret);
  104. } else {
  105. for (const k of Object.keys(xs)) ret = Math.max(xs[k](ctx), ret)
  106. }
  107. return ret;
  108. }),
  109. C(MolScript.core.math.floor, (ctx, v) => Math.floor(v[0](ctx))),
  110. C(MolScript.core.math.ceil, (ctx, v) => Math.ceil(v[0](ctx))),
  111. C(MolScript.core.math.roundInt, (ctx, v) => Math.round(v[0](ctx))),
  112. C(MolScript.core.math.abs, (ctx, v) => Math.abs(v[0](ctx))),
  113. C(MolScript.core.math.sqrt, (ctx, v) => Math.sqrt(v[0](ctx))),
  114. C(MolScript.core.math.sin, (ctx, v) => Math.sin(v[0](ctx))),
  115. C(MolScript.core.math.cos, (ctx, v) => Math.cos(v[0](ctx))),
  116. C(MolScript.core.math.tan, (ctx, v) => Math.tan(v[0](ctx))),
  117. C(MolScript.core.math.asin, (ctx, v) => Math.asin(v[0](ctx))),
  118. C(MolScript.core.math.acos, (ctx, v) => Math.acos(v[0](ctx))),
  119. C(MolScript.core.math.atan, (ctx, v) => Math.atan(v[0](ctx))),
  120. C(MolScript.core.math.sinh, (ctx, v) => Math.sinh(v[0](ctx))),
  121. C(MolScript.core.math.cosh, (ctx, v) => Math.cosh(v[0](ctx))),
  122. C(MolScript.core.math.tanh, (ctx, v) => Math.tanh(v[0](ctx))),
  123. C(MolScript.core.math.exp, (ctx, v) => Math.exp(v[0](ctx))),
  124. C(MolScript.core.math.log, (ctx, v) => Math.log(v[0](ctx))),
  125. C(MolScript.core.math.log10, (ctx, v) => Math.log10(v[0](ctx))),
  126. C(MolScript.core.math.atan2, (ctx, v) => Math.atan2(v[0](ctx), v[1](ctx))),
  127. // ============= STRING ================
  128. C(MolScript.core.str.match, (ctx, v) => v[0](ctx).test(v[1](ctx))),
  129. C(MolScript.core.str.concat, (ctx, xs) => {
  130. let ret: string[] = [];
  131. if (typeof xs.length === 'number') {
  132. for (let i = 0, _i = xs.length; i < _i; i++) ret.push(xs[i](ctx).toString());
  133. } else {
  134. for (const k of Object.keys(xs)) ret.push(xs[k](ctx).toString());
  135. }
  136. return ret.join('');
  137. }),
  138. // ============= LIST ================
  139. C(MolScript.core.list.getAt, (ctx, v) => v[0](ctx)[v[1](ctx)]),
  140. // ============= SET ================
  141. C(MolScript.core.set.has, (ctx, v) => v[0](ctx).has(v[1](ctx))),
  142. C(MolScript.core.set.isSubset, (ctx, v) => SetUtils.isSuperset(v[1](ctx) as Set<any>, v[0](ctx) as Set<any>)),
  143. // ============= FLAGS ================
  144. C(MolScript.core.flags.hasAny, (ctx, v) => {
  145. const test = v[1](ctx);
  146. const tested = v[0](ctx);
  147. if (!test) return !!tested;
  148. return (tested & test) !== 0;
  149. }),
  150. C(MolScript.core.flags.hasAll, (ctx, v) => {
  151. const test = v[1](ctx);
  152. const tested = v[0](ctx);
  153. if (!test) return !tested;
  154. return (tested & test) === test;
  155. }),
  156. ////////////////////////////////////
  157. // Structure
  158. // ============= TYPES ================
  159. C(MolScript.structureQuery.type.elementSymbol, (ctx, v) => ElementSymbol(v[0](ctx))),
  160. C(MolScript.structureQuery.type.atomName, (ctx, v) => toUpperCase(v[0](ctx))),
  161. // TODO:
  162. // C(MolScript.structureQuery.type.bondFlags, (ctx, v) => StructureRuntime.BondProperties.createFlags(env, v)),
  163. // C(MolScript.structureQuery.type.secondaryStructureFlags, (ctx, v) => StructureRuntime.AtomProperties.createSecondaryStructureFlags(env, v)),
  164. // C(MolScript.structureQuery.type.entityType, (ctx, v) => StructureRuntime.Common.entityType(v[0](ctx))),
  165. // C(MolScript.structureQuery.type.ringFingerprint, (ctx, v) => StructureRuntime.Common.ringFingerprint(env, v as any)),
  166. // C(MolScript.structureQuery.type.authResidueId, (ctx, v) => ResidueIdentifier.auth(v[0](ctx), v[1](ctx), v[2] && v[2](ctx))),
  167. // C(MolScript.structureQuery.type.labelResidueId, (ctx, v) => ResidueIdentifier.label(v[0](ctx), v[1](ctx), v[2](ctx), v[3] && v[3](ctx))),
  168. // ============= SLOTS ================
  169. // TODO: slots might not be needed after all: reducer simply pushes/pops current element
  170. C(MolScript.structureQuery.slot.element, (ctx, _) => ctx.element),
  171. // C(MolScript.structureQuery.slot.elementSetReduce, (ctx, _) => ctx.element),
  172. // ============= GENERATORS ================
  173. D(MolScript.structureQuery.generator.atomGroups, (ctx, xs) => Queries.generators.atoms({
  174. entityTest: xs['entity-test'],
  175. chainTest: xs['chain-test'],
  176. residueTest: xs['residue-test'],
  177. atomTest: xs['atom-test'],
  178. groupBy: xs['group-by']
  179. })(ctx)),
  180. D(MolScript.structureQuery.generator.all, (ctx) => Queries.generators.all(ctx)),
  181. D(MolScript.structureQuery.generator.empty, (ctx) => Queries.generators.none(ctx)),
  182. // ============= MODIFIERS ================
  183. D(MolScript.structureQuery.modifier.includeSurroundings, (ctx, xs) => Queries.modifiers.includeSurroundings(xs[0] as any, {
  184. radius: xs['radius'](ctx),
  185. wholeResidues: !!(xs['as-whole-residues'] && xs['as-whole-residues'](ctx)),
  186. elementRadius: xs['atom-radius']
  187. })(ctx)),
  188. D(MolScript.structureQuery.modifier.wholeResidues, (ctx, xs) => Queries.modifiers.wholeResidues(xs[0] as any)(ctx)),
  189. D(MolScript.structureQuery.modifier.expandProperty, (ctx, xs) => Queries.modifiers.expandProperty(xs[0] as any, xs['property'])(ctx)),
  190. D(MolScript.structureQuery.modifier.exceptBy, (ctx, xs) => Queries.modifiers.exceptBy(xs[0] as any, xs['by'] as any)(ctx)),
  191. // ============= ATOM PROPERTIES ================
  192. // ~~~ CORE ~~~
  193. D(MolScript.structureQuery.atomProperty.core.elementSymbol, atomProp(StructureProperties.atom.type_symbol)),
  194. D(MolScript.structureQuery.atomProperty.core.vdw, (ctx, _) => VdwRadius(StructureProperties.atom.type_symbol(ctx.element))),
  195. D(MolScript.structureQuery.atomProperty.core.mass, (ctx, _) => AtomWeight(StructureProperties.atom.type_symbol(ctx.element))),
  196. D(MolScript.structureQuery.atomProperty.core.atomicNumber, (ctx, _) => AtomNumber(StructureProperties.atom.type_symbol(ctx.element))),
  197. D(MolScript.structureQuery.atomProperty.core.x, atomProp(StructureProperties.atom.x)),
  198. D(MolScript.structureQuery.atomProperty.core.y, atomProp(StructureProperties.atom.y)),
  199. D(MolScript.structureQuery.atomProperty.core.z, atomProp(StructureProperties.atom.z)),
  200. D(MolScript.structureQuery.atomProperty.core.sourceIndex, atomProp(StructureProperties.atom.sourceIndex)),
  201. D(MolScript.structureQuery.atomProperty.core.atomKey, (ctx, _) => cantorPairing(ctx.element.unit.id, ctx.element.element)),
  202. // TODO:
  203. // D(MolScript.structureQuery.atomProperty.core.bondCount, (ctx, _) => ),
  204. // ~~~ TOPOLOGY ~~~
  205. // TODO
  206. // ~~~ MACROMOLECULAR ~~~
  207. // TODO:
  208. // // identifiers
  209. // labelResidueId: prop((env, v) => ResidueIdentifier.labelOfResidueIndex(env.context.model, getAddress(env, v).residue)),
  210. // authResidueId: prop((env, v) => ResidueIdentifier.authOfResidueIndex(env.context.model, getAddress(env, v).residue)),
  211. // keys
  212. D(MolScript.structureQuery.atomProperty.macromolecular.residueKey, (ctx, _) => StructureElement.residueIndex(ctx.element)),
  213. D(MolScript.structureQuery.atomProperty.macromolecular.chainKey, (ctx, _) => StructureElement.chainIndex(ctx.element)),
  214. D(MolScript.structureQuery.atomProperty.macromolecular.entityKey, (ctx, _) => StructureElement.entityIndex(ctx.element)),
  215. // mmCIF
  216. D(MolScript.structureQuery.atomProperty.macromolecular.id, atomProp(StructureProperties.atom.id)),
  217. D(MolScript.structureQuery.atomProperty.macromolecular.isHet, (ctx, _) => StructureProperties.residue.group_PDB(ctx.element) !== 'ATOM'),
  218. D(MolScript.structureQuery.atomProperty.macromolecular.label_atom_id, atomProp(StructureProperties.atom.label_atom_id)),
  219. D(MolScript.structureQuery.atomProperty.macromolecular.label_alt_id, atomProp(StructureProperties.atom.label_alt_id)),
  220. D(MolScript.structureQuery.atomProperty.macromolecular.label_asym_id, atomProp(StructureProperties.chain.label_asym_id)),
  221. D(MolScript.structureQuery.atomProperty.macromolecular.label_comp_id, atomProp(StructureProperties.residue.label_comp_id)),
  222. D(MolScript.structureQuery.atomProperty.macromolecular.label_seq_id, atomProp(StructureProperties.residue.label_seq_id)),
  223. D(MolScript.structureQuery.atomProperty.macromolecular.label_entity_id, atomProp(StructureProperties.entity.id)),
  224. D(MolScript.structureQuery.atomProperty.macromolecular.auth_atom_id, atomProp(StructureProperties.atom.auth_atom_id)),
  225. D(MolScript.structureQuery.atomProperty.macromolecular.auth_asym_id, atomProp(StructureProperties.chain.auth_asym_id)),
  226. D(MolScript.structureQuery.atomProperty.macromolecular.auth_comp_id, atomProp(StructureProperties.residue.auth_comp_id)),
  227. D(MolScript.structureQuery.atomProperty.macromolecular.auth_seq_id, atomProp(StructureProperties.residue.auth_seq_id)),
  228. D(MolScript.structureQuery.atomProperty.macromolecular.pdbx_PDB_ins_code, atomProp(StructureProperties.residue.pdbx_PDB_ins_code)),
  229. D(MolScript.structureQuery.atomProperty.macromolecular.pdbx_formal_charge, atomProp(StructureProperties.atom.pdbx_formal_charge)),
  230. D(MolScript.structureQuery.atomProperty.macromolecular.occupancy, atomProp(StructureProperties.atom.occupancy)),
  231. D(MolScript.structureQuery.atomProperty.macromolecular.B_iso_or_equiv, atomProp(StructureProperties.atom.B_iso_or_equiv)),
  232. D(MolScript.structureQuery.atomProperty.macromolecular.entityType, atomProp(StructureProperties.entity.type)),
  233. D(MolScript.structureQuery.atomProperty.macromolecular.isModified, (ctx, _) => ctx.element.unit.model.properties.modifiedResidues.parentId.has(StructureProperties.residue.label_comp_id(ctx.element))),
  234. D(MolScript.structureQuery.atomProperty.macromolecular.modifiedParentName, (ctx, _) => {
  235. const id = StructureProperties.residue.label_comp_id(ctx.element);
  236. return ctx.element.unit.model.properties.modifiedResidues.parentId.get(id) || id
  237. })
  238. // TODO
  239. // MolScript.structureQuery.atomProperty.macromolecular.secondaryStructureKey
  240. // MolScript.structureQuery.atomProperty.macromolecular.secondaryStructureFlags
  241. // ============= BOND PROPERTIES ================
  242. ];
  243. function atomProp(p: (e: StructureElement) => any): (ctx: QueryContext, _: any) => any {
  244. return (ctx, _) => p(ctx.element);
  245. }
  246. (function () {
  247. for (const s of symbols) {
  248. DefaultQueryRuntimeTable.addSymbol(s);
  249. }
  250. })();