drugflow-molstar
Version:
Molstar implementation for DrugFlow
185 lines (184 loc) • 10.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.superpositionExportHierarchy = superpositionExportHierarchy;
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*/];
}
});
});
}
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, structures_1, structures_1_1, molId, modelRef, isStrHidden, _s, _a, _b, strComp, s, name_1, e_2_1, zipData, files_1, files_1_1, _c, fn, data, bytes, buffer;
var e_2, _d, e_3, _e, e_4, _f;
var _g, _h, _j, _k, _l, _m, _o, _p;
return tslib_1.__generator(this, function (_q) {
switch (_q.label) {
case 0: return [4 /*yield*/, ctx.update({ message: 'Exporting...', isIndeterminate: true, canAbort: false })];
case 1:
_q.sent();
format = (_g = options === null || options === void 0 ? void 0 : options.format) !== null && _g !== void 0 ? _g : '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));
_q.label = 2;
case 2:
_q.trys.push([2, 9, 10, 11]);
structures_1 = tslib_1.__values(structures), structures_1_1 = structures_1.next();
_q.label = 3;
case 3:
if (!!structures_1_1.done) return [3 /*break*/, 8];
molId = structures_1_1.value;
modelRef = superpositionState.models[molId];
if (!modelRef)
return [3 /*break*/, 7];
isStrHidden = false;
_s = plugin.managers.structure.hierarchy.current.refs.get(modelRef);
if (_s.cell.state.isHidden)
isStrHidden = true;
try {
for (_a = (e_3 = void 0, tslib_1.__values(_s.components)), _b = _a.next(); !_b.done; _b = _a.next()) {
strComp = _b.value;
if (strComp.cell.state.isHidden)
isStrHidden = true;
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
}
finally { if (e_3) throw e_3.error; }
}
if (isStrHidden)
return [3 /*break*/, 7];
s = (_k = (_j = (_h = _s.transform) === null || _h === void 0 ? void 0 : _h.cell.obj) === null || _j === void 0 ? void 0 : _j.data) !== null && _k !== void 0 ? _k : (_l = _s.cell.obj) === null || _l === void 0 ? void 0 : _l.data;
if (!s)
return [3 /*break*/, 7];
if (s.models.length > 1) {
plugin.log.warn("[Export] Skipping ".concat((_m = _s.cell.obj) === null || _m === void 0 ? void 0 : _m.label, ": Multimodel exports not supported."));
return [3 /*break*/, 7];
}
if (s.units.some(function (u) { return !structure_1.Unit.isAtomic(u); })) {
plugin.log.warn("[Export] Skipping ".concat((_o = _s.cell.obj) === null || _o === void 0 ? void 0 : _o.label, ": Non-atomic model exports not supported."));
return [3 /*break*/, 7];
}
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, ((_p = entryMap.get(s.model.entryId)) !== null && _p !== void 0 ? _p : 0) + 1);
return [4 /*yield*/, ctx.update({ message: "Exporting ".concat(s.model.entryId, "..."), isIndeterminate: true, canAbort: false })];
case 4:
_q.sent();
if (!(s.elementCount > 100000)) return [3 /*break*/, 6];
// Give UI chance to update, only needed for larger structures.
return [4 /*yield*/, new Promise(function (res) { return setTimeout(res, 50); })];
case 5:
// Give UI chance to update, only needed for larger structures.
_q.sent();
_q.label = 6;
case 6:
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;
}
_q.label = 7;
case 7:
structures_1_1 = structures_1.next();
return [3 /*break*/, 3];
case 8: return [3 /*break*/, 11];
case 9:
e_2_1 = _q.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 11];
case 10:
try {
if (structures_1_1 && !structures_1_1.done && (_d = structures_1.return)) _d.call(structures_1);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 11:
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*/, 12];
(0, download_1.download)(new Blob([files[0][1]]), files[0][0]);
return [3 /*break*/, 15];
case 12:
if (!(files.length > 1)) return [3 /*break*/, 15];
zipData = {};
try {
for (files_1 = tslib_1.__values(files), files_1_1 = files_1.next(); !files_1_1.done; files_1_1 = files_1.next()) {
_c = tslib_1.__read(files_1_1.value, 2), fn = _c[0], data = _c[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;
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (files_1_1 && !files_1_1.done && (_f = files_1.return)) _f.call(files_1);
}
finally { if (e_4) throw e_4.error; }
}
return [4 /*yield*/, ctx.update({ message: "Compressing Data...", isIndeterminate: true, canAbort: false })];
case 13:
_q.sent();
return [4 /*yield*/, (0, zip_1.zip)(ctx, zipData)];
case 14:
buffer = _q.sent();
(0, download_1.download)(new Blob([new Uint8Array(buffer, 0, buffer.byteLength)]), "structures_".concat((0, date_1.getFormattedTime)(), ".zip"));
_q.label = 15;
case 15:
plugin.log.info("[Export] Done.");
return [2 /*return*/];
}
});
}); });
}