knitr::opts_knit$set(global.device = TRUE)

This lightning talk will attempt to use a toy dataset to generate covariate-matched, null-hypothesis GRanges with nullranges::matchRanges() and then visualize the results using the BentoBox framework.

Generating covariate-matched GRanges

For this example lets use a utility function to create a simulated dataset:

set.seed(123)
x <- Bioc2021::makeExampleMatchedDataSet(type = "GRanges")
x

This creates a GRanges object with random ranges and 3 metadata colums (feature1, color, and length) that describe "features" of each range.

Let's demonstrate generating a set of matched ranges by finding ranges that are FALSE for feature1 but have the same distributions of color and length.

Our focal set of interest is where feature1 is TRUE, and pool is all other ranges (where feature1 is FALSE):

focal <- x[x$feature1]
pool <- x[!x$feature1]

We can use nullranges::matchRanges() to select a subset from pool that is distributionally matched in color and range length:

library(nullranges)
mgr <- matchRanges(focal = focal,
                   pool = pool,
                   covar = ~color + length,
                   method = 'stratified',
                   replace = FALSE)

mgr

overview can show how successful our matching was:

overview(mgr)

Visualizing with BentoBox

Defining the region to plot:

library(BentoBox)
region <- bbParams(chrom = "chr1", chromstart = 1, chromend = 1000)

We can visualize these ranges by creating a BentoBox page:

bbPageCreate(width = 8.5, height = 6.5, default.units = 'inches')

Visualize the pool GRanges:

poolSet <- bbPlotRanges(data = pool,
                        params = region,
                        x = 1,
                        y = 1,
                        width = 2.5,
                        height = 2.5,
                        fill = pool$color)

bbAnnoGenomeLabel(plot = poolSet,
                  x = 1,
                  y = 3.55)

bbPlotText(label = "Pool Set",
           x = 2.25,
           y = 0.9,
           just = c("center", "bottom"),
           fontcolor = "#33A02C",
           fontface = "bold",
           fontfamily = 'mono')

Visualize the focal GRanges of interest:

focalSet <- bbPlotRanges(data = focal,
                         params = region,
                         x = 5,
                         y = 1,
                         width = 2.5,
                         height = 1,
                         fill = focal$color)

bbAnnoGenomeLabel(plot = focalSet,
                  x = 5,
                  y = 2.05)

bbPlotText(label = "Focal Set",
           x = 6.25,
           y = 0.9,
           just = c("center", "bottom"),
           fontcolor = "#1F78B4",
           fontface = "bold",
           fontfamily = 'mono')

And finally the matched GRanges:

## Matched set
matchedSet <- bbPlotRanges(data = mgr,
                           params = region,
                           x = 5,
                           y = 2.5,
                           width = 2.5,
                           height = 1,
                           fill = mgr$color)

bbAnnoGenomeLabel(plot = matchedSet,
                  x = 5,
                  y = 3.55)

bbPlotText(label = "Matched Set",
           x = 6.25,
           y = 2.75,
           just = c("center", "bottom"),
           fontcolor = "#A6CEE3",
           fontface = "bold",
           fontfamily = 'mono')

Below our GRanges, we can use BentoBox to place ggplots created from the nullranges plotPropensity() and plotCovariate() functions.

First lets plot propensity scores:

library(ggplot2)
smallText <- theme(legend.title = element_text(size=8),
                   legend.text=element_text(size=8),
                   title = element_text(size=8),
                   axis.title.x = element_text(size=8),
                   axis.title.y = element_text(size=8))

## Plot propensity scores ggplot in BentoBox
propensityPlot <-
  plotPropensity(mgr, sets=c('f','m','p')) +
  smallText +
  theme(legend.key.size = unit(0.5, 'lines'),
        title = element_blank())

bbPlotGG(plot = propensityPlot,
          x = 1, y = 4.5, width = 2.5, height = 1.5,
          just = c("left", "top"))

## Text labels
bbPlotText(label = "plotPropensity()",
            x = 1.10, y = 4.24,
            just = c("left", "bottom"),
            fontface = "bold",
            fontfamily = 'mono')
bbPlotText(label = "~color + length",
            x = 1.25, y = 4.5,
            just = c("left", "bottom"),
            fontsize = 10,
            fontfamily = "mono")

plotting color and length covariates:

## Plot "color" covariate
covColor <-
  plotCovariate(x=mgr, covar=covariates(mgr)[1], sets=c('f','m','p')) +
  smallText +
  theme(legend.text = element_blank(),
        legend.position = 'none')

bbPlotGG(plot = covColor,
          x = 3.50, y = 4.5, width = 1.8, height = 1.5,
          just = c("left", "top"))


## Plot "length" covariate
covLength <-
  plotCovariate(x=mgr, covar=covariates(mgr)[2], sets=c('f','m','p'))+
  smallText + 
  theme(legend.key.size = unit(0.5, 'lines'))

bbPlotGG(plot = covLength,
          x = 5.30, y = 4.5, width = 2.75, height = 1.5,
          just = c("left", "top"))


## Text labels
bbPlotText(label = "plotCovariate()",
           x = 3.75,
           y = 4.24,
           just = c("left", "bottom"),
           fontface = "bold",
           fontfamily = "mono")
bbPlotText(label = covariates(mgr),
           x = c(4, 5.9),
           y = 4.5,
           just = c("left", "bottom"),
           fontsize = 10,
           fontfamily = "mono")

When the plot is finished you can remove the page guides:

bbPageGuideHide()

And now your plot is publication-ready!



EricSDavis/Bioc2021 documentation built on Dec. 17, 2021, 7:22 p.m.