%\VignetteDepends{}
%\VignetteIndexEntry{Rsamtools and Work Flows with Larger Data}
%\VignettePackage{EMBL2010}

\documentclass{beamer}

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

\usepackage[authoryear,round]{natbib}
\bibliographystyle{plainnat}
\SweaveOpts{keep.source=TRUE}

\title{\Biocpkg{Rsamtools} and Work Flows with Larger Data}
\author{Martin Morgan} \institute[FHCRC]{Fred Hutchinson Cancer
  Research Center} \date{14-18 June, 2010}

\begin{document}

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

\frame{\titlepage}

\begin{frame}
  A time line
  \begin{itemize}
  \item Yesterday: generating and aligning sequences; wrestling with
    large data; establishing common work flows (e.g., ChIP-seq).
  \item Today: revising common work flows; coming to terms with data
    volume (e.g., multiplexing); analyzing designed experiments.
  \item Tomorrow: 100,000 genomes (George Church, New York Times, 7
    June 2010)
  \end{itemize}
  Themes
  \begin{itemize}
  \item Increasing confidence with early stages of the pipeline --
    reads, their qualities, and alignments \emph{per se} become less
    important.
  \item Increasing emphasis on experimental design.
  \item Increasing use of collections of whole genomes, e.g., as data
    bases to query against.
  \end{itemize}
\end{frame}

\section{Yesterday}

\subsection{Review: Work Flow}

\begin{frame}{Work flow}
  Prior to analysis
  \begin{itemize}
  \item Biological preparation, e.g., ChIP.
  \item `Sequencing': library preparation, cluster generation,
    imaging, \ldots
  \end{itemize}
  Analysis
  \begin{enumerate}
  \item Pre-processing, quality assessment, exploratory analysis
  \item Domain-specific analysis ( ChIP-seq, Digital gene expression,
    RNA-seq, Microbial / community structure, \ldots)
  \item Annotation \& integration
  \end{enumerate}\
\end{frame}

\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]{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=1, lwd=3)
@ 
    \column{.5\textwidth}
    \includegraphics[width=\textwidth,height=!]{abc-plot.pdf}
  \end{columns}
\end{frame}

\subsection{Annotation}

\begin{frame}{Annotation}
  \begin{itemize}
  \item Gene- (and chip-) centric
  \item Pathways (KEGG, GO)
  \item Community resources (e.g., BioMarts, UCSC)
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{\Biocpkg{AnnotationDbi}}
  \begin{itemize}
  \item \R{} packages with versioned data.
  \item Pre-built \Rpackage{org.*.db}, \Biocpkg{GO.db},
    \Biocpkg{KEGG.db} and custom-built.
  \end{itemize}  
  Example: starts / ends of yeast features, from SGD
<<AnnotationDbi, eval=FALSE, results=hide>>=
library(org.Sc.sgd.db)
ls('package:org.Sc.sgd.db') # Discovery
start <- toTable(org.Sc.sgdCHRLOC)
end <- toTable(org.Sc.sgdCHRLOCEND)
tbl <- merge(start, end)
@
\end{frame}

\begin{frame}[fragile]{\Biocpkg{biomaRt}}
  \begin{itemize}
  \item Web accessible annotations; from ENSEMBL
  \item Discovery: \Rfunction{listMarts}, \Rfunction{listDatasets}.
  \item Use: \Rfunction{useMart}.
  \end{itemize}
<<biomart1, eval=FALSE, results=hide>>=
library(biomaRt)
listMarts()
mart <- useMart("ensembl")
listDatasets(mart)
ens <- useMart("ensembl", 
               dataset="scerevisiae_gene_ensembl")
@
\end{frame}

\begin{frame}[fragile]{Extracting data with \Biocpkg{biomaRt}}
  \begin{itemize}
  \item Apply \emph{filters} (\Rfunction{listFilters}) and
    \emph{attributes} (\Rfunction{listAttributes})
  \end{itemize}
<<biomart2, eval=FALSE, results=hide>>=
head(listFilters(ens))
head(listAttributes(ens))
## example query
getBM(attributes=
        c("ensembl_gene_id","chromosome_name",
          "strand", "start_position", "end_position"), 
      filters="entrezgene",
      values=c(1466398,1466399,1466400), mart=ens)
@
\end{frame}

\begin{frame}[fragile]{\Biocpkg{rtracklayer}}
  Import UCSC Genome Browser data into \R{}
  \begin{itemize}
    \item Create a session: \Rfunction{browserSession}.
    \item List available genomes from UCSC: \Rfunction{ucscGenomes}.
    \item Set up a genome object: \Rfunction{genome}.
    \item List available tracks: \Rfunction{trackNames}.
  \end{itemize}
<<rtracklayer, eval=FALSE, results=hide>>=
library(rtracklayer)
session <- browserSession()
head(ucscGenomes())
genome(session) <- "hg19"
head(trackNames(session))
@
\end{frame}

\begin{frame}[fragile]{Managing tracks with \Biocpkg{rtracklayer}}
  \begin{itemize}
    \item Generate a query for UCSC: \Rfunction{ucscTableQuery}.
    \item Retrieve a UCSC track: \Rfunction{getTable}.
  \end{itemize}
<<rtracklayer2, results=hide, eval=FALSE>>=
## generate a query
query <- ucscTableQuery(session, "refGene")
## get the data
track <- getTable(query)
@

\begin{itemize}
\item Also possible to push tracks to UCSC
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Demo}
<<seq2anno, eval=FALSE>>=
system.file('script', 'seq2anno.R', package='CSAMA10')
@ 
\end{frame}

\section{Today}

\begin{frame}{Today}
  Limitations to the \Rclass{AlignedRead} class and
  \Biocpkg{ShortRead} work flow
  \begin{itemize}
  \item Hard to input an arbitrary subset of reads
  \item Sequence, quality, identifier and other information included,
    but not always necessary
  \item Reads must be aligned without indels or gaps
  \end{itemize}
\end{frame}

\subsection{Rsamtools}

\begin{frame}{\software{samtools} and \Biocpkg{Rsamtools}}
  \href{http://samtools.sourceforge.net/}{\software{samtools}}
  \begin{itemize}
  \item Data format -- text (SAM) and binary (BAM)
  \item Tools to manipulate (e.g., merge), analyze (e.g., pileup) and
    view
  \item Bindings for other languages, e.g.,
    \href{http://picard.sourceforge.net/index.shtml}{Picard}
  \end{itemize}
  \Biocpkg{Rsamtools}
  \begin{itemize}
  \item Input and represent BAM files.
  \item High-level: \Rfunction{readAligned}; with
    \Rfunarg{type="BAM"}; \Rfunction{readPileup}
  \item Flexible: \Rfunction{scanBam}
  \item Experiment-wide: \Rfunction{BamViews}
  \end{itemize}
\end{frame}

\begin{frame}{Input}
  \Rclass{ScanBamParam}
  \begin{description}
  \item[which] \Rclass{GRanges} selecting reference, genome
    coordinates, strand.
  \item[flag] select paired / mapped / mate mapped reads
  \item[what] fields to retrieve, e.g., query name, reference name,
    strand, position, width, cigar
  \end{description}
  Remote access
  \begin{itemize}
  \item E.g., 1000 genomes individual NA19240, chromosome 6, 'Solexa'
    reads, aligned with MAQ available via
    \href{ftp://ftp-trace.ncbi.nih.gov/1000genomes/ftp/pilot_data/data/NA19240/alignment/NA19240.chrom6.SLX.maq.SRP000032.2009_07.bam}{ftp}
  \end{itemize}
\end{frame}

\subsection{Gapped alignments}
\begin{frame}{Gapped alignments}
  The \Rclass{GappedAlignments} class in \Biocpkg{GenomicRanges} 
  \begin{itemize}
  \item \Rfunction{readGappedAlignments} uses \Rfunction{scanBam}
  \item Genomic coordinates, `cigar', covered intervals
  \item Cigar: run length encoding; M (match), I, D (insertion,
    deletion), N (skipped), S, H (soft, hard clip), P (padding). E.g.,
    \texttt{35M}, \texttt{18M2I15M}
  \item Accessors, subsetting, narrowing, \Rfunction{pintersect},
    \Rfunction{coverage}, \ldots
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Example}
<<scanBam-2, eval=FALSE>>=
## reads on chr 6 overlapping 100000-110000
which <- GRange("6", IRanges(100000, 110000))
param <- ScanBamParam(which=which)
## na19240url <- ftp://ftp-trace.ncbi.nih.gov/1000ge...
na19240bam <- scanBam(na19240url, param=param)
@ 
\begin{itemize}
\item Index file downloaded, or locally referenced
\item \Rfunction{scanBam} returns a nested list
  \begin{itemize}
  \item One element for each row of \Rfunction{GRanges}
  \item Nested elements correspond to \Rfunarg{what}
  \end{itemize}
\end{itemize}
\end{frame}  

\begin{frame}[fragile]{Demo}
<<seq2anno, eval=FALSE>>=
system.file('script', 'streamBam.R', package='CSAMA10')
@ 
\end{frame}

\section{Tomorrow}

\subsection{Genome Collections}
\begin{frame}[fragile]{\Rclass{BamViews}}
  \begin{itemize}
  \item Overall experiment represented by `regions of interest' (rows)
    in several samples (columns).
  \item Represent this as a `view' on which coordinated operations can
    be performed.
  \item Extended examples: \Biocpkg{Rsamtools} vignette,
    \Biocdatapkg{leeBamViews}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Demo}
<<seq2anno, eval=FALSE>>=
system.file('script', 'BamViews.R', package='CSAMA10')
@ 
\end{frame}

\section{Resources}

\begin{frame}{Resources}
  Packages
  \begin{itemize}
  \item \Biocpkg{Rsamtools}, \Biocpkg{GenomicRanges}
  \item \Biocpkg{leeBamViews}
  \end{itemize}
\end{frame}

\end{document}
