index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  7. *
  8. * Copyright (C) 2024, Protein Bioinformatics Research Group, HUN-REN RCNS
  9. *
  10. * Licensed under CC BY-NC 4.0, see LICENSE file for more info.
  11. *
  12. * @author Gabor Tusnady <tusnady.gabor@ttk.hu>
  13. * @author Csongor Gerdan <gerdan.csongor@ttk.hu>
  14. */
  15. import { ArgumentParser } from 'argparse';
  16. import fs from 'fs';
  17. import path from 'path';
  18. import gl from 'gl';
  19. import pngjs from 'pngjs';
  20. import jpegjs from 'jpeg-js';
  21. import { HeadlessPluginContext } from '../mol-plugin/headless-plugin-context';
  22. import { DefaultPluginSpec } from '../mol-plugin/spec';
  23. import { ExternalModules, defaultCanvas3DParams } from '../mol-plugin/util/headless-screenshot';
  24. import { setFSModule } from '../mol-util/data-source';
  25. import { MembraneOrientationPreset, loadWithUNITMPMembraneRepresentation } from '../tmdet-extension/behavior';
  26. import { TmDetColorThemeProvider } from '../tmdet-extension/tmdet-color-theme';
  27. import { TmDetLabelProvider } from '../tmdet-extension/labeling';
  28. import { ColorNames } from '../mol-util/color/names';
  29. const IMAGE_WIDTH = 800;
  30. const IMAGE_HEIGHT = 800;
  31. setFSModule(fs);
  32. interface Args {
  33. pdbId: string,
  34. side1: string,
  35. outDirectory: string,
  36. domain: string
  37. }
  38. function parseArguments(): Args {
  39. const parser = new ArgumentParser({ description: 'Command-line application generating PDBTM images of PDB structures' });
  40. parser.add_argument('pdbId', { help: 'PDB identifier' });
  41. parser.add_argument('--side1', { default: 'Inside', help: 'Inside|Outside|Periplasm (default: Inside)' });
  42. parser.add_argument('outDirectory', { help: 'Directory for outputs' });
  43. const domain = 'http://pdbtm.unitmp.botond.hu';
  44. parser.add_argument('--domain', { default: domain, help: `Domain of data source site (default: ${domain})` });
  45. const args = parser.parse_args();
  46. return { ...args };
  47. }
  48. async function main() {
  49. const args = parseArguments();
  50. const domain = args.domain;
  51. const structureUrl = `${domain}/api/v1/entry/${args.pdbId}.updated.cif`;
  52. const regionDescriptorUrl = `${domain}/api/v1/entry/${args.pdbId}.json`;
  53. const fileName = `${args.pdbId}.jpg`;
  54. console.log('PDB ID:', args.pdbId);
  55. console.log('Side1:', args.side1);
  56. console.log('Structure URL:', structureUrl);
  57. console.log('Region descriptor URL:', regionDescriptorUrl);
  58. console.log('Outputs:', args.outDirectory);
  59. // Create a headless plugin
  60. const externalModules: ExternalModules = { gl, pngjs, 'jpeg-js': jpegjs };
  61. const canvasParams = defaultCanvas3DParams();
  62. canvasParams.camera!.mode = "perspective";
  63. canvasParams.renderer!.backgroundColor = ColorNames.black;
  64. const plugin = new HeadlessPluginContext(
  65. externalModules,
  66. DefaultPluginSpec(),
  67. { width: IMAGE_WIDTH, height: IMAGE_HEIGHT },
  68. { canvas: canvasParams }
  69. );
  70. await plugin.init();
  71. //
  72. // TMDET extension registration section
  73. //
  74. let tree = plugin.state.behaviors.build();
  75. plugin.builders.structure.representation.registerPreset(MembraneOrientationPreset);
  76. plugin.representation.structure.themes.colorThemeRegistry.add(TmDetColorThemeProvider);
  77. plugin.managers.lociLabels.addProvider(TmDetLabelProvider);
  78. await plugin.runTask(plugin.state.behaviors.updateTree(tree, { doNotUpdateCurrent: true, doNotLogTiming: true }));
  79. await loadWithUNITMPMembraneRepresentation(plugin, {
  80. structureUrl: structureUrl,
  81. regionDescriptorUrl: regionDescriptorUrl,
  82. side1: args.side1
  83. });
  84. //
  85. // End of TMDET extension registration section
  86. //
  87. // Export images
  88. fs.mkdirSync(args.outDirectory, { recursive: true });
  89. setTimeout(async () => {
  90. plugin.saveImage(path.join(args.outDirectory, fileName), { width: 100, height: 100 });
  91. // Cleanup
  92. await plugin.clear();
  93. plugin.dispose();
  94. },
  95. 1000
  96. );
  97. }
  98. main();