---
title: "esATAC Report"
author: "Zheng Wei and Wei Zhang"
date: "`r Sys.Date()`"
output: 
    html_document:
        df_print: paged
        toc: true
        toc_float: true
        number_sections: true
        fig_height: 3.5
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r eval=TRUE, echo=TRUE,message=FALSE}
#load("Report.Rdata")
library(esATAC)
loadConfig(file.path("ReportData","SummaryInfo","config.rds"))
suminfo <- readRDS(file.path("ReportData","SummaryInfo","suminfo.rds"))
allsteps <- suminfo[["prevSteps"]]
wholesummary <- suminfo[["wholesummary"]]
filtstat <- suminfo[["filtstat"]]
```

# Summary Table

Sequence files below are set as inputs of the pipeline. 

```{r cols.print=3, eval=TRUE, echo=FALSE,warning=FALSE}

report(allsteps[["UnzipAndMerge"]])$filelist
```

Summerized infomation on sequence files has been shown showed below. You can see details in later sections

```{r eval=TRUE, echo=FALSE,warning=FALSE}
knitr::kable(x = wholesummary)
```

A nucleosome free region (NFR) must be present.
A mononucleosome peak must be present in the fragment length distribution. These are reads that span a single nucleosome, so they are longer than 147 bp but shorter than 147*2 bp. Good ATAC-seq datasets have reads that span nucleosomes (which allows for calling nucleosome positions in addition to open regions of chromatin).

```{r eval=TRUE,echo=FALSE}
library(ggplot2)
readsCounts<-report(allsteps$FragLenDistr)[["readsCounts"]]
ggplot(readsCounts[1:1000,], aes(length,counts))+geom_path(color="Red")+xlab("Fragment length (bp)")+ylab("Read counts") + theme_bw() + theme(panel.grid =element_blank()) + geom_vline(xintercept = c(147,294), linetype=2) + annotate("text", x = 147, y = max(readsCounts[1:1000,2]),label="147bp") + annotate("text", x = 147*2, y = max(readsCounts[1:1000,2]),label="147bp*2")+ labs(title = "Fragment Length Distribution") + theme(plot.title = element_text(hjust = 0.5))
```



# Sequence Statistics
## FastQC

Quality control for the sequence data
```{r eval=TRUE, echo=FALSE, warning=FALSE}
QC_path <- report(allsteps$FastQC)[["pdf"]]
```
[Click to Visit Report](`r basename(QC_path)`)

## Remove adapter

The adapter sequence are shown below. For paired end reads, if adapters were not setted, the adapters below are identified by AdapterRemoval.

```{r cols.print=2, rows.print=20, eval=TRUE, echo=FALSE,warning=FALSE}
knitr::kable(report(allsteps$RemoveAdapter)[["adapters"]])

```

The statistic of adapter removing are show below. 

```{r cols.print=2, rows.print=20, eval=TRUE, echo=FALSE,warning=FALSE}
knitr::kable(report(allsteps$RemoveAdapter)[["statistics"]])
```

For detail, you can visit [Website of AdapterRemoval on Github](https://github.com/MikkelSchubert/adapterremoval).

# Reads Alignment Statistics

## Bowtie2 alignment log

```{r eval=TRUE, echo=FALSE,warning=FALSE}
report(allsteps$Bowtie2Mapping)[["detail"]]
```

## Library complexity

```{r cols.print=2, eval=TRUE, echo=FALSE,warning=FALSE}
knitr::kable(x = report(allsteps$LibComplexQC)[["table"]])
```
The annotation you can see in section 1.

## Filtering statistics

```{r cols.print=2, eval=TRUE, echo=FALSE,warning=FALSE}
knitr::kable(x = filtstat)
```


## Fragment size distribution

```{r fegdist, eval=TRUE,message=FALSE,warning=FALSE}
library(ggplot2)

readsCounts<-report(allsteps$FragLenDistr)[["readsCounts"]]
ggplot(readsCounts[1:1000,], aes(length,counts))+geom_path(color="Red")+xlab("Fragment length (bp)")+ylab("Read counts") + theme_bw() + theme(panel.grid =element_blank()) + labs(title = "Fragment Length Distribution") + theme(plot.title = element_text(hjust = 0.5))

library(stats)
strength<-Mod(fft(readsCounts$counts))/length(readsCounts$counts)
periodx<-length(readsCounts$counts)/(1:(length(readsCounts$counts)-1))
strength<-strength[2:length(strength)]

rs1<-as.data.frame(cbind(periodx[periodx<20&periodx>2],strength[periodx<20&periodx>2],0))
rs2<-as.data.frame(cbind(periodx[periodx<400&periodx>2],strength[periodx<400&periodx>2],1))
rs<-rbind(rs1,rs2)
colnames(rs)<-c("period","strength","check")

g1<-ggplot(rs[rs["check"]==0,]) + geom_vline(xintercept = 10.4, linetype=2)+ geom_line(aes(x=period,y=strength),color="Red")+ theme_bw() + theme(panel.grid =element_blank()) + annotate("text", x = 10.4, y = max(rs[rs["check"]==0,2]), label = "10.4bp") +xlab("period") + ylab("strength") + labs(title = "the Pitch of the DNA Helix") + theme(plot.title = element_text(hjust = 0.5))

g2<-ggplot(rs[rs["check"]==1,]) + geom_vline(xintercept = 186, linetype=2)+ geom_line(aes(x=period,y=strength),color="Red")+ theme_bw() + theme(panel.grid =element_blank()) + annotate("text", x = 186, y = max(rs[rs["check"]==1,2]), label = "186bp") +xlab("period") + ylab("strength") + labs(title = "Nucleosome") + theme(plot.title = element_text(hjust = 0.5))
library(gridExtra)
grid.arrange(g1, g2, ncol=2)
```


## TSS enrichment
The nucleosome free reads (<100bp) and monnucleosome span reads (180~247bp) enrichment around transcription starting site (TSS) are shown below.
```{r TSSenrich, eval=TRUE}
library(ggplot2)
df<-report(allsteps$TSSQCNFR)[["tss"]]
g1<-ggplot(df,aes(pos,counts))+geom_line()+ geom_vline(xintercept = 0, linetype=2)+xlab("upstream<-TSS->downstream")+ylab("reads count")+theme_bw() + theme(panel.grid =element_blank()) + labs(title = "Nucleosome Free Reads") + theme(plot.title = element_text(hjust = 0.5))
df<-report(allsteps$TSSQCneucleosome)[["tss"]]
g2<-ggplot(df,aes(pos,counts))+geom_line()+ geom_vline(xintercept = 0, linetype=2)+xlab("upstream<-TSS->downstream")+ylab("reads count")+theme_bw() + theme(panel.grid =element_blank()) + labs(title = "Monnucleosome Span Reads") + theme(plot.title = element_text(hjust = 0.5))
grid.arrange(g1, g2, ncol=2)
```

# Peak Statistics

## Blacklist ratio

```{r cols.print=2, eval=TRUE, echo=FALSE,warning=FALSE}
if(!is.null(allsteps$PeakQCblacklist)){
    knitr::kable(x = report(allsteps$PeakQCblacklist)[["table"]])
}else{
    message("Blacklist ratio is not available.")
}

```

## DHS ratio

```{r cols.print=2, eval=TRUE, echo=FALSE,warning=FALSE}
if(!is.null(allsteps$PeakQCDHS)){
    knitr::kable(x = report(allsteps$PeakQCDHS)[["table"]])
}else{
    message("DHS is ratio not available.")
}

```

## Fraction of reads in peaks (FRiP)

```{r cols.print=2, eval=TRUE, echo=FALSE,warning=FALSE}
knitr::kable(x = report(allsteps$FRiPQC)[["table"]])
```



## Peak Annotation

```{r eval=TRUE, echo=FALSE, message=FALSE,warning=FALSE}
library(ChIPseeker)
peakanno <- report(allsteps$RPeakAnno)[["annoOutput.rds"]]
plotAnnoPie(x = peakanno)
```

# Gene Ontology Analysis

Gene ontology analysis for all genes around peak regions.

```{r cols.print=5, rows.print=20, eval=TRUE, echo=FALSE, warning=FALSE}
go_path <- report(allsteps$RGo)[["goOutput"]]
go_data <- read.table(file = go_path, header = TRUE, sep = "\t")
go_data <- subset(go_data, select = c("ID", "Description", "GeneRatio", "pvalue", "qvalue"))
go_data$pvalue <- signif(go_data$pvalue, digits = 3)
go_data$pvalue <- as.character(go_data$pvalue)
go_data$qvalue <- signif(go_data$qvalue, digits = 3)
go_data$qvalue <- as.character(go_data$qvalue)
if(nrow(go_data)==0){
    message("No GO terms found: empty table")
}else if(nrow(go_data) < 15){
    knitr::kable(go_data)
}else{
    knitr::kable(go_data[1:15, ])
}
```

[Click to Visit Go Analysis file](`r basename(go_path)`)

# Footprint Analysis

```{r eval=TRUE, echo=FALSE, warning=FALSE, message=FALSE}
footprint_data <- report(allsteps$CutSiteCountR)[["footprint.data"]]
if("CTCF" %in% names(footprint_data)){
    footprint_figure.name <- "CTCF"
    footprint_figure.data <- as.vector(footprint_data$CTCF)
}else{
    footprint_figure.name <- names(footprint_data[1])
    footprint_figure.data <- as.vector(footprint_data[[1]])
}
footprint_figure.length <- length(footprint_figure.data) - 200
footprint_text <- paste(footprint_figure.name, "(Footprinting)", sep = "")
```

The following figure is `r footprint_figure.name` footprint.

```{r eval=TRUE, echo=FALSE, fig.height=7, fig.width=7, results = 'asis', warning=FALSE}
plot(footprint_figure.data, type = "l", col = "blue", lwd = 2, 
    main = footprint_text,
    xlab = "Relative Distance From Motif (bp)", 
    ylab = "Cut Site Count", xaxt = "n", yaxt = "n")
axis(1, at = seq(1, 100, len = 3),
    labels = -(100 + 1 - seq(1, 100 + 1, len = 3)),
    padj = -1.0, tck = -0.01)
axis(1, at = 100 + footprint_figure.length + seq(1, 100, len = 3),
    labels = seq(0, 100, len = 3),
    padj = -1.0, tck = -0.01)
axis(2, padj = 1.0,tck = -0.02)

abline(v = c(100, 100 + footprint_figure.length + 1), lty = 2)

pdf.dir <- report(allsteps$CutSiteCountR)[["pdf.dir"]]

```

All motif footprint figures are saved as pdf files. The absolute path is `r R.utils::getAbsolutePath(pdf.dir)`.



# Annotation of items in table 


For single end sequencing data, esATAC will counts reads number. 

For paired end sequencing data, esATAC will counts read pairs or fragment number.

* __Sequence files type__
is the type of sequencing data: single end data and paired end data.
If paired end reads are stored in one file interleavedly rather than two files,
"it is call "interleaved.

* __Original total reads__
is the sample's raw total reads (pairs) number. 

* __Reads after adapter removing (ratio)__
 is the reads (pairs) number after adapter removing and
the percentage of retained reads in original total reads.
The larger value shows the better quality.

* __Total mapped reads (ratio)__	
is the reads (pairs) number mapped to reference genome and
the percentage of mapped reads in original total reads (alignment rate). 
ENCODE recommend that the alignment rate, 
or percentage of mapped reads, should be greater than 95%, 
though values >80% may be acceptable.

* __Unique locations mapped uniquely__ 	
is the number of distinct uniquely mapping reads (i.e. after removing duplicates). 

* __Non-Redundant Fraction (NRF)__ 
is the value of: Unique locations mapped uniquely 
(the number of positions in the genome that uniquely mappable reads map to) / 
the total number of uniquely mappable reads.

ENCODE recommend NRF>0.9 for ATAC-seq data.

$NRF$ value range   |Complexity
:------------------:|:---------:
$NRF<0.7$              |Concerning
$0.7\le NRF \le 0.9$|Acceptable
$NRF>0.7$              |Ideal


* __Locations with only 1 reads mapping uniquely__
is the number of genomic locations where exactly one read maps uniquely.

* __Locations with only 2 reads mapping uniquely__
is the number of genomic locations where two reads map uniquely.


* __PCR Bottlenecking Coefficients 1 (PBC1)__
is the value of: Locations with only 1 reads mapping uniquely / 
Unique locations mapped uniquely.
ENCODE recommend that PBC1>0.9 for ATAC-seq data.


$PBC1$ value range   |Bottlenecking level
:-------------------:|:---------:
$PBC1<0.7$               |Severe
$0.7\le PBC1 \le 0.9$|Moderate
$PBC1>0.7$               |None


* __PCR Bottlenecking Coefficients 2 (PBC2)__
is the value of: Locations with only 1 reads mapping uniquely /
Locations with only 2 reads mapping uniquely.
ENCODE recommend that PBC2>3 for ATAC-seq data.


$PBC2$ value range  |Bottlenecking level
:------------------:|:----------:
$PBC2<1$                |Severe
$1\le PBC2 \le 3$   |Moderate
$PBC2>3$                |None


* __Non-mitochondrial reads (ratio)__
is the percentage of non-mitochondrial read in total mapped reads.
(mitochondrial reads removed).The larger value shows the better quality.

* __Unique mapped reads (ratio)__
is the percentage of non-mitochondrial unique mapped read in total mapped reads
(multi-mapped reads removed). The larger value shows the better quality.

* __Duplicate removed reads (final for use)__
is the percentage of non-mitochondrial, 
unique mapped and non-duplicate reads in total mapped reads
(duplicate reads removed). These reads are ready to use and storage at final.
The larger value shows the better quality.


* __Nucleosome free reads (<100bp)__
is the nucleosome free reads reads shorter than 100bp for peak calling
* __Total peaks__
is the number of peak called by using nucleosome free reads (<100bp)
* __Peaks overlaped with union DHS (ratio)__
is the percentage of called peak overlaped with blacklist.
The larger value shows the better quality.

* __Peaks overlaped with blacklist (ratio)__
is the percentage of called peak overlaped with blacklist.
The smaller value shows the better quality.

* __Fraction of reads in peaks (FRiP)__
is the fraction of nucleosome free reads (<100bp) in peak.
The larger value shows the better quality.


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

