index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Adam Midlik <midlik@gmail.com>
  5. *
  6. * Example command-line application generating images of PDB structures
  7. * Build: npm install --no-save gl jpeg-js pngjs // these packages are not listed in dependencies for performance reasons
  8. * npm run build
  9. * Run: node lib/commonjs/examples/image-renderer 1cbs ../outputs_1cbs/
  10. */
  11. import { ArgumentParser } from 'argparse';
  12. import fs from 'fs';
  13. import path from 'path';
  14. import { Download, ParseCif } from '../../mol-plugin-state/transforms/data';
  15. import { ModelFromTrajectory, StructureComponent, StructureFromModel, TrajectoryFromMmCif } from '../../mol-plugin-state/transforms/model';
  16. import { StructureRepresentation3D } from '../../mol-plugin-state/transforms/representation';
  17. import { HeadlessPluginContext } from '../../mol-plugin/headless-plugin-context';
  18. import { DefaultPluginSpec } from '../../mol-plugin/spec';
  19. import { STYLIZED_POSTPROCESSING } from '../../mol-plugin/util/headless-screenshot';
  20. interface Args {
  21. pdbId: string,
  22. outDirectory: string
  23. }
  24. function parseArguments(): Args {
  25. const parser = new ArgumentParser({ description: 'Example command-line application generating images of PDB structures' });
  26. parser.add_argument('pdbId', { help: 'PDB identifier' });
  27. parser.add_argument('outDirectory', { help: 'Directory for outputs' });
  28. const args = parser.parse_args();
  29. return { ...args };
  30. }
  31. async function main() {
  32. const args = parseArguments();
  33. const url = `https://www.ebi.ac.uk/pdbe/entry-files/download/${args.pdbId}.bcif`;
  34. console.log('PDB ID:', args.pdbId);
  35. console.log('Source URL:', url);
  36. console.log('Outputs:', args.outDirectory);
  37. // Create a headless plugin
  38. const plugin = new HeadlessPluginContext(DefaultPluginSpec(), { width: 800, height: 800 });
  39. await plugin.init();
  40. // Download and visualize data in the plugin
  41. const update = plugin.build();
  42. const structure = update.toRoot()
  43. .apply(Download, { url, isBinary: true })
  44. .apply(ParseCif)
  45. .apply(TrajectoryFromMmCif)
  46. .apply(ModelFromTrajectory)
  47. .apply(StructureFromModel);
  48. const polymer = structure.apply(StructureComponent, { type: { name: 'static', params: 'polymer' } });
  49. const ligand = structure.apply(StructureComponent, { type: { name: 'static', params: 'ligand' } });
  50. polymer.apply(StructureRepresentation3D, {
  51. type: { name: 'cartoon', params: { alpha: 1 } },
  52. colorTheme: { name: 'sequence-id', params: {} },
  53. });
  54. ligand.apply(StructureRepresentation3D, {
  55. type: { name: 'ball-and-stick', params: { sizeFactor: 1 } },
  56. colorTheme: { name: 'element-symbol', params: { carbonColor: { name: 'element-symbol', params: {} } } },
  57. sizeTheme: { name: 'physical', params: {} },
  58. });
  59. await update.commit();
  60. // Export images
  61. fs.mkdirSync(args.outDirectory, { recursive: true });
  62. await plugin.saveImage(path.join(args.outDirectory, 'basic.png'));
  63. await plugin.saveImage(path.join(args.outDirectory, 'basic.jpg'));
  64. await plugin.saveImage(path.join(args.outDirectory, 'large.png'), { width: 1600, height: 1200 });
  65. await plugin.saveImage(path.join(args.outDirectory, 'large.jpg'), { width: 1600, height: 1200 });
  66. await plugin.saveImage(path.join(args.outDirectory, 'stylized.png'), undefined, STYLIZED_POSTPROCESSING);
  67. await plugin.saveImage(path.join(args.outDirectory, 'stylized.jpg'), undefined, STYLIZED_POSTPROCESSING);
  68. await plugin.saveImage(path.join(args.outDirectory, 'stylized-compressed-jpg.jpg'), undefined, STYLIZED_POSTPROCESSING, undefined, 10);
  69. // Export state loadable in Mol* Viewer
  70. await plugin.saveStateSnapshot(path.join(args.outDirectory, 'molstar-state.molj'));
  71. // Cleanup
  72. await plugin.clear();
  73. plugin.dispose();
  74. }
  75. main();