molstar
Version:
A comprehensive macromolecular library.
87 lines (86 loc) • 3.24 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __awaiter, __generator } from "tslib";
import * as util from 'util';
import * as fs from 'fs';
import fetch from 'node-fetch';
require('util.promisify').shim();
import { CIF } from '../../mol-io/reader/cif';
import { Progress } from '../../mol-task';
var readFileAsync = util.promisify(fs.readFile);
function readFile(path) {
return __awaiter(this, void 0, void 0, function () {
var input, data, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!path.match(/\.bcif$/)) return [3 /*break*/, 2];
return [4 /*yield*/, readFileAsync(path)];
case 1:
input = _a.sent();
data = new Uint8Array(input.byteLength);
for (i = 0; i < input.byteLength; i++)
data[i] = input[i];
return [2 /*return*/, data];
case 2: return [2 /*return*/, readFileAsync(path, 'utf8')];
}
});
});
}
function parseCif(data) {
return __awaiter(this, void 0, void 0, function () {
var comp, parsed;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
comp = CIF.parse(data);
return [4 /*yield*/, comp.run(function (p) { return console.log(Progress.format(p)); }, 250)];
case 1:
parsed = _a.sent();
if (parsed.isError)
throw parsed;
return [2 /*return*/, parsed.result];
}
});
});
}
export function openCif(path) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, readFile(path)];
case 1:
data = _a.sent();
return [2 /*return*/, parseCif(data)];
}
});
});
}
export function downloadCif(url, isBinary) {
return __awaiter(this, void 0, void 0, function () {
var data, _a, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0: return [4 /*yield*/, fetch(url)];
case 1:
data = _d.sent();
_a = parseCif;
if (!isBinary) return [3 /*break*/, 3];
_c = Uint8Array.bind;
return [4 /*yield*/, data.arrayBuffer()];
case 2:
_b = new (_c.apply(Uint8Array, [void 0, _d.sent()]))();
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, data.text()];
case 4:
_b = _d.sent();
_d.label = 5;
case 5: return [2 /*return*/, _a.apply(void 0, [_b])];
}
});
});
}