Skip to content

Commit fc6cdc4

Browse files
jrobinsoclaude
andcommitted
Keep the browser igv.js leaves on a failed start
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 3c8f813 commit fc6cdc4

2 files changed

Lines changed: 17 additions & 91 deletions

File tree

‎CLAUDE.md‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ Key points that span files:
8181

8282
- **Browser handle.** `js/globals.js` is a one-property module (`Globals.browser`) used by code that runs outside the
8383
widget factories (e.g. `shareHelper.js`). Prefer passing `browser` explicitly; `Globals` is the escape hatch.
84-
- **Startup error recovery.** `main()` keeps a copy of `igvConfig` from before the last-used genome and the query
85-
parameters are applied to it (`igv.createBrowser` merges the query parameters into the config it is given). If
86-
`createBrowser` fails, it retries from that copy with the session and any restored or query genome dropped, then,
87-
if the configured genome itself is what failed (recognized by igv.js's "Genome ... could not be loaded" message),
88-
once more with the first other genome from the genome list, so the Genome menu stays usable. Each attempt goes
89-
through `tryCreateBrowser`, which calls `igv.removeAllBrowsers()` on failure in case a partially constructed
90-
instance was left in the DOM. Preserve this when touching startup.
84+
- **Startup error recovery.** When `igv.createBrowser` fails, igv.js leaves the browser in the page and on
85+
`error.browser`, with an empty genome (`browser.genome.isEmpty`) if the session or the genome itself failed.
86+
`main()` reports the error and continues with that browser, so the Genome menu is available to pick another genome.
87+
There is deliberately no retry with a different genome. Preserve this when touching startup.
9188
- **Locus vs. session.** igv.js gives a session's own locus precedence over the `locus` query parameter, so
9289
`loadSessionWithLocusOverride` pre-loads the session named by `sessionURL`/`session`/`hubURL`, patches the locus, and
9390
hands the merged config to `createBrowser`. XML sessions additionally need `restoreXMLSessionGenome`, because without

‎js/app.js‎

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,11 @@ async function main(container, config) {
117117
// Initialize igv.js, creating the Browser instance
118118
const igvConfig = config.igvConfig
119119

120-
// The configuration as given, before the last genome used is applied to it, and before igv.createBrowser merges
121-
// the query parameters into it. A failed start is retried from this copy.
122-
const configuredIgvConfig = {...igvConfig, tracks: [...(igvConfig.tracks || [])], queryParametersSupported: false}
123-
124120
// Optionally change the igv genome to the last genome used.
125121
if (config.restoreLastGenome) {
126122
try {
127123
const lastGenomeId = localStorage.getItem("genomeID")
128-
if (lastGenomeId && lastGenomeId !== configuredIgvConfig.genome) {
124+
if (lastGenomeId && lastGenomeId !== igvConfig.genome) {
129125
if (config.genomes && config.genomes.find(elem => elem.id === lastGenomeId) ||
130126
(recentGenomes && recentGenomes.find(elem => elem.id === lastGenomeId)) ||
131127
((lastGenomeId.startsWith("GCA_") || lastGenomeId.startsWith("GCF_")) && lastGenomeId.length >= 13)) {
@@ -141,57 +137,25 @@ async function main(container, config) {
141137
// session is patched before the browser is created, so it opens at the requested locus directly.
142138
const sessionConfig = await loadSessionWithLocusOverride(igvConfig)
143139

144-
let browserConfig = sessionConfig || igvConfig
145-
let {browser, error} = await tryCreateBrowser(container, browserConfig)
146-
147-
if (error) {
148-
// A session is either merged into sessionConfig, or, when it was left to igv.js to load, visible in
149-
// igvConfig, as is a genome from the query parameters (as genome, reference, or genarkAccession).
150-
const failedSession = sessionConfig || igvConfig.sessionURL || igvConfig.session || igvConfig.hubURL
151-
const failedOtherGenome = ['genome', 'reference', 'genarkAccession'].some(key => igvConfig[key] !== configuredIgvConfig[key])
152-
153-
if (failedSession) {
154-
// The session was explicitly requested, so report the failure, then continue without it.
155-
alertSingleton.present(error)
156-
}
157-
158-
if (failedSession || failedOtherGenome) {
159-
// Retry with the genome from the configuration file, and no session.
160-
browserConfig = {...configuredIgvConfig}
161-
delete browserConfig.sessionURL
162-
delete browserConfig.session
163-
delete browserConfig.hubURL
164-
;({browser, error} = await tryCreateBrowser(container, browserConfig))
165-
}
166-
}
167-
168-
if (error) {
169-
// The genome from the configuration file failed to load. Report it, and load the first other genome from the
170-
// genome list, so the app is usable and another genome can be chosen from the menu. Any other failure ends
171-
// the startup.
172-
const fallbackGenome = isGenomeLoadError(error) && config.genomes ?
173-
config.genomes.find(({id}) => id !== configuredIgvConfig.genome) :
174-
undefined
175-
if (!fallbackGenome) {
176-
alertSingleton.present(error)
177-
return
178-
}
179-
const genomeError = error
180-
browserConfig = {...configuredIgvConfig, genome: fallbackGenome.id, tracks: []}
181-
delete browserConfig.reference
182-
delete browserConfig.genarkAccession
183-
;({browser, error} = await tryCreateBrowser(container, browserConfig))
184-
if (error) {
185-
alertSingleton.present(error)
140+
// igv.createBrowser leaves the browser in the page when it fails, on error.browser. Report the failure and
141+
// continue with that browser: if the failure came before a genome was set (the session, or the genome itself) it
142+
// has an empty genome, and a genome can be chosen from the Genome menu.
143+
let browser
144+
try {
145+
browser = await igv.createBrowser(container, sessionConfig || igvConfig)
146+
} catch (error) {
147+
console.error(error)
148+
alertSingleton.present(error)
149+
browser = error.browser
150+
if (!browser) {
186151
return
187152
}
188-
alertSingleton.present(new Error(`${genomeError.message}\n\nLoaded ${fallbackGenome.name || fallbackGenome.id} instead.`))
189153
}
190154
Globals.browser = browser
191155

192156
if (!sessionConfig) {
193157
// The session was loaded by igv.js, which ignores the locus parameter in favor of the session's own locus.
194-
await applyLocusParameter(browser, browserConfig)
158+
await applyLocusParameter(browser, igvConfig)
195159
}
196160

197161
const igvMain = document.getElementById('igv-main')
@@ -518,41 +482,6 @@ async function applyLocusParameter(browser, igvConfig) {
518482
}
519483
}
520484

521-
/**
522-
* Create the igv.js browser, returning it as {browser}, or the error it failed with as {error}.
523-
*/
524-
async function tryCreateBrowser(container, igvConfig) {
525-
try {
526-
return {browser: await igv.createBrowser(container, igvConfig)}
527-
} catch (error) {
528-
console.error(error)
529-
removeOrphanedBrowsers()
530-
return {error}
531-
}
532-
}
533-
534-
/**
535-
* Whether an igv.createBrowser failure is the genome failing to load, as opposed to a session, sample info, or
536-
* anything else. igv.js reports a genome failure with a message that leads with the genome: "Genome hg38 could not be
537-
* loaded: ..." or "The genome could not be loaded.", and a genome id it does not know with "Unknown genome id: ...".
538-
*/
539-
function isGenomeLoadError(error) {
540-
return /^((Genome .*|The genome) could not be loaded|Unknown genome id)/.test(error.message || '')
541-
}
542-
543-
/**
544-
* Remove any igv.js browser instances left behind by a failed igv.createBrowser call. createBrowser inserts the
545-
* browser into the DOM before loading the session, genome, and tracks, so a load failure leaves a non-functional
546-
* instance in the container with no handle returned to the caller.
547-
*/
548-
function removeOrphanedBrowsers() {
549-
try {
550-
igv.removeAllBrowsers()
551-
} catch (e) {
552-
console.error(e)
553-
}
554-
}
555-
556485
function createAppBookmarkHandler($bookmark_button) {
557486

558487
$bookmark_button.on('click', (e) => {

0 commit comments

Comments
 (0)