molstar
Version:
A comprehensive macromolecular library.
187 lines • 8.54 kB
JavaScript
"use strict";
/**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
* @author Sebastian Bittrich <sebastian.bittrich@rcsb.org>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.convert = void 0;
var tslib_1 = require("tslib");
var cif_1 = require("../../mol-io/reader/cif");
var cif_2 = require("../../mol-io/writer/cif");
var util = (0, tslib_1.__importStar)(require("util"));
var fs = (0, tslib_1.__importStar)(require("fs"));
var zlib = (0, tslib_1.__importStar)(require("zlib"));
var mol_task_1 = require("../../mol-task");
var binary_cif_1 = require("../../mol-io/common/binary-cif");
var encoder_1 = require("../../mol-io/writer/cif/encoder");
function showProgress(p) {
process.stdout.write("\r" + new Array(80).join(' '));
process.stdout.write("\r" + mol_task_1.Progress.format(p));
}
var readFileAsync = util.promisify(fs.readFile);
var unzipAsync = util.promisify(zlib.unzip);
function readFile(ctx, filename) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var isGz, input, str, data, _a;
return (0, tslib_1.__generator)(this, function (_b) {
switch (_b.label) {
case 0:
isGz = /\.gz$/i.test(filename);
if (!filename.match(/\.bcif/)) return [3 /*break*/, 5];
return [4 /*yield*/, readFileAsync(filename)];
case 1:
input = _b.sent();
if (!isGz) return [3 /*break*/, 3];
return [4 /*yield*/, unzipAsync(input)];
case 2:
input = _b.sent();
_b.label = 3;
case 3: return [4 /*yield*/, cif_1.CIF.parseBinary(new Uint8Array(input)).runInContext(ctx)];
case 4: return [2 /*return*/, _b.sent()];
case 5:
str = void 0;
if (!isGz) return [3 /*break*/, 8];
_a = unzipAsync;
return [4 /*yield*/, readFileAsync(filename)];
case 6: return [4 /*yield*/, _a.apply(void 0, [_b.sent()])];
case 7:
data = _b.sent();
str = data.toString('utf8');
return [3 /*break*/, 10];
case 8: return [4 /*yield*/, readFileAsync(filename, 'utf8')];
case 9:
str = _b.sent();
_b.label = 10;
case 10: return [4 /*yield*/, cif_1.CIF.parseText(str).runInContext(ctx)];
case 11: return [2 /*return*/, _b.sent()];
}
});
});
}
function getCIF(ctx, filename) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var parsed;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, readFile(ctx, filename)];
case 1:
parsed = _a.sent();
if (parsed.isError) {
throw new Error(parsed.toString());
}
return [2 /*return*/, parsed.result];
}
});
});
}
function getCategoryInstanceProvider(cat, fields) {
return {
name: cat.name,
instance: function () { return cif_2.CifWriter.categoryInstance(fields, { data: cat, rowCount: cat.rowCount }); }
};
}
function classify(name, field) {
var type = (0, cif_1.getCifFieldType)(field);
if (type['@type'] === 'str') {
return { name: name, type: 0 /* Str */, value: field.str, valueKind: field.valueKind };
}
else if (type['@type'] === 'float') {
var encoder = (0, binary_cif_1.classifyFloatArray)(field.toFloatArray({ array: Float64Array }));
return cif_2.CifWriter.Field.float(name, field.float, { valueKind: field.valueKind, encoder: encoder, typedArray: Float64Array });
}
else {
var encoder = (0, binary_cif_1.classifyIntArray)(field.toIntArray({ array: Int32Array }));
return cif_2.CifWriter.Field.int(name, field.int, { valueKind: field.valueKind, encoder: encoder, typedArray: Int32Array });
}
}
function convert(path, asText, hints, filter) {
var _this = this;
if (asText === void 0) { asText = false; }
return mol_task_1.Task.create('BinaryCIF', function (ctx) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
var encodingProvider, cif, encoder, maxProgress, _i, _a, b, _b, _c, c, current, _d, _e, b, _f, _g, c, cat, fields, _h, _j, f, ret;
return (0, tslib_1.__generator)(this, function (_k) {
switch (_k.label) {
case 0:
encodingProvider = hints
? cif_2.CifWriter.createEncodingProviderFromJsonConfig(hints)
: { get: function (c, f) { return void 0; } };
return [4 /*yield*/, getCIF(ctx, path)];
case 1:
cif = _k.sent();
encoder = cif_2.CifWriter.createEncoder({
binary: !asText,
encoderName: 'mol*/ciftools cif2bcif',
binaryAutoClassifyEncoding: true,
binaryEncodingPovider: encodingProvider
});
if (filter) {
encoder.setFilter(encoder_1.Category.filterOf(filter));
}
maxProgress = 0;
for (_i = 0, _a = cif.blocks; _i < _a.length; _i++) {
b = _a[_i];
maxProgress += b.categoryNames.length;
for (_b = 0, _c = b.categoryNames; _b < _c.length; _b++) {
c = _c[_b];
maxProgress += b.categories[c].fieldNames.length;
}
}
current = 0;
_d = 0, _e = cif.blocks;
_k.label = 2;
case 2:
if (!(_d < _e.length)) return [3 /*break*/, 11];
b = _e[_d];
encoder.startDataBlock(b.header);
_f = 0, _g = b.categoryNames;
_k.label = 3;
case 3:
if (!(_f < _g.length)) return [3 /*break*/, 10];
c = _g[_f];
cat = b.categories[c];
fields = [];
_h = 0, _j = cat.fieldNames;
_k.label = 4;
case 4:
if (!(_h < _j.length)) return [3 /*break*/, 7];
f = _j[_h];
fields.push(classify(f, cat.getField(f)));
current++;
if (!ctx.shouldUpdate) return [3 /*break*/, 6];
return [4 /*yield*/, ctx.update({ message: 'Encoding...', current: current, max: maxProgress })];
case 5:
_k.sent();
_k.label = 6;
case 6:
_h++;
return [3 /*break*/, 4];
case 7:
encoder.writeCategory(getCategoryInstanceProvider(b.categories[c], fields));
current++;
if (!ctx.shouldUpdate) return [3 /*break*/, 9];
return [4 /*yield*/, ctx.update({ message: 'Encoding...', current: current, max: maxProgress })];
case 8:
_k.sent();
_k.label = 9;
case 9:
_f++;
return [3 /*break*/, 3];
case 10:
_d++;
return [3 /*break*/, 2];
case 11: return [4 /*yield*/, ctx.update('Exporting...')];
case 12:
_k.sent();
ret = encoder.getData();
return [4 /*yield*/, ctx.update('Done.\n')];
case 13:
_k.sent();
return [2 /*return*/, ret];
}
});
}); }).run(showProgress, 250);
}
exports.convert = convert;
//# sourceMappingURL=converter.js.map