api.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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, StructureProperties } 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, numModels: number[]) => 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'). Ligands can also be exported as 'SDF', 'MOL', or 'MOL2'.`, supportedValues: ['cif', 'bcif', 'sdf', 'mol', 'mol2'] },
  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. { name: 'transformation', type: QueryParamType.JSON, defaultValue: void 0, description: `Transformation to apply to coordinates in '_atom_site'. Accepts a 4x4 transformation matrix, provided as array of 16 float values.` }
  46. ];
  47. export type Encoding = 'cif' | 'bcif' | 'sdf' | 'mol' | 'mol2';
  48. export interface CommonQueryParamsInfo {
  49. model_nums?: number[],
  50. encoding?: Encoding,
  51. copy_all_categories?: boolean
  52. data_source?: string
  53. }
  54. export const AtomSiteSchemaElement = {
  55. label_entity_id: { type: QueryParamType.String, groupName: 'atom_site' },
  56. label_asym_id: { type: QueryParamType.String, groupName: 'atom_site' },
  57. auth_asym_id: { type: QueryParamType.String, groupName: 'atom_site'},
  58. label_comp_id: { type: QueryParamType.String, groupName: 'atom_site' },
  59. auth_comp_id: { type: QueryParamType.String, groupName: 'atom_site' },
  60. label_seq_id: { type: QueryParamType.Integer, groupName: 'atom_site' },
  61. auth_seq_id: { type: QueryParamType.Integer, groupName: 'atom_site' },
  62. pdbx_PDB_ins_code: { type: QueryParamType.String, groupName: 'atom_site' },
  63. label_atom_id: { type: QueryParamType.String, groupName: 'atom_site' },
  64. auth_atom_id: { type: QueryParamType.String, groupName: 'atom_site' },
  65. type_symbol: { type: QueryParamType.String, groupName: 'atom_site' }
  66. };
  67. export interface AtomSiteSchemaElement {
  68. label_entity_id?: string,
  69. label_asym_id?: string,
  70. auth_asym_id?: string,
  71. label_comp_id?: string,
  72. auth_comp_id?: string,
  73. label_seq_id?: number,
  74. auth_seq_id?: number,
  75. pdbx_PDB_ins_code?: string,
  76. label_atom_id?: string,
  77. auth_atom_id?: string,
  78. type_symbol?: string
  79. }
  80. export type AtomSiteSchema = AtomSiteSchemaElement | AtomSiteSchemaElement[]
  81. const AtomSiteTestJsonParam: QueryParamInfo = {
  82. name: 'atom_site',
  83. type: QueryParamType.JSON,
  84. description: 'Object or array of objects describing atom properties. Names are same as in wwPDB mmCIF dictionary of the atom_site category.',
  85. exampleValues: [[{ label_seq_id: 30, label_asym_id: 'A' }, { label_seq_id: 31, label_asym_id: 'A' }], { label_comp_id: 'ALA' }]
  86. };
  87. export const AtomSiteTestRestParams = (function() {
  88. const params: QueryParamInfo[] = [];
  89. for (const k of Object.keys(AtomSiteSchemaElement)) {
  90. const p = (AtomSiteSchemaElement as any)[k] as QueryParamInfo;
  91. p.name = k;
  92. params.push(p);
  93. }
  94. return params;
  95. })();
  96. const RadiusParam: QueryParamInfo = {
  97. name: 'radius',
  98. type: QueryParamType.Float,
  99. defaultValue: 5,
  100. exampleValues: [5],
  101. description: 'Value in Angstroms.',
  102. validation(v: any) {
  103. if (v < 1 || v > 10) {
  104. throw `Invalid radius for residue interaction query (must be a value between 1 and 10).`;
  105. }
  106. }
  107. };
  108. const AssemblyNameParam: QueryParamInfo = {
  109. name: 'assembly_name',
  110. type: QueryParamType.String,
  111. description: 'Assembly name. If none is provided, crystal symmetry (where available) or deposited model is used.'
  112. };
  113. function Q<Params = any>(definition: Partial<QueryDefinition<Params>>) {
  114. return definition;
  115. }
  116. const QueryMap = {
  117. 'full': Q<{} | undefined>({ niceName: 'Full Structure', query: () => Queries.generators.all, description: 'The full structure.' }),
  118. 'ligand': Q<{ atom_site: AtomSiteSchema }>({
  119. niceName: 'Ligand',
  120. description: 'Coordinates of the first group satisfying the given criteria.',
  121. query: (p, _s, numModels) => {
  122. const tests = getAtomsTests(p.atom_site);
  123. const ligands = Queries.combinators.merge(tests.map(test => Queries.generators.atoms({
  124. ...test,
  125. unitTest: ctx => StructureProperties.unit.model_num(ctx.element) === numModels[0],
  126. groupBy: ctx => StructureProperties.residue.key(ctx.element)
  127. })));
  128. return Queries.filters.first(ligands);
  129. },
  130. jsonParams: [ AtomSiteTestJsonParam ],
  131. restParams: AtomSiteTestRestParams
  132. }),
  133. 'atoms': Q<{ atom_site: AtomSiteSchema }>({
  134. niceName: 'Atoms',
  135. description: 'Atoms satisfying the given criteria.',
  136. query: p => {
  137. return Queries.combinators.merge(getAtomsTests(p.atom_site).map(test => Queries.generators.atoms(test)));
  138. },
  139. jsonParams: [ AtomSiteTestJsonParam ],
  140. restParams: AtomSiteTestRestParams
  141. }),
  142. 'symmetryMates': Q<{ radius: number }>({
  143. niceName: 'Symmetry Mates',
  144. description: 'Computes crystal symmetry mates within the specified radius.',
  145. query: () => Queries.generators.all,
  146. structureTransform(p, s) {
  147. return StructureSymmetry.builderSymmetryMates(s, p.radius).run();
  148. },
  149. jsonParams: [ RadiusParam ],
  150. filter: QuerySchemas.assembly
  151. }),
  152. 'assembly': Q<{ name: string }>({
  153. niceName: 'Assembly',
  154. description: 'Computes structural assembly.',
  155. query: () => Queries.generators.all,
  156. structureTransform(p, s) {
  157. return StructureSymmetry.buildAssembly(s, '' + (p.name || '1')).run();
  158. },
  159. jsonParams: [{
  160. name: 'name',
  161. type: QueryParamType.String,
  162. defaultValue: '1',
  163. exampleValues: ['1'],
  164. description: 'Assembly name.'
  165. }],
  166. filter: QuerySchemas.assembly
  167. }),
  168. 'residueInteraction': Q<{ atom_site: AtomSiteSchema, radius: number, assembly_name: string }>({
  169. niceName: 'Residue Interaction',
  170. description: 'Identifies all residues within the given radius from the source residue. Takes crystal symmetry into account.',
  171. query(p) {
  172. const tests = getAtomsTests(p.atom_site);
  173. const center = Queries.combinators.merge(tests.map(test => Queries.generators.atoms({
  174. ...test,
  175. entityTest: test.entityTest
  176. ? ctx => test.entityTest!(ctx) && ctx.element.unit.conformation.operator.isIdentity
  177. : ctx => ctx.element.unit.conformation.operator.isIdentity
  178. })));
  179. return Queries.modifiers.includeSurroundings(center, { radius: p.radius !== void 0 ? p.radius : 5, wholeResidues: true });
  180. },
  181. structureTransform(p, s) {
  182. if (p.assembly_name) return StructureSymmetry.buildAssembly(s, '' + p.assembly_name).run();
  183. return StructureSymmetry.builderSymmetryMates(s, p.radius !== void 0 ? p.radius : 5).run();
  184. },
  185. jsonParams: [ AtomSiteTestJsonParam, RadiusParam, AssemblyNameParam ],
  186. restParams: [ ...AtomSiteTestRestParams, RadiusParam, AssemblyNameParam ],
  187. filter: QuerySchemas.interaction
  188. }),
  189. 'residueSurroundings': Q<{ atom_site: AtomSiteSchema, radius: number }>({
  190. niceName: 'Residue Surroundings',
  191. description: 'Identifies all residues within the given radius from the source residue.',
  192. query(p) {
  193. const center = Queries.combinators.merge(getAtomsTests(p.atom_site).map(test => Queries.generators.atoms(test)));
  194. return Queries.modifiers.includeSurroundings(center, { radius: p.radius, wholeResidues: true });
  195. },
  196. jsonParams: [ AtomSiteTestJsonParam, RadiusParam ],
  197. restParams: [ ...AtomSiteTestRestParams, RadiusParam ],
  198. filter: QuerySchemas.interaction
  199. })
  200. };
  201. export type QueryName = keyof typeof QueryMap
  202. export type QueryParams<Q extends QueryName> = Partial<(typeof QueryMap)[Q]['@params']>
  203. export function getQueryByName(name: QueryName): QueryDefinition {
  204. return QueryMap[name] as QueryDefinition;
  205. }
  206. export const QueryList = (function () {
  207. const list: { name: string, definition: QueryDefinition }[] = [];
  208. for (const k of Object.keys(QueryMap)) list.push({ name: k, definition: <QueryDefinition>QueryMap[k as QueryName] });
  209. list.sort(function (a, b) { return a.name < b.name ? -1 : a.name > b.name ? 1 : 0; });
  210. return list;
  211. })();
  212. // normalize the queries
  213. (function () {
  214. for (let q of QueryList) {
  215. const m = q.definition;
  216. m.name = q.name;
  217. m.jsonParams = m.jsonParams || [];
  218. m.restParams = m.restParams || m.jsonParams;
  219. }
  220. })();
  221. function _normalizeQueryParams(params: { [p: string]: string }, paramList: QueryParamInfo[]): { [p: string]: string | number | boolean } {
  222. const ret: any = {};
  223. for (const p of paramList) {
  224. const key = p.name;
  225. const value = params[key];
  226. let el: any;
  227. if (typeof value === 'undefined' || (typeof value !== 'undefined' && value !== null && value['length'] === 0)) {
  228. if (p.required) {
  229. throw `The parameter '${key}' is required.`;
  230. }
  231. if (typeof p.defaultValue !== 'undefined') el = p.defaultValue;
  232. } else {
  233. switch (p.type) {
  234. case QueryParamType.JSON: el = JSON.parse(value); break;
  235. case QueryParamType.String: el = value; break;
  236. case QueryParamType.Integer: el = parseInt(value); break;
  237. case QueryParamType.Float: el = parseFloat(value); break;
  238. case QueryParamType.Boolean: el = Boolean(+value); break;
  239. }
  240. if (p.validation) p.validation(el);
  241. }
  242. if (typeof el === 'undefined') continue;
  243. if (p.groupName) {
  244. if (typeof ret[p.groupName] === 'undefined') ret[p.groupName] = {};
  245. ret[p.groupName][key] = el;
  246. } else {
  247. ret[key] = el;
  248. }
  249. }
  250. return ret;
  251. }
  252. export function normalizeRestQueryParams(query: QueryDefinition, params: any) {
  253. // return params;
  254. return _normalizeQueryParams(params, query.restParams);
  255. }
  256. export function normalizeRestCommonParams(params: any): CommonQueryParamsInfo {
  257. return {
  258. model_nums: params.model_nums ? ('' + params.model_nums).split(',').map(n => n.trim()).filter(n => !!n).map(n => +n) : void 0,
  259. data_source: params.data_source,
  260. copy_all_categories: Boolean(params.copy_all_categories),
  261. encoding: mapEncoding(('' + params.encoding).toLocaleLowerCase())
  262. };
  263. }
  264. function mapEncoding(value: string) {
  265. switch (value) {
  266. case 'bcif':
  267. return 'bcif';
  268. case 'mol':
  269. return 'mol';
  270. case 'mol2':
  271. return 'mol2';
  272. case 'sdf':
  273. return 'sdf';
  274. default:
  275. return 'cif';
  276. }
  277. }