@jbrowse/plugin-linear-genome-view
Version:
JBrowse 2 linear genome view
46 lines (45 loc) • 1.9 kB
JavaScript
import { getSession, mergeIntervals } from '@jbrowse/core/util';
import { getSnapshot } from '@jbrowse/mobx-state-tree';
import { when } from 'mobx';
export function getExonsAndCDS(transcripts) {
return transcripts.flatMap(transcript => transcript
.get('subfeatures')
?.filter(f => f.get('type') === 'exon' || f.get('type') === 'CDS') ??
[]);
}
export function calculateInitialViewState(regions, viewWidth) {
const totalBp = regions.reduce((sum, r) => sum + (r.end - r.start), 0);
const bpPerPx = totalBp / (viewWidth * 0.9);
const numPaddings = Math.max(0, regions.length - 1);
const interRegionPaddingWidth = 2;
const totalPaddingPx = numPaddings * interRegionPaddingWidth;
const totalContentPx = totalBp / bpPerPx + totalPaddingPx;
const centerPx = totalContentPx / 2;
const offsetPx = Math.round(centerPx - viewWidth / 2);
return { bpPerPx, offsetPx };
}
export async function collapseIntrons({ view, transcripts, assembly, padding, }) {
const r0 = transcripts[0]?.get('refName');
if (!r0) {
return;
}
const refName = assembly.getCanonicalRefName2(r0);
const subs = getExonsAndCDS(transcripts);
const snapshot = getSnapshot(view);
const { id, offsetPx, bpPerPx, ...rest } = snapshot;
const mergedRegions = mergeIntervals(subs.map(f => ({
refName,
start: f.get('start') - padding,
end: f.get('end') + padding,
assemblyName: view.assemblyNames[0],
})), padding);
const initialState = calculateInitialViewState(mergedRegions, view.width);
const newView = getSession(view).addView('LinearGenomeView', {
...rest,
tracks: rest.tracks.map(({ id, ...r }) => r),
displayedRegions: mergedRegions,
bpPerPx: initialState.bpPerPx,
offsetPx: initialState.offsetPx,
});
await when(() => newView.initialized);
}