api.ts 15 KB

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