operators.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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), P.MonadicParser.whitespace),
  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. type: h.binaryLeft,
  38. rule: h.infixOp(/OR|\||\|\|/i),
  39. map: (op, s1, s2) => B.struct.combinator.merge([s1, s2])
  40. },
  41. /*
  42. {
  43. '@desc': 'Selects atoms within a specified distance of a selection',
  44. '@examples': ['within (5.0, LYS:A.CA)'],
  45. name: 'aaa',
  46. type: h.prefixRemoveKet,
  47. rule: h.prefixOpNoWhiteSpace(/within\s*\(\s*([-+]?[0-9]*\.?[0-9]+)\s*,/, 1).map((x: any) => {
  48. console.log(x)
  49. return parseFloat(x)}),
  50. map: (radius: number, selection: Expression) => {
  51. return B.struct.modifier.includeSurroundings({ 0: selection, radius });
  52. }
  53. },
  54. */
  55. {
  56. '@desc': 'Selects atoms in s1 that are within X Angstroms of any atom in s2.',
  57. '@examples': ['chain A WITHIN 3 OF chain B'],
  58. name: 'within',
  59. abbr: ['w2.'],
  60. // type: h.binaryLeft,
  61. type: h.prefixRemoveKet,
  62. rule: h.prefixOpNoWhiteSpace(/within\s*\(\s*([-+]?[0-9]*\.?[0-9]+)\s*,/i, 1).map((x: any) => {
  63. console.log(x);
  64. return parseFloat(x);
  65. }),
  66. map: (radius: number, target: Expression) => {
  67. return B.struct.filter.within({
  68. 0: B.struct.generator.all(),
  69. target,
  70. 'max-radius': radius,
  71. });
  72. },
  73. },
  74. {
  75. '@desc': 'Selects atoms which have the same keyword as the atoms in a given selection',
  76. '@examples': ['same resid as name FE'],
  77. name: 'same',
  78. type: h.prefix,
  79. rule: h.prefixOp(new RegExp(`SAME\\s+(${propNames})\\s+AS`, 'i'), 1).map((x: any) => macroproperties[x].property),
  80. map: (property: Expression, source: Expression) => {
  81. return B.struct.filter.withSameAtomProperties({
  82. '0': B.struct.generator.all(),
  83. source,
  84. property
  85. });
  86. }
  87. },
  88. {
  89. '@desc':
  90. 'Selects atoms in s1 whose identifiers name and resi match atoms in s2.',
  91. '@examples': ['chain A LIKE chain B'],
  92. name: 'like',
  93. type: h.binaryLeft,
  94. rule: h.infixOp(/LIKE|l\./i),
  95. map: (op: string, selection: Expression, source: Expression) => {
  96. return B.struct.filter.withSameAtomProperties({
  97. 0: selection,
  98. source,
  99. property: B.core.type.compositeKey([
  100. B.ammp('label_atom_id'),
  101. B.ammp('label_seq_id'),
  102. ]),
  103. });
  104. },
  105. },
  106. ];