alignment.js
Version:
A suite of reusable [React](http://reactjs.org/) components for creating a variety of visualizations involving [multiple sequence alignments](https://en.wikipedia.org/wiki/Multiple_sequence_alignment). [View the live demo here](http://alignment.hyphy.org/
127 lines (106 loc) • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var BAMReader = /*#__PURE__*/function () {
function BAMReader(bam_file) {
_classCallCheck(this, BAMReader);
this.bam_file = bam_file;
}
_createClass(BAMReader, [{
key: "bamToFasta",
value: function bamToFasta(bam_record, window_start, window_end) {
var query = bam_record.getReadBases(),
cigar_information = bam_record.cigar().split(/(\d+|[A-Z])/).filter(function (x) {
return x;
}),
number_of_cigar_pairs = cigar_information.length / 2;
var alignment = "",
position = 0,
action,
stride,
match,
insertion,
deletion;
for (var i = 0; i < number_of_cigar_pairs; i++) {
stride = +cigar_information[2 * i];
action = cigar_information[2 * i + 1];
match = action == "M";
insertion = action == "I";
deletion = action == "D";
if (match || insertion) {
if (match) {
alignment += query.slice(position, position + stride);
}
position += stride;
} else if (deletion) {
if (alignment.length > 0 || i < number_of_cigar_pairs) {
alignment += "-".repeat(stride);
}
}
}
var reference_start = bam_record.get("start"),
reference_end = bam_record.get("end"),
left_pad_amount = Math.max(0, reference_start - window_start),
left_pad = "-".repeat(left_pad_amount),
right_pad_amount = Math.max(0, window_end - reference_end),
right_pad = "-".repeat(right_pad_amount),
left_trim_amount = Math.max(0, window_start - reference_start),
right_trim_amount = Math.max(0, reference_end - window_end),
full_length = alignment.length,
inner_fasta = alignment.slice(left_trim_amount, full_length - right_trim_amount);
var fasta_record = {
header: bam_record.get("name"),
seq: left_pad + inner_fasta + right_pad
};
return fasta_record;
}
}, {
key: "fasta_window",
value: function () {
var _fasta_window = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(start_site, end_site) {
var _this = this;
var header, reference;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return this.bam_file.getHeader();
case 2:
header = _context.sent;
reference = header[1].data[0].value;
_context.next = 6;
return this.bam_file.getRecordsForRange(reference, start_site, end_site).then(function (records) {
var fasta = records.map(function (bam_record) {
return _this.bamToFasta(bam_record, start_site, end_site);
});
fasta.number_of_sequences = fasta.length;
fasta.number_of_sites = fasta[0].seq.length;
return fasta;
});
case 6:
return _context.abrupt("return", _context.sent);
case 7:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function fasta_window(_x, _x2) {
return _fasta_window.apply(this, arguments);
}
return fasta_window;
}()
}]);
return BAMReader;
}();
var _default = BAMReader;
exports["default"] = _default;