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

\documentclass[a4paper, notitlepage]{beamer}

\usepackage{BioconductorSlides}
\title{Sequences, Reads, Ranges, and Alignments}
\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}{Overview}
  \begin{enumerate}
  \item Sequences -- \Biocpkg{Biostrings}, \Biocpkg{BSgenome},
    (\Biocpkg{Rsamtools})
  \item Reads -- \Biocpkg{ShortRead}
  \item Ranges -- \Biocpkg{GenomicRanges}
  \item Alignments -- \Biocpkg{Rsamtools}
  \item Working with large data
  \end{enumerate}
\end{frame}

\begin{frame}{Sequences -- \Biocpkg{Biostrings}}
  \begin{tabular}{lll}
    One & Many \\\hline
    \Rclass{DNAString} & \Rclass{DNAStringSet} \\
    \Rclass{RNAString} & \Rclass{RNAStringSet} \\
    \Rclass{AAString} & \Rclass{AAStringSet} \\
    \Rclass{BString} & \Rclass{BStringSet}
  \end{tabular}
  \bigskip\par
  \begin{tabular}{ll}
    Operation & Examples \\\hline
    Access & \Rfunction{length}, \Rfunction{nchar} \\
    Compare & \Rfunction{match}, \Rfunction{sort} \\
    Edit & \Rfunction{subseq}, \Rfunction{translate} \\
    Count & \Rfunction{alphabetFrequency}, \Rfunction{consensusMatrix} \\
    Match & \Rfunction{stringDist}, \Rfunction{matchPattern} \\
    & \Rfunction{pairwiseAlignment}, \Rfunction{matchPWM} \\
    I/O & \Rfunction{readDNAStringSet}, \Rfunction{writeXStringSet}
  \end{tabular}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Whole-genome sequences}
  \Biocpkg{BSgenome}
  \begin{itemize}
  \item Pre-built or custom-made -- \texttt{vignette("BSgenomeForge")}
  \item Load chromome-at-a-time or selected sequences into memory
  \end{itemize}
  \Biocpkg{Rsamtools}: \Rfunction{FaFile}
  \begin{itemize}
  \item Point to standard FASTA file on disk
  \item Index -- \Rfunction{indexFa}
  \item Query -- \Rfunction{scanFa}
  \end{itemize}
  Tasks?
\end{frame}

\begin{frame}[fragile]
  \frametitle{Reads -- \Biocpkg{ShortRead}}
\begin{semiverbatim}
 HWI-EAS299_4_30M2BAAXX:5:1:1513:1024 length=37
GTTTTGTCCAAGTTCTGGTAGCTGAATCCTGGGGCGC
+HWI-EAS299_4_30M2BAAXX:5:1:1513:1024 length=37
IIIIIIIIIIIIIIIIIIIIIIIIIIII+HIIII<IE
\end{semiverbatim}
  Input
  \begin{itemize}
  \item \Rfunction{readFastq}
  \item \Rfunction{FastqSampler} \& \Rfunction{yield};
    \Rfunction{FastqStreamer} \& \Rfunction{yield}
  \item \Rfunction{trimTails}, \ldots
  \end{itemize}
  Manipulation
  \begin{itemize}
  \item \Rfunction{sread}, \Rfunction{quality}, \ldots
  \item \Rfunction{frequentSequences}, \Rfunction{alphabetByCycle}, \ldots
  \item \Rfunction{qa}, \Rfunction{report}
  \end{itemize}
\end{frame}

\begin{frame}{Ranges -- \Biocpkg{GenomicRanges}}
  \Rclass{GRanges}
  \begin{itemize}
  \item Ranges in genomic space -- seqname, start / end / width,
    strand ($+$, $-$, $*$)
  \item Annotation (e.g., regulatory regions, genes, exons) or data
    (e.g., reads, peaks, variants)
  \end{itemize}
  Operations
  \begin{description}
  \item[Intra-range] \Rfunction{flank}, \Rfunction{narrow}, \ldots
  \item[Inter-range] \Rfunction{range}, \Rfunction{disjoin},
    \Rfunction{gaps}, \Rfunction{coverage}, \ldots
  \item[Between] \Rfunction{countOverlaps}, \Rfunction{findOverlaps},
    \Rfunction{summarizeOverlaps} (\Biocpkg{GenomicRanges}), \ldots
  \end{description}
\end{frame}

\begin{frame}{Ranges -- \Biocpkg{GenomicRanges}}
  \Rclass{GRangesList}
  \begin{itemize}
  \item List-like, where all elements are \Rclass{GRanges}
  \item Annontation (e.g., exons within genes) or data (e.g., gapped
    alignments)
  \end{itemize}
  Advanced: derived from \Biocpkg{IRanges} package \Rclass{CompressedList} class
  \begin{itemize}
  \item \Rclass{CompressedList}: \emph{many} homogenous elements
  \item Internally, a single \Rclass{GRanges} and a `partitioning'
    describing how elements of the \Rclass{GRanges} are separated into
    groups.
  \item Some operations (e.g., \Rfunction{unlist}, \Rfunction{relist})
    very fast.
  \item Common pattern for implementation: unlist, transform, relist.
  \end{itemize}
  Tasks?
\end{frame}

\begin{frame}{Alignments -- \Biocpkg{Rsamtools}}
  \Rclass{GappedAlignments} (\Rclass{GAlignments})
  \begin{itemize}
  \item \Rfunction{readGappedAlignments} (\Rfunction{readGAlignments})
  \item (Bioc-devel: \Rfunction{readGAlignmentsList})
  \item Also: \Rfunction{scanBam}, \Rfunction{asBam},
    \Rfunction{indexBam}, \Rfunction{quickCountBam}, \ldots
  \end{itemize}
  \Rclass{BamFile} / \Rclass{BamFileList}
  \begin{itemize}
  \item A \emph{type} (for method selection, e.g.,
    \Rfunction{coverage}) and \Rfunarg{yieldSize}
  \end{itemize}
  \Rclass{ScanBamParam}: optional argument to input functions
  \begin{itemize}
  \item \Rfunarg{what}: (additional) fields to return
  \item \Rfunarg{which}: restrict query to specific \Rclass{GRanges}
  \item \Rfunarg{flag}: restrict read input, e.g., marked as passing
    quality control, not optical duplicates, plus strand
  \item \ldots
  \end{itemize}
  Tasks?
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Working with large data}
  \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}{Acknowledgements}
  \begin{columns}[t]
    \column{.5\textwidth}
    \Biocpkg{IRanges}, \Biocpkg{GenomicRanges}
    \begin{itemize}
    \item Michael Lawrence
    \item Herv\'e Pag\`es
    \item Patrick Aboyoun
    \item Valerie Obenchain
    \end{itemize}
    \Biocpkg{Biostrings}, \Biocpkg{BSgenome}
    \begin{itemize}
    \item Herv\'e Pag\`es
    \end{itemize}
    \column{.5\textwidth}
    And\ldots
    \begin{itemize}
    \item Marc Carlson
    \item Paul Shannon
    \item Dan Tenenbaum
    \end{itemize}
    \Bioconductor{} mailing list
  \end{columns}
\end{frame}

\end{document}
