create-ions.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env node
  2. /**
  3. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  4. *
  5. * @author Josh McMenemy <josh.mcmenemy@gmail.com>
  6. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  7. */
  8. import * as argparse from 'argparse';
  9. import * as path from 'path';
  10. import util from 'util';
  11. import fs from 'fs';
  12. require('util.promisify').shim();
  13. const writeFile = util.promisify(fs.writeFile);
  14. import { DatabaseCollection } from '../../mol-data/db';
  15. import { CCD_Schema } from '../../mol-io/reader/cif/schema/ccd';
  16. import { DefaultDataOptions, ensureDataAvailable, readCCD } from './util';
  17. function extractIonNames(ccd: DatabaseCollection<CCD_Schema>) {
  18. const ionNames: string[] = [];
  19. for (const k in ccd) {
  20. const { chem_comp } = ccd[k];
  21. if (chem_comp.name.value(0).toUpperCase().includes(' ION')) {
  22. ionNames.push(chem_comp.id.value(0));
  23. }
  24. }
  25. // these are extra ions that don't have ION in their name
  26. ionNames.push('NCO', 'OHX');
  27. return ionNames;
  28. }
  29. function writeIonNamesFile(filePath: string, ionNames: string[]) {
  30. const output = `/**
  31. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  32. *
  33. * Code-generated ion names params file. Names extracted from CCD components.
  34. *
  35. * @author molstar/chem-comp-dict/create-ions cli
  36. */
  37. export const IonNames = new Set(${JSON.stringify(ionNames).replace(/"/g, "'").replace(/,/g, ', ')});
  38. `;
  39. writeFile(filePath, output);
  40. }
  41. async function run(out: string, options = DefaultDataOptions) {
  42. await ensureDataAvailable(options);
  43. const ccd = await readCCD();
  44. const ionNames = extractIonNames(ccd);
  45. if (!fs.existsSync(path.dirname(out))) {
  46. fs.mkdirSync(path.dirname(out));
  47. }
  48. writeIonNamesFile(out, ionNames);
  49. }
  50. const parser = new argparse.ArgumentParser({
  51. add_help: true,
  52. description: 'Extract and save IonNames from CCD.'
  53. });
  54. parser.add_argument('out', {
  55. help: 'Generated file output path.'
  56. });
  57. parser.add_argument('--forceDownload', '-f', {
  58. action: 'store_true',
  59. help: 'Force download of CCD and PVCD.'
  60. });
  61. parser.add_argument('--ccdUrl', '-c', {
  62. help: 'Fetch the CCD from a custom URL. This forces download of the CCD.',
  63. required: false
  64. });
  65. interface Args {
  66. out: string,
  67. forceDownload?: boolean,
  68. ccdUrl?: string
  69. }
  70. const args: Args = parser.parse_args();
  71. run(args.out, { forceDownload: args.forceDownload, ccdUrl: args.ccdUrl });