%\VignetteDepends{}
%\VignetteIndexEntry{R and Bioconductor Packages for Short Read Analysis}
%\VignettePackage{EMBL2010}

\documentclass{beamer}

\usepackage{BioconductorSlides}
\AtBeginSection[]{%
  \begin{frame}<beamer>{Outline}
    \tableofcontents
  \end{frame}}

\SweaveOpts{keep.source=TRUE}

\title{\R{} / \Bioconductor{} Packages for Short Read Analysis}
\author{Martin Morgan (\href{mailto:mtmorgan@fhcrc.org}{mtmorgan@fhcrc.org})}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
%% \institute[EMBL]{}
\date{14-18 June, 2010}


\begin{document}

<<setup, echo=FALSE>>=
options(width = 40)
@ 

\frame{\titlepage}

\begin{frame}{Announcements / Acknowledgments}
  Announcements
  \begin{itemize}
  \item Annual conference in Seattle, 28-30 July (`Developer Day' 28
    July) \url{https://secure.bioconductor.org/BioC2010}
  \item Two positions available -- software and web development
    \url{http://www.fhcrc.org/about/jobs/index.html} and search for
    positions 23129, 23133.
  \end{itemize}
  Bioconductor team
  \begin{itemize}
  \item Patrick Aboyoun, Marc Carlson, Nishant Gopalakrishnan, Herv\'e
    Pag\`es, Chao-Jen Wong
  \item Wolfgang Huber, Vince Carey, Rafael Irizarry, Robert
    Gentleman.
  \end{itemize}
  Resources
  \begin{itemize}
  \item Bioconductor web site \url{http://bioconductor.org}
  \end{itemize}
  
\end{frame}

\section{Work flow}

\subsection{Experiments and Technologies}
\begin{frame}{Experiments and Technologies}
  Sequence-based experiments
  \begin{itemize}
  \item ChIP, Differential expression, RNA-seq, Metagenomic, \ldots
  \end{itemize}
  Technology
  \begin{itemize}
  \item Illumina, Roche / 454, AB SOLiD, Complete Genomics, \ldots
  \item Third-generation: PacBio, Ion Torrent, Oxford Nanopore, \ldots
  \end{itemize}
  Relevant issues in analysis
  \begin{itemize}
  \item Experimental design, replication, sample preparation artifacts
  \end{itemize}
\end{frame}

\subsection{Pre-processing}
\begin{frame}{Pre-processing}
  Vendor and third-party
  \begin{itemize}
  \item Image processing, base calling
  \item Machine quality assessment
  \item Alignment
  \end{itemize}
  \Bioconductor{} packages
  \begin{itemize}
  \item Quality assessment and representation: \Biocpkg{ShortRead},
    \Biocpkg{GenomicRanges}
  \item Read remediation, trimming, primer removal, specialized
    manipulation: \Biocpkg{IRanges}, \Biocpkg{ShortRead},
    \Biocpkg{Biostrings}
  \item Specialized alignment tasks: \Biocpkg{Biostrings},
    \Biocpkg{BSgenome}
  \end{itemize}
\end{frame}

\subsection{Analysis}
\begin{frame}{Analysis}
  Domain-specific, e.g.,
  \begin{itemize}
  \item ChIP-seq: \Biocpkg{chipseq}, \Biocpkg{ChIPseqR},
    \Biocpkg{CSAR}, \Biocpkg{BayesPeak}
  \item Differential expression: \Biocpkg{DESeq}, \Biocpkg{edgeR},
    \Biocpkg{baySeq}
  \item RNA-seq: \Biocpkg{Genominator}
  \end{itemize}
  Examples (`experiment data' packages)
  \begin{itemize}
  \item \Biocdatapkg{EatonEtAlChIPseq}, \Biocdatapkg{leeBamViews} %
  \end{itemize}
\end{frame}

\subsection{Annotation and Integration}
\begin{frame}{Annotation and Integration}
  Annotation
  \begin{itemize}
  \item Gene-centric: \Biocpkg{AnnotationDbi}, \Rpkg{org.*.db},
    \Biocpkg{KEGG.db}, \Biocpkg{GO.db}, \Biocpkg{Category},
    \Biocpkg{GOstats}
  \item Genome coordinate: \Biocpkg{GenomicFeatures},
    \Biocpkg{ChIPpeakAnno}
  \end{itemize}
  Integration
  \begin{itemize}
  \item Digital and microarray differential expression
  \item RNAseq and gene ontology / pathway,  \Biocpkg{goseq}
  \item HapMap, 1000 genomes, UCSC, Sequence Read Archive, GEO,
    ArrayExpress, \Biocpkg{rtracklayer}, \Biocpkg{biomaRt},
    \Biocpkg{Rsamtools}, \Biocpkg{GEOquery}, \Biocpkg{SRAdb}
  \end{itemize}
\end{frame}

\section{Examples (Psuedo-Code)}

\subsection{Quality Assessment}
\begin{frame}[fragile]{Quality Assessment}
<<qa, eval=FALSE>>=
library(ShortRead)
dir <-                 # Input
    "/mnt/fred/solexa/xxx/100524_HWI-EAS88_0005"
sp <- SolexaPath(dir)  # Many other formats
qa <- qa(sp)           # Collate statistics -- slow
rpt <- report(qa)      # Create report 
browseURL(rpt)         # View in browser
@   
\end{frame}

\subsection{454 Microbiome Pre-Processing}
\begin{frame}[fragile]{454 Microbiome Pre-Processing}
<<bar, eval=FALSE>>=
library(ShortRead)
dir <- "/not/public"
bar <- read454(dir)              # Input
code <- narrow(sread(bar), 1, 8) # Extract bar code
aBar <- bar[code == "AAGCGCTT"]  # Subset one bar code
noBar <-                         # Remove bar code
    narrow(aBar, 11, width(aBar)) 
pcrPrimer <- "GGACTACCVGGGTATCTAAT"
trimmed <-                       # Remove primer
    trimLRPatterns(pcrPrimer, noBar, Lfixed=FALSE)
writeFastq(trimmed,              # Output
           file.path(dir, "trimmed.fastq"))
@ 
\end{frame}

\subsection{Differential Expression}
\begin{frame}[fragile]{Differential Expression}
<<DESeq, eval=FALSE>>=
library(DESeq)
tsvFile <-            # Input
    system.file("extra", "TagSeqExample.tab", 
                package="DESeq")
counts <- read.delim(tsvFile, header=TRUE, 
              stringsAsFactors=TRUE, row.names="gene")
condition <- factor(c("T", "T", "T", "Tb", "N", "N"))
cds <- newCountDataSet(counts, condition)
cds1 <-               # Effective library size
    estimateSizeFactors(cds0)
cds2 <-               # Variance, estimated from mean
    estimateVarianceFunctions(cds2)
res <-                # Negative binomial test
    nbinomTest(cds2, "T", "N")
@ 
\end{frame}

\section{ShortRead}

\subsection{Input and exploration}

\begin{frame}[fragile]{\Biocpkg{ShortRead} data input}

<<library, results=hide>>=
library(EatonEtAlChIPseq)
fl <- system.file("extdata", 
  "GSM424494_wt_G2_orc_chip_rep1_S288C_14.mapview.txt.gz",
  package="EatonEtAlChIPseq") 
aln <- readAligned(fl, type = "MAQMapview")
@ 
\end{frame}

\begin{frame}[fragile]{The \Rclass{AlignedRead} class}
<<aln>>=
aln
table(strand(aln), useNA="always")
@ 
\end{frame}

\begin{frame}[fragile]{Accessing reads, base quality, and other data}
<<sread>>=
head(sread(aln), 3)    
@ 
\end{frame}

\begin{frame}[fragile]{Alphabet by cycle}
  Expectation: nucleotide use independent of cycle
<<abc>>=
alnp <- aln[strand(aln) == "+"]
abc <- alphabetByCycle(sread(alnp))
class(abc)
abc[1:6,1:4]
@ 
\end{frame}

\begin{frame}[fragile]{Alphabet by cycle}
  \begin{columns}
    \column{.5\textwidth} 
    \Rfunction{matplot} takes a matrix and plots each column as a set
    of points
<<abc-plot,fig=TRUE,include=FALSE,prefix=FALSE>>=
tabc <- t(abc[1:4,])
matplot(tabc, type="l",
        lty=rep(1, 4))
@ 
    \column{.5\textwidth}
    \includegraphics[width=\textwidth,height=!]{abc-plot.pdf}
  \end{columns}
\end{frame}

\begin{frame}[fragile]{Quality by cycle}
  \begin{columns}
    \column{.45\textwidth} Encoded quality scores can be decoded to
    their numerical values and represented as a matrix. Calculating
    the average of the column means creates a vector of average
    quality scores across cycle.
<<quality-plot,fig=TRUE,include=FALSE,prefix=FALSE>>=
m <- as(quality(alnp), 
        "matrix")
plot(colMeans(m), type="b")
@ 
    \column{.5\textwidth}
    \includegraphics[width=\textwidth,height=!]{quality-plot.pdf}
  \end{columns}
\end{frame}

\subsection{Manipulation}

\begin{frame}[fragile]{Recoding and updating}
  \begin{enumerate}
  \item Access the chromosome information
  \item Extract the chromosome number from the factor level
  \item Recode the chromosome number to roman (!), create new levels,
    and update the chromosome
  \item Update the \Rclass{AlignedRead}
  \end{enumerate}
<<chr-recode>>=
chrom <- chromosome(alnp)
i <- sub("S288C_([[:digit:]]+)", "\\1", levels(chrom))
levels(chrom) <- paste("chr", as.roman(i), sep="")
alnp <- renew(alnp, chromosome=chrom)
@
\end{frame}

%% \subsection{Quality assessment}

%% \begin{frame}[fragile]{Quality assessment}
%%   Two-step process
%%   \begin{enumerate}
%%   \item \Rfunction{qa}: visit each input file and collate
%%     statistics. Long and computationally intensive; can be done in
%%     parallel.
%%   \item \Rfunction{report}: summarize collected statisitcs into an
%%     HTML-based report
%%   \end{enumerate}
%% <<qa-report,eval=FALSE>>=
%% bowtieDir <- "/path/to/alignments"
%% qa <- qa(bowtieDir, ".*map$", type="Bowtie")
%% rpt <- report(qa)
%% browseURL(rpt)
%% @ 
%% \end{frame}

\section{Resources}

\begin{frame}{Resources}
  \Bioconductor{} Web site
  \begin{itemize}
  \item \url{http://bioconductor.org}
  \item `\href{http://bioconductor.org/docs/install/}{Installation}',
    `\href{http://bioconductor.org/packages/release/Software.html}{Software}',
    and `\href{http://bioconductor.org/docs/mailList.html}{Mailing
      lists}' links.
  \end{itemize}
  Help in \R{}
  \begin{itemize}
  \item \Rfunction{help.start()} to view a help browser.
  \item \text{help(package = "Biostrings")}
  \item \texttt{?readAligned}
  \item \Rfunction{browseVignettes("GenomicRanges")}
  \end{itemize}
\end{frame}

\end{document}
