api.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Queries, Structure, StructureQuery, StructureSymmetry } from '../../../mol-model/structure';
  7. import { getAtomsTests } from '../query/atoms';
  8. import { CifWriter } from '../../../mol-io/writer/cif';
  9. import { QuerySchemas } from '../query/schemas';
  10. export enum QueryParamType {
  11. JSON,
  12. String,
  13. Integer,
  14. Boolean,
  15. Float
  16. }
  17. export interface QueryParamInfo<T extends string | number = string | number> {
  18. name: string,
  19. type: QueryParamType,
  20. description?: string,
  21. required?: boolean,
  22. defaultValue?: any,
  23. exampleValues?: any[],
  24. validation?: (v: T) => void,
  25. supportedValues?: string[],
  26. groupName?: string
  27. }
  28. export interface QueryDefinition<Params = any> {
  29. name: string,
  30. niceName: string,
  31. exampleId: string, // default is 1cbs
  32. query: (params: Params, structure: Structure) => StructureQuery,
  33. description: string,
  34. jsonParams: QueryParamInfo[],
  35. restParams: QueryParamInfo[],
  36. structureTransform?: (params: any, s: Structure) => Promise<Structure>,
  37. filter?: CifWriter.Category.Filter,
  38. '@params': Params
  39. }
  40. export const CommonQueryParamsInfo: QueryParamInfo[] = [
  41. { name: 'model_nums', type: QueryParamType.String, description: `A comma-separated list of model ids (i.e. 1,2). If set, only include atoms with the corresponding '_atom_site.pdbx_PDB_model_num' field.` },
  42. { name: 'encoding', type: QueryParamType.String, defaultValue: 'cif', description: `Determines the output encoding (text based 'CIF' or binary 'BCIF').`, supportedValues: ['cif', 'bcif'] },
  43. { name: 'copy_all_categories', type: QueryParamType.Boolean, defaultValue: false, description: 'If true, copy all categories from the input file.' },
  44. { name: 'data_source', type: QueryParamType.String, defaultValue: '', description: 'Allows to control how the provided data source ID maps to input file (as specified by the server instance config).' }
  45. ];
  46. export interface CommonQueryParamsInfo {
  47. model_nums?: number[],
  48. encoding?: 'cif' | 'bcif',
  49. copy_all_categories?: boolean
  50. data_source?: string
  51. }
  52. export const AtomSiteSchemaElement = {
  53. label_entity_id: { type: QueryParamType.String, groupName: 'atom_site' },
  54. label_asym_id: { type: QueryParamType.String, groupName: 'atom_site' },
  55. auth_asym_id: { type: QueryParamType.String, groupName: 'atom_site'},
  56. label_comp_id: { type: QueryParamType.String, groupName: 'atom_site' },
  57. auth_comp_id: { type: QueryParamType.String, groupName: 'atom_site' },
  58. label_seq_id: { type: QueryParamType.Integer, groupName: 'atom_site' },
  59. auth_seq_id: { type: QueryParamType.Integer, groupName: 'atom_site' },
  60. pdbx_PDB_ins_code: { type: QueryParamType.String, groupName: 'atom_site' },
  61. label_atom_id: { type: QueryParamType.String, groupName: 'atom_site' },
  62. auth_atom_id: { type: QueryParamType.String, groupName: 'atom_site' },
  63. type_symbol: { type: QueryParamType.String, groupName: 'atom_site' }
  64. };
  65. export interface AtomSiteSchemaElement {
  66. label_entity_id?: string,
  67. label_asym_id?: string,
  68. auth_asym_id?: string,
  69. label_comp_id?: string,
  70. auth_comp_id?: string,
  71. label_seq_id?: number,
  72. auth_seq_id?: number,
  73. pdbx_PDB_ins_code?: string,
  74. label_atom_id?: string,
  75. auth_atom_id?: string,
  76. type_symbol?: string
  77. }
  78. export type AtomSiteSchema = AtomSiteSchemaElement | AtomSiteSchemaElement[]
  79. const AtomSiteTestJsonParam: QueryParamInfo = {
  80. name: 'atom_site',
  81. type: QueryParamType.JSON,
  82. description: 'Object or array of objects describing atom properties. Names are same as in wwPDB mmCIF dictionary of the atom_site category.',
  83. exampleValues: [[{ label_seq_id: 30, label_asym_id: 'A' }, { label_seq_id: 31, label_asym_id: 'A' }], { label_comp_id: 'ALA' }]
  84. };
  85. export const AtomSiteTestRestParams = (function() {
  86. const params: QueryParamInfo[] = [];
  87. for (const k of Object.keys(AtomSiteSchemaElement)) {
  88. const p = (AtomSiteSchemaElement as any)[k] as QueryParamInfo;
  89. p.name = k;
  90. params.push(p);
  91. }
  92. return params;
  93. })();
  94. const RadiusParam: QueryParamInfo = {
  95. name: 'radius',
  96. type: QueryParamType.Float,
  97. defaultValue: 5,
  98. exampleValues: [5],
  99. description: 'Value in Angstroms.',
  100. validation(v: any) {
  101. if (v < 1 || v > 10) {
  102. throw `Invalid radius for residue interaction query (must be a value between 1 and 10).`;
  103. }
  104. }
  105. };
  106. function Q<Params = any>(definition: Partial<QueryDefinition<Params>>) {
  107. return definition;
  108. }
  109. const QueryMap = {
  110. 'full': Q<{} | undefined>({ niceName: 'Full Structure', query: () => Queries.generators.all, description: 'The full structure.' }),
  111. 'atoms': Q<{ atom_site: AtomSiteSchema }>({
  112. niceName: 'Atoms',
  113. description: 'Atoms satisfying the given criteria.',
  114. query: p => {
  115. return Queries.combinators.merge(getAtomsTests(p.atom_site).map(test => Queries.generators.atoms(test)));
  116. },
  117. jsonParams: [ AtomSiteTestJsonParam ],
  118. restParams: AtomSiteTestRestParams
  119. }),
  120. 'symmetryMates': Q<{ radius: number }>({
  121. niceName: 'Symmetry Mates',
  122. description: 'Computes crystal symmetry mates within the specified radius.',
  123. query: () => Queries.generators.all,
  124. structureTransform(p, s) {
  125. return StructureSymmetry.builderSymmetryMates(s, p.radius).run();
  126. },
  127. jsonParams: [ RadiusParam ],
  128. filter: QuerySchemas.assembly
  129. }),
  130. 'assembly': Q<{ name: string }>({
  131. niceName: 'Assembly',
  132. description: 'Computes structural assembly.',
  133. query: () => Queries.generators.all,
  134. structureTransform(p, s) {
  135. return StructureSymmetry.buildAssembly(s, '' + (p.name || '1')).run();
  136. },
  137. jsonParams: [{
  138. name: 'name',
  139. type: QueryParamType.String,
  140. defaultValue: '1',
  141. exampleValues: ['1'],
  142. description: 'Assembly name.'
  143. }],
  144. filter: QuerySchemas.assembly
  145. }),
  146. 'residueInteraction': Q<{ atom_site: AtomSiteSchema, radius: number }>({
  147. niceName: 'Residue Interaction',
  148. description: 'Identifies all residues within the given radius from the source residue. Takes crystal symmetry into account.',
  149. query(p) {
  150. const tests = getAtomsTests(p.atom_site);
  151. const center = Queries.combinators.merge(tests.map(test => Queries.generators.atoms({
  152. ...test,
  153. entityTest: test.entityTest
  154. ? ctx => test.entityTest!(ctx) && ctx.element.unit.conformation.operator.isIdentity
  155. : ctx => ctx.element.unit.conformation.operator.isIdentity
  156. })));
  157. return Queries.modifiers.includeSurroundings(center, { radius: p.radius !== void 0 ? p.radius : 5, wholeResidues: true });
  158. },
  159. structureTransform(p, s) {
  160. return StructureSymmetry.builderSymmetryMates(s, p.radius !== void 0 ? p.radius : 5).run();
  161. },
  162. jsonParams: [ AtomSiteTestJsonParam, RadiusParam ],
  163. restParams: [ ...AtomSiteTestRestParams, RadiusParam ],
  164. filter: QuerySchemas.interaction
  165. }),
  166. 'residueSurroundings': Q<{ atom_site: AtomSiteSchema, radius: number }>({
  167. niceName: 'Residue Surroundings',
  168. description: 'Identifies all residues within the given radius from the source residue.',
  169. query(p) {
  170. const center = Queries.combinators.merge(getAtomsTests(p.atom_site).map(test => Queries.generators.atoms(test)));
  171. return Queries.modifiers.includeSurroundings(center, { radius: p.radius, wholeResidues: true });
  172. },
  173. jsonParams: [ AtomSiteTestJsonParam, RadiusParam ],
  174. restParams: [ ...AtomSiteTestRestParams, RadiusParam ],
  175. filter: QuerySchemas.interaction
  176. })
  177. };
  178. export type QueryName = keyof typeof QueryMap
  179. export type QueryParams<Q extends QueryName> = Partial<(typeof QueryMap)[Q]['@params']>
  180. export function getQueryByName(name: QueryName): QueryDefinition {
  181. return QueryMap[name] as QueryDefinition;
  182. }
  183. export const QueryList = (function () {
  184. const list: { name: string, definition: QueryDefinition }[] = [];
  185. for (const k of Object.keys(QueryMap)) list.push({ name: k, definition: <QueryDefinition>QueryMap[k as QueryName] });
  186. list.sort(function (a, b) { return a.name < b.name ? -1 : a.name > b.name ? 1 : 0; });
  187. return list;
  188. })();
  189. // normalize the queries
  190. (function () {
  191. for (let q of QueryList) {
  192. const m = q.definition;
  193. m.name = q.name;
  194. m.jsonParams = m.jsonParams || [];
  195. m.restParams = m.restParams || m.jsonParams;
  196. }
  197. })();
  198. function _normalizeQueryParams(params: { [p: string]: string }, paramList: QueryParamInfo[]): { [p: string]: string | number | boolean } {
  199. const ret: any = {};
  200. for (const p of paramList) {
  201. const key = p.name;
  202. const value = params[key];
  203. let el: any;
  204. if (typeof value === 'undefined' || (typeof value !== 'undefined' && value !== null && value['length'] === 0)) {
  205. if (p.required) {
  206. throw `The parameter '${key}' is required.`;
  207. }
  208. if (typeof p.defaultValue !== 'undefined') el = p.defaultValue;
  209. } else {
  210. switch (p.type) {
  211. case QueryParamType.JSON: el = JSON.parse(value); break;
  212. case QueryParamType.String: el = value; break;
  213. case QueryParamType.Integer: el = parseInt(value); break;
  214. case QueryParamType.Float: el = parseFloat(value); break;
  215. case QueryParamType.Boolean: el = Boolean(+value); break;
  216. }
  217. if (p.validation) p.validation(el);
  218. }
  219. if (typeof el === 'undefined') continue;
  220. if (p.groupName) {
  221. if (typeof ret[p.groupName] === 'undefined') ret[p.groupName] = {};
  222. ret[p.groupName][key] = el;
  223. } else {
  224. ret[key] = el;
  225. }
  226. }
  227. return ret;
  228. }
  229. export function normalizeRestQueryParams(query: QueryDefinition, params: any) {
  230. // return params;
  231. return _normalizeQueryParams(params, query.restParams);
  232. }
  233. export function normalizeRestCommonParams(params: any): CommonQueryParamsInfo {
  234. return {
  235. model_nums: params.model_nums ? ('' + params.model_nums).split(',').map(n => n.trim()).filter(n => !!n).map(n => +n) : void 0,
  236. data_source: params.data_source,
  237. copy_all_categories: Boolean(params.copy_all_categories),
  238. encoding: ('' + params.encoding).toLocaleLowerCase() === 'bcif' ? 'bcif' : 'cif'
  239. };
  240. }