UNPKG

drugflow-molstar

Version:
153 lines (152 loc) 8.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.superpositionExportHierarchy = void 0; var tslib_1 = require("tslib"); var utf8_1 = require("Molstar/mol-io/common/utf8"); var structure_1 = require("Molstar/mol-model/structure"); var mol_task_1 = require("Molstar/mol-task"); var date_1 = require("Molstar/mol-util/date"); var download_1 = require("Molstar/mol-util/download"); var zip_1 = require("Molstar/mol-util/zip/zip"); var commands_1 = require("Molstar/mol-plugin/commands"); function superpositionExportHierarchy(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(_superpositionExportHierarchy(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.superpositionExportHierarchy = superpositionExportHierarchy; function _superpositionExportHierarchy(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, customState, superpositionState, segmentIndex, files, entryMap, structures, _i, structures_1, molId, modelRef, isStrHidden, _s, _a, _b, strComp, s, name_1, zipData, _c, files_1, _d, fn, data, bytes, buffer; var _e, _f, _g, _h, _j, _k, _l, _m; return tslib_1.__generator(this, function (_o) { switch (_o.label) { case 0: return [4 /*yield*/, ctx.update({ message: 'Exporting...', isIndeterminate: true, canAbort: false })]; case 1: _o.sent(); format = (_e = options === null || options === void 0 ? void 0 : options.format) !== null && _e !== void 0 ? _e : 'cif'; customState = plugin.customState; superpositionState = customState.superpositionState; segmentIndex = superpositionState.activeSegment - 1; files = []; entryMap = new Map(); structures = superpositionState.loadedStructs[segmentIndex].slice(); if (superpositionState.alphafold.ref) structures.push("AF-".concat(customState.initParams.moleculeId)); _i = 0, structures_1 = structures; _o.label = 2; case 2: if (!(_i < structures_1.length)) return [3 /*break*/, 7]; molId = structures_1[_i]; modelRef = superpositionState.models[molId]; if (!modelRef) return [3 /*break*/, 6]; isStrHidden = false; _s = plugin.managers.structure.hierarchy.current.refs.get(modelRef); if (_s.cell.state.isHidden) isStrHidden = true; for (_a = 0, _b = _s.components; _a < _b.length; _a++) { strComp = _b[_a]; if (strComp.cell.state.isHidden) isStrHidden = true; } if (isStrHidden) return [3 /*break*/, 6]; s = (_h = (_g = (_f = _s.transform) === null || _f === void 0 ? void 0 : _f.cell.obj) === null || _g === void 0 ? void 0 : _g.data) !== null && _h !== void 0 ? _h : (_j = _s.cell.obj) === null || _j === void 0 ? void 0 : _j.data; if (!s) return [3 /*break*/, 6]; if (s.models.length > 1) { plugin.log.warn("[Export] Skipping ".concat((_k = _s.cell.obj) === null || _k === void 0 ? void 0 : _k.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((_l = _s.cell.obj) === null || _l === void 0 ? void 0 : _l.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, ((_m = entryMap.get(s.model.entryId)) !== null && _m !== void 0 ? _m : 0) + 1); return [4 /*yield*/, ctx.update({ message: "Exporting ".concat(s.model.entryId, "..."), isIndeterminate: true, canAbort: false })]; case 3: _o.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. _o.sent(); _o.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; } _o.label = 6; case 6: _i++; return [3 /*break*/, 2]; case 7: if (files.length === 0) { commands_1.PluginCommands.Toast.Show(plugin, { title: 'Export Models', message: 'No visible structure in the 3D view to export!', key: 'superposition-toast-1', timeoutMs: 7000 }); return [2 /*return*/]; } 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 (_c = 0, files_1 = files; _c < files_1.length; _c++) { _d = files_1[_c], fn = _d[0], data = _d[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: _o.sent(); return [4 /*yield*/, (0, zip_1.zip)(ctx, zipData)]; case 10: buffer = _o.sent(); (0, download_1.download)(new Blob([new Uint8Array(buffer, 0, buffer.byteLength)]), "structures_".concat((0, date_1.getFormattedTime)(), ".zip")); _o.label = 11; case 11: plugin.log.info("[Export] Done."); return [2 /*return*/]; } }); }); }); }