api.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 { Query, Queries, Structure, Element, StructureSymmetry } from 'mol-model/structure';
  7. export enum QueryParamType {
  8. String,
  9. Integer,
  10. Float
  11. }
  12. export interface QueryParamInfo {
  13. name: string,
  14. type: QueryParamType,
  15. description?: string,
  16. required?: boolean,
  17. defaultValue?: any,
  18. exampleValue?: string,
  19. validation?: (v: any) => void
  20. }
  21. export interface QueryDefinition {
  22. name: string,
  23. niceName: string,
  24. exampleId: string, // default is 1cbs
  25. query: (params: any, structure: Structure) => Query,
  26. description: string,
  27. params: QueryParamInfo[],
  28. structureTransform?: (params: any, s: Structure) => Promise<Structure>
  29. }
  30. const AtomSiteParameters = {
  31. entity_id: <QueryParamInfo>{ name: 'entity_id', type: QueryParamType.String, description: 'Corresponds to the \'_entity.id\' or \'*.label_entity_id\' field, depending on the context.' },
  32. label_asym_id: <QueryParamInfo>{ name: 'label_asym_id', type: QueryParamType.String, description: 'Corresponds to the \'_atom_site.label_asym_id\' field.' },
  33. auth_asym_id: <QueryParamInfo>{ name: 'auth_asym_id', type: QueryParamType.String, exampleValue: 'A', description: 'Corresponds to the \'_atom_site.auth_asym_id\' field.' },
  34. label_seq_id: <QueryParamInfo>{ name: 'label_seq_id', type: QueryParamType.Integer, description: 'Residue seq. number. Corresponds to the \'_atom_site.label_seq_id\' field.' },
  35. auth_seq_id: <QueryParamInfo>{ name: 'auth_seq_id', type: QueryParamType.Integer, exampleValue: '200', description: 'Author residue seq. number. Corresponds to the \'_atom_site.auth_seq_id\' field.' },
  36. label_comp_id: <QueryParamInfo>{ name: 'label_comp_id', type: QueryParamType.String, description: 'Residue name. Corresponds to the \'_atom_site.label_comp_id\' field.' },
  37. auth_comp_id: <QueryParamInfo>{ name: 'auth_comp_id', type: QueryParamType.String, exampleValue: 'REA', description: 'Author residue name. Corresponds to the \'_atom_site.auth_comp_id\' field.' },
  38. pdbx_PDB_ins_code: <QueryParamInfo>{ name: 'pdbx_PDB_ins_code', type: QueryParamType.String, description: 'Corresponds to the \'_atom_site.pdbx_PDB_ins_code\' field.' },
  39. };
  40. // function entityTest(params: any): Element.Predicate | undefined {
  41. // if (typeof params.entity_id === 'undefined') return void 0;
  42. // const p = Queries.props.entity.id, id = '' + params.entityId;
  43. // return Element.property(l => p(l) === id);
  44. // }
  45. function entityTest1_555(params: any): Element.Predicate | undefined {
  46. const oper = Queries.props.unit.operator_name;
  47. if (typeof params.entity_id === 'undefined') return Element.property(l => oper(l) === '1_555');
  48. const p = Queries.props.entity.id, id = '' + params.entityId;
  49. return Element.property(l => p(l) === id && oper(l) === '1_555');
  50. }
  51. function chainTest(params: any): Element.Predicate | undefined {
  52. if (typeof params.label_asym_id !== 'undefined') {
  53. const p = Queries.props.chain.label_asym_id, id = '' + params.label_asym_id;
  54. return Element.property(l => p(l) === id);
  55. }
  56. if (typeof params.auth_asym_id !== 'undefined') {
  57. const p = Queries.props.chain.auth_asym_id, id = '' + params.auth_asym_id;
  58. return Element.property(l => p(l) === id);
  59. }
  60. return void 0;
  61. }
  62. function residueTest(params: any): Element.Predicate | undefined {
  63. const props: Element.Property<any>[] = [], values: any[] = [];
  64. if (typeof params.label_seq_id !== 'undefined') {
  65. props.push(Queries.props.residue.label_seq_id);
  66. values.push(+params.label_seq_id);
  67. }
  68. if (typeof params.auth_seq_id !== 'undefined') {
  69. props.push(Queries.props.residue.auth_seq_id);
  70. values.push(+params.auth_seq_id);
  71. }
  72. if (typeof params.label_comp_id !== 'undefined') {
  73. props.push(Queries.props.residue.label_comp_id);
  74. values.push(params.label_comp_id);
  75. }
  76. if (typeof params.auth_comp_id !== 'undefined') {
  77. props.push(Queries.props.residue.auth_comp_id);
  78. values.push(params.auth_comp_id);
  79. }
  80. if (typeof params.pdbx_PDB_ins_code !== 'undefined') {
  81. props.push(Queries.props.residue.pdbx_PDB_ins_code);
  82. values.push(params.pdbx_PDB_ins_code);
  83. }
  84. switch (props.length) {
  85. case 0: return void 0;
  86. case 1: return Element.property(l => props[0](l) === values[0]);
  87. case 2: return Element.property(l => props[0](l) === values[0] && props[1](l) === values[1]);
  88. case 3: return Element.property(l => props[0](l) === values[0] && props[1](l) === values[1] && props[2](l) === values[2]);
  89. default: {
  90. const len = props.length;
  91. return Element.property(l => {
  92. for (let i = 0; i < len; i++) if (!props[i](l) !== values[i]) return false;
  93. return true;
  94. });
  95. }
  96. }
  97. }
  98. // function buildResiduesQuery(params: any): Query.Provider {
  99. // return Queries.generators.atoms({ entityTest: entityTest(params), chainTest: chainTest(params), residueTest: residueTest(params) });
  100. // }
  101. const QueryMap: { [id: string]: Partial<QueryDefinition> } = {
  102. 'full': { niceName: 'Full Structure', query: () => Query(Queries.generators.all), description: 'The full structure.' },
  103. 'residueInteraction': {
  104. niceName: 'Residues Inside a Sphere',
  105. description: 'Identifies all residues within the given radius from the source residue.',
  106. query(p) {
  107. const center = Queries.generators.atoms({ entityTest: entityTest1_555(p), chainTest: chainTest(p), residueTest: residueTest(p) });
  108. return Query(Queries.modifiers.includeSurroundings(center, { radius: p.radius, wholeResidues: true }));
  109. },
  110. structureTransform(p, s) {
  111. return StructureSymmetry.builderSymmetryMates(s, p.radius).run();
  112. },
  113. params: [
  114. AtomSiteParameters.entity_id,
  115. AtomSiteParameters.label_asym_id,
  116. AtomSiteParameters.auth_asym_id,
  117. AtomSiteParameters.label_comp_id,
  118. AtomSiteParameters.auth_comp_id,
  119. AtomSiteParameters.pdbx_PDB_ins_code,
  120. AtomSiteParameters.label_seq_id,
  121. AtomSiteParameters.auth_seq_id,
  122. {
  123. name: 'radius',
  124. type: QueryParamType.Float,
  125. defaultValue: 5,
  126. exampleValue: '5',
  127. description: 'Value in Angstroms.',
  128. validation(v: any) {
  129. if (v < 1 || v > 10) {
  130. throw `Invalid radius for residue interaction query (must be a value between 1 and 10).`;
  131. }
  132. }
  133. },
  134. ]
  135. },
  136. };
  137. export function getQueryByName(name: string): QueryDefinition {
  138. return QueryMap[name] as QueryDefinition;
  139. }
  140. export const QueryList = (function () {
  141. const list: { name: string, definition: QueryDefinition }[] = [];
  142. for (const k of Object.keys(QueryMap)) list.push({ name: k, definition: <QueryDefinition>QueryMap[k] });
  143. list.sort(function (a, b) { return a.name < b.name ? -1 : a.name > b.name ? 1 : 0 });
  144. return list;
  145. })();
  146. // normalize the queries
  147. (function () {
  148. for (let q of QueryList) {
  149. const m = q.definition;
  150. m.name = q.name;
  151. m.params = m.params || [];
  152. }
  153. })();
  154. function _normalizeQueryParams(params: { [p: string]: string }, paramList: QueryParamInfo[]): { [p: string]: string | number | boolean } {
  155. const ret: any = {};
  156. for (const p of paramList) {
  157. const key = p.name;
  158. const value = params[key];
  159. if (typeof value === 'undefined' || (typeof value !== 'undefined' && value !== null && value['length'] === 0)) {
  160. if (p.required) {
  161. throw `The parameter '${key}' is required.`;
  162. }
  163. if (typeof p.defaultValue !== 'undefined') ret[key] = p.defaultValue;
  164. } else {
  165. switch (p.type) {
  166. case QueryParamType.String: ret[key] = value; break;
  167. case QueryParamType.Integer: ret[key] = parseInt(value); break;
  168. case QueryParamType.Float: ret[key] = parseFloat(value); break;
  169. }
  170. if (p.validation) p.validation(ret[key]);
  171. }
  172. }
  173. return ret;
  174. }
  175. export function normalizeQueryParams(query: QueryDefinition, params: any) {
  176. return _normalizeQueryParams(params, query.params);
  177. }