operators.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2017-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  4. * @author Panagiotis Tourlas <panagiot_tourlov@hotmail.com>
  5. *
  6. * @author Koya Sakuma
  7. * This module is based on jmol transpiler from MolQL and modified in similar manner as pymol and vmd tranpilers
  8. */
  9. import * as P from '../../../mol-util/monadic-parser';
  10. import * as h from '../helper';
  11. import { MolScriptBuilder } from '../../../mol-script/language/builder';
  12. const B = MolScriptBuilder;
  13. import { OperatorList } from '../types';
  14. import { Expression } from '../../language/expression';
  15. import { macroproperties } from './macroproperties'
  16. const propNames = Object.keys(macroproperties).sort(h.strLenSortFn)
  17. .filter(name => !macroproperties[name].isUnsupported).join('|');
  18. export const operators: OperatorList = [
  19. {
  20. '@desc': 'Selects atoms that are not included in s1.',
  21. '@examples': ['not ARG'],
  22. name: 'not',
  23. type: h.prefix,
  24. rule: P.MonadicParser.alt(P.MonadicParser.regex(/NOT/i).skip(P.MonadicParser.whitespace), P.MonadicParser.string('!').skip(P.MonadicParser.optWhitespace)),
  25. map: (op, selection) => h.invertExpr(selection),
  26. },
  27. {
  28. '@desc': 'Selects atoms included in both s1 and s2.',
  29. '@examples': ['ASP and .CA'],
  30. name: 'and',
  31. type: h.binaryLeft,
  32. rule: P.MonadicParser.alt(h.infixOp(/AND|&/i), P.MonadicParser.whitespace),
  33. //rule: h.infixOp(/AND|&/i),
  34. map: (op, selection, by) => B.struct.modifier.intersectBy({ 0: selection, by })
  35. },
  36. {
  37. '@desc': 'Selects atoms included in either s1 or s2.',
  38. '@examples': ['ASP or GLU'],
  39. name: 'or',
  40. type: h.binaryLeft,
  41. rule: h.infixOp(/OR|\||\|\|/i),
  42. map: (op, s1, s2) => B.struct.combinator.merge([s1, s2])
  43. },
  44. /*
  45. {
  46. '@desc': 'Selects atoms within a specified distance of a selection',
  47. '@examples': ['within (5.0, LYS:A.CA)'],
  48. name: 'aaa',
  49. type: h.prefixRemoveKet,
  50. rule: h.prefixOpNoWhiteSpace(/within\s*\(\s*([-+]?[0-9]*\.?[0-9]+)\s*,/, 1).map((x: any) => {
  51. console.log(x)
  52. return parseFloat(x)}),
  53. map: (radius: number, selection: Expression) => {
  54. return B.struct.modifier.includeSurroundings({ 0: selection, radius });
  55. }
  56. },
  57. */
  58. {
  59. '@desc':'Selects atoms in s1 that are within X Angstroms of any atom in s2.',
  60. '@examples': ['chain A WITHIN 3 OF chain B'],
  61. name: 'within',
  62. abbr: ['w2.'],
  63. // type: h.binaryLeft,
  64. type: h.prefixRemoveKet,
  65. rule: h.prefixOpNoWhiteSpace(/within\s*\(\s*([-+]?[0-9]*\.?[0-9]+)\s*,/i, 1).map((x: any) => {
  66. console.log(x)
  67. return parseFloat(x)}),
  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. ];