@jbrowse/plugin-linear-genome-view
Version:
JBrowse 2 linear genome view
147 lines (146 loc) • 6.06 kB
JavaScript
import { getSession, localStorageSetItem, parseLocString, } from '@jbrowse/core/util';
import { addDisposer } from '@jbrowse/mobx-state-tree';
import { autorun, when } from 'mobx';
function tryParseJson(s) {
try {
const v = JSON.parse(s);
return v && typeof v === 'object'
? v
: undefined;
}
catch {
return undefined;
}
}
export function setupInitAutorun(self) {
addDisposer(self, autorun(async function initAutorun() {
const { init, initialized } = self;
if (!initialized) {
return;
}
if (init) {
const session = getSession(self);
const { assemblyManager } = session;
if (init.tracklist) {
self.activateTrackSelector();
const currentWidth = self.volatileWidth;
await when(() => self.volatileWidth !== currentWidth, {
timeout: 500,
}).catch(() => {
});
}
try {
if (init.loc) {
const asm = await assemblyManager.waitForAssembly(init.assembly);
if (!asm) {
throw new Error('Assembly not found');
}
await self.navToSearchString({
input: init.loc,
assembly: asm,
});
}
else {
self.showAllRegionsInAssembly(init.assembly);
}
}
catch (e) {
console.error(init, e);
session.notifyError(`${e}`, e);
}
if (init.tracks) {
const idsNotFound = [];
for (const t of init.tracks) {
try {
if (typeof t === 'string') {
self.showTrack(t);
}
else {
self.showTrack(t.trackId, t.trackSnapshot ?? {}, t.displaySnapshot ?? {});
}
}
catch (e) {
const trackId = typeof t === 'string' ? t : t.trackId;
if (/Could not resolve identifier/.exec(`${e}`)) {
idsNotFound.push(trackId);
}
else {
throw e;
}
}
}
if (idsNotFound.length) {
session.notifyError(`Could not resolve identifiers: ${idsNotFound.join(',')}`, new Error(`Could not resolve identifiers: ${idsNotFound.join(',')}`));
}
}
if (init.nav !== undefined) {
self.setHideHeader(!init.nav);
}
if (self.highlight.length) {
const fallback = self.assemblyNames[0];
if (fallback) {
const normalized = self.highlight.map(h => h.assemblyName ? h : { ...h, assemblyName: fallback });
if (normalized.some((h, i) => h !== self.highlight[i])) {
self.setHighlight(normalized);
}
}
}
if (init.highlight) {
for (const h of init.highlight) {
const json = h.trim().startsWith('{') && tryParseJson(h);
if (json &&
typeof json.refName === 'string' &&
typeof json.start === 'number' &&
typeof json.end === 'number') {
self.addToHighlights({
refName: json.refName,
start: json.start,
end: json.end,
assemblyName: typeof json.assemblyName === 'string'
? json.assemblyName
: init.assembly,
color: typeof json.color === 'string' ? json.color : undefined,
label: typeof json.label === 'string' ? json.label : undefined,
});
}
else {
const p = parseLocString(h, refName => assemblyManager.isValidRefName(refName, init.assembly));
const { start, end } = p;
if (start !== undefined && end !== undefined) {
self.addToHighlights({
...p,
start,
end,
assemblyName: init.assembly,
});
}
}
}
}
self.setInit(undefined);
}
}, { name: 'LGVInit' }));
}
export function setupCoarseDynamicBlocksAutorun(self) {
addDisposer(self, autorun(function coarseDynamicBlocksAutorun() {
if (self.initialized) {
self.setCoarseDynamicBlocks(self.dynamicBlocks);
}
}, { delay: 500, name: 'LGVCoarseDynamicBlocks' }));
}
export function setupLocalStorageAutorun(self) {
addDisposer(self, autorun(function localStorageAutorun() {
const s = (s) => JSON.stringify(s);
const { showCytobandsSetting, showCenterLine, colorByCDS, showTrackOutlines, trackLabels, } = self;
localStorageSetItem('lgv-showCytobands', s(showCytobandsSetting));
localStorageSetItem('lgv-showCenterLine', s(showCenterLine));
localStorageSetItem('lgv-colorByCDS', s(colorByCDS));
localStorageSetItem('lgv-showTrackOutlines', s(showTrackOutlines));
localStorageSetItem('lgv-trackLabels', trackLabels);
}, { name: 'LGVLocalStorage' }));
}
export function doAfterAttach(self) {
setupInitAutorun(self);
setupCoarseDynamicBlocksAutorun(self);
setupLocalStorageAutorun(self);
}