molstar
Version:
A comprehensive macromolecular library.
336 lines (335 loc) • 16.4 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getModelsAndStructure = exports.printModelStats = exports.printSymmetryInfo = exports.printUnits = exports.printRings = exports.printSequence = exports.printBonds = exports.printSecStructure = exports.residueLabel = exports.atomLabel = exports.readCifFile = void 0;
var tslib_1 = require("tslib");
var argparse = tslib_1.__importStar(require("argparse"));
require('util.promisify').shim();
var structure_1 = require("../../mol-model/structure");
// import { Run, Progress } from '../../mol-task'
var int_1 = require("../../mol-data/int");
var helpers_1 = require("./helpers");
var linear_algebra_1 = require("../../mol-math/linear-algebra");
var mmcif_1 = require("../../mol-model-formats/structure/mmcif");
var sequence_1 = require("../../mol-model/sequence");
var secondary_structure_1 = require("../../mol-model-formats/structure/property/secondary-structure");
var symmetry_1 = require("../../mol-model-formats/structure/property/symmetry");
var mol_task_1 = require("../../mol-task");
function downloadFromPdb(pdb) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var parsed;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, helpers_1.downloadCif)("http://www.ebi.ac.uk/pdbe/static/entry/".concat(pdb, "_updated.cif"), false)];
case 1:
parsed = _a.sent();
return [2 /*return*/, parsed.blocks[0]];
}
});
});
}
function readCifFile(path) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var parsed;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, helpers_1.openCif)(path)];
case 1:
parsed = _a.sent();
return [2 /*return*/, parsed.blocks[0]];
}
});
});
}
exports.readCifFile = readCifFile;
function atomLabel(model, aI) {
var _a = model.atomicHierarchy, atoms = _a.atoms, residues = _a.residues, chains = _a.chains, residueAtomSegments = _a.residueAtomSegments, chainAtomSegments = _a.chainAtomSegments;
var label_atom_id = atoms.label_atom_id, label_comp_id = atoms.label_comp_id;
var label_seq_id = residues.label_seq_id;
var label_asym_id = chains.label_asym_id;
var rI = residueAtomSegments.index[aI];
var cI = chainAtomSegments.index[aI];
return "".concat(label_asym_id.value(cI), " ").concat(label_comp_id.value(aI), " ").concat(label_seq_id.value(rI), " ").concat(label_atom_id.value(aI));
}
exports.atomLabel = atomLabel;
function residueLabel(model, rI) {
var _a = model.atomicHierarchy, atoms = _a.atoms, residues = _a.residues, chains = _a.chains, residueAtomSegments = _a.residueAtomSegments, chainAtomSegments = _a.chainAtomSegments;
var label_comp_id = atoms.label_comp_id;
var label_seq_id = residues.label_seq_id;
var label_asym_id = chains.label_asym_id;
var aI = residueAtomSegments.offsets[rI];
var cI = chainAtomSegments.index[aI];
return "".concat(label_asym_id.value(cI), " ").concat(label_comp_id.value(aI), " ").concat(label_seq_id.value(rI));
}
exports.residueLabel = residueLabel;
function printSecStructure(model) {
console.log('\nSecondary Structure\n=============');
var residues = model.atomicHierarchy.residues;
var secondaryStructure = secondary_structure_1.ModelSecondaryStructure.Provider.get(model);
if (!secondaryStructure)
return;
var key = secondaryStructure.key, elements = secondaryStructure.elements;
var count = residues._rowCount;
var rI = 0;
while (rI < count) {
var start = rI;
while (rI < count && key[start] === key[rI])
rI++;
rI--;
var e = elements[key[start]];
if (e.kind !== 'none')
console.log("".concat(e.kind, ": ").concat(residueLabel(model, start), " - ").concat(residueLabel(model, rI)));
rI++;
}
}
exports.printSecStructure = printSecStructure;
function printBonds(structure, showIntra, showInter) {
if (showIntra) {
console.log('\nIntra Unit Bonds\n=============');
for (var _a = 0, _b = structure.units; _a < _b.length; _a++) {
var unit = _b[_a];
if (!structure_1.Unit.isAtomic(unit))
continue;
var elements = unit.elements;
var _c = unit.bonds, a = _c.a, b = _c.b, edgeCount = _c.edgeCount;
var model = unit.model;
if (!edgeCount)
continue;
for (var bI = 0, _bI = edgeCount * 2; bI < _bI; bI++) {
var x = a[bI], y = b[bI];
if (x >= y)
continue;
console.log("".concat(atomLabel(model, elements[x]), " -- ").concat(atomLabel(model, elements[y])));
}
}
}
if (showInter) {
console.log('\nInter Unit Bonds\n=============');
var bonds = structure.interUnitBonds;
for (var _d = 0, _e = structure.units; _d < _e.length; _d++) {
var unit = _e[_d];
if (!structure_1.Unit.isAtomic(unit))
continue;
for (var _f = 0, _g = bonds.getConnectedUnits(unit.id); _f < _g.length; _f++) {
var pairBonds = _g[_f];
if (!pairBonds.areUnitsOrdered || pairBonds.edgeCount === 0)
continue;
var unitA = pairBonds.unitA, unitB = pairBonds.unitB, edgeCount = pairBonds.edgeCount;
var uA = structure.unitMap.get(unitA);
var uB = structure.unitMap.get(unitB);
console.log("".concat(unitA, " - ").concat(unitB, ": ").concat(edgeCount, " bond(s)"));
for (var _h = 0, _k = pairBonds.connectedIndices; _h < _k.length; _h++) {
var aI = _k[_h];
for (var _l = 0, _m = pairBonds.getEdges(aI); _l < _m.length; _l++) {
var bond = _m[_l];
console.log("".concat(atomLabel(uA.model, uA.elements[aI]), " -- ").concat(atomLabel(uB.model, uB.elements[bond.indexB])));
}
}
}
}
}
}
exports.printBonds = printBonds;
function printSequence(model) {
console.log('\nSequence\n=============');
var byEntityKey = model.sequence.byEntityKey;
for (var _a = 0, _b = Object.keys(byEntityKey); _a < _b.length; _a++) {
var key = _b[_a];
var _c = byEntityKey[+key], sequence = _c.sequence, entityId = _c.entityId;
var seqId = sequence.seqId, compId = sequence.compId;
console.log("".concat(entityId, " (").concat(sequence.kind, " ").concat(seqId.value(0), ", ").concat(seqId.value(seqId.rowCount - 1), ") (").concat(compId.value(0), ", ").concat(compId.value(compId.rowCount - 1), ")"));
console.log("".concat(sequence_1.Sequence.getSequenceString(sequence)));
}
console.log();
}
exports.printSequence = printSequence;
function printRings(structure) {
console.log('\nRings\n=============');
for (var _a = 0, _b = structure.units; _a < _b.length; _a++) {
var unit = _b[_a];
if (!structure_1.Unit.isAtomic(unit))
continue;
var _c = unit.rings, all = _c.all, byFingerprint = _c.byFingerprint;
var fps = [];
for (var i = 0, _i = Math.min(5, all.length); i < _i; i++) {
fps[fps.length] = structure_1.UnitRing.fingerprint(unit, all[i]);
}
if (all.length > 5)
fps.push('...');
console.log("Unit ".concat(unit.id, ", ").concat(all.length, " ring(s), ").concat(byFingerprint.size, " different fingerprint(s).\n ").concat(fps.join(', ')));
}
console.log();
}
exports.printRings = printRings;
function printUnits(structure) {
console.log('\nUnits\n=============');
var l = structure_1.StructureElement.Location.create(structure);
for (var _a = 0, _b = structure.units; _a < _b.length; _a++) {
var unit = _b[_a];
l.unit = unit;
var elements = unit.elements;
var size = int_1.OrderedSet.size(elements);
if (structure_1.Unit.isAtomic(l.unit)) {
console.log("Atomic unit ".concat(unit.id, " ").concat(unit.conformation.operator.name, ": ").concat(size, " elements"));
}
else if (structure_1.Unit.isCoarse(l.unit)) {
console.log("Coarse unit ".concat(unit.id, " ").concat(unit.conformation.operator.name, " (").concat(structure_1.Unit.isSpheres(l.unit) ? 'spheres' : 'gaussians', "): ").concat(size, " elements."));
var props = structure_1.StructureProperties.coarse;
var modelSeq = l.unit.model.sequence;
for (var j = 0, _j = Math.min(size, 3); j < _j; j++) {
l.element = int_1.OrderedSet.getAt(elements, j);
var residues = [];
var start = props.seq_id_begin(l), end = props.seq_id_end(l);
var compId = modelSeq.byEntityKey[props.entityKey(l)].sequence.compId.value;
for (var e = start; e <= end; e++)
residues.push(compId(e));
console.log("".concat(props.asym_id(l), ":").concat(start, "-").concat(end, " (").concat(residues.join('-'), ") ").concat(props.asym_id(l), " [").concat(props.x(l).toFixed(2), ", ").concat(props.y(l).toFixed(2), ", ").concat(props.z(l).toFixed(2), "]"));
}
if (size > 3)
console.log("...");
}
}
}
exports.printUnits = printUnits;
function printSymmetryInfo(model) {
console.log('\nSymmetry Info\n=============');
var symmetry = symmetry_1.ModelSymmetry.Provider.get(model);
if (!symmetry)
return;
var _a = symmetry.spacegroup.cell, size = _a.size, anglesInRadians = _a.anglesInRadians;
console.log("Spacegroup: ".concat(symmetry.spacegroup.name, " size: ").concat(linear_algebra_1.Vec3.toString(size), " angles: ").concat(linear_algebra_1.Vec3.toString(anglesInRadians)));
console.log("Assembly names: ".concat(symmetry.assemblies.map(function (a) { return a.id; }).join(', ')));
// NCS example: 1auy
console.log("NCS operators: ".concat(symmetry.ncsOperators && symmetry.ncsOperators.map(function (a) { return a.name; }).join(', ')));
}
exports.printSymmetryInfo = printSymmetryInfo;
function printModelStats(models) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var i, m;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log('\nModels\n=============');
i = 0;
_a.label = 1;
case 1:
if (!(i < models.frameCount)) return [3 /*break*/, 4];
return [4 /*yield*/, mol_task_1.Task.resolveInContext(models.getFrameAtIndex(i))];
case 2:
m = _a.sent();
if (m.coarseHierarchy.isDefined) {
console.log("".concat(m.label, " ").concat(m.modelNum, ": ").concat(m.atomicHierarchy.atoms._rowCount, " atom(s), ").concat(m.coarseHierarchy.spheres.count, " sphere(s), ").concat(m.coarseHierarchy.gaussians.count, " gaussian(s)"));
}
else {
console.log("".concat(m.label, " ").concat(m.modelNum, ": ").concat(m.atomicHierarchy.atoms._rowCount, " atom(s)"));
}
_a.label = 3;
case 3:
i++;
return [3 /*break*/, 1];
case 4:
console.log();
return [2 /*return*/];
}
});
});
}
exports.printModelStats = printModelStats;
function getModelsAndStructure(frame) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var models, structure;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, mmcif_1.trajectoryFromMmCIF)(frame).run()];
case 1:
models = _a.sent();
structure = structure_1.Structure.ofModel(models.representative);
return [2 /*return*/, { models: models, structure: structure }];
}
});
});
}
exports.getModelsAndStructure = getModelsAndStructure;
function run(frame, args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, models, structure;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, getModelsAndStructure(frame)];
case 1:
_a = _b.sent(), models = _a.models, structure = _a.structure;
if (args.models)
printModelStats(models);
if (args.seq)
printSequence(models.representative);
if (args.units)
printUnits(structure);
if (args.sym)
printSymmetryInfo(models.representative);
if (args.rings)
printRings(structure);
if (args.intraBonds)
printBonds(structure, true, false);
if (args.interBonds)
printBonds(structure, false, true);
if (args.sec)
printSecStructure(models.representative);
return [2 /*return*/];
}
});
});
}
function runDL(pdb, args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var mmcif;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, downloadFromPdb(pdb)];
case 1:
mmcif = _a.sent();
run(mmcif, args);
return [2 /*return*/];
}
});
});
}
function runFile(filename, args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var mmcif;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, readCifFile(filename)];
case 1:
mmcif = _a.sent();
run(mmcif, args);
return [2 /*return*/];
}
});
});
}
var parser = new argparse.ArgumentParser({
add_help: true,
description: 'Print info about a structure, mainly to test and showcase the mol-model module'
});
parser.add_argument('--download', '-d', { help: 'Pdb entry id' });
parser.add_argument('--file', '-f', { help: 'filename' });
parser.add_argument('--models', { help: 'print models info', action: 'store_true' });
parser.add_argument('--seq', { help: 'print sequence', action: 'store_true' });
parser.add_argument('--units', { help: 'print units', action: 'store_true' });
parser.add_argument('--sym', { help: 'print symmetry', action: 'store_true' });
parser.add_argument('--rings', { help: 'print rings', action: 'store_true' });
parser.add_argument('--intraBonds', { help: 'print intra unit bonds', action: 'store_true' });
parser.add_argument('--interBonds', { help: 'print inter unit bonds', action: 'store_true' });
parser.add_argument('--mod', { help: 'print modified residues', action: 'store_true' });
parser.add_argument('--sec', { help: 'print secoundary structure', action: 'store_true' });
var args = parser.parse_args();
if (args.download)
runDL(args.download, args);
else if (args.file)
runFile(args.file, args);
;