UNPKG

molstar

Version:

A comprehensive macromolecular library.

129 lines (128 loc) 7.04 kB
"use strict"; /** * Copyright (c) 2021 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.exportHierarchy = void 0; var tslib_1 = require("tslib"); var utf8_1 = require("../../mol-io/common/utf8"); var structure_1 = require("../../mol-model/structure"); var mol_task_1 = require("../../mol-task"); var date_1 = require("../../mol-util/date"); var download_1 = require("../../mol-util/download"); var zip_1 = require("../../mol-util/zip/zip"); function exportHierarchy(plugin, options) { return tslib_1.__awaiter(this, void 0, void 0, function () { var e_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, plugin.runTask(_exportHierarchy(plugin, options), { useOverlay: true })]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: e_1 = _a.sent(); console.error(e_1); plugin.log.error("Model export failed. See console for details."); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); } exports.exportHierarchy = exportHierarchy; function _exportHierarchy(plugin, options) { var _this = this; return mol_task_1.Task.create('Export', function (ctx) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var format, structures, files, entryMap, _i, structures_1, _s, s, name_1, zipData, _a, files_1, _b, fn, data, bytes, buffer; var _c, _d, _e, _f, _g, _h, _j, _k; return tslib_1.__generator(this, function (_l) { switch (_l.label) { case 0: return [4 /*yield*/, ctx.update({ message: 'Exporting...', isIndeterminate: true, canAbort: false })]; case 1: _l.sent(); format = (_c = options === null || options === void 0 ? void 0 : options.format) !== null && _c !== void 0 ? _c : 'cif'; structures = plugin.managers.structure.hierarchy.current.structures; files = []; entryMap = new Map(); _i = 0, structures_1 = structures; _l.label = 2; case 2: if (!(_i < structures_1.length)) return [3 /*break*/, 7]; _s = structures_1[_i]; s = (_f = (_e = (_d = _s.transform) === null || _d === void 0 ? void 0 : _d.cell.obj) === null || _e === void 0 ? void 0 : _e.data) !== null && _f !== void 0 ? _f : (_g = _s.cell.obj) === null || _g === void 0 ? void 0 : _g.data; if (!s) return [3 /*break*/, 6]; if (s.models.length > 1) { plugin.log.warn("[Export] Skipping ".concat((_h = _s.cell.obj) === null || _h === void 0 ? void 0 : _h.label, ": Multimodel exports not supported.")); return [3 /*break*/, 6]; } if (s.units.some(function (u) { return !structure_1.Unit.isAtomic(u); })) { plugin.log.warn("[Export] Skipping ".concat((_j = _s.cell.obj) === null || _j === void 0 ? void 0 : _j.label, ": Non-atomic model exports not supported.")); return [3 /*break*/, 6]; } name_1 = entryMap.has(s.model.entryId) ? "".concat(s.model.entryId, "_").concat(entryMap.get(s.model.entryId) + 1, ".").concat(format) : "".concat(s.model.entryId, ".").concat(format); entryMap.set(s.model.entryId, ((_k = entryMap.get(s.model.entryId)) !== null && _k !== void 0 ? _k : 0) + 1); return [4 /*yield*/, ctx.update({ message: "Exporting ".concat(s.model.entryId, "..."), isIndeterminate: true, canAbort: false })]; case 3: _l.sent(); if (!(s.elementCount > 100000)) return [3 /*break*/, 5]; // Give UI chance to update, only needed for larger structures. return [4 /*yield*/, new Promise(function (res) { return setTimeout(res, 50); })]; case 4: // Give UI chance to update, only needed for larger structures. _l.sent(); _l.label = 5; case 5: try { files.push([name_1, (0, structure_1.to_mmCIF)(s.model.entryId, s, format === 'bcif', { copyAllCategories: true })]); } catch (e) { if (format === 'cif' && s.elementCount > 2000000) { plugin.log.warn("[Export] The structure might be too big to be exported as Text CIF, consider using the BinaryCIF format instead."); } throw e; } _l.label = 6; case 6: _i++; return [3 /*break*/, 2]; case 7: if (!(files.length === 1)) return [3 /*break*/, 8]; (0, download_1.download)(new Blob([files[0][1]]), files[0][0]); return [3 /*break*/, 11]; case 8: if (!(files.length > 1)) return [3 /*break*/, 11]; zipData = {}; for (_a = 0, files_1 = files; _a < files_1.length; _a++) { _b = files_1[_a], fn = _b[0], data = _b[1]; if (data instanceof Uint8Array) { zipData[fn] = data; } else { bytes = new Uint8Array((0, utf8_1.utf8ByteCount)(data)); (0, utf8_1.utf8Write)(bytes, 0, data); zipData[fn] = bytes; } } return [4 /*yield*/, ctx.update({ message: "Compressing Data...", isIndeterminate: true, canAbort: false })]; case 9: _l.sent(); return [4 /*yield*/, (0, zip_1.zip)(ctx, zipData)]; case 10: buffer = _l.sent(); (0, download_1.download)(new Blob([new Uint8Array(buffer, 0, buffer.byteLength)]), "structures_".concat((0, date_1.getFormattedTime)(), ".zip")); _l.label = 11; case 11: plugin.log.info("[Export] Done."); return [2 /*return*/]; } }); }); }); }