@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
96 lines (95 loc) • 2.84 kB
JavaScript
export function stitch(subfeats, sequence) {
return subfeats.map(sub => sequence.slice(sub.start, sub.end)).join('');
}
function getItemId(feat) {
return `${feat.start}-${feat.end}`;
}
export function filterSuccessiveElementsWithSameStartAndEndCoord(list) {
return list.filter((item, pos, ary) => !pos || getItemId(item) !== getItemId(ary[pos - 1]));
}
export function revlist(list, seqlen) {
return list
.map(sub => ({
...sub,
start: seqlen - sub.end,
end: seqlen - sub.start,
}))
.sort((a, b) => a.start - b.start);
}
export function calculateUTRs(cds, exons) {
if (!cds.length) {
return [];
}
if (exons.length < cds.length) {
console.warn('exons.length less than cds.length, cant calculate UTR properly', { exons, cds });
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 fivePrimeUTRs = [
...exons.slice(0, firstCdsIdx),
{
start: firstCdsExon.start,
end: firstCds.start,
},
].map(elt => ({
...elt,
type: 'five_prime_UTR',
}));
const threePrimeUTRs = [
{
start: lastCds.end,
end: lastCdsExon.end,
},
...exons.slice(lastCdsIdx + 1),
].map(elt => ({
...elt,
type: 'three_prime_UTR',
}));
return [...fivePrimeUTRs, ...threePrimeUTRs];
}
export function calculateUTRs2(cds, parentFeat) {
if (!cds.length) {
return [];
}
const firstCds = cds.at(0);
const lastCds = cds.at(-1);
const fivePrimeUTRs = [
{
start: parentFeat.start,
end: firstCds.start,
},
].map(elt => ({
...elt,
type: 'five_prime_UTR',
}));
const threePrimeUTRs = [
{
start: lastCds.end,
end: parentFeat.end,
},
].map(elt => ({
...elt,
type: 'three_prime_UTR',
}));
return [...fivePrimeUTRs, ...threePrimeUTRs];
}
export function ellipses(slug) {
return slug.length > 20 ? `${slug.slice(0, 20)}...` : slug;
}
export function replaceUndefinedWithNull(obj) {
return JSON.parse(JSON.stringify(obj, (_, v) => (v === undefined ? null : v)));
}
export function formatSubfeatures(obj, depth, parse, currentDepth = 0, returnObj = {}) {
if (depth <= currentDepth) {
return;
}
obj.subfeatures?.map(sub => {
formatSubfeatures(sub, depth, parse, currentDepth + 1, returnObj);
parse(sub);
});
}