index.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env node
  2. /**
  3. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  4. *
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import * as argparse from 'argparse';
  8. import * as fs from 'fs';
  9. import * as path from 'path';
  10. import fetch from 'node-fetch';
  11. import { UniqueArray } from '../../mol-data/generic';
  12. const LIPIDS_DIR = path.resolve(__dirname, '../../../../build/lipids/');
  13. const MARTINI_LIPIDS_PATH = path.resolve(LIPIDS_DIR, 'martini_lipids.itp');
  14. const MARTINI_LIPIDS_URL = 'http://www.cgmartini.nl/images/parameters/lipids/Collections/martini_v2.0_lipids_all_201506.itp';
  15. async function ensureAvailable(path: string, url: string) {
  16. if (FORCE_DOWNLOAD || !fs.existsSync(path)) {
  17. const name = url.substr(url.lastIndexOf('/') + 1);
  18. console.log(`downloading ${name}...`);
  19. const data = await fetch(url);
  20. if (!fs.existsSync(LIPIDS_DIR)) {
  21. fs.mkdirSync(LIPIDS_DIR);
  22. }
  23. fs.writeFileSync(path, await data.text());
  24. console.log(`done downloading ${name}`);
  25. }
  26. }
  27. async function ensureLipidsAvailable() { await ensureAvailable(MARTINI_LIPIDS_PATH, MARTINI_LIPIDS_URL); }
  28. async function run(out: string) {
  29. await ensureLipidsAvailable();
  30. const lipidsItpStr = fs.readFileSync(MARTINI_LIPIDS_PATH, 'utf8');
  31. const lipids = UniqueArray.create<string>();
  32. const reLipid = /\[moleculetype\]\n; molname nrexcl\n +([a-zA-Z]{3,5})/g;
  33. let m: RegExpExecArray | null;
  34. while ((m = reLipid.exec(lipidsItpStr)) !== null) {
  35. const v = m[0].substr(m[0].lastIndexOf(' ') + 1);
  36. UniqueArray.add(lipids, v, v);
  37. }
  38. const lipidNames = JSON.stringify(lipids.array);
  39. if (out) {
  40. const output = `/**
  41. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  42. *
  43. * Code-generated lipid params file. Names extracted from Martini FF lipids itp.
  44. *
  45. * @author molstar/lipid-params cli
  46. */
  47. export const LipidNames = new Set(${lipidNames.replace(/"/g, "'").replace(/,/g, ', ')});
  48. `;
  49. fs.writeFileSync(out, output);
  50. } else {
  51. console.log(lipidNames);
  52. }
  53. }
  54. const parser = new argparse.ArgumentParser({
  55. addHelp: true,
  56. description: 'Create lipid params (from martini lipids itp)'
  57. });
  58. parser.addArgument([ '--out', '-o' ], {
  59. help: 'Generated lipid params output path, if not given printed to stdout'
  60. });
  61. parser.addArgument([ '--forceDownload', '-f' ], {
  62. action: 'storeTrue',
  63. help: 'Force download of martini lipids itp'
  64. });
  65. interface Args {
  66. out: string
  67. forceDownload: boolean
  68. }
  69. const args: Args = parser.parseArgs();
  70. const FORCE_DOWNLOAD = args.forceDownload;
  71. run(args.out || '').catch(e => {
  72. console.error(e);
  73. });