%% \VignetteIndexEntry{Talk: Sequences, Reads, Alignments, and Ranges}

\documentclass[a4paper, notitlepage]{beamer}

\usepackage{BioconductorSlides}
\title{Working with Large Data}
\author{
  Martin
  Morgan\footnote{\href{mailto:mtmorgan@fhcrc.org}{mtmorgan@fhcrc.org}}
}
\date{June 23 -- 28, 2013}

<<setup, echo=FALSE>>=
library(Morgan2013)
@ 

\begin{document}

\begin{frame}
  \maketitle
\end{frame}

\begin{frame}[t, fragile]
  \frametitle{Write efficient \R{} code}
  \begin{columns}[t]
    \column{.4\textwidth}
    \begin{enumerate}
    \item \textbf<1>{Vectorize}
    \item \textbf<2>{Pre-allocate and fill}
    \item \textbf<3>{Exploit existing software}
    \item \textbf<4>{Use appropriate algorithms}
    \item \textbf<5>{Avoid expensive conveniences}
    \end{enumerate}
    \column{.6\textwidth}
    \begin{description}
      \only<1>{
      \item[Symptom] It takes too long to perform a simple
        mathematical calculation
      \item[Solution] Vectorize, e.g., \Rcode{log(x)}, \Rcode{x \%*\% y}
      }\only<2>{
      \item[Symptom] A loop appends to a result vector; it starts
        quickly but then gets slower and slower!
      \item[Solution] Fill a pre-allocated result vector or, better,
        use \Rcode{result <- lapply(x, function(...) ...)}.
      }\only<3>{
      \item[Symptom] Everyone must be doing this, why do I feel like
        I'm re-inventing the wheel (and doing it poorly!)?
      \item[Solution] Use established, performant software, e.g.,
        \Biocpkg{limma}, \Biocpkg{DESeq2}
      }\only<4>{
      \item[Symptom] Execution time increases dramatically as data
        size increases
      \item[Solution] Choose algorithms that scale linearly or better
        with data size.
      }\only<5>{
      \item[Symptom] An over-zealous `feature' hurts performance
      \item[Solution] Avoid the feature, e.g., \Rcode{l =
          list(a=1:3); unlist(l, use.names=FALSE)}
      }\only<6>{
      \item 
      }
    \end{description}
  \end{columns}
  \medskip\only<6>{
  \begin{center}
    \begin{minipage}[c]{.8\linewidth}
      \emph{There are many fast ways to get the wrong answer} --
      R.\ Gentleman
    \end{minipage}
  \end{center}
  }
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Manage data and processors}
  \begin{columns}[t]
    \column{.37\textwidth}
    \bigskip\par
    \begin{enumerate}
    \item \textbf<1>{Restriction}
    \item \textbf<2>{Sampling}
    \item \textbf<3>{Iteration}
    \item \textbf<4>{Parallel evaluation}
    \end{enumerate}
    \column{.63\textwidth}
\begin{semiverbatim}\only<1>{
library(Rsamtools)
gr <- GRanges("chr7", 
  IRanges(100000, width=100))
param <- ScanBamParam(
  what = c("rname", "pos", "cigar"),
  which = gr)
scanBam("a.bam", param = param)
}\only<2>{
library(ShortRead)
samp <- FastqSampler("end1.fastq")
yield(samp)
set.seed(123); yield(samp)
set.seed(123); yield(samp)
}\only<3>{
library(Rsamtools)
bf <-
  BamFile("a.bam", yieldSize=1e6)
open(bf)
repeat \{
  gl <- readGAlignments(bf)
  if (length(gl) == 0)
    break;
  ## do work
\}
close(bf)
}\only<4>{
library(parallel)
options(mc.cores=detectCores)
fls <- c("a.bam", "b.bam")

## sequential
x0 <- lapply(fls, countBam)

## parallel, 1 core per BAM file
x1 <- mclapply(fls, countBam)

identical(x0, x1)  # TRUE
}\end{semiverbatim}
\only<4>{Other parallel solutions possible, e.g., clusters, Windows}
  \end{columns}
\end{frame}

\begin{frame}{Acknowledgments}
  \begin{columns}
    \column{.5\textwidth}
    \begin{itemize}
    \item Vince Carey
    \item Michael Lawrence
    \item Herv\'e Pag\`es
    \item Valerie Obenchain
    \end{itemize}
    \column{.5\textwidth}
    \begin{itemize}
    \item Marc Carlson
    \item Paul Shannon
    \item Dan Tenenbaum
    \end{itemize}
  \end{columns}
\end{frame}

\end{document}
