bioseq-ts
Version:
Biological Sequence in Javascript, written in Typescript.
72 lines • 2.86 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FastaUtils = void 0;
var loglevel_colored_prefix_1 = require("loglevel-colored-prefix");
var BioSeq_1 = require("./BioSeq");
var BioSeqSet_1 = require("./BioSeqSet");
var ParseUtilsAbstract_1 = require("./ParseUtilsAbstract");
var FastaUtils = /** @class */ (function (_super) {
__extends(FastaUtils, _super);
function FastaUtils(logLevel) {
if (logLevel === void 0) { logLevel = 'INFO'; }
var _this = _super.call(this) || this;
_this.logLevel = logLevel;
_this.log = new loglevel_colored_prefix_1.Logger(logLevel);
return _this;
}
/**
* Parse FASTA string chunk to a BioSeqSet object
*
* @param {string} chunk
* @returns
* @memberof FastaUtils
*/
FastaUtils.prototype.parse = function (chunk) {
var log = this.log.getLogger('BioSeq::parse');
if (chunk[0] !== '>') {
throw Error("invalid fasta");
}
var set = [];
var seqs = chunk.split('>').filter(function (i) { return i !== ''; });
seqs.forEach(function (seq) {
var parts = seq.split('\n');
var header = parts.shift();
var sequence = parts.join('').replace(/ /g, '');
if (header && sequence && parts.length > 0) {
var bioseq = new BioSeq_1.BioSeq(header, sequence);
set.push(bioseq);
}
else {
throw Error("Invalid FASTA.\nOffending segment: " + seq);
}
});
var bioSeqSet = new BioSeqSet_1.BioSeqSet(set);
log.debug("Parsed " + bioSeqSet.getBioSeqs().length + " sequences");
return bioSeqSet;
};
FastaUtils.prototype.write = function (input) {
var log = this.log.getLogger('BioSeq::write');
var fasta = '';
log.debug("Making Fasta formated string of " + input.getBioSeqs.length + " sequences");
input.getBioSeqs().forEach(function (bioseq) {
fasta += ">" + bioseq.header + "\n" + bioseq.seq + "\n";
});
return fasta;
};
return FastaUtils;
}(ParseUtilsAbstract_1.ParseUtils));
exports.FastaUtils = FastaUtils;
//# sourceMappingURL=FastaUtils.js.map