---
title: "BinQuasi"
author: "Emily Goren"
date: "June 18, 2018"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{BinQuasi Quick Start Guide}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


## Introduction

This package provides code to call peaks in ChIP-seq data with biological replicates using the BinQuasi algorithm of Goren, Liu, Wang, and Wang (2018) [doi.org/10.1093/bioinformatics/bty227](https://doi.org/10.1093/bioinformatics/bty227).


## Data Preprocessing

BinQuasi accepts sorted and indexed BAM files (note that it does not perform genome alignment of raw reads). If your BAM files are not indexed and sorted, we recommend using [samtools](http://www.htslib.org/).


## Peak Calling

Once installed, BinQuasi calls peaks with the function "BQ()." Below is code to run BinQuasi with all default settings, where the sorted and indexed BAM files are stored in the directory specified by "fpath" under the file names "C1.bam", "
C2.bam" and "I1.bam", "I2.bam" for ChIP and input files, respectively. 

```{r}
library(BinQuasi)
fpath <- paste0(system.file(package = 'BinQuasi'), '/extdata/')
results <- BQ(fpath, 
              ChIP.files = c('C1.bam', 'C2.bam'), 
              control.files = c('I1.bam', 'I2.bam'))
head(results$peaks)
```

See the package documentation for information on changing the default settings.
```{r}
?BQ
```


## Exporting Results

The code below saves the called peaks in [BED format](http://genome.ucsc.edu/FAQ/FAQformat#format1) in the file "BinQuasiPeaks.bed".
```{r}
# Sort peaks by p-value
opeaks <- results$peaks[order(results$peaks$P.val),]
# Name the peaks by rank
opeaks$name <- paste0('BQ_Peak_', 1:nrow(opeaks))
# Save as .bed file, setting the scores to be -log10(p-value)
bedout <- data.frame(chrom = opeaks$chr,
                     chromStart = opeaks$start,
                     chromEnd = opeaks$end,
                     name = opeaks$name,
                     score = -log10(opeaks$P.val),
                     strand = c(rep(".",  nrow(opeaks))))
head(bedout)
write.table(bedout, file="BinQuasiPeaks.bed", quote = FALSE, sep = "\t", row.names = FALSE, col.names = FALSE)
```