Capturing a JBrowse view from a script
An agent that can see what it built recovers from mistakes a validator cannot
catch: an empty track, a refName mismatch, a region with no data. Inside JBrowse
Desktop the MCP screenshot tool already waits and reports for you. From a
script there are two tools:
@jbrowse/img |
@jbrowse/capture |
|
|---|---|---|
| how | server-side React, no browser | Puppeteer against a real instance |
| output | SVG, PNG or PDF | PNG, JPEG or WebP |
| speed | fast, no Chromium download | slower, launches a browser |
| shows | the tracks | the whole app: chrome, menus, dialogs, overview ideogram |
| covers | the SVG-export rendering path | canvas and WebGPU rendering, exactly as a user sees it |
- Use
@jbrowse/imgby default; most requests are just a static figure of some tracks. - Use
@jbrowse/capturewhen the result depends on the real application: a canvas-rendered display, a dialog or menu, or state you want to click into and read back.
## a static render
npx @jbrowse/img --genome hg38 --track hg38-ncbiRefSeqCurated --loc BRCA1 --out brca1.png
## the real app
npx @jbrowse/capture --genome hg38 --track hg38-ncbiRefSeqCurated --loc BRCA1 -o brca1.png
## your own config, or a whole session spec
npx @jbrowse/capture --config https://example.org/config.json --assembly mygenome \
--loc "chr3:25,325,000-25,361,000" --track my_track -o out.png
npx @jbrowse/capture --genome hg38 --spec spec.json -o out.png--genomenames a hosted assembly,--configany config URL, and--loctakes a gene name on a config with a text index.@jbrowse/capturedrives the public JBrowse Web build, so a config and its data have to be URLs that page may fetch.--instance http://localhost:3000points it at a build of your own, served by anything that honorsRangerequests (npx servedoes,python3 -m http.serverdoes not).--jsonprints the result as one object: the URL, what did not settle, and the session asjb.sessionSummary()describes it, including the region a gene name landed on and any track's error or notice.- To change the view and look again without a new browser each time, use the
library: the page
openJBrowsereturns publisheswindow.jb, so Driving the live JBrowse session and Recipes for driving JBrowse from an agent apply throughpage.evaluate. The package README has the loop.
Knowing when the render is done
JBrowse loads a config, builds a session, resolves an assembly, fetches each track, and then draws to a canvas. A screenshot taken before the last step is a plausible picture of an empty browser, so nothing downstream flags it.
For a page that is loading, wait on one selector:
await page.waitForSelector('[data-app-phase="ready"]')- The session renders that itself:
readywhen no view is resolving an assembly and no display is fetching. It is positive, so it cannot be true before the app exists. @jbrowse/capturegates it further on the assembly and track ids the app publishes beside the marker, so a config URL that 404s, atrackIdthe config does not define and an assembly name that does not match all fail there rather than photographing an empty browser.- After a click the app is already
readyand stays that way until the click's work registers, so a wait forreadyreturns at once on the pre-click frame.waitForAppSettled(page)requiresreadyto hold for a beat.
For one display rather than the whole app:
data-testidnames the display type (pileup-display,wiggle-display) and never changes;data-display-idnames the individual display.- Readiness is a separate attribute:
[data-display-phase="ready"]once its fetch is done,[data-display-drawn="true"]once it has painted. displaySettledanddisplayPaintedin@jbrowse/capturebuild the combined selectors.- A dotplot and a synteny level publish no phase, so they report through
data-display-drawnalone, andpendingin the capture result names anything that had still not painted.
Practical notes
- The browser renders in software unless you give it a GPU. A view that is slow or fails on volume under headless Chromium may be fine in a real one, so do not conclude anything about JBrowse's limits from a headless run.
- In a container, pass nothing;
@jbrowse/capturealready launches Chromium with--no-sandbox. - A retina image is
--dpr 2, the default. Drop it to 1 for a screenshot you only intend to read. - A slow remote file outlives the default budget. Raise
--timeout. - Read the image you produced. An empty track is obvious in a picture and invisible in an exit code.