%\VignetteIndexEntry{estrogen}
%\VignetteDepends{Biobase,affy}
%\VignetteKeywords{Expression Analysis}
%\VignettePackage{}

\documentclass[a4paper]{article}
\usepackage{bioCexercises}

\usepackage{times}
\usepackage{a4wide}
\usepackage{/home/tliu/arch/x86_64/R-2.3-patched/share/texmf/Sweave}
\begin{document}

\thetitle{Working with Affymetrix data: estrogen, a 2x2 factorial design 
example}
\theoccasion{June 2004}
\theauthor{Robert Gentleman, Wolfgang Huber}


\begin{exercises}
%%--------------------Ex.1------------------------------ 
\item {\bf Preliminaries.}  
To go through this exercise, you need to have installed R>=1.9.0, the
libraries \Rpack{Biobase}, \Rpack{affy}, \Rpack{hgu95av2},
\Rpack{hgu95av2cdf}, and \Rpack{vsn} from the Bioconductor release 1.4.


\begin{Schunk}
\begin{Sinput}
> library(affy)
> library(estrogen)
> library(vsn)
> library(genefilter)
\end{Sinput}
\end{Schunk}

%%--------------------Ex.2------------------------------ 
\item {\bf Load the data.}  
\begin{exercise}
\item Find the directory where the example cel files are. The directory path
should end in\\
\file{.../R/library/estrogen/extdata}. 
\begin{Schunk}
\begin{Sinput}
> datadir = system.file("extdata", package = "estrogen")
> datadir
\end{Sinput}
\begin{Soutput}
[1] "/tmp/Rinst1652618732/estrogen/extdata"
\end{Soutput}
\begin{Sinput}
> dir(datadir)
\end{Sinput}
\begin{Soutput}
 [1] "bad.cel"         "estrogen.txt"    "high10-1.cel"    "high10-2.cel"   
 [5] "high48-1.cel"    "high48-2.cel"    "low10-1.cel"     "low10-2.cel"    
 [9] "low48-1.cel"     "low48-2.cel"     "phenoData.txt"   "targLimma.txt"  
[13] "workspace.RData"
\end{Soutput}
\begin{Sinput}
> setwd(datadir)
\end{Sinput}
\end{Schunk}

The function \code{system.file} here is used to find the subdirectory
\code{extdata} of the estrogen package on your computer's harddisk.  To use
your own data, set \code{datadir} to the appropriate path instead.

%%---------- 2b
\item The file \file{estrogen.txt} contains information on the samples that
were hybridized onto the arrays. Look at it in a text editor. Again, to use
your own data, you need to preprare a similar file with the appropriate
information on your arrays and samples.
To load it into a \code{phenoData} object
\begin{Schunk}
\begin{Sinput}
> pd = read.phenoData("estrogen.txt", header = TRUE, row.names = 1)
> pData(pd)
\end{Sinput}
\begin{Soutput}
             estrogen time.h
low10-1.cel    absent     10
low10-2.cel    absent     10
high10-1.cel  present     10
high10-2.cel  present     10
low48-1.cel    absent     48
low48-2.cel    absent     48
high48-1.cel  present     48
high48-2.cel  present     48
\end{Soutput}
\end{Schunk}
\code{phenoData} objects are where the Bioconductor stores information about 
samples, for example, treatment conditions in a cell line experiment or 
clinical or histopathological characteristics of tissue biopsies. 
The \code{header} option lets the \code{read.phenoData} function know that
the first line in the file contains column headings, and the 
\code{row.names} option indicates that the first column of the file contains 
the row names.


%%---------- 2c
\item Load the data from the CEL files as well as the phenotypic data 
into an \code{AffyBatch} object. 

\begin{Schunk}
\begin{Sinput}
> a = ReadAffy(filenames = rownames(pData(pd)), phenoData = pd, 
+     verbose = TRUE)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Sinput}
> a
\end{Sinput}
\begin{Soutput}
AffyBatch object
size of arrays=640x640 features (25611 kb)
cdf=HG_U95Av2 (12625 affyids)
number of samples=8
number of genes=12625
annotation=hgu95av2
\end{Soutput}
\end{Schunk}

\end{exercise}

\begin{figure}[h]
\begin{center}	
  \includegraphics[width=0.49\textwidth]{estrogen-image1}
  \includegraphics[width=0.49\textwidth]{estrogen-image2}
  \caption{\label{estrogen-image} see exercise 4.}
 \end{center}
\end{figure}

%%--------------------Ex.3------------------------------ 
\item {\bf Normalization.}  
\begin{exercise}
\item Now we can use the function \func{expresso} to normalize the
data and calculate expression values. 

\begin{Schunk}
\begin{Sinput}
> x <- expresso(a, bg.correct = FALSE, normalize.method = "vsn", 
+     normalize.param = list(subsample = 1000), pmcorrect.method = "pmonly", 
+     summary.method = "medianpolish")
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Sinput}
> x
\end{Sinput}
\begin{Soutput}
Expression Set (exprSet) with 
	12625 genes
	8 samples
		 phenoData object with 2 variables and 8 cases
	 varLabels
		estrogen: read from file
		time.h: read from file
\end{Soutput}
\end{Schunk}

The parameter \code{subsample} determines the time consumption, as well as
the precision of the calibration. The default (if you leave away the parameter
\code{normalize.param  = list(subsample=1000)}) is 20000; here we chose a smaller
value for the sake of demonstration. There is the possibility that
\code{expresso} is not working properly due to  memory problems
(normally it should work with 384 MB). Then you should end this session, 
start a new R session, and load the libraries and data by typing
 
\begin{Schunk}
\begin{Sinput}
> library(affy)
> library(estrogen)
> library(vsn)
> datadir = system.file("extdata", package = "estrogen")
> setwd(datadir)
> load("workspace.RData")
\end{Sinput}
\end{Schunk}
\code{image.RData} includes the expression set \code{x} and the 
affybatch \code{a}. Then you can continue with the next paragraph.

\item What are other available methods for \textit{normalization}, and
\textit{expression value calculation}? You can consult the vignettes for the
\Rpack{affy} package for this. Choose another method (for example,
MAS5 or RMA) and compare the results. For example, look at scatterplots of the
probe set summaries for the same arrays between different methods.

\begin{Schunk}
\begin{Sinput}
> normalize.methods(a)
\end{Sinput}
\begin{Soutput}
[1] "constant"         "contrasts"        "invariantset"     "loess"           
[5] "qspline"          "quantiles"        "quantiles.robust" "vsn"             
\end{Soutput}
\begin{Sinput}
> express.summary.stat.methods
\end{Sinput}
\begin{Soutput}
[1] "avgdiff"      "liwong"       "mas"          "medianpolish" "playerout"   
\end{Soutput}
\end{Schunk}
\end{exercise}


%%--------------------Ex.4------------------------------ 
\item {\bf Looking at the CEL file images.}  
The \func{image} function allows us to look at the spatial 
distribution of the intensities on a chip. This can be useful for 
quality control. Fortunately, all of the 8 celfiles that we have 
just loaded do not show any remarkable spatial artifacts 
(see Fig.~\ref{estrogen-image}).
%
\begin{Schunk}
\begin{Sinput}
> image(a[, 1])
\end{Sinput}
\end{Schunk}
%
But we have another example:
%
\begin{Schunk}
\begin{Sinput}
> badc = ReadAffy("bad.cel")
> image(badc)
\end{Sinput}
\end{Schunk}
%
Note that in these images, row 1 is at the bottom, and row 640 
at the top.

\begin{figure}[h]
\begin{center}	
  \includegraphics[width=0.49\textwidth]{estrogen-hist}
  \caption{\label{estrogen-hist} see exercise 5.}
 \end{center}
\end{figure}

%

%%--------------------Ex.5------------------------------ 
\item {\bf Histograms.}  
Another way to visualize what is going on on a chip is to 
look at the histogram of its intensity distribution.  
Because of the large dynamical range ($O(10^4)$), it is 
useful to look at the log-transformed values
(see Fig.~\ref{estrogen-hist}):
%
\begin{Schunk}
\begin{Sinput}
> hist(log2(intensity(a[, 4])), breaks = 100, col = "blue")
\end{Sinput}
\end{Schunk}
%
\begin{figure}[h]
\begin{center}	
  \includegraphics[width=0.8\textwidth]{estrogen-boxplotb}
  \caption{\label{estrogen-boxplot} see exercise 6.}
 \end{center}
\end{figure}

\begin{figure}[h]
\begin{center}	
  \includegraphics[width=0.49\textwidth]{estrogen-scp1}
  \includegraphics[width=0.49\textwidth]{estrogen-scp2} \\
  \includegraphics[width=0.49\textwidth]{estrogen-scp3}
  \includegraphics[width=0.49\textwidth]{estrogen-scp4}
  \caption{\label{estrogen-scp} see exercise 7.}
 \end{center}
\end{figure}

\begin{figure}[h]
\begin{center}	
  \includegraphics[width=0.8\textwidth]{estrogen-heatmap}
  \caption{\label{estrogen-heatmap} see exercise 8.}
 \end{center}
\end{figure}

\begin{figure}[h]
\begin{center}	
  \includegraphics[width=\textwidth]{estrogen-eff-show}
  \caption{\label{estrogen-eff} see exercise 9.}
 \end{center}
\end{figure}

%%--------------------Ex.6------------------------------ 
\item {\bf Boxplot.}  
To compare the intensity distribution across several chips, 
we can look at the boxplots, both of the raw intensities 
\code{a} and the normalized probe set values \code{x}
(see Fig.~\ref{estrogen-boxplot}):
%
\begin{Schunk}
\begin{Sinput}
> boxplot(a, col = "red")
> boxplot(data.frame(exprs(x)), col = "blue")
\end{Sinput}
\end{Schunk}
%
In the commands above, note the different syntax: \code{a} is an 
object of type \code{AffyBatch}, and the \code{boxplot} function 
has been programmed to know automatically what to do with it. 
\code{exprs(x)} is an object of type \code{matrix}. 
What happens if you do \code{boxplot(x)} or 
\code{boxplot(exprs(x))}?

\begin{Schunk}
\begin{Sinput}
> class(x)
\end{Sinput}
\begin{Soutput}
[1] "exprSet"
attr(,"package")
[1] "Biobase"
\end{Soutput}
\begin{Sinput}
> class(exprs(x))
\end{Sinput}
\begin{Soutput}
[1] "matrix"
\end{Soutput}
\end{Schunk}

%%--------------------Ex.7------------------------------ 
\item {\bf Scatterplot.}  
The scatterplot is a visualization that is useful for assessing the
variation (or reproducibility, depending on how you look at it)
between chips. We can look at all probes, the perfect match probes 
only, the mismatch probes only, and of course also at the 
normalized, probe-set-summarized data:
(see Fig.~\ref{estrogen-scp}):
%
%
Why are the arrays that were made at $t=48$h much brighter than those at
$t=10$h?  Look at histograms and scatterplots of the probe intensities from
chips at 10h and at48h to see whether you can find any evidence of saturation,
changes in experimental protocol, or other quality problems. Distinguish
between probes that are supposed to represent genes (you can access these
e.g. through the functions \code{pm()}) and control probes.

\begin{Schunk}
\begin{Sinput}
> plot(exprs(a)[, 1:2], log = "xy", pch = ".", main = "all")
> plot(pm(a)[, 1:2], log = "xy", pch = ".", main = "pm")
> plot(mm(a)[, 1:2], log = "xy", pch = ".", main = "mm")
> plot(exprs(x)[, 1:2], pch = ".", main = "x")
\end{Sinput}
\end{Schunk}

%%----------Ex.8----------
\item {\bf Heatmap.}  
Select the 50 genes with the highest variation (standard deviation) 
across chips. (see Fig.~\ref{estrogen-heatmap}):

\begin{Schunk}
\begin{Sinput}
> rsd <- rowSds(exprs(x))
> sel <- order(rsd, decreasing = TRUE)[1:50]
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Sinput}
> heatmap(exprs(x)[sel, ], col = gentlecol(256))
\end{Sinput}
\end{Schunk}

%%----------Ex.9----------
\item {\bf ANOVA.}  
Now we can start analysing our data for biological effects.
We set up a linear model with main effects for the level of estrogen  
(\code{estrogen}) and the time (\code{time.h}). Both are factors with 
2 levels.

\begin{Schunk}
\begin{Sinput}
> lm.coef = function(y) lm(y ~ estrogen * time.h)$coefficients
> eff = esApply(x, 1, lm.coef)
\end{Sinput}
\end{Schunk}

%% For all k = 1:12625:
%%
%% eff[1,k] + eff[2,k]*est + eff[3,k]*tim + res[,k] ==  exprs(x)[k,] 
%%
%% where tim = pData(x)$time.h
%%       est = as.numeric(factor(pData(x)$estrogen))-1

For each gene, we obtain the fitted coefficients for main effects and 
interaction:
\begin{Schunk}
\begin{Sinput}
> dim(eff)
\end{Sinput}
\begin{Soutput}
[1]     4 12625
\end{Soutput}
\begin{Sinput}
> rownames(eff)
\end{Sinput}
\begin{Soutput}
[1] "(Intercept)"            "estrogenpresent"        "time.h"                
[4] "estrogenpresent:time.h"
\end{Soutput}
\begin{Sinput}
> affyids <- colnames(eff)
\end{Sinput}
\end{Schunk}

Let's bring up the mapping from the vendor's probe set identifier to 
gene names.
\begin{Schunk}
\begin{Sinput}
> library(hgu95av2)
> ls("package:hgu95av2")
\end{Sinput}
\begin{Soutput}
 [1] "hgu95av2"             "hgu95av2ACCNUM"       "hgu95av2CHR"         
 [4] "hgu95av2CHRLENGTHS"   "hgu95av2CHRLOC"       "hgu95av2ENZYME"      
 [7] "hgu95av2ENZYME2PROBE" "hgu95av2GENENAME"     "hgu95av2GO"          
[10] "hgu95av2GO2ALLPROBES" "hgu95av2GO2PROBE"     "hgu95av2LOCUSID"     
[13] "hgu95av2MAP"          "hgu95av2MAPCOUNTS"    "hgu95av2OMIM"        
[16] "hgu95av2ORGANISM"     "hgu95av2PATH"         "hgu95av2PATH2PROBE"  
[19] "hgu95av2PFAM"         "hgu95av2PMID"         "hgu95av2PMID2PROBE"  
[22] "hgu95av2PROSITE"      "hgu95av2QC"           "hgu95av2QCDATA"      
[25] "hgu95av2REFSEQ"       "hgu95av2SUMFUNC"      "hgu95av2SYMBOL"      
[28] "hgu95av2UNIGENE"     
\end{Soutput}
\end{Schunk}

Let's now first look at the {\bf estrogen main effect}, and print the
top 3 genes with largest effect in one direction, as well as in the
other direction. Then, look at the {\bf estrogen:time interaction}.

%
\begin{Schunk}
\begin{Sinput}
> lowest <- sort(eff[2, ], decreasing = FALSE)[1:3]
> mget(names(lowest), hgu95av2GENENAME)
\end{Sinput}
\begin{Soutput}
$`36617_at`
[1] "inhibitor of DNA binding 1, dominant negative helix-loop-helix protein"

$`846_s_at`
[1] "BCL2-antagonist/killer 1"

$`37294_at`
[1] "B-cell translocation gene 1, anti-proliferative"
\end{Soutput}
\begin{Sinput}
> highest <- sort(eff[2, ], decreasing = TRUE)[1:3]
> mget(names(highest), hgu95av2GENENAME)
\end{Sinput}
\begin{Soutput}
$`910_at`
[1] "thymidine kinase 1, soluble"

$`31798_at`
[1] "trefoil factor 1 (breast cancer, estrogen-inducible sequence expressed in)"

$`40117_at`
[1] "MCM6 minichromosome maintenance deficient 6 (MIS5 homolog, S. pombe) (S. cerevisiae)"
\end{Soutput}
\begin{Sinput}
> hist(eff[4, ], breaks = 100, col = "blue", main = "estrogen:time interaction")
> highia <- sort(eff[4, ], decreasing = TRUE)[1:3]
> mget(names(highia), hgu95av2GENENAME)
\end{Sinput}
\begin{Soutput}
$`1651_at`
[1] "ubiquitin-conjugating enzyme E2C"

$`40412_at`
[1] "pituitary tumor-transforming 1"

$`1945_at`
[1] "cyclin B1"
\end{Soutput}
\end{Schunk}


\end{exercises}
\end{document}



