operators.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2017-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Koya Sakuma <koya.sakuma.work@gmail.com>
  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 { OperatorList } from '../types';
  11. import { Expression } from '../../language/expression';
  12. import { macroproperties } from './macroproperties';
  13. const propNames = Object.keys(macroproperties).sort(h.strLenSortFn)
  14. .filter(name => !macroproperties[name].isUnsupported).join('|');
  15. export const operators: OperatorList = [
  16. {
  17. '@desc': 'Selects atoms that are not included in s1.',
  18. '@examples': ['not ARG'],
  19. name: 'not',
  20. type: h.prefix,
  21. rule: P.MonadicParser.alt(P.MonadicParser.regex(/NOT/i).skip(P.MonadicParser.whitespace), P.MonadicParser.string('!').skip(P.MonadicParser.optWhitespace)),
  22. map: (op, selection) => h.invertExpr(selection),
  23. },
  24. {
  25. '@desc': 'Selects atoms included in both s1 and s2.',
  26. '@examples': ['ASP and .CA'],
  27. name: 'and',
  28. type: h.binaryLeft,
  29. rule: P.MonadicParser.alt(h.infixOp(/AND|&/i)),
  30. // rule: h.infixOp(/AND|&/i),
  31. map: (op, selection, by) => B.struct.modifier.intersectBy({ 0: selection, by })
  32. },
  33. {
  34. '@desc': 'Selects atoms included in either s1 or s2.',
  35. '@examples': ['ASP or GLU'],
  36. name: 'or',
  37. abbr: [','],
  38. type: h.binaryLeft,
  39. rule: P.MonadicParser.alt(h.infixOp(/OR|\||\|\||,/i)),
  40. // rule: h.infixOp(/OR|\||\|\|/i),
  41. map: (op, s1, s2) => B.struct.combinator.merge([s1, s2])
  42. },
  43. /*
  44. {
  45. '@desc': 'Selects atoms within a specified distance of a selection',
  46. '@examples': ['within (5.0, LYS:A.CA)'],
  47. name: 'aaa',
  48. type: h.prefixRemoveKet,
  49. rule: h.prefixOpNoWhiteSpace(/within\s*\(\s*([-+]?[0-9]*\.?[0-9]+)\s*,/, 1).map((x: any) => {
  50. console.log(x)
  51. return parseFloat(x)}),
  52. map: (radius: number, selection: Expression) => {
  53. return B.struct.modifier.includeSurroundings({ 0: selection, radius });
  54. }
  55. },
  56. */
  57. {
  58. '@desc': 'Selects atoms in s1 that are within X Angstroms of any atom in s2.',
  59. '@examples': ['chain A WITHIN 3 OF chain B'],
  60. name: 'within',
  61. abbr: ['w2.'],
  62. // type: h.binaryLeft,
  63. type: h.prefixRemoveKet,
  64. rule: h.prefixOpNoWhiteSpace(/within\s*\(\s*([-+]?[0-9]*\.?[0-9]+)\s*,/i, 1).map((x: any) => {
  65. console.log(x);
  66. return parseFloat(x);
  67. }),
  68. map: (radius: number, target: Expression) => {
  69. return B.struct.filter.within({
  70. 0: B.struct.generator.all(),
  71. target,
  72. 'max-radius': radius,
  73. });
  74. },
  75. },
  76. {
  77. '@desc': 'Selects atoms which have the same keyword as the atoms in a given selection',
  78. '@examples': ['same resid as name FE'],
  79. name: 'same',
  80. type: h.prefix,
  81. rule: h.prefixOp(new RegExp(`SAME\\s+(${propNames})\\s+AS`, 'i'), 1).map((x: any) => macroproperties[x].property),
  82. map: (property: Expression, source: Expression) => {
  83. return B.struct.filter.withSameAtomProperties({
  84. '0': B.struct.generator.all(),
  85. source,
  86. property
  87. });
  88. }
  89. },
  90. {
  91. '@desc':
  92. 'Selects atoms in s1 whose identifiers name and resi match atoms in s2.',
  93. '@examples': ['chain A LIKE chain B'],
  94. name: 'like',
  95. type: h.binaryLeft,
  96. rule: h.infixOp(/LIKE|l\./i),
  97. map: (op: string, selection: Expression, source: Expression) => {
  98. return B.struct.filter.withSameAtomProperties({
  99. 0: selection,
  100. source,
  101. property: B.core.type.compositeKey([
  102. B.ammp('label_atom_id'),
  103. B.ammp('label_seq_id'),
  104. ]),
  105. });
  106. },
  107. },
  108. ];