---
title: "Unsegmented block bootstrap"
author: "Michael Love"
date: "`r format(Sys.Date(), '%m/%d/%Y')`"
output:
  rmarkdown::html_document:
    highlight: tango
    toc: true
    toc_float: true
vignette: |
  %\VignetteIndexEntry{5. Unsegmented block bootstrap}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

In this vignette, we demonstrate the unsegmented block bootstrap
functionality implemented in *nullranges*. "Unsegmented" refers to
the fact that this implementation does not consider segmentation of
the genome for sampling of blocks, see the segmented block bootstrap
vignette for the alternative implementation.

# Timing on DHS peaks

First we use the DNase hypersensitivity peaks in A549 downloaded from
*AnnotationHub*, and pre-processed as described in the
*nullrangesOldData* package.

```{r}
library(nullrangesData)
dhs <- DHSA549Hg38()
```

```{r}
library(nullranges)
```

The following chunk of code evaluates various types of
bootstrap/permutation schemes, first within chromosome, and then
across chromosome (the default). The default `type` is bootstrap, and
the default for `withinChrom` is `FALSE` (bootstrapping with blocks
moving across chromosomes).

```{r}
set.seed(5) # reproducibility
library(microbenchmark)
blockLength <- 5e5
microbenchmark(
  list=alist(
    p_within=bootRanges(dhs, blockLength=blockLength,
                        type="permute", withinChrom=TRUE),
    b_within=bootRanges(dhs, blockLength=blockLength,
                        type="bootstrap", withinChrom=TRUE),
    p_across=bootRanges(dhs, blockLength=blockLength,
                        type="permute", withinChrom=FALSE),
    b_across=bootRanges(dhs, blockLength=blockLength,
                        type="bootstrap", withinChrom=FALSE)
  ), times=10)
```

# Visualize on synthetic data

We create some synthetic ranges in order to visualize the different
options of the unsegmented bootstrap implemented in *nullranges*.

```{r}
library(GenomicRanges)
seq_nms <- rep(c("chr1","chr2","chr3"),c(4,5,2))
gr <- GRanges(seqnames=seq_nms,
              IRanges(start=c(1,101,121,201,
                              101,201,216,231,401,
                              1,101),
                      width=c(20, 5, 5, 30,
                              20, 5, 5, 5, 30,
                              80, 40)),
              seqlengths=c(chr1=300,chr2=450,chr3=200),
              chr=factor(seq_nms))
```

The following function uses functionality from *plotgardener* to plot
the ranges.  Note in the plotting helper function that `chr` will be
used to color ranges by chromosome of origin.


```{r}
suppressPackageStartupMessages(library(plotgardener))
plotGRanges <- function(gr) {
  pageCreate(width = 5, height = 2, xgrid = 0,
                ygrid = 0, showGuides = FALSE)
  for (i in seq_along(seqlevels(gr))) {
    chrom <- seqlevels(gr)[i]
    chromend <- seqlengths(gr)[[chrom]]
    suppressMessages({
      p <- pgParams(chromstart = 0, chromend = chromend,
                    x = 0.5, width = 4*chromend/500, height = 0.5,
                    at = seq(0, chromend, 50),
                    fill = colorby("chr", palette=palette.colors))
      prngs <- plotRanges(data = gr, params = p,
                          chrom = chrom,
                          y = 0.25 + (i-1)*.7,
                          just = c("left", "bottom"))
      annoGenomeLabel(plot = prngs, params = p, y = 0.30 + (i-1)*.7)
    })
  }
}
```

```{r toyranges, fig.width=5, fig.height=2}
plotGRanges(gr)
```

## Within chromosome

Visualizing two permutations of blocks within chromosome:

```{r perm-within, fig.width=5, fig.height=2}
for (i in 1:2) {
  gr_prime <- bootRanges(gr, blockLength=100, type="permute", withinChrom=TRUE)
  plotGRanges(gr_prime)
}
```

Visualizing two bootstraps within chromosome:

```{r boot-within, fig.width=5, fig.height=2}
for (i in 1:2) {
  gr_prime <- bootRanges(gr, blockLength=100, withinChrom=TRUE)
  plotGRanges(gr_prime)
}
```

## Across chromosome

Visualizing two permutations of blocks across chromosome. Here we use
larger blocks than previously.

```{r perm-across, fig.width=5, fig.height=2}
for (i in 1:2) {
  gr_prime <- bootRanges(gr, blockLength=200, type="permute", withinChrom=FALSE)
  plotGRanges(gr_prime)
}
```

Visualizing two bootstraps across chromosome:

```{r boot-across, fig.width=5, fig.height=2}
for (i in 1:2) {
  gr_prime <- bootRanges(gr, blockLength=200, withinChrom=FALSE)
  plotGRanges(gr_prime)
}
```

# Session information

```{r}
sessionInfo()
```