---
title: "Reading Data for plotgardener"
output:
    rmarkdown::html_vignette
vignette: >
    %\VignetteIndexEntry{Reading Data for plotgardener}
    %\VignetteEngine{knitr::rmarkdown}
    %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
    fig.align = "center",
    fig.width = 3,
    fig.height = 3,
    collapse = TRUE,
    comment = "#>",
    warning = FALSE,
    message = FALSE
)
library(grid)
library(plotgardener)
library(plotgardenerData)
```

`plotgardener` handles a wide array of genomic data types in various formats 
and file types. Not only does it work with `data.frames`, `data.tables`, 
`tibbles`, and Bioconductor `GRanges` and `GInteractions` objects, but 
it can also read in common genomic file types like BED, BEDPE, bigWig, 
and .hic files. While files can be read directly into `plotgardener` plotting 
functions, `plotgardener` also provides functions for reading in these large 
genomic data sets to work with them within the R environment:

- `readBigwig()`: Read in entire bigWig files, or read in specific 
genomic regions or strands of bigWig data. Please note that this function does
not work on Windows.
```{r eval=TRUE, echo=TRUE, message=FALSE}
bwFile <- system.file("extdata/test.bw", package="plotgardenerData")

## Read in entire file
bwFileData <- readBigwig(file = bwFile)

## Read in specified region
bwRegion <- readBigwig(file = bwFile,
                        chrom = "chr2",
                        chromstart = 1,
                        chromend = 1500)
     
## Read in specified region on "+" strand
bwRegionPlus <- readBigwig(file = bwFile,
                            chrom = "chr2",
                            chromstart = 1, 
                            chromend = 1500,
                            strand = "+")
```

The resulting file will contain `seqnames`, `start`, `end`, `width`, 
`strand`, and `score` columns:
```{r eval=TRUE, echo=TRUE}
head(bwRegion)
```

- `readHic()`: Read in genomic regions of .hic files with various 
data resolutions and normalizations.
```{r eval=TRUE, echo=TRUE, message=FALSE}
hicFile <- system.file("extdata/test_chr22.hic", package="plotgardenerData")

hicDataChrom <- readHic(file = hicFile,
    chrom = "22", assembly = "hg19",
    resolution = 250000, res_scale = "BP", norm = "NONE"
)

hicDataChromRegion <- readHic(file = hicFile,
    chrom = "22", assembly = "hg19",
    chromstart = 20000000, chromend = 47500000,
    resolution = 100000, res_scale = "BP", norm = "KR"
)

```

These data will be output in 3-column dataframe in sparse upper triangular 
matrix format:

```{r eval=TRUE, echo=TRUE}
head(hicDataChromRegion)
```

It is also possible to use `readHic` for interchromosomal Hi-C data:

```{r eval=FALSE, echo=TRUE, message=FALSE}
twoChroms <- readHic(file = "/path/to/hic",
    chrom = "chr1", altchrom = "chr2",
    resolution = 250000, res_scale = "BP"
)
```

For other filetypes, we recommend reading in files with `data.table` 
or `rtracklayer`.
```{r eval=FALSE, echo=TRUE, message=FALSE}
library(data.table)
data <- data.table::fread("/path/to/file")

library(rtracklayer)
data <- rtracklayer::import(con = "/path/to/file", format = "fileFormat")
```

### Session Info
```{r}
sessionInfo()
```
