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/
89 lines (71 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.fastaToText = fastaToText;
exports.fnaParser = fnaParser;
exports.fnaToText = fnaToText;
exports["default"] = void 0;
var _phylotree = require("phylotree");
var _jointSort = _interopRequireDefault(require("./jointSort.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function fastaParser(fasta) {
if (_typeof(fasta) == "object") return fasta;
var seqs = [],
header,
in_header,
start,
seq;
for (var i = 0; i < fasta.length; i++) {
if (fasta[i] == ">" || i == fasta.length - 1) {
if (header) {
seq = fasta.slice(start, i).replace(/\s/g, "");
seqs.push({
header: header,
seq: seq
});
}
in_header = true;
start = i + 1;
}
if (fasta[i] == "\n" && in_header) {
in_header = false;
header = fasta.slice(start, i);
start = i + 1;
}
}
seqs.number_of_sequences = seqs.length;
seqs.number_of_sites = seqs[0].seq.length;
return seqs;
}
function fastaToText(fasta) {
return fasta.map(function (entry) {
return ">" + entry.header + "\n" + entry.seq;
}).join("\n") + "\n";
}
function fnaParser(fna, sortFASTA) {
var i = fna.length - 2,
current_char = fna[i];
while (current_char != "\n") {
i--;
current_char = fna[i];
}
var sequence_data = fastaParser(fna.slice(0, i + 1)),
newick = fna.slice(i + 1, fna.length - 1),
tree = new _phylotree.phylotree(newick);
if (sortFASTA) {
(0, _jointSort["default"])(sequence_data, tree);
}
return {
sequence_data: sequence_data,
tree: tree
};
}
function fnaToText(fna) {
return fna.sequence_data.map(function (record) {
return ">" + record.header + "\n" + record.seq;
}).join("\n") + "\n" + fna.tree.newick_string;
}
var _default = fastaParser;
exports["default"] = _default;