create-ions.ts 2.2 KB

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