---
title: "The legacy CAGEset class"
author:
 - "Vanja Haberle"
 - "Charles Plessy"
package: CAGEr
output: 
  BiocStyle::html_document:
    toc: true
vignette: >
  %\VignetteIndexEntry{The legacy CAGEset class}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding[utf8]{inputenc}
  %\VignetteEncoding{UTF-8}
---

```{r setup, echo = FALSE, results = "hide"}
options(signif = 3, digits = 3)
knitr::opts_chunk$set(tidy = FALSE, cache = TRUE, autodep = TRUE, fig.height = 5.5,
                      message = FALSE, error = FALSE, warning = TRUE)
set.seed(0xdada)
```

This document reproduces the commands used in older versions of the CAGEr
vignette that were based on the legacy `CAGEset` class.  Its purpose is to check
that the results of the computations are identical with the old documentation
and with the current documentation based on the new `CAGEexp` class.

```{r}
library(CAGEr)
library(BSgenome.Drerio.UCSC.danRer7)
```

```{r}
inputDir <- system.file("extdata", package = "CAGEr")
pathsToInputFiles <- list.files(inputDir, pattern = "ctss", full.names = TRUE)
basename(pathsToInputFiles)
```

```{r}
myCAGEset <- new("CAGEset", genomeName = "BSgenome.Drerio.UCSC.danRer7",
  inputFiles = pathsToInputFiles, inputFilesType = "ctss",
  sampleLabels = c("zf_30p_dome", "zf_high", 
  "zf_prim6_rep1", "zf_prim6_rep2", "zf_unfertilized_egg"))
myCAGEset
TSS.df <- read.table(system.file("extdata/Zf.unfertilized.egg.chr17.ctss", 
  package = "CAGEr"))
colnames(TSS.df) <- c("chr", "pos", "strand", "zf_unfertilized_egg")
TSS.df$chr <- as.character(TSS.df$chr)
TSS.df$pos <- as.integer(TSS.df$pos)
TSS.df$strand <- as.character(TSS.df$strand)
TSS.df$zf_unfertilized_egg <- as.integer(TSS.df$zf_unfertilized_egg)
head(TSS.df)
```

```{r}
myCAGEset.coerced <- as(TSS.df, "CAGEset")
myCAGEset.coerced
```

```{r}
getCTSS(myCAGEset)
ctss <- CTSStagCount(myCAGEset)
head(ctss)
sampleLabels(myCAGEset)
```

```{r}
corr.m <- plotCorrelation(myCAGEset, samples = "all", method = "pearson")
```

```{r}
mergeSamples(myCAGEset, mergeIndex = c(3,2,4,4,1), 
  mergedSampleLabels = c("zf_unfertilized_egg", 
  "zf_high", "zf_30p_dome", "zf_prim6"))
```

```{r}
librarySizes(myCAGEset)
plotReverseCumulatives(myCAGEset, fitInRange = c(5, 1000), onePlot = TRUE)
normalizeTagCount(myCAGEset, method = "powerLaw", 
  fitInRange = c(5, 1000), alpha = 1.2, T = 5*10^4)
exportCTSStoBedGraph(myCAGEset, values = "normalized", 
  format = "bedGraph", oneFile = TRUE)
exportCTSStoBedGraph(myCAGEset, values = "normalized", format = "BigWig")
```

```{r}
clusterCTSS(object = myCAGEset, threshold = 1, thresholdIsTpm = TRUE, 
  nrPassThreshold = 1, method = "distclu", maxDist = 20, 
  removeSingletons = TRUE, keepSingletonsAbove = 5)
tc <- tagClusters(myCAGEset, sample = "zf_unfertilized_egg")
head(tc)
```

```{r}
cumulativeCTSSdistribution(myCAGEset, clusters = "tagClusters")
quantilePositions(myCAGEset, clusters = "tagClusters", qLow = 0.1, qUp = 0.9)
tc <- tagClusters(myCAGEset, sample = "zf_unfertilized_egg",
  returnInterquantileWidth = TRUE,  qLow = 0.1, qUp = 0.9)
head(tc)
exportToBed(object = myCAGEset, what = "tagClusters", 
  qLow = 0.1, qUp = 0.9, oneFile = TRUE)
plotInterquantileWidth(myCAGEset, clusters = "tagClusters", 
  tpmThreshold = 3, qLow = 0.1, qUp = 0.9)
```

```{r}
aggregateTagClusters(myCAGEset, tpmThreshold = 5, 
  qLow = 0.1, qUp = 0.9, maxDist = 100)
consensusCl <- consensusClusters(myCAGEset)
head(consensusCl)
cumulativeCTSSdistribution(myCAGEset, clusters = "consensusClusters",
  useMulticore = TRUE)
quantilePositions(myCAGEset, clusters = "consensusClusters", qLow = 0.1,
  qUp = 0.9, useMulticore = TRUE)
consensusCl <- consensusClusters(myCAGEset, sample = "zf_unfertilized_egg",
  returnInterquantileWidth = TRUE,  qLow = 0.1, qUp = 0.9)
head(consensusCl)
```

```{r}
getExpressionProfiles(myCAGEset, what = "consensusClusters", tpmThreshold = 10, 
  nrPassThreshold = 1, method = "som", xDim = 4, yDim = 2)
plotExpressionProfiles(myCAGEset, what = "consensusClusters")
class3_1 <- extractExpressionClass(myCAGEset, 
  what = "consensusClusters", which = "3_1")
head(class3_1)
```

```{r}
exportToBed(myCAGEset, what = "consensusClusters", 
  colorByExpressionProfile = TRUE)
cumulativeCTSSdistribution(myCAGEset, clusters = "consensusClusters")
```

```{r}
scoreShift(myCAGEset, groupX = "zf_unfertilized_egg", groupY = "zf_prim6",
  testKS = TRUE, useTpmKS = FALSE)
shifting.promoters <- getShiftingPromoters(myCAGEset, 
  tpmThreshold = 5, scoreThreshold = 0.6,
  fdrThreshold = 0.01)
head(shifting.promoters)
```

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