bioseq-ts
Version:
Biological Sequence in Javascript, written in Typescript.
183 lines • 6.3 kB
JavaScript
"use strict";
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BioSeqSet = void 0;
var crypto_1 = __importDefault(require("crypto"));
var BioSeq_1 = require("./BioSeq");
var BioSeqSet = /** @class */ (function () {
function BioSeqSet(set, partitionTable) {
if (set === void 0) { set = []; }
if (partitionTable === void 0) { partitionTable = []; }
this.set = set;
this.partitionTable = this.isValidPartitionTable(partitionTable) ? partitionTable : [this.calcPartitionTable()];
this.hash = crypto_1.default.createHash('sha256');
this.hashString = '';
}
/**
* Creates a unique identifier based on the content of the dataset
*
* @returns {string}
* @memberof BioSeqSet
*/
BioSeqSet.prototype.getHash = function () {
var _this = this;
if (!this.hashString) {
this.getBioSeqs()
.map(function (seq) { return seq.header + '_' + seq.seq; })
.sort()
.forEach(function (item) {
_this.hash.update(item);
});
this.hashString = this.hash.digest('hex');
}
return this.hashString;
};
/**
* Returns the set of BioSeqs.
*
* @returns
* @memberof BioSeqSet
*/
BioSeqSet.prototype.getBioSeqs = function () {
return this.set;
};
/**
* Returns the partition of the set
*
* @returns
* @memberof BioSeqSet
*/
BioSeqSet.prototype.getPartitions = function () {
return this.partitionTable;
};
/**
* Concatenate the sequences of two BioSeqSets. Keep header of the current set.
*
* @param {BioSeqSet} newset
* @memberof BioSeqSet
*/
BioSeqSet.prototype.concatSequences = function (newBioSeqSet) {
var concatenated = [];
var newset = newBioSeqSet.getBioSeqs();
if (newset.length !== this.set.length) {
throw Error('Cannot concat BioSeqSets with different lengths');
}
var max1 = this.maxLength();
var max2 = newBioSeqSet.maxLength();
this.set.forEach(function (item, i) {
var header = item.header;
var seq = item.seq + '-'.repeat(max1 - item.seq.length) + newset[i].seq + '-'.repeat(max2 - newset[i].seq.length);
var bioseq = new BioSeq_1.BioSeq(header, seq);
concatenated.push(bioseq);
});
var newPartitions = this.partitionTable;
newPartitions.push([max1 + 1, max1 + max2]);
return new BioSeqSet(concatenated, newPartitions);
};
/**
* Return the length of the longest sequence in the set (with or without gaps).
*
* @param {boolean} [gapless=false]
* @returns {number}
* @memberof BioSeqSet
*/
BioSeqSet.prototype.maxLength = function (gapless) {
if (gapless === void 0) { gapless = false; }
var max = 0;
this.set.forEach(function (bioseq) {
var curr = bioseq.seq.length;
if (gapless) {
curr = bioseq.noGaps().length;
}
max = max > curr ? max : curr;
});
return max;
};
/**
* Returns a boolean if all sequences are aligned, or if they all have the same length
*
* @returns {boolean}
* @memberof BioSeqSet
*/
BioSeqSet.prototype.isAligned = function () {
var e_1, _a;
var maxL = this.maxLength();
try {
for (var _b = __values(this.set), _c = _b.next(); !_c.done; _c = _b.next()) {
var bioseq = _c.value;
if (bioseq.seq.length !== maxL) {
return false;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return true;
};
/**
* Returns the partition of the dataset. [start, end]
*
* @private
* @returns
* @memberof BioSeqSet
*/
BioSeqSet.prototype.calcPartitionTable = function () {
var partition = [1, this.maxLength()];
return partition;
};
/**
* Minimal sanity check to see if it is a valid partition table
*
* @private
* @param {IPartition[]} partitionTable
* @returns {boolean}
* @memberof BioSeqSet
*/
BioSeqSet.prototype.isValidPartitionTable = function (partitionTable) {
var e_2, _a;
var lastCoord = 0;
try {
for (var partitionTable_1 = __values(partitionTable), partitionTable_1_1 = partitionTable_1.next(); !partitionTable_1_1.done; partitionTable_1_1 = partitionTable_1.next()) {
var partition = partitionTable_1_1.value;
var rightStart = partition[0] === lastCoord + 1;
lastCoord = partition[1];
var rightOrder = partition[1] - partition[0] > 0;
if (!rightStart || !rightOrder) {
return false;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (partitionTable_1_1 && !partitionTable_1_1.done && (_a = partitionTable_1.return)) _a.call(partitionTable_1);
}
finally { if (e_2) throw e_2.error; }
}
if (lastCoord !== this.maxLength()) {
return false;
}
return true && partitionTable.length !== 0;
};
return BioSeqSet;
}());
exports.BioSeqSet = BioSeqSet;
//# sourceMappingURL=BioSeqSet.js.map