UNPKG

molstar

Version:

A comprehensive macromolecular library.

104 lines (103 loc) 4.82 kB
#!/usr/bin/env node "use strict"; /** * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var argparse = tslib_1.__importStar(require("argparse")); var fs = tslib_1.__importStar(require("fs")); var path = tslib_1.__importStar(require("path")); var node_fetch_1 = tslib_1.__importDefault(require("node-fetch")); var generic_1 = require("../../mol-data/generic"); var LIPIDS_DIR = path.resolve(__dirname, '../../../../build/lipids/'); var MARTINI_LIPIDS_PATH = path.resolve(LIPIDS_DIR, 'martini_lipids.itp'); var MARTINI_LIPIDS_URL = 'http://www.cgmartini.nl/images/parameters/lipids/Collections/martini_v2.0_lipids_all_201506.itp'; function ensureAvailable(path, url) { return tslib_1.__awaiter(this, void 0, void 0, function () { var name_1, data, _a, _b, _c; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: if (!(FORCE_DOWNLOAD || !fs.existsSync(path))) return [3 /*break*/, 3]; name_1 = url.substr(url.lastIndexOf('/') + 1); console.log("downloading ".concat(name_1, "...")); return [4 /*yield*/, (0, node_fetch_1.default)(url)]; case 1: data = _d.sent(); if (!fs.existsSync(LIPIDS_DIR)) { fs.mkdirSync(LIPIDS_DIR); } _b = (_a = fs).writeFileSync; _c = [path]; return [4 /*yield*/, data.text()]; case 2: _b.apply(_a, _c.concat([_d.sent()])); console.log("done downloading ".concat(name_1)); _d.label = 3; case 3: return [2 /*return*/]; } }); }); } function ensureLipidsAvailable() { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ensureAvailable(MARTINI_LIPIDS_PATH, MARTINI_LIPIDS_URL)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); } var extraLipids = ['DMPC']; function run(out) { return tslib_1.__awaiter(this, void 0, void 0, function () { var lipidsItpStr, lipids, reLipid, m, v, _i, extraLipids_1, v, lipidNames, output; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ensureLipidsAvailable()]; case 1: _a.sent(); lipidsItpStr = fs.readFileSync(MARTINI_LIPIDS_PATH, 'utf8'); lipids = generic_1.UniqueArray.create(); reLipid = /\[moleculetype\]\n; molname nrexcl\n +([a-zA-Z]{3,5})/g; while ((m = reLipid.exec(lipidsItpStr)) !== null) { v = m[0].substr(m[0].lastIndexOf(' ') + 1); generic_1.UniqueArray.add(lipids, v, v); } for (_i = 0, extraLipids_1 = extraLipids; _i < extraLipids_1.length; _i++) { v = extraLipids_1[_i]; generic_1.UniqueArray.add(lipids, v, v); } lipidNames = JSON.stringify(lipids.array); if (out) { output = "/**\n * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.\n *\n * Code-generated lipid params file. Names extracted from Martini FF lipids itp.\n *\n * @author molstar/lipid-params cli\n */\n\nexport const LipidNames = new Set(".concat(lipidNames.replace(/"/g, "'").replace(/,/g, ', '), ");\n"); fs.writeFileSync(out, output); } else { console.log(lipidNames); } return [2 /*return*/]; } }); }); } var parser = new argparse.ArgumentParser({ add_help: true, description: 'Create lipid params (from martini lipids itp)' }); parser.add_argument('--out', '-o', { help: 'Generated lipid params output path, if not given printed to stdout' }); parser.add_argument('--forceDownload', '-f', { action: 'store_true', help: 'Force download of martini lipids itp' }); var args = parser.parse_args(); var FORCE_DOWNLOAD = args.forceDownload; run(args.out || '').catch(function (e) { console.error(e); });