@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
78 lines (77 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.stitch = stitch;
exports.dedupe = dedupe;
exports.revlist = revlist;
exports.calculateUTRs = calculateUTRs;
exports.calculateUTRs2 = calculateUTRs2;
exports.ellipses = ellipses;
exports.replaceUndefinedWithNull = replaceUndefinedWithNull;
exports.formatSubfeatures = formatSubfeatures;
function stitch(subfeats, sequence) {
return subfeats.map(sub => sequence.slice(sub.start, sub.end)).join('');
}
function getItemId(feat) {
return `${feat.start}-${feat.end}`;
}
function dedupe(list) {
return list.filter((item, pos, ary) => !pos || getItemId(item) !== getItemId(ary[pos - 1]));
}
function revlist(list, seqlen) {
return list
.map(sub => ({
...sub,
start: seqlen - sub.end,
end: seqlen - sub.start,
}))
.sort((a, b) => a.start - b.start);
}
function calculateUTRs(cds, exons) {
if (!cds.length) {
return [];
}
const firstCds = cds.at(0);
const lastCds = cds.at(-1);
const firstCdsIdx = exons.findIndex(exon => exon.end >= firstCds.start && exon.start <= firstCds.start);
const lastCdsIdx = exons.findIndex(exon => exon.end >= lastCds.end && exon.start <= lastCds.end);
const lastCdsExon = exons[lastCdsIdx];
const firstCdsExon = exons[firstCdsIdx];
const fiveUTRs = [
...exons.slice(0, firstCdsIdx),
{ start: firstCdsExon.start, end: firstCds.start },
].map(elt => ({ ...elt, type: 'five_prime_UTR' }));
const threeUTRs = [
{ start: lastCds.end, end: lastCdsExon.end },
...exons.slice(lastCdsIdx + 1),
].map(elt => ({ ...elt, type: 'three_prime_UTR' }));
return [...fiveUTRs, ...threeUTRs];
}
function calculateUTRs2(cds, parentFeat) {
if (!cds.length) {
return [];
}
const firstCds = cds.at(0);
const lastCds = cds.at(-1);
const fiveUTRs = [{ start: parentFeat.start, end: firstCds.start }].map(elt => ({ ...elt, type: 'five_prime_UTR' }));
const threeUTRs = [{ start: lastCds.end, end: parentFeat.end }].map(elt => ({
...elt,
type: 'three_prime_UTR',
}));
return [...fiveUTRs, ...threeUTRs];
}
function ellipses(slug) {
return slug.length > 20 ? `${slug.slice(0, 20)}...` : slug;
}
function replaceUndefinedWithNull(obj) {
return JSON.parse(JSON.stringify(obj, (_, v) => (v === undefined ? null : v)));
}
function formatSubfeatures(obj, depth, parse, currentDepth = 0, returnObj = {}) {
var _a;
if (depth <= currentDepth) {
return;
}
(_a = obj.subfeatures) === null || _a === void 0 ? void 0 : _a.map(sub => {
formatSubfeatures(sub, depth, parse, currentDepth + 1, returnObj);
parse(sub);
});
}