\documentclass{beamer}
\usetheme{Warsaw}
\author{Sean Davis}
\title{Introduction to the ExpressionSet}


\section{Background}

<<setup,results='hide',echo=FALSE>>=
library(knitr)
opts_chunk$set(cache=TRUE,out.width='0.5\\textwidth')
@ 

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}[fragile]{Our Goal}
\begin{center}
\includegraphics[width=0.6\textwidth]{figure/heatmap70}
\end{center}
\end{frame}

\begin{frame}[fragile]{The Genomics Table Triumvirate}
\includegraphics[width=1.0\textwidth]{GenomicTableTriumvirate}
\end{frame}

<<geoquery,echo=FALSE,results='hide',message=FALSE,warnings=FALSE>>=
library(GEOquery)
eset = getGEO('GSE1577')[[1]]
@ 

\begin{frame}[fragile]{Gene information--featureData}
<<featureData,echo=TRUE,results='hide'>>=
head(fData(eset))
@ 

\scriptsize
<<featureData2,echo=FALSE>>=
head(fData(eset)[,c(11,12,13,10)])
@ 

\end{frame}


\begin{frame}[fragile]{Sample Information--phenoData}
<<phenoData,echo=TRUE,results='hide'>>=
head(pData(eset))
@ 

\scriptsize

<<phenoData2,echo=FALSE>>=
pData(eset)[c(1:3,10:12),c(1:2,4,6:8,10:11)]
@ 

\end{frame}


\begin{frame}[fragile]{Assay Data (expression)--assayData}

<<assayData,echo=TRUE,results='hide'>>=
head(assayDataElement(eset,'exprs')) 
# OR 
head(exprs(eset))
@ 

\scriptsize
<<assayData2,echo=FALSE>>=
head(assayDataElement(eset,'exprs')) # head(exprs(eset))
@ 

\end{frame}

\begin{frame}{The Parts of an ExpressionSet}
\begin{description}
\item[The Data] \hfill \\
typically a \textit{matrix}-like object (or multiple matrices)
    \pause
\item[The Sample Annotations] \hfill \\
typically a dataframe-like object
    \pause
\item[The Feature Annotation (Gene information)] \hfill \\
typically a dataframe-like object
\end{description}
\end{frame}

\section{Working with an Example ExpressionSet}

\begin{frame}[fragile]{An Example ExpressionSet}
\small
<<getgse,messages=FALSE,warnings=FALSE>>=
browseURL("http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE1577")
library(GEOquery)
eset = getGEO('GSE1577')[[1]]
@ 
\end{frame}

\begin{frame}[fragile]{An Example ExpressionSet}
\small
<<exprs1>>=
class(eset)
@ 

<<exprs2,results='hide'>>=
help(ExpressionSet)
help('ExpressionSet-class')
@ 

\end{frame}

\begin{frame}[fragile]{An Example ExpressionSet}
\scriptsize
<<exprs10>>=
show(eset)
@ 
\end{frame}

\begin{frame}[fragile]{An Example ExpressionSet}
\scriptsize
<<exprs20>>=
# dimensions of the ExpressionSet
dim(eset)
# Number of features
nrow(eset) 
# number of samples
ncol(eset) 
@ 
\end{frame}

\begin{frame}[fragile]{An Example ExpressionSet}
\scriptsize
<<exprs30>>=
# "names" of samples--must be unique
sampleNames(eset)[1:8]
# "names" of features--must be unique
featureNames(eset)[1:8]
@ 
\end{frame}  

\begin{frame}[fragile]{Sample Information--phenoData}
\scriptsize
<<phenoData22,echo=TRUE,results='hide'>>=
head(pData(eset))
class(pData(eset))
colnames(pData(eset))
all.equal(sampleNames(eset),rownames(pData(eset)))
summary(pData(eset))
@ 
\end{frame}

\begin{frame}[fragile]{Gene information--featureData}
<<featureData22,echo=TRUE,results='hide'>>=
head(fData(eset))
class(fData(eset))
colnames(fData(eset))
all.equal(featureNames(eset),rownames(fData(eset)))
summary(fData(eset))
@ 
\end{frame}

\begin{frame}[fragile]{Assay Data (expression)--assayData}
<<assayData22,echo=TRUE,results='hide'>>=
head(assayDataElement(eset,'exprs')) 
# OR 
head(exprs(eset))
class(exprs(eset))
summary(exprs(eset))
all.equal(colnames(exprs(eset)),sampleNames(eset))
all.equal(rownames(exprs(eset)),featureNames(eset))
@ 
\end{frame}

\begin{frame}[fragile]{Subsetting ExpressionSets}
\begin{itemize}
  \item Subsetting works similarly to data.frames or matrices
  \item Columns represent samples
  \item Rows represent genes (or features)
\end{itemize}
<<subsetting10,results='hide'>>=
eset[1:10,]
eset[,1:10]
@ 
\end{frame}

\begin{frame}[fragile]{subsetting ExpressionSets}
How can we subset our ExpressionSet to include only the ``Bone Marrow'' samples?
\scriptsize
\pause
<<subsetting20>>=
levels(pData(eset)$source_name_ch1)
@ 
\pause
<<subsetting30>>=
eset[,pData(eset)$source_name_ch1=="Bone marrow sample"]
@ 
\end{frame}

\begin{frame}[fragile]{Heatmap Preliminaries}
\begin{itemize}
\item Load the gplots library and look at the heatmap.2 help
<<heatmapprelim,results='hide'>>=
library(gplots)
?heatmap.2
@ 
\item The heatmap.2 function takes as a minimal input a matrix of values.
\end{itemize}
\end{frame} 

\begin{frame}[fragile]{Prepare for heatmap}
Are our values in the log space?
\pause
<<heatmap10,results='hide'>>=
summary(exprs(eset))
hist(exprs(eset))
@ 
\end{frame}

\begin{frame}[fragile]{Prepare for heatmap}
How can we put the expression values in log2 space?
\pause
<<heatmap20,results='hide'>>=
summary(log2(exprs(eset)))
hist(log2(exprs(eset)))
@ 
\end{frame}

\begin{frame}[fragile]{Prepare for heatmap}
How can we replace the expression values in our ExpressionSet with the log2-transformed values?
\pause
<<heatmap30,results='hide'>>=
exprs(eset) = log2(exprs(eset))
hist(exprs(eset))
@
\end{frame}

\begin{frame}[fragile]{Prepare for heatmap}
How can we compute the expression standard deviation for each feature in the ExpressionSet?
\pause
<<heatmap40,results='hide'>>=
stdDev = apply(exprs(eset),1,sd)
hist(stdDev)
@ 
\end{frame}

\begin{frame}[fragile]{Prepare for heatmap}
How can we subset the expression data to include the top 20 most variable (most informative) genes?
\pause
<<heatmap50,results='hide'>>=
eset2 = eset[order(stdDev,decreasing=TRUE)[1:20],]
@ 
\end{frame}

\begin{frame}[fragile]{Heatmap}
\begin{center}
<<heatmap60,results='hide',fig.show='hide'>>=
heatmap.2(exprs(eset2),trace='none')
@
\end{center}

<<heatmap70,results='hide',echo=FALSE,fig.show='hide'>>=
heatmap.2(exprs(eset2),trace='none',labRow=fData(eset2)$'Gene Symbol',
  labCol=pData(eset2)$source_name_ch1)
@ 
\end{frame}

\begin{frame}{Challenge Exercises}
\begin{itemize}
  \item Use the pheatmap package to make a more interesting and informative heatmap that includes the sample types as a color bar.
  \item Make a multidimensional scaling (MDS) plot of the samples using the top 200 most variable genes.
  \item Use the featureData in the ExpressionSet and the grep function to construct a new ExpressionSet subset that contains transcription factor genes.
  \item Make a histogram of the Pearson correlation of the correlation between the first gene and all the other genes.
\end{itemize}
\end{frame}

\end{document}
