molstar
Version:
A comprehensive macromolecular library.
148 lines • 6.42 kB
JavaScript
"use strict";
/**
* Copyright (c) 2017-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterUnitGraph = void 0;
var tslib_1 = require("tslib");
var generic_1 = require("../../mol-data/generic");
var InterUnitGraph = /** @class */ (function () {
function InterUnitGraph(map) {
this.map = map;
var count = 0;
var edges = [];
var edgeKeyIndex = new Map();
var vertexKeyIndex = new Map();
this.map.forEach(function (pairEdgesArray) {
pairEdgesArray.forEach(function (pairEdges) {
count += pairEdges.edgeCount;
pairEdges.connectedIndices.forEach(function (indexA) {
pairEdges.getEdges(indexA).forEach(function (edgeInfo) {
var unitA = pairEdges.unitA, unitB = pairEdges.unitB;
var edgeKey = InterUnitGraph.getEdgeKey(indexA, unitA, edgeInfo.indexB, unitB);
edgeKeyIndex.set(edgeKey, edges.length);
var vertexKey = InterUnitGraph.getVertexKey(indexA, unitA);
var e = vertexKeyIndex.get(vertexKey);
if (e === undefined)
vertexKeyIndex.set(vertexKey, [edges.length]);
else
e.push(edges.length);
edges.push((0, tslib_1.__assign)((0, tslib_1.__assign)({}, edgeInfo), { indexA: indexA, unitA: unitA, unitB: unitB }));
});
});
});
});
this.edgeCount = count;
this.edges = edges;
this.edgeKeyIndex = edgeKeyIndex;
this.vertexKeyIndex = vertexKeyIndex;
}
/** Get an array of unit-pair-edges that are connected to the given unit */
InterUnitGraph.prototype.getConnectedUnits = function (unit) {
if (!this.map.has(unit))
return emptyArray;
return this.map.get(unit);
};
/** Index into this.edges */
InterUnitGraph.prototype.getEdgeIndex = function (indexA, unitA, indexB, unitB) {
var edgeKey = InterUnitGraph.getEdgeKey(indexA, unitA, indexB, unitB);
var index = this.edgeKeyIndex.get(edgeKey);
return index !== undefined ? index : -1;
};
/** Check if edge exists */
InterUnitGraph.prototype.hasEdge = function (indexA, unitA, indexB, unitB) {
return this.getEdgeIndex(indexA, unitA, indexB, unitB) !== -1;
};
/** Get inter-unit edge given a pair of indices and units */
InterUnitGraph.prototype.getEdge = function (indexA, unitA, indexB, unitB) {
var index = this.getEdgeIndex(indexA, unitA, indexB, unitB);
return index !== -1 ? this.edges[index] : undefined;
};
/** Indices into this.edges */
InterUnitGraph.prototype.getEdgeIndices = function (index, unit) {
return this.vertexKeyIndex.get(InterUnitGraph.getVertexKey(index, unit)) || [];
};
return InterUnitGraph;
}());
exports.InterUnitGraph = InterUnitGraph;
(function (InterUnitGraph) {
var UnitPairEdges = /** @class */ (function () {
function UnitPairEdges(unitA, unitB, edgeCount, connectedIndices, edgeMap) {
this.unitA = unitA;
this.unitB = unitB;
this.edgeCount = edgeCount;
this.connectedIndices = connectedIndices;
this.edgeMap = edgeMap;
}
UnitPairEdges.prototype.hasEdges = function (indexA) {
return this.edgeMap.has(indexA);
};
UnitPairEdges.prototype.getEdges = function (indexA) {
if (!this.edgeMap.has(indexA))
return emptyArray;
return this.edgeMap.get(indexA);
};
Object.defineProperty(UnitPairEdges.prototype, "areUnitsOrdered", {
get: function () {
return this.unitA < this.unitB;
},
enumerable: false,
configurable: true
});
return UnitPairEdges;
}());
InterUnitGraph.UnitPairEdges = UnitPairEdges;
function getEdgeKey(indexA, unitA, indexB, unitB) {
return indexA + "|" + unitA + "|" + indexB + "|" + unitB;
}
InterUnitGraph.getEdgeKey = getEdgeKey;
function getVertexKey(index, unit) {
return index + "|" + unit;
}
InterUnitGraph.getVertexKey = getVertexKey;
//
function addMapEntry(map, a, b) {
if (map.has(a))
map.get(a).push(b);
else
map.set(a, [b]);
}
var Builder = /** @class */ (function () {
function Builder() {
this.map = new Map();
}
Builder.prototype.startUnitPair = function (unitA, unitB) {
this.uA = unitA;
this.uB = unitB;
this.mapAB = new Map();
this.mapBA = new Map();
this.linkedA = generic_1.UniqueArray.create();
this.linkedB = generic_1.UniqueArray.create();
this.linkCount = 0;
};
Builder.prototype.finishUnitPair = function () {
if (this.linkCount === 0)
return;
addMapEntry(this.map, this.uA, new UnitPairEdges(this.uA, this.uB, this.linkCount, this.linkedA.array, this.mapAB));
addMapEntry(this.map, this.uB, new UnitPairEdges(this.uB, this.uA, this.linkCount, this.linkedB.array, this.mapBA));
};
Builder.prototype.add = function (indexA, indexB, props) {
addMapEntry(this.mapAB, indexA, { indexB: indexB, props: props });
addMapEntry(this.mapBA, indexB, { indexB: indexA, props: props });
generic_1.UniqueArray.add(this.linkedA, indexA, indexA);
generic_1.UniqueArray.add(this.linkedB, indexB, indexB);
this.linkCount += 1;
};
Builder.prototype.getMap = function () {
return this.map;
};
return Builder;
}());
InterUnitGraph.Builder = Builder;
})(InterUnitGraph || (InterUnitGraph = {}));
exports.InterUnitGraph = InterUnitGraph;
var emptyArray = [];
//# sourceMappingURL=inter-unit-graph.js.map