atoms.ts 4.1 KB

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