UNPKG

molstar

Version:

A comprehensive macromolecular library.

79 lines (78 loc) 3.27 kB
"use strict"; /** * 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.getOutputFilename = getOutputFilename; exports.getExtendedHeaderJson = getExtendedHeaderJson; exports.queryBox = queryBox; const tslib_1 = require("tslib"); const execute_1 = require("./query/execute"); const console_logger_1 = require("../../../mol-util/console-logger"); const DataFormat = tslib_1.__importStar(require("../common/data-format")); const config_1 = require("../config"); const file_handle_1 = require("../../common/file-handle"); function getOutputFilename(source, id, { asBinary, box, detail, forcedSamplingLevel }) { function n(s) { return (s || '').replace(/[ \n\t]/g, '').toLowerCase(); } function r(v) { return Math.round(10 * v) / 10; } const det = forcedSamplingLevel !== void 0 ? `l${forcedSamplingLevel}` : `d${Math.min(Math.max(0, detail | 0), config_1.LimitsConfig.maxOutputSizeInVoxelCountByPrecisionLevel.length - 1)}`; const boxInfo = box.kind === 'Cell' ? 'cell' : `${box.kind === 'Cartesian' ? 'cartn' : 'frac'}_${r(box.a[0])}_${r(box.a[1])}_${r(box.a[2])}_${r(box.b[0])}_${r(box.b[1])}_${r(box.b[2])}`; return `${n(source)}_${n(id)}-${boxInfo}_${det}.${asBinary ? 'bcif' : 'cif'}`; } /** Reads the header and includes information about available detail levels */ async function getExtendedHeaderJson(filename, sourceId) { console_logger_1.ConsoleLogger.log('Header', sourceId); try { if (!filename) { console_logger_1.ConsoleLogger.error(`Header ${sourceId}`, 'Empty filename.'); return void 0; } const header = { ...await readHeader(filename, sourceId) }; const { sampleCount } = header.sampling[0]; const maxVoxelCount = sampleCount[0] * sampleCount[1] * sampleCount[2]; const precisions = config_1.LimitsConfig.maxOutputSizeInVoxelCountByPrecisionLevel .map((maxVoxels, precision) => ({ precision, maxVoxels })); const availablePrecisions = []; for (const p of precisions) { availablePrecisions.push(p); if (p.maxVoxels > maxVoxelCount) break; } header.availablePrecisions = availablePrecisions; header.isAvailable = true; return JSON.stringify(header, null, 2); } catch (e) { console_logger_1.ConsoleLogger.error(`Header ${sourceId}`, e); return void 0; } } async function queryBox(params, outputProvider) { return await (0, execute_1.execute)(params, outputProvider); } async function readHeader(filename, sourceId) { let file; try { if (!filename) return void 0; file = await (0, file_handle_1.fileHandleFromPathOrUrl)(filename, filename); const header = await DataFormat.readHeader(file); return header.header; } catch (e) { console_logger_1.ConsoleLogger.error(`Info ${sourceId}`, e); return void 0; } finally { if (file) file.close(); } }