UNPKG

molstar

Version:

A comprehensive macromolecular library.

92 lines (91 loc) 3.58 kB
"use strict"; /** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadCif = exports.openCif = void 0; var tslib_1 = require("tslib"); var util = tslib_1.__importStar(require("util")); var fs = tslib_1.__importStar(require("fs")); var node_fetch_1 = tslib_1.__importDefault(require("node-fetch")); require('util.promisify').shim(); var cif_1 = require("../../mol-io/reader/cif"); var mol_task_1 = require("../../mol-task"); var readFileAsync = util.promisify(fs.readFile); function readFile(path) { return tslib_1.__awaiter(this, void 0, void 0, function () { var input, data, i; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!path.match(/\.bcif$/)) return [3 /*break*/, 2]; return [4 /*yield*/, readFileAsync(path)]; case 1: input = _a.sent(); data = new Uint8Array(input.byteLength); for (i = 0; i < input.byteLength; i++) data[i] = input[i]; return [2 /*return*/, data]; case 2: return [2 /*return*/, readFileAsync(path, 'utf8')]; } }); }); } function parseCif(data) { return tslib_1.__awaiter(this, void 0, void 0, function () { var comp, parsed; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: comp = cif_1.CIF.parse(data); return [4 /*yield*/, comp.run(function (p) { return console.log(mol_task_1.Progress.format(p)); }, 250)]; case 1: parsed = _a.sent(); if (parsed.isError) throw parsed; return [2 /*return*/, parsed.result]; } }); }); } function openCif(path) { return tslib_1.__awaiter(this, void 0, void 0, function () { var data; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, readFile(path)]; case 1: data = _a.sent(); return [2 /*return*/, parseCif(data)]; } }); }); } exports.openCif = openCif; function downloadCif(url, isBinary) { return tslib_1.__awaiter(this, void 0, void 0, function () { var data, _a, _b, _c; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: return [4 /*yield*/, (0, node_fetch_1.default)(url)]; case 1: data = _d.sent(); _a = parseCif; if (!isBinary) return [3 /*break*/, 3]; _c = Uint8Array.bind; return [4 /*yield*/, data.arrayBuffer()]; case 2: _b = new (_c.apply(Uint8Array, [void 0, _d.sent()]))(); return [3 /*break*/, 5]; case 3: return [4 /*yield*/, data.text()]; case 4: _b = _d.sent(); _d.label = 5; case 5: return [2 /*return*/, _a.apply(void 0, [_b])]; } }); }); } exports.downloadCif = downloadCif;