parser.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Copyright (c) 2017-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. * @author Koya Sakuma < koya.sakuma.work@gmail.com>
  4. * Adapted from MolQL project
  5. **/
  6. import * as P from '../../../mol-util/monadic-parser';
  7. import * as h from '../helper';
  8. import { MolScriptBuilder } from '../../../mol-script/language/builder';
  9. const B = MolScriptBuilder;
  10. import { properties, structureMap } from './properties';
  11. import { operators } from './operators';
  12. import { keywords } from './keywords';
  13. import { AtomGroupArgs } from '../types';
  14. import { Transpiler } from '../transpiler';
  15. import { OperatorList } from '../types';
  16. // const propertiesDict = h.getPropertyRules(properties);
  17. // const slash = P.MonadicParser.string('/');
  18. // <, <=, =, >=, >, !=, and LIKE
  19. const valueOperators: OperatorList = [
  20. {
  21. '@desc': 'value comparisons',
  22. '@examples': [],
  23. name: '=',
  24. abbr: ['=='],
  25. type: h.binaryLeft,
  26. rule: P.MonadicParser.regexp(/\s*(LIKE|>=|<=|=|!=|>|<)\s*/i, 1),
  27. map: (op, e1, e2) => {
  28. let expr;
  29. if (e1 === 'structure') {
  30. expr = B.core.flags.hasAny([B.ammp('secondaryStructureFlags'), structureMap(e2)]);
  31. } else if (e2 === 'structure') {
  32. expr = B.core.flags.hasAny([B.ammp('secondaryStructureFlags'), structureMap(e1)]);
  33. } else if (e1.head !== undefined) {
  34. if (e1.head.name === 'core.type.regex') {
  35. expr = B.core.str.match([e1, B.core.type.str([e2])]);
  36. }
  37. } else if (e2.head !== undefined) {
  38. if (e2.head.name === 'core.type.regex') {
  39. expr = B.core.str.match([e2, B.core.type.str([e1])]);
  40. }
  41. } else if (op.toUpperCase() === 'LIKE') {
  42. if (e1.head) {
  43. expr = B.core.str.match([
  44. B.core.type.regex([`^${e2}$`, 'i']),
  45. B.core.type.str([e1])
  46. ]);
  47. } else {
  48. expr = B.core.str.match([
  49. B.core.type.regex([`^${e1}$`, 'i']),
  50. B.core.type.str([e2])
  51. ]);
  52. }
  53. }
  54. if (!expr) {
  55. if (e1.head) e2 = h.wrapValue(e1, e2);
  56. if (e2.head) e1 = h.wrapValue(e2, e1);
  57. switch (op) {
  58. case '=':
  59. expr = B.core.rel.eq([e1, e2]);
  60. break;
  61. case '!=':
  62. expr = B.core.rel.neq([e1, e2]);
  63. break;
  64. case '>':
  65. expr = B.core.rel.gr([e1, e2]);
  66. break;
  67. case '<':
  68. expr = B.core.rel.lt([e1, e2]);
  69. break;
  70. case '>=':
  71. expr = B.core.rel.gre([e1, e2]);
  72. break;
  73. case '<=':
  74. expr = B.core.rel.lte([e1, e2]);
  75. break;
  76. default: throw new Error(`value operator '${op}' not supported`);
  77. }
  78. }
  79. return B.struct.generator.atomGroups({ 'atom-test': expr });
  80. }
  81. }
  82. ];
  83. function atomExpressionQuery(x: any[]) {
  84. const [resno, inscode, chainname, atomname, altloc] = x[1];
  85. const tests: AtomGroupArgs = {};
  86. if (chainname) {
  87. // should be configurable, there is an option in Jmol to use auth or label
  88. tests['chain-test'] = B.core.rel.eq([B.ammp('auth_asym_id'), chainname]);
  89. }
  90. const resProps = [];
  91. if (resno) resProps.push(B.core.rel.eq([B.ammp('auth_seq_id'), resno]));
  92. if (inscode) resProps.push(B.core.rel.eq([B.ammp('pdbx_PDB_ins_code'), inscode]));
  93. if (resProps.length) tests['residue-test'] = h.andExpr(resProps);
  94. const atomProps = [];
  95. if (atomname) atomProps.push(B.core.rel.eq([B.ammp('auth_atom_id'), atomname]));
  96. if (altloc) atomProps.push(B.core.rel.eq([B.ammp('label_alt_id'), altloc]));
  97. if (atomProps.length) tests['atom-test'] = h.andExpr(atomProps);
  98. return B.struct.generator.atomGroups(tests);
  99. }
  100. const lang = P.MonadicParser.createLanguage({
  101. Integer: () => P.MonadicParser.regexp(/-?[0-9]+/).map(Number).desc('integer'),
  102. Parens: function (r: any) {
  103. return P.MonadicParser.alt(
  104. r.Parens,
  105. r.Operator,
  106. r.Expression
  107. ).wrap(P.MonadicParser.string('('), P.MonadicParser.string(')'));
  108. },
  109. Expression: function (r: any) {
  110. return P.MonadicParser.alt(
  111. r.Keywords,
  112. r.Resno.lookahead(P.MonadicParser.regexp(/\s*(?!(LIKE|>=|<=|!=|[:^%/.=><]))/i)).map((x: any) => B.struct.generator.atomGroups({
  113. 'residue-test': B.core.rel.eq([B.ammp('auth_seq_id'), x])
  114. })),
  115. r.AtomExpression.map(atomExpressionQuery),
  116. r.ValueQuery,
  117. r.Element.map((x: string) => B.struct.generator.atomGroups({
  118. 'atom-test': B.core.rel.eq([B.acp('elementSymbol'), B.struct.type.elementSymbol(x)])
  119. })),
  120. r.Resname.map((x: string) => B.struct.generator.atomGroups({
  121. 'residue-test': B.core.rel.eq([B.ammp('label_comp_id'), x])
  122. })),
  123. );
  124. },
  125. Operator: function (r: any) {
  126. return h.combineOperators(operators, P.MonadicParser.alt(r.Parens, r.Expression));
  127. },
  128. AtomExpression: function (r: any) {
  129. return P.MonadicParser.seq(
  130. P.MonadicParser.lookahead(r.AtomPrefix),
  131. P.MonadicParser.seq(
  132. r.Resno.or(P.MonadicParser.of(null)),
  133. r.Inscode.or(P.MonadicParser.of(null)),
  134. r.Chainname.or(P.MonadicParser.of(null)),
  135. r.Atomname.or(P.MonadicParser.of(null)),
  136. r.Altloc.or(P.MonadicParser.of(null)),
  137. r.Model.or(P.MonadicParser.of(null))
  138. )
  139. );
  140. },
  141. AtomPrefix: () => P.MonadicParser.regexp(/[0-9:^%/.]/).desc('atom-prefix'),
  142. Chainname: () => P.MonadicParser.regexp(/:([A-Za-z]{1,3})/, 1).desc('chainname'),
  143. Model: () => P.MonadicParser.regexp(/\/([0-9]+)/, 1).map(Number).desc('model'),
  144. Element: () => P.MonadicParser.regexp(/_([A-Za-z]{1,3})/, 1).desc('element'),
  145. Atomname: () => P.MonadicParser.regexp(/\.([a-zA-Z0-9]{1,4})/, 1).map(B.atomName).desc('atomname'),
  146. Resname: () => P.MonadicParser.regexp(/[a-zA-Z0-9]{1,4}/).desc('resname'),
  147. Resno: (r: any) => r.Integer.desc('resno'),
  148. Altloc: () => P.MonadicParser.regexp(/%([a-zA-Z0-9])/, 1).desc('altloc'),
  149. Inscode: () => P.MonadicParser.regexp(/\^([a-zA-Z0-9])/, 1).desc('inscode'),
  150. // BracketedResname: function (r) {
  151. // return P.MonadicParser.regexp(/\.([a-zA-Z0-9]{1,4})/, 1)
  152. // .desc('bracketed-resname')
  153. // // [0SD]
  154. // },
  155. // ResnoRange: function (r) {
  156. // return P.MonadicParser.regexp(/\.([\s]){1,3}/, 1)
  157. // .desc('resno-range')
  158. // // 123-200
  159. // // -12--3
  160. // },
  161. Keywords: () => P.MonadicParser.alt(...h.getKeywordRules(keywords)),
  162. Query: function (r: any) {
  163. return P.MonadicParser.alt(
  164. r.Operator,
  165. r.Parens,
  166. r.Expression
  167. ).trim(P.MonadicParser.optWhitespace);
  168. },
  169. Number: function () {
  170. return P.MonadicParser.regexp(/-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?/)
  171. .map(Number)
  172. .desc('number');
  173. },
  174. String: function () {
  175. const w = h.getReservedWords(properties, keywords, operators)
  176. .sort(h.strLenSortFn).map(h.escapeRegExp).join('|');
  177. return P.MonadicParser.alt(
  178. P.MonadicParser.regexp(new RegExp(`(?!(${w}))[A-Z0-9_]+`, 'i')),
  179. P.MonadicParser.regexp(/'((?:[^"\\]|\\.)*)'/, 1),
  180. P.MonadicParser.regexp(/"((?:[^"\\]|\\.)*)"/, 1).map(x => B.core.type.regex([`^${x}$`, 'i']))
  181. );
  182. },
  183. Value: function (r: any) {
  184. return P.MonadicParser.alt(r.Number, r.String);
  185. },
  186. ValueParens: function (r: any) {
  187. return P.MonadicParser.alt(
  188. r.ValueParens,
  189. r.ValueOperator,
  190. r.ValueExpressions
  191. ).wrap(P.MonadicParser.string('('), P.MonadicParser.string(')'));
  192. },
  193. ValuePropertyNames: function () {
  194. return P.MonadicParser.alt(...h.getPropertyNameRules(properties, /LIKE|>=|<=|=|!=|>|<|\)|\s/i));
  195. },
  196. ValueOperator: function (r: any) {
  197. return h.combineOperators(valueOperators, P.MonadicParser.alt(r.ValueParens, r.ValueExpressions));
  198. },
  199. ValueExpressions: function (r: any) {
  200. return P.MonadicParser.alt(
  201. r.Value,
  202. r.ValuePropertyNames
  203. );
  204. },
  205. ValueQuery: function (r: any) {
  206. return P.MonadicParser.alt(
  207. r.ValueOperator.map((x: any) => {
  208. if (x.head) {
  209. if (x.head.name.startsWith('structure-query.generator')) return x;
  210. } else {
  211. if (typeof x === 'string' && x.length <= 4) {
  212. return B.struct.generator.atomGroups({
  213. 'residue-test': B.core.rel.eq([B.ammp('label_comp_id'), x])
  214. });
  215. }
  216. }
  217. throw new Error(`values must be part of an comparison, value '${x}'`);
  218. })
  219. );
  220. }
  221. });
  222. export const transpiler: Transpiler = str => lang.Query.tryParse(str);