%\VignetteIndexEntry{Rcade Vignette}
%\VignetteDepends{}
%\VignetteKeywords{Rcade}
%\VignettePackage{Rcade}
\documentclass{article}

\usepackage{amsmath}
\usepackage[authoryear,round]{natbib}
\usepackage{hyperref}
\usepackage{cite}
\usepackage{Sweave}

\textwidth=6.2in
\textheight=8.5in
%\parskip=.3cm
\oddsidemargin=.1in
\evensidemargin=.1in
\headheight=-.3in

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Robject}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textsf{#1}}}
\newcommand{\Rmethod}[1]{{\texttt{#1}}}
\newcommand{\Rfunarg}[1]{{\texttt{#1}}}
\newcommand{\Rclass}[1]{{\textit{#1}}}

\SweaveOpts{keep.source=TRUE} 

\newcommand{\classdef}[1]{
  {\em #1}
}



\begin{document}
\title{\Rpackage{Rcade}: R-based analysis of ChIP-seq And Differential Expression data}

\author{Jonathan Cairns}

\maketitle

\tableofcontents

\section{Introduction}

\Rpackage{Rcade} is a tool that analyses ChIP-seq data and couples the results to an existing Differential Expression (DE) analysis. A key application of \Rpackage{Rcade} is in inferring the direct targets of a transcription factor (TF) - these targets should exhibit TF binding activity, and their expression levels should change in response to a perturbation of the TF.

The ChIP-seq analysis element of \Rpackage{Rcade} is performed through methods from the \Rpackage{baySeq} package, with respect to a user-defined universe of potential binding sites. This means that \Rpackage{Rcade} avoids the noise issues associated with peak-calling and focuses instead on robust quantification of binding activity. Any universe can be selected, and \Rpackage{Rcade} provides functionality to accommodate the common case where bins are defined relative to genomic annotation features.

In some situations, it may be appropriate to define the binding site universe based on a set of peak-calls from other data sets. However, it is inappropriate to use peak-calls from the ChIP-seq data used in the \Rpackage{Rcade} analysis - such an analysis would be prone to confirmation bias.

\Rpackage{Rcade} uses a fully Bayesian modelling approach. In particular, it uses log-odds values, or $B$-values, in both its input and output. The log-odds value is related to the posterior probability ($PP$) of an event, as per the formula $B = \frac{\log(PP)}{\log(1 - PP)}$. $PP$-values should not be confused with the frequentist concept of \emph{p}-values.

%FAQs? TODO

\section{Model}
\Rpackage{Rcade} perform analysis on any set of genes, with each gene uniquely identified by a gene ID. In the below example, we use Ensembl gene IDs.

Each gene is assumed to have some number of associated binding sites, each of which can be active or inactive as inferred from the ChIP-seq data. Additionally, every gene has one or more expression values, each of which is either DE or not DE under some perturbation - for example, knockdown or stimulation of a TF of interest. It is assumed that, conditional on a gene having both a ChIP-seq signal and a DE signature, the ChIP-seq and expression data associated with that gene are independent. All pairwise interactions between ChIP and DE data are considered for a given gene.

The ``gene ID" does not have to refer to literal genes. For example, a user with transcript-specific expression data could use transcript IDs instead.

%FIXME Transcript

\section{Simple workflow}
In this vignette, we will use the example data provided in the \Rpackage{Rcade} package. These data were obtained from two sources, each pertaining to the transcription factor STAT1. All of the experiments were performed with cells from the HeLa cell line: 

\begin{itemize}
	\item Differential Expression data from Array Express, \\{\tt http://www.ebi.ac.uk/arrayexpress}, under accession number E-GEOD-11299. In this experiment, HeLa cells were stimulated with IFN$\gamma$, and a time course microarray experiment was performed. We have assessed DE status between the 0h and 6h time points.
	\item STAT1 ChIP-seq data from the Snyder lab, as part of the ENCODE consortium\\ %(\cite{Birney2007}).
	Input DCC accession numbers: wgEncodeEH000611 and wgEncodeEH000612\\
	ChIP DCC accession number: wgEncodeEH000614\\
	Here, HeLa cells were stimulated 
\end{itemize}

To keep the size of the package down, all of these files have been truncated. Thus, they only contain data pertinent to a handful of selected genes. %Instructions for recreating the full files from the original data sources are in the appendix. FIXME separate vignette?

The location of these data files will vary from system to system. To find the data directory on your computer, use the following code:

<<term=FALSE>>=
dir <- file.path(system.file("extdata", package="Rcade"), "STAT1")
dir
@

\subsection{Preliminary}
Load the \Rpackage{Rcade} package:

<<>>=
library(Rcade)
@

To perform an \Rpackage{Rcade} analysis, you will need the following input data:

\subsubsection{DE summary}
\begin{description}
	\item[DE matrix:] Bayesian information about the DE status of each gene. Any Bayesian source can be used for this purpose:
	\begin{itemize}
		\item \Rpackage{limma}: Full DE results obtained with \Rfunction{topTable(..., number=Inf)}
		\item \Rpackage{baySeq}. %FIXME instructions for obtaining B values from baySeq
	\end{itemize}

At time of writing, \Rpackage{edgeR} and \Rpackage{DEseq} do not supply Bayesian output.

The DE data must contain the following fields:
\begin{description}
	\item[geneID] - gene IDs used to link DE results to genes.
	\item[logFC] - The log fold change associated with each gene.
	\item[B] - B values (log-odds).
\end{description}

<<>>=
DE <- read.csv(file.path(dir, "DE.csv"))
@

\item[DElookup:] We also need to tell \Rpackage{Rcade} which columns of the DE matrix are important. This is done using an object of the form \Rfunction{list(RcadeField1=DEdataField1, ...)}. You may omit the name \Rfunction{RcadeField1} if it is the same as \Rfunction{DEdataField1}. Any fields that are specified in addition to the three required fields above will not be manipulated in the analysis, but will be carried through and appear in the output.

<<>>=
DElookup <- list(GeneID="ENSG", logFC="logFC", B="B",
"Genes.Location", "Symbol")
@

\end{description}

\subsubsection{ChIP-seq data}

\begin{description}
	\item[.bam and .bai files:] Sets of aligned reads. These reads should have already undergone sequence-level pre-processing, such as any read trimming and adaptor removal that may be required. Moreover, they should have appropriate index files - for example, using the \Rfunction{indexBam} function in the package \Rpackage{Rbamtools}.
	Example .bam and .bai files are provided in the package:
<<>>=
dir(dir, pattern = ".bam")
@

	\item[Targets information:]	A matrix containing information about the .Bam files to be used in the analysis.

	Required fields:
	\begin{description}
		\item[fileID] - ID associated with the file.
		\item[sampleID] - ID associated with the sample that was sequenced. Technical replicates of the same population should have the same sampleID.
		\item[factor] - The antibody used in the experiment. Control files should be labelled "Input".
		\item[filepath] - The .bam file's name/path.
	\end{description}
<<>>=
targets <- read.csv(file.path(dir, "targets.csv"), as.is = TRUE)
targets
@
\end{description}
%FIXME Input -> control

\subsubsection{Annotation information}
In the ChIP-seq analysis, \Rpackage{Rcade} performs its analysis based on the counts in user-defined bin regions. These regions are specified with a \Robject{GRanges} object from the package \Rpackage{GenomicRanges}.

A common requirement is to define bins about genomic annotation features: \Rpackage{Rcade} provides simple functionality to generate an appropriate \Robject{GRanges} object, through the \Rfunction{defineBins()} function. 

For the STAT1 ChIP-seq data, we'll be defining bins about Ensembl-derived transcription start sites. Download full transcript annotation information from Ensembl using \Rpackage{biomaRt}, as follows:

<<eval = FALSE>>=
library(biomaRt)

anno <- getBM(
		attributes= c("ensembl_gene_id", "chromosome_name",
			"transcript_start", "transcript_end", "strand"),
		mart= useDataset("hsapiens_gene_ensembl", useMart("ensembl"))
	)

##order, to reduce size of ChIPannoZones object later
anno <- anno[order(anno$chromosome_name),]
##use appropriate column names
colnames(anno) <- c("ENSG","chr","start","end","str")
@

%we're using a reduced version of the annotation, as follows. Do not use this in your own analysis!

<<echo=FALSE>>=
anno <- read.csv(file.path(dir, "anno.csv"))

anno <- anno[order(anno$chromosome_name),]
colnames(anno) <- c("ENSG","chr","start","end","str")
@

We define bins based on the annotation with the \Rfunction{defineBins()} function, as follows:

<<>>=
ChIPannoZones <- defineBins(anno, zone=c(-1500, 1500), geneID="ENSG")
@

The \Rfunarg{zone=c(-1500,1500)} argument defines the zone of interest: it starts 1500bp $5'$ of each TSS (\Rfunarg{-1500}), and ends 1500bp $3'$ of each TSS (\Rfunarg{1500}). This zone was chosen based on mapping peak-calls to TSSs using the \Rpackage{ChIPpeakAnno} package, and plotting the positional distribution of these peak-calls. %FIXME more detail

The object \emph{ChIPannoZones} can now be used in the \Rpackage{Rcade} analysis.

\subsection{Analysis function}

We can parallelize \Rpackage{Rcade} by supplying a compute cluster (though this is optional):
<<eval=FALSE>>=
library(parallel)
cl <- makeCluster(4, "SOCK")
@

If you don't have a compute cluster or a multicore machine, you can disable parallel processing:

<<>>= 
cl <- NULL
@

We now process the data with the \Rfunction{RcadeAnalysis()} function, obtaining an \Rpackage{Rcade} object:

<<>>=
Rcade <- RcadeAnalysis(DE, ChIPannoZones, ChIPtargets=targets, ChIPfileDir = dir,
	shift = 0, cl=cl, DElookup=DElookup)
Rcade
@

The \Rpackage{Rcade} object stores information from the DE analysis:
<<>>=
x <- getDE(Rcade)
@

It also contains the ChIP-seq analysis:
<<>>=
x <- getChIP(Rcade)
@

and the Rcade table obtained from linking the ChIP-seq analysis with the DE data:
<<>>=
x <- getRcade(Rcade)
@

\section{Plotting, QC}
We can perform Principle Component Analysis on the counts with the \Rfunction{plotPCA} function. Usually, one would expect similar samples to cluster together on this plot, and so we can identify samples that do not fit this assumption - further investigation is required to determine the significance of such a finding. (Output is shown in Figure \ref{fig:P1}.) %FIXME choose components

<<P1, include = FALSE>>=
plotPCA(Rcade)
@

\begin{figure}
	\begin{center}
<<P1fig,fig=TRUE,echo=FALSE>>=
<<P1>>
@
	\end{center}
	\caption{Output from \Rfunction{plotPCA(Rcade)}.}
	\label{fig:P1}
\end{figure}

The MM plot shows log-ratios from DE plotted against log-ratios from the ChIP-seq. The colour of each point corresponds to the probability of both ChIP and DE being present. (Output is shown in Figure \ref{fig:P2}.)

<<P2, include = FALSE>>=
plotMM(Rcade)
@

\begin{figure}
	\begin{center}
<<P2fig,fig=TRUE,echo=FALSE>>=
<<P2>>
@
	\end{center}
	\caption{Output from \Rfunction{plotMM(Rcade)}.}
	\label{fig:P2}
\end{figure}


The function \Rfunction{plotBBB()} plots log-odds values for ChIP-seq, DE, and combined ChIP-seq/DE analysis together. The package \Rpackage{rgl} is required for this 3D plot. (Output is shown in Figure \ref{fig:P3}.)

<<P3, eval = FALSE>>=
library(rgl)
plotBBB(Rcade)
@

\begin{figure}
	\begin{center}
	\includegraphics[width=\textwidth]{plotBBB.png}
	\end{center}
	\caption{Output from \Rfunction{plotBBB(Rcade)}, subsequently saved to disk with the command \Rfunction{rgl.snapshot(filename="plotBBB.png")}}
	\label{fig:P3}
\end{figure}

% - FIXME Read histogram in individual bins

\section{Output}
To export the Rcade results to disk in a user-friendly .csv format, use \Rfunction{exportRcade()} function as follows:

<<eval=FALSE>>=
exportRcade(Rcade, directory="RcadeOutput")
@

Usually, the file of interest is ``DEandChIP.csv", which contains the genes most likely to have both DE and ChIP signals. A full explanation of all of the files can be found in the help page:

<<eval=FALSE>>=
?exportRcade
@

By default, Rcade outputs the top 1000 geneIDs for each hypothesis. To increase the number of geneIDs, use a larger value for cutoffArg:

<<eval=FALSE>>=
exportRcade(Rcade, directory="RcadeOutput", cutoffArg=2000)
@

Alternative cutoff methods are described in the \Rfunction{exportRcade} help page - for example,
<<eval=FALSE>>=
exportRcade(Rcade, directory="RcadeOutput", cutoffMode="B", cutoffArg=0)
@



% - TODO Extract sequences? (e.g. to FASTQ)

%\section{Advanced}
% - Recovering from failed analysis (TODO)
% - Multiple epitopes (TODO)
% - FIXME Fiddling with the prior?

\section{Questions/Bug reports}

If you have any questions about \Rpackage{Rcade}, or encounter a bug, please post a message to the BioConductor mailing list at {\tt bioconductor@r-project.org}. This will help anybody who later searches for help on the same query.

\section{Session Info}

<<>>=
sessionInfo()
@

\section{Acknowledgements}

Differential Expression data from Array Express, {\tt http://www.ebi.ac.uk/arrayexpress}, under accession number E-GEOD-11299.

STAT1 ChIP-seq data from the Snyder lab, as part of the ENCODE consortium\\ %(\cite{Birney2007}).
Input DCC accession numbers: wgEncodeEH000611 and wgEncodeEH000612\\
ChIP DCC accession number: wgEncodeEH000614

%\bibliographystyle{plainnat}
%\bibliography{Rcade} FIXME

\end{document}

