molstar
Version:
A comprehensive macromolecular library.
115 lines (114 loc) • 5.82 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* Taken/adapted from DensityServer (https://github.com/dsehnal/DensityServer)
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryBox = exports.getExtendedHeaderJson = exports.getOutputFilename = void 0;
var tslib_1 = require("tslib");
var File = tslib_1.__importStar(require("../common/file"));
var execute_1 = require("./query/execute");
var console_logger_1 = require("../../../mol-util/console-logger");
var DataFormat = tslib_1.__importStar(require("../common/data-format"));
var config_1 = require("../config");
var file_handle_1 = require("../../common/file-handle");
function getOutputFilename(source, id, _a) {
var asBinary = _a.asBinary, box = _a.box, detail = _a.detail, forcedSamplingLevel = _a.forcedSamplingLevel;
function n(s) { return (s || '').replace(/[ \n\t]/g, '').toLowerCase(); }
function r(v) { return Math.round(10 * v) / 10; }
var det = forcedSamplingLevel !== void 0
? "l".concat(forcedSamplingLevel)
: "d".concat(Math.min(Math.max(0, detail | 0), config_1.LimitsConfig.maxOutputSizeInVoxelCountByPrecisionLevel.length - 1));
var boxInfo = box.kind === 'Cell'
? 'cell'
: "".concat(box.kind === 'Cartesian' ? 'cartn' : 'frac', "_").concat(r(box.a[0]), "_").concat(r(box.a[1]), "_").concat(r(box.a[2]), "_").concat(r(box.b[0]), "_").concat(r(box.b[1]), "_").concat(r(box.b[2]));
return "".concat(n(source), "_").concat(n(id), "-").concat(boxInfo, "_").concat(det, ".").concat(asBinary ? 'bcif' : 'cif');
}
exports.getOutputFilename = getOutputFilename;
/** Reads the header and includes information about available detail levels */
function getExtendedHeaderJson(filename, sourceId) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var header, _a, sampleCount, maxVoxelCount, precisions, availablePrecisions, _i, precisions_1, p, e_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
console_logger_1.ConsoleLogger.log('Header', sourceId);
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
if (!filename || !File.exists(filename)) {
console_logger_1.ConsoleLogger.error("Header ".concat(sourceId), 'File not found.');
return [2 /*return*/, void 0];
}
_a = [{}];
return [4 /*yield*/, readHeader(filename, sourceId)];
case 2:
header = tslib_1.__assign.apply(void 0, _a.concat([_b.sent()]));
sampleCount = header.sampling[0].sampleCount;
maxVoxelCount = sampleCount[0] * sampleCount[1] * sampleCount[2];
precisions = config_1.LimitsConfig.maxOutputSizeInVoxelCountByPrecisionLevel
.map(function (maxVoxels, precision) { return ({ precision: precision, maxVoxels: maxVoxels }); });
availablePrecisions = [];
for (_i = 0, precisions_1 = precisions; _i < precisions_1.length; _i++) {
p = precisions_1[_i];
availablePrecisions.push(p);
if (p.maxVoxels > maxVoxelCount)
break;
}
header.availablePrecisions = availablePrecisions;
header.isAvailable = true;
return [2 /*return*/, JSON.stringify(header, null, 2)];
case 3:
e_1 = _b.sent();
console_logger_1.ConsoleLogger.error("Header ".concat(sourceId), e_1);
return [2 /*return*/, void 0];
case 4: return [2 /*return*/];
}
});
});
}
exports.getExtendedHeaderJson = getExtendedHeaderJson;
function queryBox(params, outputProvider) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, execute_1.execute)(params, outputProvider)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
}
exports.queryBox = queryBox;
function readHeader(filename, sourceId) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var file, _a, header, e_2;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 3, 4, 5]);
if (!filename)
return [2 /*return*/, void 0];
_a = file_handle_1.fileHandleFromDescriptor;
return [4 /*yield*/, File.openRead(filename)];
case 1:
file = _a.apply(void 0, [_b.sent(), filename]);
return [4 /*yield*/, DataFormat.readHeader(file)];
case 2:
header = _b.sent();
return [2 /*return*/, header.header];
case 3:
e_2 = _b.sent();
console_logger_1.ConsoleLogger.error("Info ".concat(sourceId), e_2);
return [2 /*return*/, void 0];
case 4:
if (file)
file.close();
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
}
});
});
}
;