%\VignetteIndexEntry{R Investigation of NimbleGen Oligoarrays}
%\VignetteDepends{Ringo, mclust}
%\VignetteKeywords{microarray ChIP-chip NimbleGen nimblegen}
%\VignettePackage{Ringo} % name of package

%%%% HEAD SECTION: START EDITING BELOW %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[11pt, a4paper, fleqn]{article}
\usepackage{geometry}\usepackage{color}
\definecolor{darkblue}{rgb}{0.0,0.0,0.75}
\usepackage[%
baseurl={http://www.bioconductor.org},%
pdftitle={Introduction to Ringo},%
pdfauthor={Joern Toedling},%
pdfsubject={Ringo Vignette},%
pdfkeywords={Bioconductor},%
pagebackref,bookmarks,colorlinks,linkcolor=darkblue,citecolor=darkblue,%
filecolor=darkblue,urlcolor=darkblue,pagecolor=darkblue,%
raiselinks,plainpages,pdftex]{hyperref}

\usepackage{verbatim} % for multi-line comments
\usepackage{amsmath,a4,t1enc, graphicx}
\usepackage{natbib}
\bibpunct{(}{)}{;}{a}{,}{,}

\parindent0mm
\parskip2ex plus0.5ex minus0.3ex

\newcommand{\Robject}[1]{{\texttt{#1}}}
\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}
\newcommand{\Rclass}[1]{{\textit{#1}}}
\newcommand{\Rmethod}[1]{{\textit{#1}}}
\newcommand{\Rfunarg}[1]{{\texttt{#1}}}
\newcommand{\phead}[1]{{\flushleft \sf \small \textbf{#1} \quad}}

\newcommand{\myincfig}[3]{%
  \begin{figure}[h!tb]
    \begin{center}
      \includegraphics[width=#2]{#1}
      \caption{\label{#1}\textit{#3}}
    \end{center}
  \end{figure}
}

\addtolength{\textwidth}{2cm}
\addtolength{\oddsidemargin}{-1cm}
\addtolength{\evensidemargin}{-1cm}
\addtolength{\textheight}{2cm}
\addtolength{\topmargin}{-1cm}
\addtolength{\skip\footins}{1cm}

%%%%%%% START EDITING HERE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{Sweave}
\begin{document}

 % produce no 'eps' figures

\title{Ringo - R Investigation of NimbleGen Oligoarrays}
\author{Joern Toedling}
\maketitle


\section{Introduction}

The package \Rpackage{Ringo} deals with the analysis of two-color
oligonucleotide microarrays used in ChIP-chip projects.
The package was started to facilitate the analysis of
two-color microarrays from the company
NimbleGen\footnote{for NimbleGen one-color microarrays,
we recommend the Bioconductor package \Rpackage{oligo}},
but the package has a modular design,
such that the platform-specific functionality is encapsulated
and analogous two-color tiling array platforms can also be
processed.
The package employs functions from other
packages of the Bioconductor project \citep{bioconductor}
and provides additional
ChIP-chip-specific and NimbleGen-specific functionalities.

\begin{Schunk}
\begin{Sinput}
> library("Ringo")
\end{Sinput}
\end{Schunk}

If you use Ringo for analyzing your data, please cite:
\begin{itemize}
\item{Joern Toedling, Oleg Sklyar, Tammo Krueger, Jenny J Fischer, Silke Sperling, Wolfgang Huber (2007). Ringo - an R/Bioconductor package for analyzing ChIP-chip readouts. \textsl{BMC Bioinformatics}, 8:221.}
\end{itemize}
\nocite{Ringo2007}

\subsection*{Getting help}

If possible, please send questions about \Rpackage{Ringo} to the
Bioconductor mailing list.\\
See \url{http://www.bioconductor.org/docs/mailList.html} \\
Their archive of questions and responses may prove helpful, too.


\section{Reading in the raw data}

For each microarray, the scanning output consists of two files, one
holding the Cy3 intensities, the other one the Cy5 intensities.
These files are tab-delimited text files.

The package comes with (shortened) example scanner output files,
in NimbleGen's \emph{pair} format.
These files are excerpts of the ChIP-chip demo data that NimbleGen
provide at their FTP site for free download.
Their biological context, identification of DNA binding sites
of complexes containing Suz12 in human cells,
has been described before \citep{Squazzo2006}.

\begin{Schunk}
\begin{Sinput}
> exDir <- system.file("exData",package="Ringo")
> list.files(exDir, pattern="pair.txt")
\end{Sinput}
\begin{Soutput}
[1] "MOD_20551_PMT1_pair.txt" "MOD_20742_PMT1_pair.txt"
\end{Soutput}
\begin{Sinput}
> head(read.delim(file.path(exDir,"MOD_20551_PMT1_pair.txt"),
+                 skip=1))[,c(1,4:7,9)]
\end{Sinput}
\begin{Soutput}
    IMAGE_ID          PROBE_ID POSITION   X   Y      PM
1 20551_PMT1 SUZ100P0000021781        1 269  78 1149.33
2 20551_PMT1 SUZ100P0000021783       16 682 779 1192.00
3 20551_PMT1 SUZ100P0000021785       31  92 405  685.56
4 20551_PMT1 SUZ100P0000021787       46 219 608  562.67
5 20551_PMT1 SUZ100P0000021789       61 217 418  584.56
6 20551_PMT1 SUZ100P0000021791       76 147 406  636.22
\end{Soutput}
\end{Schunk}

In addition, there is a text file that holds details on the samples,
including which two \emph{pair} files belong to which
sample\footnote{You may have to construct such a targets file for your own
data. The \texttt{scripts} directory of this package contains a script
\texttt{convertSampleKeyTxt.R} as an inspiration how the file
\texttt{SampleKey.txt} provided by NimbleGen could be used for this.}.

\begin{Schunk}
\begin{Sinput}
> read.delim(file.path(exDir,"example_targets.txt"), header=TRUE)
\end{Sinput}
\begin{Soutput}
  SlideNumber             FileNameCy3             FileNameCy5
1       Suz12 MOD_20551_PMT1_pair.txt MOD_20742_PMT1_pair.txt
               Species   Cy3   Cy5
1 Homo sapiens (human) total Suz12
\end{Soutput}
\end{Schunk}

The columns \texttt{FileNameCy3} and \texttt{FileNameCy5} hold which
of the raw data files belong to which sample.
The immuno-precipitated extract was colored with the Cy5 dye in the
experiment, so the column \texttt{Cy5} essentially holds which
antibody has been used for the immuno-precipitation, in this case one
against the protein \texttt{Suz12}.

Furthermore, there is a file describing the reporter categories
on the array (you might know these Spot Types files from
\Rpackage{limma}~\citep{limma05})\footnote{The spot types file is
  usally not provided by the array manufacturer, but needs to be
  created manually. You can use the file that comes with the package
  as a template and extend it as needed. See:\\
  <your-install-directory-of-Rpackages>/Ringo/exData/spottypes.txt.
}
\begin{Schunk}
\begin{Sinput}
> read.delim(file.path(exDir,"spottypes.txt"), header=TRUE)
\end{Sinput}
\begin{Soutput}
  SpotType GENE_EXPR_OPTION PROBE_ID  Color
1    Probe         FORWARD*        *  black
2    Probe         REVERSE*        *  black
3    Probe           BLOCK*        *  black
4 Negative    NGS_CONTROLS*        * yellow
5    Empty            EMPTY        *  white
6   H_Code           H_CODE        *    red
7   V_Code           V_CODE        *   blue
8   Random           RANDOM        *  green
\end{Soutput}
\end{Schunk}

Reading all these files, we can read in the raw reporter intensities
and obtain an object of class \Rclass{RGList}, a class defined
in package \Rpackage{limma}.

\begin{Schunk}
\begin{Sinput}
> exRG <- readNimblegen("example_targets.txt","spottypes.txt",path=exDir)
\end{Sinput}
\end{Schunk}

This object is essentially a list and contains the raw intensities of
the two hybridizations for the red and green channel plus information
on the reporters on the array and on the analyzed samples.

\begin{Schunk}
\begin{Sinput}
> head(exRG$R)
\end{Sinput}
\begin{Soutput}
     MOD_20742_PMT1_pair
[1,]              613.22
[2,]              841.67
[3,]              659.56
[4,]              494.44
[5,]              469.33
[6,]              544.11
\end{Soutput}
\begin{Sinput}
> head(exRG$G)
\end{Sinput}
\begin{Soutput}
     MOD_20551_PMT1_pair
[1,]             1149.33
[2,]             1192.00
[3,]              685.56
[4,]              562.67
[5,]              584.56
[6,]              636.22
\end{Soutput}
\begin{Sinput}
> head(exRG$genes)
\end{Sinput}
\begin{Soutput}
  GENE_EXPR_OPTION          PROBE_ID POSITION   X   Y Status                ID
1         FORWARD1 SUZ100P0000021781        1 269  78  Probe SUZ100P0000021781
2         FORWARD1 SUZ100P0000021783       16 682 779  Probe SUZ100P0000021783
3         FORWARD1 SUZ100P0000021785       31  92 405  Probe SUZ100P0000021785
4         FORWARD1 SUZ100P0000021787       46 219 608  Probe SUZ100P0000021787
5         FORWARD1 SUZ100P0000021789       61 217 418  Probe SUZ100P0000021789
6         FORWARD1 SUZ100P0000021791       76 147 406  Probe SUZ100P0000021791
\end{Soutput}
\begin{Sinput}
> exRG$targets
\end{Sinput}
\begin{Soutput}
  SlideNumber             FileNameCy3             FileNameCy5
1       Suz12 MOD_20551_PMT1_pair.txt MOD_20742_PMT1_pair.txt
               Species   Cy3   Cy5
1 Homo sapiens (human) total Suz12
\end{Soutput}
\end{Schunk}

Users can alternatively supply raw two-color ChIP-chip readouts
from other platforms in \Rclass{RGList} format and consecutively use
\Rpackage{Ringo} to analyze that data.
See Section~\ref{sec-agilent} for an example.


\section{Mapping reporters to genomic coordinates}

By \emph{reporters}, we mean the oligo-nucleotides or PCR products
that have been fixated on the array for measuring the abundance of
corresponding genomic fragments in the ChIP-chip experiment.

Each reporter has a unique identifier and (ideally) a unique sequence,
but can, and probably does, appear in multiple copies as
\emph{features} on the array surface.

A mapping of reporters to genomic coordinates is usually provided
by the array manufacturer, such as in NimbleGen's *.POS files.
If the reporter sequences are provided as well,
you may consider to perform a custom
mapping of these sequences to the genome of interest, using alignment
tools such as \emph{Exonerate} \citep{Slater2005}
or functions provided by the Bioconductor package
\Rpackage{Biostrings} \citep{Biostrings}.

Such a re-mapping of reporters to the genome can sometimes be
necessary, for example when the array has designed on
an outdated assembly of the genome.
Re-mapping also provides the advantage that you can allow non-perfect
matches of reporters to the genome, if desired.

Once reporters have been mapped to the genome, this mapping needs to
be made available to the data analysis functions.
While a \Rclass{data.frame} may be an obvious way of representing such
a mapping, repeatedly extracting sub-sets of the data frame related to
a genomic region of interest turns out to be too slow for practical
purposes.
\Rpackage{Ringo}, similar to the Bioconductor package
\Rpackage{tilingArray}, employs an object of class \Rclass{probeAnno}
to store the mapping between reporters on the microarray and genomic
positions.
Per chromosome, the object holds four vectors of equal length and
ordering that specify at which genomic positions reporter matches
start and end, what identifiers or indices these reporters have in the
intensities data, and whether these reporters match uniquely to the
genomic positions.

\begin{Schunk}
\begin{Sinput}
> load(file.path(exDir,"exampleProbeAnno.rda"))
> ls(exProbeAnno)
\end{Sinput}
\begin{Soutput}
[1] "9.end"    "9.index"  "9.start"  "9.unique"
\end{Soutput}
\begin{Sinput}
> show(exProbeAnno)
\end{Sinput}
\begin{Soutput}
A 'probeAnno' object holding the mapping between
reporters and genomic positions.
Chromosomes: 9
Microarray platform: NimbleGen MOD_2003-12-05_SUZ12_1in2 
Genome: H.sapiens (hg18)
\end{Soutput}
\begin{Sinput}
> head(exProbeAnno["9.start"])
\end{Sinput}
\begin{Soutput}
[1]  531857  861532 1269645 2497214 2685042 3795381
\end{Soutput}
\begin{Sinput}
> head(exProbeAnno["9.end"])
\end{Sinput}
\begin{Soutput}
[1]  531916  861591 1269704 2497273 2685101 3795440
\end{Soutput}
\end{Schunk}

The function \Rfunction{posToProbeAnno} allows generation
of a valid \Rclass{probeAnno} object,
either from a file that corresponds to a NimbleGen \texttt{POS} file
or from a \Rclass{data.frame} objects that holds the same
information.
The package's \texttt{scripts} directory contains a script
\texttt{mapReportersWithBiostrings.R}, which shows how to use
\Rpackage{Biostrings} for mapping the reporter sequences of the
provided example data, and some Perl scripts that allow the conversion
of multiple output files from common alignment tools such as Exonerate
into one file that corresponds to a POS file.
The function \Rfunction{validObject} can be used to perform a
quick check whether a generated \Rclass{probeAnno} object will
probably work with  other \Rpackage{Ringo} functions.


\section{Quality assessment}

The \Rfunction{image} function allows us to look at the spatial 
distribution of the intensities on a chip. This can be useful to
detect obvious artifacts on the array, such as scratches, bright
spots, finger prints etc. that might render parts or all of the
readouts useless. 

\begin{Schunk}
\begin{Sinput}
> par(mar=c(0.01,0.01,0.01,0.01), bg="black")
> image(exRG, 1, channel="green", mycols=c("black","green4","springgreen"))
\end{Sinput}
\end{Schunk}


\myincfig{Ringo-imageRG}{0.5\textwidth}{Spatial distribution of raw
reporter intensities laid out by the reporter position on the
microarray surface.}

See figure \ref{Ringo-imageRG} for the image.
Since the provided example data set only holds the intensities for
reporters mapped to the forward strand of~chromosome 9,
the image only shows the few green dots of these reporters'
positions.
We see, however, that these chromosome 9 reporters are well
distributed over the whole array surface rather than being clustered
together in one part of the array.

It may also be useful to look at the absolute distribution of
the single-channel densities. \Rpackage{limma}'s function
\Rfunction{plotDensities} may be useful for this purpose.
%
\begin{Schunk}
\begin{Sinput}
> plotDensities(exRG)
\end{Sinput}
\end{Schunk}
\includegraphics{Ringo-plotDensities}

In addition, the data file loaded above also contains a
\emph{GFF (General Feature Format)}
file of all transcripts on human chromosome 9 annotated in the
\href{http://www.ensembl.org}{Ensembl} database
(release 46, August 2007).
The script \texttt{retrieveGenomicFeatureAnnotation.R} in the
package's scripts directory contains
example source code showing how the Bioconductor package
\Rpackage{biomaRt} can be used to generate such an annotated
genome features \Rclass{data.frame}.

\begin{Schunk}
\begin{Sinput}
> head(exGFF[,c("name","symbol","chr","strand","start","end")])
\end{Sinput}
\begin{Soutput}
             name symbol chr strand  start    end
1 ENST00000382507          9      1   1056   1620
2 ENST00000326592          9     -1   5841  19587
3 ENST00000382502          9     -1  24394  25841
4 ENST00000355822          9     -1  24921  25277
5 ENST00000382501          9     -1  25028  25856
6 ENST00000382500  FOXD4   9     -1 105991 108951
\end{Soutput}
\end{Schunk}

To assess the impact of the small distance between reporters
on the data, one can look at the autocorrelation plot.
For each base-pair lag $d$, it is assessed how strong the intensities
of reporters at genomic positions $x+d$ are correlated with the probe
intensities at positions $x$.

The computed correlation is plotted against the lag $d$.

\begin{Schunk}
\begin{Sinput}
> exAc <- autocor(exRG, probeAnno=exProbeAnno, chrom="9", lag.max=1000)
> plot(exAc)
\end{Sinput}
\end{Schunk}
\includegraphics{Ringo-autocorRG0}

We see some auto-correlation between probe position up to 800 base pairs
apart. Since the sonicated fragments that are hybridized to the
array have an average size in the range of up to 1000~bp,
such a degree of auto-correlation up to this distance can be expected.


\section{Preprocessing}

Following quality assessment of the raw data,
we perform normalization of the probe intensities
and derive fold changes of reporters' intensities in the enriched
sample divided by their intensities in the non-enriched \emph{input}
sample and take the (generalized) logarithm of these ratios.

We use the variance-stabilizing normalization \citep{HuberVSN}
or probe intensities and generate an \texttt{ExpressionSet} object
of the normalized probe levels.

\begin{Schunk}
\begin{Sinput}
> exampleX <- preprocess(exRG)
> sampleNames(exampleX) <-
+  with(exRG$targets, paste(Cy5,"vs",Cy3,sep="_"))
> print(exampleX)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
ExpressionSet (storageMode: lockedEnvironment)
assayData: 991 features, 1 samples 
  element names: exprs 
protocolData: none
phenoData
  sampleNames: Suz12_vs_total
  varLabels: SlideNumber FileNameCy3 ... Cy5 (6 total)
  varMetadata: varLabel labelDescription
featureData: none
experimentData: use 'experimentData(object)'
Annotation:  
\end{Soutput}
\end{Schunk}

Among the provided alternative preprocessing options is also the
Tukey-biweight scaling procedure that NimbleGen have used to scale
ChIP-chip readouts so that the data is centered on zero.

\begin{Schunk}
\begin{Sinput}
> exampleX.NG <- preprocess(exRG, method="nimblegen")
> sampleNames(exampleX.NG) <- sampleNames(exampleX)
\end{Sinput}
\end{Schunk}

The effects of different preprocessing procedures on the data, can be
assessed using the \Rfunction{corPlot} function.

\begin{Schunk}
\begin{Sinput}
> corPlot(cbind(exprs(exampleX),exprs(exampleX.NG)),
+         grouping=c("VSN normalized","Tukey-biweight scaled"))
\end{Sinput}
\end{Schunk}
\includegraphics{Ringo-comparePreprocessings}

The same function can also be used to assess the correlation between
biological and technical replicates among the microarray samples.


\section{Visualize intensities along the chromosome}

The function \Rfunction{chipAlongChrom} provides a way to visualize
the ChIP-chip data in a specified genome region.
For convenience, this function can also be invoked by using the
function \Rfunction{plot} with an \Rclass{ExpressionSet} object
as first argument and a \Rclass{probeAnno} object as second
argument.

\begin{Schunk}
\begin{Sinput}
> plot(exampleX, exProbeAnno, chrom="9", xlim=c(34318000,34321000),
+      ylim=c(-2,4), gff=exGFF, colPal=c("skyblue", "darkblue"))
\end{Sinput}
\end{Schunk}
%
\myincfig{Ringo-chipAlongChrom}{0.98\textwidth}{Normalized probe
  intensities around the TSS of the \texttt{Nudt2} gene.}

See the result in figure \ref{Ringo-chipAlongChrom}.


\section{Smoothing of probe intensities}

Since the response of reporters to the same amount of hybridized
genome material varies greatly, due to probe GC content, melting
temperature,  secondary structure etc., it is suggested to do a
smoothing over individual  probe intensities before looking for
ChIP-enriched regions.

Here, we slide a window of 800 bp width along the chromosome and
replace the intensity at e genomic position $x_0$ by the median over
the intensities of those reporters inside the window  that is centered
at~$x_0$.

\begin{Schunk}
\begin{Sinput}
> smoothX <- computeRunningMedians(exampleX, probeAnno=exProbeAnno,
+ modColumn = "Cy5", allChr = "9", winHalfSize = 400)
> sampleNames(smoothX) <- paste(sampleNames(exampleX),"smoothed")
\end{Sinput}
\end{Schunk}
%
\begin{Schunk}
\begin{Sinput}
> combX <- combine(exampleX, smoothX)
> plot(combX, exProbeAnno, chrom="9", xlim=c(34318000,34321000), 
+      ylim=c(-2,4), gff=exGFF, colPal=c("skyblue", "steelblue"))
\end{Sinput}
\end{Schunk}
\myincfig{Ringo-smoothAlongChrom}{0.98\textwidth}{Normalized and
  smoothed probe intensities around the TSS of the \texttt{Nudt2}
  gene.}

See the smoothed probe levels in figure \ref{Ringo-smoothAlongChrom}.


\section{Finding ChIP-enriched regions}
\label{sec-threshold}

To identify antibody-enriched genomic regions, we require the
following:
\begin{itemize}
\item smoothed intensities of reporters mapped to this region exceed a
  certain threshold $y_0$
\item the region contains at least three probe match positions
\item each affected position is less than a defined maximum
  distance~$d_{max}$
apart from another affected position in the region (we require a
certain probe spacing to have confidence in detected
peaks\footnote{Note that the term ''peak'', while commonly used in
  ChIP-chip context, is slightly misleading and the term
  "ChIP-enriched region", or "cher" in shorthand, is more
  appropriate. Within such regions the actual signal could show two or
  more actual signal peaks or none at all (long plateau).})
\end{itemize}

For setting the threshold $y_0$, one has to assess the
expected (smoothed) probe levels in non-enriched genomic regions,
i.e. the \emph{null distribution} of probe levels.
In a perfect world, we could use a log ratio of $0$ as definite
cut-off.
In this case the ``enriched'' DNA and the input DNA sample would be
present in equal amounts, so no antibody-bound epitope,
could be found at this genomic site.
In practice, there are some reasons why zero may be a too naive
cut-off for calling a probe-hit genomic site \emph{enriched} in our
case.
See~\citet{BourgonPhD} for an extensive discussion on problematic
issues with ChIP-chip experiments.
We will just briefly mention a few issues here.
For once, during the immuno-precipitation, some non-antibody-bound
regions may be pulled down in the assay and consequently enriched or
some enriched DNA may cross-hybridize to other reporters.
Furthermore, since genomic fragments after sonication are mostly a lot
larger than the genomic distance between two probe-matched genomic
positions, auto-correlation between reporters certainly is existent.
Importantly, different reporters measure the same DNA amount with a
different efficiency even after normalizing the probe levels, due to 
sequence properties of the probe, varying quality of the synthesis of
reporters on the array and other reasons.
To ameliorate this fact, we employ the sliding-window smoothing
approach.

The aforementioned issues make it difficult to come up with a
reasonable estimate for the null distribution of smoothed probe levels
in non-enriched genomic regions. 
See Figure \ref{Ringo-histogramSmoothed} for the two histograms.
We present one way (out of many) for objectively choosing the
threshold~$y_0$.
The histograms suggest the smoothed reporter levels follow a mixture
of two distributions, one being the null distribution of non-affected
reporters and the other one the alternative one for the smoothed
reporter values in ChIP-enriched regions.
We assume the null distribution is symmetric and its mode is the one
close to zero in the histogram. By mirroring its part left of the mode
over the mode, we end up with an estimated null distribution.
For the alternative distribution, we only assume that it is
stochastically larger than the null distribution and that its mass to
the left of the estimated mode of the null distribution is
negligible.
We estimate an upper bound $y_0$ for values arising from the null
distribution and conclude that smoothed probe levels \mbox{$y > y_0$}
are more likely to arise from the ChIP enrichment distribution
than from the null distribution.
These estimates are indicated by red vertical lines in the histograms.

\begin{Schunk}
\begin{Sinput}
> (y0 <- apply(exprs(smoothX),2,upperBoundNull))
\end{Sinput}
\begin{Soutput}
Suz12_vs_total smoothed 
              0.7392157 
\end{Soutput}
\end{Schunk}
%
%
\myincfig{Ringo-histogramSmoothed}{0.65\textwidth}{Histograms of reporter intensities after smoothing of reporter level. The red vertical line is the cutoff values suggested by the histogram.}
%
Since antibodies vary in their efficiency to bind to their target epitope,
we suggest to obtain a different threshold for each antibody. In the example
data, however, we have only one antibody against \texttt{Suz12}.

While this threshold worked well for us, 
we do not claim this way to be a gold standard for determining the
threshold. 
In particular, it does not take into account the auto-correlation between
near-by reporters. See \citet{BourgonPhD} for a more sophisticated
algorithm that does take it into account.

\begin{Schunk}
\begin{Sinput}
> chersX <- findChersOnSmoothed(smoothX, probeAnno=exProbeAnno, thresholds=y0,
+    allChr="9", distCutOff=600, cellType="human")
> chersX <- relateChers(chersX, exGFF)
> chersXD <- as.data.frame.cherList(chersX)
\end{Sinput}
\end{Schunk}
%
\begin{Schunk}
\begin{Sinput}
> chersXD[order(chersXD$maxLevel, decreasing=TRUE),]
\end{Sinput}
\begin{Soutput}
                                      name chr    start      end cellType
1 human.Suz12_vs_total smoothed.chr9.cher1   9 34319028 34319854    human
3 human.Suz12_vs_total smoothed.chr9.cher3   9 34580420 34582384    human
2 human.Suz12_vs_total smoothed.chr9.cher2   9 34579444 34579760    human
                 antibody
1 Suz12_vs_total smoothed
3 Suz12_vs_total smoothed
2 Suz12_vs_total smoothed
                                                                         features
1 ENST00000379158 ENST00000379154 ENST00000379155 ENST00000346365 ENST00000337747
3                                                 ENST00000378980 ENST00000351266
2                                                 ENST00000378980 ENST00000351266
   maxLevel      score
1 1.9958907 52.1036588
3 1.5341497 47.8557696
2 0.7882005  0.5865187
\end{Soutput}
\end{Schunk}

Note that in \Rpackage{Ringo} functions, 
``ChIP-enriched region'' is abbreviated to ``cher''.
 
One characteristic of enriched regions that can be used for sorting
them is the element \texttt{maxLevel}, that is the highest smoothed
probe level in the enriched region.
Alternatively, one can sort by the \texttt{score},
that is the sum of smoothed probe levels minus the threshold.
It is a discretized version of to the area under the curve with the
baseline being the  threshold.

\begin{Schunk}
\begin{Sinput}
> plot(chersX[[1]], smoothX, probeAnno=exProbeAnno, gff=exGFF,
+      paletteName="Spectral")
\end{Sinput}
\end{Schunk}
%
\myincfig{Ringo-plotCher}{0.9\textwidth}{One of the identified Suz12-antibody
enriched regions on chromosome 9.}

Figure~\ref{Ringo-plotCher} displays an identified enriched region, 
which is located upstream of the \texttt{Nudt2} gene. 
This ChIP-enriched region was already obvious in plots of the 
normalized data (see Figure \ref{Ringo-smoothAlongChrom}).
While it is reassuring that our method recovers it as well, a number
of other approaches would undoubtedly have reported it as well.


\section{Agilent data}
\label{sec-agilent}

The package \Rpackage{Ringo} can also be applied to ChIP-chip data
from manufacturers other than NimbleGen.
As long as the data is supplied as an \Rclass{RGList} or
\Rclass{ExpressionSet}, the functions of the package can be used,
although certain function arguments may need to be changed from their
default setting.
As an example, we demonstrate how \Rpackage{Ringo} can be used for
the analysis of ChIP-chip data generated on two-color microarrays from
Agilent.
These data have been described in \citet{Schmidt2008}, and
the raw data files were downloaded from the ArrayExpress
database~\citep[accession: E-TABM-485]{Parkinson2009}.
The data are ChIP-chip measurements of the
histone modification H3K4me3 in a 300~kb region of chromosome 17
in mouse Tc1 liver cells.
These demo data files included in this package
are only excerpts of the original data files.

First we read in the raw data using the function
\Rfunction{read.maimages} from package \Rpackage{limma}.

\begin{Schunk}
\begin{Sinput}
> agiDir <- system.file("agilentData", package="Ringo")
> arrayfiles <- list.files(path=agiDir,
+  pattern="H3K4Me3_Tc1Liver_sol1_mmChr17_part.txt")
> RG <- read.maimages(arrayfiles, source="agilent", path=agiDir)
\end{Sinput}
\end{Schunk}
Annotation of the one sample was provided and we created a targets
file that contains this sample annotation.
\begin{Schunk}
\begin{Sinput}
> at <- readTargets(file.path(agiDir,"targets.txt"))
> RG$targets <- at
\end{Sinput}
\end{Schunk}
Have a look at the raw data structure in R.
\begin{Schunk}
\begin{Sinput}
> show(RG)
\end{Sinput}
\begin{Soutput}
An object of class "RGList"
$G
[1] 134.16390  51.76190  49.59016  87.10769 136.00000
774 more rows ...

$Gb
[1] 48.0 49.0 49.0 49.0 49.5
774 more rows ...

$R
[1] 110.85250  68.84127  69.40984  92.67692 254.26670
774 more rows ...

$Rb
[1] 65.0 64.0 65.0 65.5 66.0
774 more rows ...

$targets
      SlideNumber                               FileName      Species Tissue
1 H3K4me3Tc1Liver H3K4Me3_Tc1Liver_sol1_mmChr17_part.txt Mus musculus  Liver
    Cy3     Cy5 Antibody
1 input H3K4me3  H3K4me3

$genes
  Row Col ProbeUID ControlType         ProbeName                  GeneName
1   1   1        0           1 MmCGHBrightCorner         MmCGHBrightCorner
2   1   2        1           1        DarkCorner                DarkCorner
3   1   3        1           1        DarkCorner                DarkCorner
4   1   4        3           0    A_68_P31052153 chr17:012323564-012323623
5   1  95      185           0    A_68_P31152765 chr17:033822110-033822154
             SystematicName
1         MmCGHBrightCorner
2                DarkCorner
3                DarkCorner
4 chr17:012323564-012323623
5 chr17:033822110-033822154
774 more rows ...

$source
[1] "agilent"
\end{Soutput}
\end{Schunk}

We can only perform limited quality assessment of these data, as the
data only consist of one sample and the demo data files are only a
short excerpt of the full raw data from that microarray.
Have a look at the spatial distribution of the raw intensities on the
microarray surface.

\begin{Schunk}
\begin{Sinput}
> par(mar=c(0.01,0.01,0.01,0.01), bg="black")
> image(RG, 1, channel="red", dim1="Col", dim2="Row",
+       mycols=c("sienna","darkred","orangered"))
\end{Sinput}
\end{Schunk}
%
\myincfig{Ringo-imageAgiRG}{0.6\textwidth}{Spatial distribution of raw
reporter intensities of the Agilent microarray, laid out by the
reporter position on the microarray surface.}

See the result in Figure~\ref{Ringo-imageAgiRG}.
Again, the sparseness of the image is due to the fact that the example
data is only a short excerpt of the original raw data file from the
microarray.

To create a  \Rclass{probeAnno} object for this array, there are two
possibilites:
\begin{enumerate}
\item one option -- in many cases the preferable one -- is to remap
  all the probe sequences from the array description file to the
  current genome build
\item often, as in this case, the systematic name Agilent gives to
  reporters on the array corresponds to the genomic coordinates these
  reporters were designed to match. Therefore, if one decides to
  accept the mapping provided by the manufacturer, these systematic
  names can be used to generate a \Rclass{probeAnno} object.
\end{enumerate}

Here, we show the second option and use the systematic names of the
reporters, as provided in the raw data file.

\begin{Schunk}
\begin{Sinput}
> pA <- extractProbeAnno(RG, "agilent", genome="mouse", 
+    microarray="Agilent Tiling Chr17")
\end{Sinput}
\end{Schunk}

\phead{Genome annotation}
The provided Agilent example data relate to chromosome~17 of the Mouse
genome. We have used the package \Rpackage{biomaRt} to retrieve
annotated genes on this chromosome from the Ensembl
database\footnote{See the script
  \texttt{retrieveGenomicFeatureAnnotation.R} in the package's
  \texttt{scripts} directory for details.}.

\begin{Schunk}
\begin{Sinput}
> load(file=file.path(agiDir,"mm9chr17.RData"))
\end{Sinput}
\end{Schunk}

\phead{Data processing}
We preprocess the data in the same way as the previous example data
set.
\begin{Schunk}
\begin{Sinput}
> X <- preprocess(RG[RG$genes$ControlType==0,], method="nimblegen", 
+                 idColumn="ProbeName")
> sampleNames(X) <- X$SlideNumber
\end{Sinput}
\end{Schunk}
We visualize the data in the region around the start site of the gene
\textit{Rab11b} on chromosome~17.
For setting the parameters of the sliding-window smoothing, we
first investigate the spacing between adjacent reporter matches on the
genome.
\begin{Schunk}
\begin{Sinput}
> probeDists <- diff(pA["17.start"])
> br <- c(0, 100, 200, 300, 500, 1000, 10000, max(probeDists))
> table(cut(probeDists, br))
\end{Sinput}
\begin{Soutput}
         (0,100]        (100,200]        (200,300]        (300,500] 
               0              338              221              104 
     (500,1e+03]    (1e+03,1e+04] (1e+04,2.13e+07] 
              71               39                2 
\end{Soutput}
\end{Schunk}
The majority of match positions are \mbox{100--300~bp} apart.
\begin{Schunk}
\begin{Sinput}
> smoothX <- computeRunningMedians(X, modColumn="Antibody",
+               winHalfSize=500, min.probes=3, probeAnno=pA)
> sampleNames(smoothX) <- paste(sampleNames(X),"smooth",sep=".")
\end{Sinput}
\end{Schunk}
%
We visualize the data after smoothing in the region around
the start site of the gene \textit{Rab11b} on chromosome~17.
%
\begin{Schunk}
\begin{Sinput}
> combX <- combine(X, smoothX)
> plot(combX, pA, chr="17", coord=33887000+c(0, 13000),
+      gff=mm9chr17, maxInterDistance=450, paletteName="Paired")
\end{Sinput}
\end{Schunk}
\myincfig{Ringo-agiSmoothAlongChrom}{0.98\textwidth}{Normalized and
  smoothed Agilent reporter intensities around the TSS of the gene
  \textit{Rap11b}.}
See the result in Figure~\ref{Ringo-agiSmoothAlongChrom}.

\phead{Find ChIP-enriched regions}
We compare two approaches to determine the threshold $y_0$ above
which smoothed reporter levels are considered to indicate enrichment.
The first one is the non-parametric approach that we introduced
before~(see Section~\ref{sec-threshold}). The second one is
similar. Only both the null distribution and the alternative
distribution are assumed to be Gaussians. The threshold is minimal
reporter levels with a sufficiently small $p$-value under the Gaussian
null distribution.
\begin{Schunk}
\begin{Sinput}
> y0  <- upperBoundNull(exprs(smoothX))
> y0G <- twoGaussiansNull(exprs(smoothX))
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Sinput}
> hist(exprs(smoothX), n=100, main=NA, 
+      xlab="Smoothed expression level [log2]")
> abline(v=y0, col="red", lwd=2)
> abline(v=y0G, col="blue", lwd=2)
> legend(x="topright", lwd=2, col=c("red","blue"),
+        legend=c("Non-parametric symmetric Null", "Gaussian Null"))
\end{Sinput}
\end{Schunk}
Even though the non-parametric estimate is usually preferable,
with few data points such as in this case, the estimate derived from a
Gaussian null distribution might provide a better threshold for
enrichment. We are going to use this threshold $y_{0G}$ for the
identification of ChIP-enriched regions.
\begin{Schunk}
\begin{Sinput}
> chersX <- findChersOnSmoothed(smoothX, probeAnno=pA, threshold=y0G,
+                               cellType="Tc1Liver")
> chersX <- relateChers(chersX, gff=mm9chr17, upstream=5000)
\end{Sinput}
\end{Schunk}

We find 14 ChIP-enriched regions.

\begin{Schunk}
\begin{Sinput}
> chersXD <- as.data.frame(chersX)
> head(chersXD[order(chersXD$maxLevel, decreasing=TRUE),])
\end{Sinput}
\begin{Soutput}
                                           name chr    start      end cellType
6   Tc1Liver.H3K4me3Tc1Liver.smooth.chr17.cher6  17 33725511 33734380 Tc1Liver
4   Tc1Liver.H3K4me3Tc1Liver.smooth.chr17.cher4  17 33640444 33642033 Tc1Liver
5   Tc1Liver.H3K4me3Tc1Liver.smooth.chr17.cher5  17 33642962 33644147 Tc1Liver
10 Tc1Liver.H3K4me3Tc1Liver.smooth.chr17.cher10  17 33795820 33798894 Tc1Liver
2   Tc1Liver.H3K4me3Tc1Liver.smooth.chr17.cher2  17 33634150 33635943 Tc1Liver
12 Tc1Liver.H3K4me3Tc1Liver.smooth.chr17.cher12  17 33814429 33816303 Tc1Liver
                 antibody           features maxLevel     score
6  H3K4me3Tc1Liver.smooth ENSMUSG00000024300 5.511084 121.48346
4  H3K4me3Tc1Liver.smooth                    5.079047  23.08641
5  H3K4me3Tc1Liver.smooth                    5.008862  15.14508
10 H3K4me3Tc1Liver.smooth ENSMUSG00000059208 4.938792  43.52889
2  H3K4me3Tc1Liver.smooth                    4.930897  31.81080
12 H3K4me3Tc1Liver.smooth ENSMUSG00000059208 4.541763  17.97481
\end{Soutput}
\end{Schunk}

This concludes the example of how \Rpackage{Ringo} can be used to
analyze other types of ChIP-chip data.

\section{Concluding Remarks}

The package \Rpackage{Ringo} aims to facilitate the analysis
ChIP-chip readouts.
We constructed it during the analysis of a ChIP-chip experiment for
the genome-wide identification of modified histone sites on data
gained from NimbleGen two-color microarrays.
Analogous two-color microarray platforms, however, can also be
processed.
Key functionalities of \Rpackage{Ringo} are data read-in,
quality assessment, preprocessing of the raw data,
and visualization of the raw and preprocessed data.
The package also contains algorithms for the detection of
for ChIP-enriched genomic regions.
While one of these algorithm worked quite well with our data,
we do not claim it to be the definite algorithm for that task.

\phead{Further reading}
For an extended tutorial about how to use \Rpackage{Ringo} for the
analysis of ChIP-chip data, please refer to \cite{Toedling2008} and
the corresponding Bioconductor package \Rpackage{ccTutorial}.

\phead{Package versions}
This vignette was generated using the following package versions:

\begin{itemize}\raggedright
  \item R version 2.12.0 RC (2010-10-11 r53293), \verb|i386-pc-mingw32|
  \item Base packages: base, datasets, grDevices, graphics, grid,
    methods, stats, utils
  \item Other packages: Biobase~2.10.0, Matrix~0.999375-44,
    RColorBrewer~1.0-2, Ringo~1.14.0, lattice~0.19-13, limma~3.6.1,
    mclust~3.4.7
  \item Loaded via a namespace (and not attached):
    AnnotationDbi~1.12.0, DBI~0.2-5, KernSmooth~2.23-4, RSQLite~0.9-2,
    annotate~1.28.0, genefilter~1.32.0, splines~2.12.0,
    survival~2.35-8, tools~2.12.0, xtable~1.5-6
\end{itemize}
%\small
\section*{Acknowledgments}

Many thanks to Wolfgang Huber, Oleg Sklyar,
Tammo Kr\"uger, Richard Bourgon,
and Matt Ritchie for source code contributions to
and lots of helpful suggestions on Ringo, 
Todd Richmond and NimbleGen Systems, Inc. for 
providing us with the example ChIP-chip data.\\
This work was supported by the
European Union (FP6 HeartRepair, LSHM-CT-2005-018630).

\small
%%% BIBLIOGRAPHY STARTS HERE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bibliographystyle{abbrvnat}
\bibliography{Ringo-Bibliography}

\end{document}
