Export R script
The linear genome view's Export image... dialog offers R beside SVG and
PNG. It downloads jbrowse.R, a short script that redraws the tracks open in
the view, over the regions on screen, in R:
- Each track is one call to ggjbrowse, which runs JBrowse's own data engine inside R and reads the track with the settings it has in the view.
- The figure is one call,
jbrowse_stack(), which stacks the tracks as the view does.source()the script and the figure appears;Rscriptwritesfigure.png.
library(ggjbrowse)
locus <- "ctgA:1,001-9,000"
# GFF3Tabix genes
genes <- jbrowse_features("https://jbrowse.org/code/jb2/main/test_data/volvox/volvox.sort.gff3.gz",
locus,
display = list(heightMode = "fit", height = 100),
width = 1588)
# volvox-sorted.bam
reads <- jbrowse_alignments("https://jbrowse.org/code/jb2/main/test_data/volvox/volvox-sorted.bam",
locus,
display = list(color = list(field = "mapq")))
fig <- jbrowse_stack("GFF3Tabix genes" = genes,
"volvox-sorted.bam" = reads)
print(fig)Each reader returns the tables its track draws, and every band is an ordinary
ggplot2 plot:
- One band: draw it alone with
autoplot(reads, "pileup")and add ageom_*, a scale or a theme - Your own data: join your table to
genes$features - Labels: a feature track exports at the height the view gives it, so it
drops the labels the view drops; change
"fit"to"grow"to draw them all
Supported tracks
The script draws feature tracks (GFF3, GTF, BED, BigBed), variant tracks and multi-sample variant matrices (VCF), quantitative tracks (BigWig, multi-BigWig) and alignments tracks (BAM, CRAM). Several regions on screen draw end to end, as the view lays them out.
Tracks and settings left out
A track ggjbrowse cannot read yet, such as an arc or GWAS track, and a setting it cannot draw, such as facets on a feature track, go in a comment block at the top of the script that lists where the figure differs from the view.
Data file paths
The script opens each track by its path or URL, so every track needs one an R session can open.
- Local files on the web: a track opened with Choose File in jbrowse-web has neither, so the exporter leaves it out and names it in the header. JBrowse Desktop opens files by path, which the script uses directly.
- Indexes: a file over HTTP needs its index beside it at the usual name, or the track's declared index, which the script passes along.
- Contig names: a contig the assembly calls
chr1and the file calls1reads under the file's name, from the assembly's aliases.
Requirements
R with ggjbrowse 0.12.0 or later:
remotes::install_github("GMOD/ggjbrowse@v0.12.0") installs the release the
script is tested against. It brings ggplot2, patchwork and V8; no
Bioconductor packages are needed. The script stops with that install line if the
installed ggjbrowse is older.
Differences from the view
The data, rows, colors and keys are the browser's own, computed by the same engine. The text, antialiasing and panel spacing are ggplot2's.