UNPKG

drugflow-molstar

Version:
199 lines (198 loc) 8.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.alignAndSuperposeWithSIFTSMapping = alignAndSuperposeWithSIFTSMapping; var tslib_1 = require("tslib"); var int_1 = require("Molstar/mol-data/int"); var minimize_rmsd_1 = require("Molstar/mol-math/linear-algebra/3d/minimize-rmsd"); var sifts_mapping_1 = require("./sifts-mapping"); var element_1 = require("Molstar/mol-model/structure/structure/element"); function alignAndSuperposeWithSIFTSMapping(structures, options) { var e_1, _a; var _b, _c; var indexMap = new Map(); for (var i = 0; i < structures.length; i++) { var includeResidueTest = (_b = options === null || options === void 0 ? void 0 : options.includeResidueTest) !== null && _b !== void 0 ? _b : _includeAllResidues; if ((options === null || options === void 0 ? void 0 : options.applyTestIndex) && !options.applyTestIndex.includes(i)) includeResidueTest = _includeAllResidues; buildIndex(structures[i], indexMap, i, (_c = options === null || options === void 0 ? void 0 : options.traceOnly) !== null && _c !== void 0 ? _c : true, includeResidueTest); } var index = Array.from(indexMap.values()); // TODO: support non-first structure pivots var pairs = findPairs(structures.length, index); var zeroOverlapPairs = []; var failedPairs = []; var entries = []; try { for (var pairs_1 = tslib_1.__values(pairs), pairs_1_1 = pairs_1.next(); !pairs_1_1.done; pairs_1_1 = pairs_1.next()) { var p = pairs_1_1.value; if (p.count === 0) { zeroOverlapPairs.push([p.i, p.j]); } else { var _d = tslib_1.__read(getPositionTables(index, p.i, p.j, p.count), 2), a = _d[0], b = _d[1]; var transform = minimize_rmsd_1.MinimizeRmsd.compute({ a: a, b: b }); if (Number.isNaN(transform.rmsd)) { failedPairs.push([p.i, p.j]); } else { entries.push({ transform: transform, pivot: p.i, other: p.j }); } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (pairs_1_1 && !pairs_1_1.done && (_a = pairs_1.return)) _a.call(pairs_1); } finally { if (e_1) throw e_1.error; } } return { entries: entries, zeroOverlapPairs: zeroOverlapPairs, failedPairs: failedPairs }; } function getPositionTables(index, pivot, other, N) { var e_2, _a; var xs = minimize_rmsd_1.MinimizeRmsd.Positions.empty(N); var ys = minimize_rmsd_1.MinimizeRmsd.Positions.empty(N); var o = 0; try { for (var index_1 = tslib_1.__values(index), index_1_1 = index_1.next(); !index_1_1.done; index_1_1 = index_1.next()) { var pivots = index_1_1.value.pivots; var a = pivots[pivot]; var b = pivots[other]; if (!a || !b) continue; var l = Math.min(a[2] - a[1], b[2] - b[1]); // TODO: check if residue types match? for (var i = 0; i < l; i++) { var eI = (a[1] + i); xs.x[o] = a[0].conformation.x(eI); xs.y[o] = a[0].conformation.y(eI); xs.z[o] = a[0].conformation.z(eI); eI = (b[1] + i); ys.x[o] = b[0].conformation.x(eI); ys.y[o] = b[0].conformation.y(eI); ys.z[o] = b[0].conformation.z(eI); o++; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (index_1_1 && !index_1_1.done && (_a = index_1.return)) _a.call(index_1); } finally { if (e_2) throw e_2.error; } } return [xs, ys]; } function findPairs(N, index) { var e_3, _a; var pairwiseCounts = []; for (var i = 0; i < N; i++) { pairwiseCounts[i] = []; for (var j = 0; j < N; j++) pairwiseCounts[i][j] = 0; } try { for (var index_2 = tslib_1.__values(index), index_2_1 = index_2.next(); !index_2_1.done; index_2_1 = index_2.next()) { var pivots = index_2_1.value.pivots; for (var i = 0; i < N; i++) { if (!pivots[i]) continue; var lI = pivots[i][2] - pivots[i][1]; for (var j = i + 1; j < N; j++) { if (!pivots[j]) continue; var lJ = pivots[j][2] - pivots[j][1]; pairwiseCounts[i][j] = pairwiseCounts[i][j] + Math.min(lI, lJ); } } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (index_2_1 && !index_2_1.done && (_a = index_2.return)) _a.call(index_2); } finally { if (e_3) throw e_3.error; } } var ret = []; for (var j = 1; j < N; j++) { ret[j - 1] = { i: 0, j: j, count: pairwiseCounts[0][j] }; } // TODO: support non-first structure pivots // for (let i = 0; i < N - 1; i++) { // let max = 0, maxJ = i; // for (let j = i + 1; j < N; j++) { // if (pairwiseCounts[i][j] > max) { // maxJ = j; // max = pairwiseCounts[i][j]; // } // } // ret[i] = { i, j: maxJ, count: max }; // } return ret; } function _includeAllResidues() { return true; } function buildIndex(structure, index, sI, traceOnly, includeTest) { var e_4, _a, _b; var loc = element_1.StructureElement.Location.create(structure); try { for (var _c = tslib_1.__values(structure.units), _d = _c.next(); !_d.done; _d = _c.next()) { var unit = _d.value; if (unit.kind !== 0 /* Unit.Kind.Atomic */) continue; var elements = unit.elements, model = unit.model; loc.unit = unit; var map = sifts_mapping_1.SIFTSMapping.Provider.get(model).value; if (!map) return; var dbName = map.dbName, accession = map.accession, num = map.num; var chainsIt = int_1.Segmentation.transientSegments(unit.model.atomicHierarchy.chainAtomSegments, elements); var residuesIt = int_1.Segmentation.transientSegments(unit.model.atomicHierarchy.residueAtomSegments, elements); var traceElementIndex = unit.model.atomicHierarchy.derived.residue.traceElementIndex; while (chainsIt.hasNext) { var chainSegment = chainsIt.move(); residuesIt.setSegment(chainSegment); while (residuesIt.hasNext) { var residueSegment = residuesIt.move(); var rI = residueSegment.index; if (!dbName[rI]) continue; var traceElement = traceElementIndex[rI]; var start = void 0, end = void 0; if (traceOnly) { start = traceElement; if (start === -1) continue; end = start + 1; } else { start = elements[residueSegment.start]; end = elements[residueSegment.end - 1] + 1; } loc.element = (traceElement >= 0 ? traceElement : start); if (!includeTest(loc, rI, start, end)) continue; var key = "".concat(dbName[rI], "-").concat(accession[rI].split('-')[0], "-").concat(num[rI]); if (!index.has(key)) { index.set(key, { key: key, pivots: (_b = {}, _b[sI] = [unit, start, end], _b) }); } else { var entry = index.get(key); if (!entry.pivots[sI]) { entry.pivots[sI] = [unit, start, end]; } } } } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_4) throw e_4.error; } } }