atoms.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 { QueryPredicate, StructureElement, StructureProperties as Props, Queries } from 'mol-model/structure';
  7. import { AtomsQueryParams } from 'mol-model/structure/query/queries/generators';
  8. export function getAtomsTests(params: any): Partial<AtomsQueryParams>[] {
  9. if (!params) return [{ }];
  10. if (Array.isArray(params)) {
  11. return params.map(p => atomsTest(p));
  12. } else {
  13. return [atomsTest(params)];
  14. }
  15. }
  16. function atomsTest(params: any): Partial<AtomsQueryParams> {
  17. return {
  18. entityTest: entityTest(params),
  19. chainTest: chainTest(params),
  20. residueTest: residueTest(params),
  21. atomTest: atomTest(params)
  22. };
  23. }
  24. function entityTest(params: any): QueryPredicate | undefined {
  25. if (!params || typeof params.entity_id === 'undefined') return void 0;
  26. const p = Props.entity.id, id = '' + params.label_entity_id;
  27. return ctx => p(ctx.element) === id;
  28. }
  29. function chainTest(params: any): QueryPredicate | undefined {
  30. if (!params) return void 0;
  31. if (typeof params.label_asym_id !== 'undefined') {
  32. const p = Props.chain.label_asym_id, id = '' + params.label_asym_id;
  33. return ctx => p(ctx.element) === id;
  34. }
  35. if (typeof params.auth_asym_id !== 'undefined') {
  36. const p = Props.chain.auth_asym_id, id = '' + params.auth_asym_id;
  37. return ctx => p(ctx.element) === id;
  38. }
  39. return void 0;
  40. }
  41. function residueTest(params: any): QueryPredicate | undefined {
  42. if (!params) return void 0;
  43. const props: StructureElement.Property<any>[] = [], values: any[] = [];
  44. if (typeof params.label_seq_id !== 'undefined') {
  45. props.push(Props.residue.label_seq_id);
  46. values.push(+params.label_seq_id);
  47. }
  48. if (typeof params.auth_seq_id !== 'undefined') {
  49. props.push(Props.residue.auth_seq_id);
  50. values.push(+params.auth_seq_id);
  51. }
  52. if (typeof params.label_comp_id !== 'undefined') {
  53. props.push(Props.residue.label_comp_id);
  54. values.push(params.label_comp_id);
  55. }
  56. if (typeof params.auth_comp_id !== 'undefined') {
  57. props.push(Props.residue.auth_comp_id);
  58. values.push(params.auth_comp_id);
  59. }
  60. if (typeof params.pdbx_PDB_ins_code !== 'undefined') {
  61. props.push(Props.residue.pdbx_PDB_ins_code);
  62. values.push(params.pdbx_PDB_ins_code);
  63. }
  64. return andEqual(props, values);
  65. }
  66. function atomTest(params: any): QueryPredicate | undefined {
  67. if (!params) return void 0;
  68. const props: StructureElement.Property<any>[] = [], values: any[] = [];
  69. if (typeof params.label_atom_id !== 'undefined') {
  70. props.push(Props.atom.label_atom_id);
  71. values.push(+params.label_atom_id);
  72. }
  73. if (typeof params.auth_atom_id !== 'undefined') {
  74. props.push(Props.atom.auth_atom_id);
  75. values.push(+params.auth_atom_id);
  76. }
  77. if (typeof params.type_symbol !== 'undefined') {
  78. props.push(Props.atom.type_symbol);
  79. values.push(+params.type_symbol);
  80. }
  81. return andEqual(props, values);
  82. }
  83. function andEqual(props: StructureElement.Property<any>[], values: any[]): QueryPredicate | undefined {
  84. switch (props.length) {
  85. case 0: return void 0;
  86. case 1: return ctx => props[0](ctx.element) === values[0];
  87. case 2: return ctx => props[0](ctx.element) === values[0] && props[1](ctx.element) === values[1];
  88. case 3: return ctx => props[0](ctx.element) === values[0] && props[1](ctx.element) === values[1] && props[2](ctx.element) === values[2];
  89. default: {
  90. const len = props.length;
  91. return ctx => {
  92. for (let i = 0; i < len; i++) if (!props[i](ctx.element) !== values[i]) return false;
  93. return true;
  94. };
  95. }
  96. }
  97. }