molstar
Version:
A comprehensive macromolecular library.
171 lines (170 loc) • 7.33 kB
JavaScript
/**
* Copyright (c) 2019-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Ludovic Autin <ludovic.autin@gmail.com>
*/
import { __awaiter, __generator } from "tslib";
import { CIF } from '../../mol-io/reader/cif';
import { parsePDB } from '../../mol-io/reader/pdb/parser';
import { Asset } from '../../mol-util/assets';
import { Vec3 } from '../../mol-math/linear-algebra';
export function parseCif(plugin, 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*/, plugin.runTask(comp)];
case 1:
parsed = _a.sent();
if (parsed.isError)
throw parsed;
return [2 /*return*/, parsed.result];
}
});
});
}
export function parsePDBfile(plugin, data, id) {
return __awaiter(this, void 0, void 0, function () {
var comp, parsed;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
comp = parsePDB(data, id);
return [4 /*yield*/, plugin.runTask(comp)];
case 1:
parsed = _a.sent();
if (parsed.isError)
throw parsed;
return [2 /*return*/, parsed.result];
}
});
});
}
function downloadCif(plugin, url, isBinary, assetManager) {
return __awaiter(this, void 0, void 0, function () {
var type, asset;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
type = isBinary ? 'binary' : 'string';
return [4 /*yield*/, plugin.runTask(assetManager.resolve(Asset.getUrlAsset(assetManager, url), type))];
case 1:
asset = _b.sent();
_a = {};
return [4 /*yield*/, parseCif(plugin, asset.data)];
case 2: return [2 /*return*/, (_a.cif = _b.sent(), _a.asset = asset, _a)];
}
});
});
}
function downloadPDB(plugin, url, id, assetManager) {
return __awaiter(this, void 0, void 0, function () {
var asset;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, assetManager.resolve(Asset.getUrlAsset(assetManager, url), 'string').run()];
case 1:
asset = _b.sent();
_a = {};
return [4 /*yield*/, parsePDBfile(plugin, asset.data, id)];
case 2: return [2 /*return*/, (_a.pdb = _b.sent(), _a.asset = asset, _a)];
}
});
});
}
export function getFromPdb(plugin, pdbId, assetManager) {
return __awaiter(this, void 0, void 0, function () {
var _a, cif, asset;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, downloadCif(plugin, "https://models.rcsb.org/".concat(pdbId, ".bcif"), true, assetManager)];
case 1:
_a = _b.sent(), cif = _a.cif, asset = _a.asset;
return [2 /*return*/, { mmcif: cif.blocks[0], asset: asset }];
}
});
});
}
export function getFromOPM(plugin, pdbId, assetManager) {
return __awaiter(this, void 0, void 0, function () {
var asset;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, plugin.runTask(assetManager.resolve(Asset.getUrlAsset(assetManager, "https://opm-assets.storage.googleapis.com/pdb/".concat(pdbId.toLowerCase(), ".pdb")), 'string'))];
case 1:
asset = _b.sent();
_a = {};
return [4 /*yield*/, parsePDBfile(plugin, asset.data, pdbId)];
case 2: return [2 /*return*/, (_a.pdb = _b.sent(), _a.asset = asset, _a)];
}
});
});
}
export function getFromCellPackDB(plugin, id, baseUrl, assetManager) {
return __awaiter(this, void 0, void 0, function () {
var isBinary, _a, cif, asset, name_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(id.toLowerCase().endsWith('.cif') || id.toLowerCase().endsWith('.bcif'))) return [3 /*break*/, 2];
isBinary = id.toLowerCase().endsWith('.bcif');
return [4 /*yield*/, downloadCif(plugin, "".concat(baseUrl, "/other/").concat(id), isBinary, assetManager)];
case 1:
_a = _b.sent(), cif = _a.cif, asset = _a.asset;
return [2 /*return*/, { mmcif: cif.blocks[0], asset: asset }];
case 2:
name_1 = id.endsWith('.pdb') ? id.substring(0, id.length - 4) : id;
return [4 /*yield*/, downloadPDB(plugin, "".concat(baseUrl, "/other/").concat(name_1, ".pdb"), name_1, assetManager)];
case 3: return [2 /*return*/, _b.sent()];
}
});
});
}
export function getStructureMean(structure) {
var xSum = 0, ySum = 0, zSum = 0;
for (var i = 0, il = structure.units.length; i < il; ++i) {
var unit = structure.units[i];
var elements = unit.elements;
var _a = unit.conformation, x = _a.x, y = _a.y, z = _a.z;
for (var j = 0, jl = elements.length; j < jl; ++j) {
var eI = elements[j];
xSum += x(eI);
ySum += y(eI);
zSum += z(eI);
}
}
var elementCount = structure.elementCount;
return Vec3.create(xSum / elementCount, ySum / elementCount, zSum / elementCount);
}
export function getFloatValue(value, offset) {
// if the last byte is a negative value (MSB is 1), the final
// float should be too
var negative = value.getInt8(offset + 2) >>> 31;
// this is how the bytes are arranged in the byte array/DataView
// buffer
var _a = [
// get first three bytes as unsigned since we only care
// about the last 8 bits of 32-bit js number returned by
// getUint8().
// Should be the same as: getInt8(offset) & -1 >>> 24
value.getUint8(offset),
value.getUint8(offset + 1),
value.getUint8(offset + 2),
// get the last byte, which is the exponent, as a signed int
// since it's already correct
value.getInt8(offset + 3)
], b0 = _a[0], b1 = _a[1], b2 = _a[2], exponent = _a[3];
var mantissa = b0 | (b1 << 8) | (b2 << 16);
if (negative) {
// need to set the most significant 8 bits to 1's since a js
// number is 32 bits but our mantissa is only 24.
mantissa |= 255 << 24;
}
return mantissa * Math.pow(10, exponent);
}