mol-script.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { MolScriptBuilder } from '../mol-script/language/builder';
  2. import { compile, QuerySymbolRuntime, DefaultQueryRuntimeTable } from '../mol-script/runtime/query/compiler';
  3. import { QueryContext, Structure, StructureQuery } from '../mol-model/structure';
  4. import { readCifFile, getModelsAndStructure } from '../apps/structure-info/model';
  5. import { CustomPropSymbol } from '../mol-script/language/symbol';
  6. import Type from '../mol-script/language/type';
  7. import { parseMolScript } from '../mol-script/language/parser';
  8. import * as util from 'util';
  9. import { transpileMolScript } from '../mol-script/script/mol-script/symbols';
  10. import { formatMolScript } from '../mol-script/language/expression-formatter';
  11. import { StructureQualityReport, StructureQualityReportProvider } from '../extensions/pdbe/structure-quality-report/prop';
  12. import fetch from 'node-fetch';
  13. import { CustomPropertyDescriptor } from '../mol-model/custom-property';
  14. // import Examples from 'mol-script/script/mol-script/examples'
  15. // import { parseMolScript } from 'mol-script/script/mol-script/parser'
  16. // //import { compileAST } from 'mol-script/script/mol-script/compile';
  17. // for (const e of Examples) {
  18. // const expr = parseMolScript(e.value)[0];
  19. // console.log(e.name, util.inspect(expr, true, 10, true));
  20. // }
  21. // const exprs = parseMolScript(`(sel.atom.atom-groups
  22. // :residue-test (= atom.auth_comp_id ALA)
  23. // ;; ho ho ho
  24. // :atom-test (set.has { _C _N } atom.el)) ; comm
  25. // ;; this is a comment
  26. // ((hi) (ho))`);
  27. // ;; :residue-test (= atom.label_comp_id REA)
  28. const exprs = parseMolScript(`(sel.atom.atom-groups
  29. :residue-test (> pdbe.structure-quality.issue-count 0)
  30. :atom-test (= atom.el _C))`);
  31. const tsp = transpileMolScript(exprs[0]);
  32. // console.log(util.inspect(exprs, true, 10, true));
  33. console.log(util.inspect(tsp, true, 10, true));
  34. console.log(formatMolScript);
  35. console.log(formatMolScript(tsp));
  36. // //console.log(expr);
  37. const expr = MolScriptBuilder.core.math.add([1, 2, 3]);
  38. const compiled = compile<number>(expr);
  39. const result = compiled(new QueryContext(Structure.Empty));
  40. console.log(result);
  41. const CustomProp = CustomPropertyDescriptor({
  42. name: 'test_prop',
  43. cifExport: { prefix: '', categories: [ ]},
  44. symbols: {
  45. residueIndex: QuerySymbolRuntime.Dynamic(CustomPropSymbol('custom.test-prop', 'residue-index', Type.Num), ctx => {
  46. const e = ctx.element;
  47. // console.log(e.element, e.unit.model.atomicHierarchy.residueAtomSegments.index[e.element])
  48. return e.unit.model.atomicHierarchy.residueAtomSegments.index[e.element];
  49. })
  50. }
  51. });
  52. DefaultQueryRuntimeTable.addCustomProp(CustomProp);
  53. DefaultQueryRuntimeTable.addCustomProp(StructureQualityReportProvider.descriptor);
  54. export async function testQ() {
  55. const frame = await readCifFile('e:/test/quick/1cbs_updated.cif');
  56. const { structure } = await getModelsAndStructure(frame);
  57. const model = structure.models[0];
  58. const rawData = await fetch(`https://www.ebi.ac.uk/pdbe/api/validation/residuewise_outlier_summary/entry/${model.entryId.toLowerCase()}`, { timeout: 1500 });
  59. const data = StructureQualityReport.fromJson(model, await rawData.json());
  60. StructureQualityReportProvider.set(model, { serverUrl: '' }, data);
  61. let expr = MolScriptBuilder.struct.generator.atomGroups({
  62. 'atom-test': MolScriptBuilder.core.rel.eq([
  63. MolScriptBuilder.struct.atomProperty.core.elementSymbol(),
  64. MolScriptBuilder.es('C')
  65. ]),
  66. // 'residue-test': MolScriptBuilder.core.rel.eq([
  67. // MolScriptBuilder.struct.atomProperty.macromolecular.label_comp_id(),
  68. // 'REA'
  69. // ])
  70. 'residue-test': MolScriptBuilder.core.rel.inRange([CustomProp.symbols.residueIndex.symbol(), 1, 5])
  71. });
  72. expr = tsp;
  73. const compiled = compile<StructureQuery>(expr);
  74. const result = compiled(new QueryContext(structure));
  75. console.log(result);
  76. }
  77. testQ();