UNPKG

pdbe-molstar

Version:
119 lines (118 loc) 6.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.superpositionExportHierarchy = superpositionExportHierarchy; const tslib_1 = require("tslib"); const utf8_1 = require("molstar/lib/mol-io/common/utf8"); const structure_1 = require("molstar/lib/mol-model/structure"); const commands_1 = require("molstar/lib/mol-plugin/commands"); const mol_task_1 = require("molstar/lib/mol-task"); const date_1 = require("molstar/lib/mol-util/date"); const download_1 = require("molstar/lib/mol-util/download"); const zip_1 = require("molstar/lib/mol-util/zip/zip"); const plugin_custom_state_1 = require("./plugin-custom-state"); function superpositionExportHierarchy(plugin, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield plugin.runTask(_superpositionExportHierarchy(plugin, options), { useOverlay: true }); } catch (e) { console.error(e); plugin.log.error(`Model export failed. See console for details.`); } }); } function _superpositionExportHierarchy(plugin, options) { return mol_task_1.Task.create('Export', (ctx) => tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h; yield ctx.update({ message: 'Exporting...', isIndeterminate: true, canAbort: false }); const format = (_a = options === null || options === void 0 ? void 0 : options.format) !== null && _a !== void 0 ? _a : 'cif'; // const { structures } = plugin.managers.structure.hierarchy.current; const customState = (0, plugin_custom_state_1.PluginCustomState)(plugin); if (!customState.initParams) throw new Error('customState.initParams has not been initialized'); if (!customState.superpositionState) throw new Error('customState.superpositionState has not been initialized'); const superpositionState = customState.superpositionState; const segmentIndex = superpositionState.activeSegment - 1; const files = []; const entryMap = new Map(); const structures = superpositionState.loadedStructs[segmentIndex].slice(); if (!customState.initParams.moleculeId) throw new Error('initParams.moleculeId is not defined'); if (superpositionState.alphafold.ref) structures.push(`AF-${customState.initParams.moleculeId}`); for (const molId of structures) { const modelRef = superpositionState.models[molId]; if (!modelRef) continue; let isStrHidden = false; const _s = plugin.managers.structure.hierarchy.current.refs.get(modelRef); if (_s.cell.state.isHidden) isStrHidden = true; for (const strComp of _s.components) { if (strComp.cell.state.isHidden) isStrHidden = true; } if (isStrHidden) continue; const s = (_d = (_c = (_b = _s.transform) === null || _b === void 0 ? void 0 : _b.cell.obj) === null || _c === void 0 ? void 0 : _c.data) !== null && _d !== void 0 ? _d : (_e = _s.cell.obj) === null || _e === void 0 ? void 0 : _e.data; if (!s) continue; if (s.models.length > 1) { plugin.log.warn(`[Export] Skipping ${(_f = _s.cell.obj) === null || _f === void 0 ? void 0 : _f.label}: Multimodel exports not supported.`); continue; } if (s.units.some((u) => !structure_1.Unit.isAtomic(u))) { plugin.log.warn(`[Export] Skipping ${(_g = _s.cell.obj) === null || _g === void 0 ? void 0 : _g.label}: Non-atomic model exports not supported.`); continue; } const name = entryMap.has(s.model.entryId) ? `${s.model.entryId}_${entryMap.get(s.model.entryId) + 1}.${format}` : `${s.model.entryId}.${format}`; entryMap.set(s.model.entryId, ((_h = entryMap.get(s.model.entryId)) !== null && _h !== void 0 ? _h : 0) + 1); yield ctx.update({ message: `Exporting ${s.model.entryId}...`, isIndeterminate: true, canAbort: false }); if (s.elementCount > 100000) { // Give UI chance to update, only needed for larger structures. yield new Promise(res => setTimeout(res, 50)); } try { files.push([name, (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; } } 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; } if (files.length === 1) { (0, download_1.download)(new Blob([files[0][1]]), files[0][0]); } else if (files.length > 1) { const zipData = {}; for (const [fn, data] of files) { if (data instanceof Uint8Array) { zipData[fn] = data; } else { const bytes = new Uint8Array((0, utf8_1.utf8ByteCount)(data)); (0, utf8_1.utf8Write)(bytes, 0, data); zipData[fn] = bytes; } } yield ctx.update({ message: `Compressing Data...`, isIndeterminate: true, canAbort: false }); const buffer = yield (0, zip_1.zip)(ctx, zipData); (0, download_1.download)(new Blob([new Uint8Array(buffer, 0, buffer.byteLength)]), `structures_${(0, date_1.getFormattedTime)()}.zip`); } plugin.log.info(`[Export] Done.`); })); }