%\VignetteDepends{IRanges, GenomicRanges, Biostrings}
%\VignetteIndexEntry{The IRanges, GenomicRanges, and Biostrings packages}
%\VignettePackage{CSAMA10}

\documentclass{beamer}

\usepackage{BioconductorSlides}
\SweaveOpts{keep.source=TRUE, eps=FALSE, width=9, height=3}

\title{\Biocpkg{IRanges}, \Biocpkg{GenomicRanges}, and \Biocpkg{Biostrings}}
\subtitle{\Bioconductor{} Infrastructure Packages for Sequence Analysis}
\author{Patrick Aboyoun}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
\date{14-18 June, 2010}

\begin{document}

<<setup, echo=FALSE>>=
options(width = 64)
library(GenomicRanges)
library(Biostrings)
library(CSAMA10)
plotGRanges <- function(x, xlim = x, main = deparse(substitute(x)),
                       sep = 0.5, ...) 
{
  height <- 1
  if (is(xlim, "Ranges") || is(xlim, "GRanges"))
    xlim <- c(min(start(xlim)), max(end(xlim)))
  bins <- disjointBins(IRanges(start(x), end(x) + 1))
  plot.new()
  plot.window(xlim, c(0, max(bins)*(height + sep)))
  ybottom <- bins * (sep + height) - height
  col <- ifelse(as.vector(strand(x) == "+"), "blue",
         ifelse(as.vector(strand(x) == "-"), "red", "purple"))
  rect(start(x) - 0.5, ybottom, end(x) + 0.5, ybottom + height, col = col, ...)
  title(main)
  axis(1)
}
@

\frame{\titlepage}

\section{Introduction}

\begin{frame}[t]
  \frametitle{Motivation}
  \begin{block}{Short term: Ad-hoc sequence analysis tools}
    \begin{itemize}
      \item QA/QC for NGS experiments.
      \item Finding TF binding sites (ChIP-seq) or \textit{de novo}
        exon locations (RNA-seq) using coverage of alignments across
        a genome. (peak finding)
      \item Motif finding (e.g. PWM) for TF binding sites from ChIP-seq.
      \item Finding differentially expressed genes in RNA-seq.
      \item Comparing prevalence of known alternative spliced transcripts
        in RNA-seq.
      \item Finding \textit{de novo} alternative splicing of transcripts
        in RNA-seq.
      \item etc.
    \end{itemize}
  \end{block}
  \begin{block}{Long term: Sequence analysis pipelines}
    Work in progress and outside scope of the labs.
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Example: Comparing transcript isoforms in RNA-seq}
{\small
\begin{verbatim}
> txExonOverlapCountsGroupedByGene
CompressedSplitDataFrameList of length 15160 
$FBgn0000556 
DataFrame with 2 rows and 3 columns 
      tx_id             exon_counts total_count 
  <integer> <CompressedIntegerList>   <integer> 
1      5375                33,1099         1132 
2      5374                 0,1099         1099 

$FBgn0000559 
DataFrame with 3 rows and 3 columns 
      tx_id             exon_counts total_count 
  <integer> <CompressedIntegerList>   <integer> 
1      3681            10,52,92,...         541 
2      3679             0,52,92,...         531 
3      3680             0,52,92,...         531 

... 
<15158 more elements> 
\end{verbatim}
}
\end{frame}

\begin{frame}[t]
  \frametitle{\Bioconductor{} Sequence Packages}
\begin{figure}[ht]
 \centering
  \includegraphics[width=4in,height=3in]{packageStack}
\end{figure}
\end{frame}

\begin{frame}[t]
  \frametitle{\Bioconductor{} Sequence Infrastructure Packages}
  \begin{block}{\Biocpkg{IRanges}}
    \begin{itemize}
      \item Long sequences (compressed \& pointer referenced)
      \item Views on long sequences
      \item Integer interval tools (e.g. interval overlap)
      \item Genomic intervals I (\Rclass{RangedData})
    \end{itemize}
  \end{block}

  \begin{block}{\Biocpkg{GenomicRanges}}
    \begin{itemize}
      \item Genomic intervals II (\Rclass{GenomicRanges})
      \item Discontiguous genomic interval sets (\Rclass{GenomicRangesList})
    \end{itemize}
  \end{block}

  \begin{block}{\Biocpkg{Biostrings}}
    \begin{itemize}
      \item Long DNA/RNA/amino acids sequences
      \item Sequence \& PWM matching and pairwise alignment tools
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{\Bioconductor{} Sequence Infrastructure Classes}
  \begin{block}{Ranges (as sequences \& intervals)}
    \Rclass{IRanges}
  \end{block}

  \begin{block}{Genomic intervals with data}
    \Rclass{GRanges}, \Rclass{RangedData}
  \end{block}

  \begin{block}{Genomic interval sets (e.g. spliced transcripts)}
    \Rclass{GRangesList}
  \end{block}

  \begin{block}{Long piecewise constant sequences}
    \Rclass{Rle}, \Rclass{RleList}
  \end{block}

  \begin{block}{Long (biological) strings}
    \Rclass{DNAString}, \Rclass{RNAString}, \Rclass{AAString},
    \Rclass{BString}, \Rclass{DNAStringSet}, \ldots
  \end{block}

  \begin{block}{Views on long sequences}
    \Rclass{RleViews}, \Rclass{RleListViews}, \Rclass{XStringViews},
    \ldots
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{Concept I: Run-Length Encoding (RLE)}
  \begin{block}{Issue}
    \begin{itemize}
      \item Chromosomes can be hundreds of million of base
            pairs long, making them hard to manage in computer
            memory.
      \item Fortunately, coverage vectors tend to follow an
            integer step function.
    \end{itemize}
  \end{block}

  \begin{block}{Solution}
    \begin{itemize}
      \item Run-length encoding (RLE) is a common compression
            technique for storing long sequences with lengthy
            repeats.
      \item An RLE couples values with run lengths, e.g. the
            vector 0, 0, 0, 1, 1, 2 would be represented as
            (3) 0's, (2) 1's, and (1) 2.
      \item The \Biocpkg{IRanges} package uses the
            \Rclass{Rle} and \Rclass{RleList} classes to house
            coverage vectors.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{Concept II: Sequence Views}
  \begin{block}{Issue}
    \begin{itemize}
      \item Chromosomes can be hundreds of million of base
            pairs long, making subsequence selection inefficient.
    \end{itemize}
  \end{block}

  \begin{block}{Solution}
    \begin{itemize}
      \item Store the original sequence using a pass-by-reference
            semantic.
      \item Associate ranges with the sequence to select subsequence.
      \item Example:
        \begin{itemize}
          \item 7007-letter sequence:
            \Rcode{$<<$SNIP-3000$>>$AGATTCA$<<$SNIP-4000$>>$}
          \item View range: \Rcode{[3001, 3007]}
          \item $=>$ 7-letter subsequence: \Rcode{AGATTCA}
        \end{itemize}
    \end{itemize}
  \end{block}
\end{frame}


\section{Genomic Intervals with Data}

\begin{frame}[t,fragile]
  \frametitle{Naive representation for intervals with data}
  \begin{block}{Data characteristics}
    \begin{itemize}
      \item Genomic coordinates consist of chromosome, position,
        and potentially strand information
      \item May have additional values, such as GC content or
        alignment coverage
    \end{itemize}
  \end{block}

  \begin{block}{\Rclass{data.frame} approach}
{\small
<<naive-GRanges-1>>=
chr <- c("chr1", "chr2", "chr1")
strand <- c("+", "+", "-")
start <- c(3L, 4L, 1L)
end <- c(7L, 5L, 3L)
naive <- data.frame(chr = chr, strand = strand,
                    start = start, end = end)
@
}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{BioC representations for intervals with data}
  \begin{block}{\Rclass{GRanges}}
    \begin{itemize}
      \item Used by \Biocpkg{GenomicFeatures}, a transcript annotation
        generator
      \item Intervals not required to be grouped by chromosome/contig
      \item Methods strand aware
      \item \Rclass{GRangesList} class can hold exons within spliced
        transcripts
    \end{itemize}
  \end{block}
\vskip12pt
  \begin{block}{\Rclass{RangedData}}
    \begin{itemize}
      \item Used by \Biocpkg{rtracklayer}, a genome browser interface
      \item Intervals grouped by chromosome/contig
      \item Methods strand unaware
      \item Preceded \Rclass{GRanges} class
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{GRanges} construction}
  \begin{block}{\Rclass{GRanges} constructor}
    \begin{itemize}
      \item Instances are created using the \Rfunction{GRanges}
        constructor.
      \item Starts and ends are wrapped in an \Rclass{IRanges}
        constructor.
      \item Chromosome/contig supplied to \Rfunarg{seqnames}
        argument.
      \item Underlying sequence lengths can be suppled to
        \Rfunarg{seqlengths} argument.
    \end{itemize}
  \end{block}

  \begin{block}{\Rclass{GRanges} example}
{\small
<<GRanges-construction>>=
bioc <- GRanges(seqnames = chr,
                ranges = IRanges(start = start, end = end),
                strand = strand,
                seqlengths = c("chr1" = 24, "chr2" = 18))
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{GRanges} display}
  \begin{block}{\Rclass{GRanges} \Rfunction{show} method}
{\small
<<GRanges-display>>=
bioc
@
}
  \end{block}

  \begin{block}{Note}
    \begin{itemize}
      \item Optional interval data would appear to the right of
        \Rcode{|} divider.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{GRanges} class decomposition}
  \begin{block}{\Rclass{GRanges} slots}
{\footnotesize
<<GRanges-slots>>=
getSlots("GRanges")
@
}
  \end{block}

  \begin{block}{Notes}
    \begin{itemize}
      \item If (mostly) sorted, \Rclass{Rle} vectors reduce memory
        usage and provide faster group selection
      \item \Rfunarg{elementMetadata} holds optional interval data
      \item \Rfunarg{metadata} holds optional whole object info
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{Interval operations}
  \begin{block}{Intra-interval}
    \Rfunction{flank}, \Rfunction{resize}, \Rfunction{shift}
  \end{block}

  \begin{block}{Inter-interval I}
    \Rfunction{disjoin}, \Rfunction{gaps}, \Rfunction{reduce},
    \Rfunction{range}
  \end{block}

  \begin{block}{Inter-interval II}
    \Rfunction{coverage}
  \end{block}

  \begin{block}{Between two interval sets I}
    \Rfunction{union}, \Rfunction{intersect}, \Rfunction{setdiff}
  \end{block}

  \begin{block}{Between two interval sets II}
    \Rfunction{punion}, \Rfunction{pintersect}, \Rfunction{psetdiff}
  \end{block}

  \begin{block}{Between two interval sets III}
    \Rfunction{findOverlaps}, \Rfunction{countOverlaps}, \Rfunction{\%in\%},
    \Rfunction{match}
  \end{block}

  \begin{block}{Low level}
    \Rfunction{start}, \Rfunction{end}, \Rfunction{width}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Creating a new \Rclass{GRanges} object}
  \begin{block}{New object to use in interval operations}
{\small
<<ranges-construct>>=
ir <- IRanges(c(1, 8, 14, 15, 19, 34, 40), 
              width=c(12, 6,  6, 15, 6, 2, 7))
strand <- rep(c("+", "-"), c(4,3))
grngs <- GRanges(seqnames = "chr1", ranges = ir,
                 strand = strand,
                 seqlengths = c("chr1" = 50))
@
}
<<ranges-plot, fig=TRUE, echo=FALSE>>=
plotGRanges(grngs)
@
{\tiny
\newline
  blue = positive strand, red = negative strand
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{GRanges} subsetting}
  \begin{block}{seqselect}
{\small
<<ranges-subset>>=
seqselect(grngs, strand(grngs) == "-")
@
}
  \end{block}

  \begin{block}{Other functions}
    \Rfunction{[}, \Rfunction{head}, \Rfunction{tail},
    \Rfunction{window}, \Rfunction{subset},
    \Rfunction{subsetByOverlaps}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Intra-interval (1/2)}
  \begin{block}{Shifting intervals}
  If your interval bounds are off by 1, you can shift them.
{\small
<<ranges-shift>>=
shift(grngs, 1)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Intra-interval (2/2)}
  \begin{block}{Resizing intervals}
  ``Growing" alignment intervals to an estimated fragment length.
{\small
<<ranges-resize-1>>=
resize(grngs, 10)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Inter-interval I}
<<ranges-reduce-plot, echo=FALSE, fig=TRUE>>=
plotGRanges(reduce(grngs))
@
\newline
<<ranges-gaps-plot, echo=FALSE, fig=TRUE>>=
plotGRanges(gaps(grngs))
@
\newline
<<ranges-disjoin-plot, echo=FALSE, fig=TRUE>>=
plotGRanges(disjoin(grngs))
@
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Overlap detection}
  \begin{block}{Finding interval overlaps}
    \Rfunction{findOverlap} and \Rfunction{countOverlaps}
    produce a mapping and a tabulation of interval overlaps,
    respectively
  \end{block}
{\small
<<ranges-overlap>>=
ol <- findOverlaps(grngs, reduce(grngs))
as.matrix(ol)
countOverlaps(reduce(grngs), grngs)
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Elementwise counts of overlapping intervals}
  \begin{block}{Coverage}
    \begin{itemize}
      \item \Rfunction{coverage} counts number of ranges over each position
      \item Subset by strand to get stranded coverage
    \end{itemize}
  \end{block}
  \begin{block}{}
{\small
<<ranges-coverage>>=
cover <- coverage(grngs)
@
}
<<ranges-coverage-plot, height=4, echo=FALSE, fig=TRUE>>=
cover <- coverage(grngs)
plotGRanges(grngs)
cover <- as.integer(cover[["chr1"]])
mat <- cbind(seq_along(cover)-0.5, cover)
d <- diff(cover) != 0
mat <- rbind(cbind(mat[d,1]+1, mat[d,2]), mat)
mat <- mat[order(mat[,1]),]
lines(mat, col="black", lwd=4)
axis(2)
@
  \end{block}
\end{frame}


\section{Coverage and Other Piecewise Constant Measures}

\begin{frame}[t]
  \frametitle{Piecewise constant measures}
  \begin{block}{Issue restated}
    \begin{itemize}
      \item The number of genomic positions in a genome is often in the
        billions for higher organisms, making it challenging to represent
        in memory.
      \item Some data across a genome tend to be sparse (i.e. large
        stretches of ``no information")
    \end{itemize}
  \end{block}

  \begin{block}{\Rclass{Rle} and \Rclass{RleList} classes}
    \begin{itemize}
      \item Solve the set of problems for positional measures that tend
      to have consecutively repeating values.
      \item \emph{Do not} address the more general problem of positional
        measures that constantly fluxuate, such as conservation scores.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Numerous \Rclass{Rle} methods (1/2)}
{\scriptsize
\begin{verbatim}
 [1] "!"             "["             "[<-"           "%in%"         
 [5] "aggregate"     "as.character"  "as.complex"    "as.data.frame"
 [9] "as.factor"     "as.integer"    "as.logical"    "as.numeric"   
[13] "as.raw"        "as.vector"     "c"             "chartr"       
[17] "coerce"        "Complex"       "cor"           "cov"          
[21] "diff"          "end"           "findRange"     "findRun"      
[25] "gsub"          "IQR"           "is.na"         "is.unsorted"  
[29] "length"        "levels"        "levels<-"      "mad"          
[33] "match"         "Math"          "Math2"         "mean"         
[37] "median"        "nchar"         "nrun"          "Ops"          
[41] "paste"         "pmax"          "pmax.int"      "pmin"         
[45] "pmin.int"      "quantile"      "rep"           "rep.int"      
[49] "rev"           "runLength"     "runLength<-"   "runmean"      
[53] "runmed"        "runq"          "runsum"        "runValue"     
[57] "runValue<-"    "runwtsum"      "sd"            "seqselect"    
[61] "seqselect<-"   "shiftApply"    "show"          "slice"        
[65] "smoothEnds"    "sort"          "split"         "splitRanges"  
[69] "start"         "sub"           "substr"        "substring"    
[73] "summary"       "Summary"       "table"         "tolower"      
[77] "toupper"       "unique"        "var"           "Views"        
[81] "which"         "width"         "window"       
\end{verbatim}
}
\end{frame}

\begin{frame}[t]
  \frametitle{Numerous \Rclass{Rle} methods (2/2)}
{\small
  \begin{block}{\Rfunction{Arith}}
    \Rfunction{+}, \Rfunction{-}, \Rfunction{*}, \Rfunction{\^},
    \Rfunction{\%\%}, \Rfunction{\%/\%}, \Rfunction{/}
  \end{block}
  \begin{block}{\Rfunction{Compare}}
    \Rfunction{==}, \Rfunction{>}, \Rfunction{<}, \Rfunction{!=}, 
    \Rfunction{<=}, \Rfunction{>=}
  \end{block}
  \begin{block}{\Rfunction{Logic}}
    \Rfunction{\&}, \Rfunction{|}
  \end{block}
  \begin{block}{\Rfunction{Math}}
    \Rfunction{abs}, \Rfunction{sign}, \Rfunction{sqrt},
    \Rfunction{ceiling}, \Rfunction{floor}, \Rfunction{trunc},
    \Rfunction{cummax}, \Rfunction{cummin}, \Rfunction{cumprod},
    \Rfunction{cumsum}, \Rfunction{log}, \Rfunction{log10},
    \Rfunction{log2}, \Rfunction{log1p},  \Rfunction{acos},
    \Rfunction{acosh}, \Rfunction{asin}, \Rfunction{asinh}, \ldots
  \end{block}
  \begin{block}{\Rfunction{Math2}}
    \Rfunction{round}, \Rfunction{signif}
  \end{block}
  \begin{block}{\Rfunction{Summary}}
    \Rfunction{max}, \Rfunction{min}, \Rfunction{range},
    \Rfunction{prod}, \Rfunction{sum}, \Rfunction{any},
    \Rfunction{all}
  \end{block}
  \begin{block}{\Rfunction{Complex}}
    \Rfunction{Arg}, \Rfunction{Conj}, \Rfunction{Im},
    \Rfunction{Mod}, \Rfunction{Re}
  \end{block}
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Coverage example}
  Coverage from a \textit{Saccharomyces cerevisiae} (Yeast) experiment
  contained in two objects \Robject{posCover} \& \Robject{negCover}
<<coverage-data-1, echo=FALSE>>=
data(posCover)
data(negCover)
@
{\footnotesize
<<coverage-data-2>>=
c(class(posCover), class(negCover))
posCover[["chrI"]]
negCover[["chrI"]]
posCover[["chrI"]] + negCover[["chrI"]]
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Plotting coverage}
  \begin{block}{Custom function}
{\footnotesize
<<plotCoverage>>=
plotCoverage <-
function(x, chrom, start=1, end=length(x[[chrom]]), col="blue",
         xlab="Index", ylab="Coverage", main=chrom)
{
    xWindow <- as.vector(window(x[[chrom]], start, end))
    x <- start:end
    xlim <- c(start, end)
    ylim <- c(0, max(xWindow))
    plot(x = start, y = 0, xlim = xlim, ylim = ylim,
         xlab = xlab, ylab = ylab, main = main, type = "n")
    polygon(c(start, x, end), c(0, xWindow, 0), col = col)
}
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Plotting coverage on one strand}
\begin{block}{Plotting chr1+ coverage}
{\small
<<cover-all-chr1-pos,fig=TRUE,include=FALSE,prefix=FALSE,width=8,height=5>>=
plotCoverage(posCover, "chrI")
@
}
\end{block}
\begin{figure}[ht]
  \centering
  \includegraphics[width=4in,height=2.5in]{cover-all-chr1-pos}
\end{figure}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Plotting stranded coverage}
  \begin{block}{Custom function}
{\footnotesize
<<plotCoverage>>=
plotCoverageStrands <-
function(pos, neg, chrom, start=1, end=length(pos[[chrom]]),
         pos.col="blue", neg.col="red", xlab="Index",
         ylab="Coverage", main=chrom)
{
    posWindow <- as.vector(window(pos[[chrom]], start, end))
    negWindow <- as.vector(window(neg[[chrom]], start, end))
    x <- start:end
    xlim <- c(start, end)
    ylim <- c(-1, 1) * min(max(posWindow), max(negWindow))
    plot(x = start, y = 0, xlim = xlim, ylim = ylim,
         xlab = xlab, ylab = ylab, main = main, type = "n")
    polygon(c(start, x, end), c(0, posWindow, 0), col = pos.col)
    polygon(c(start, x, end), c(0, - negWindow, 0), col = neg.col)
}
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Plotting coverage on both strands}
\begin{block}{Plotting chr1 coverage, both strands}
{\small
<<cover-all-chr1,fig=TRUE,include=FALSE,prefix=FALSE,width=8,height=5>>=
plotCoverageStrands(posCover, negCover, "chrI")
@
}
\end{block}
\begin{figure}[ht]
  \centering
  \includegraphics[width=4in,height=2.5in]{cover-all-chr1}
\end{figure}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Plotting Coverage on both strands}
\begin{block}{Plotting chr1 coverage, both strands}
{\footnotesize
<<cover-window-chr1,fig=TRUE,include=FALSE,prefix=FALSE,width=8,height=5>>=
plotCoverageStrands(posCover, negCover, "chrI", 135000, 145000)
@
}
\end{block}
\begin{figure}[ht]
  \centering
  \includegraphics[width=4in,height=2.25in]{cover-window-chr1}
\end{figure}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Smoothing coverage}
\begin{block}{Running window mean}
{\scriptsize
<<cover-smooth-chr1,fig=TRUE,include=FALSE,prefix=FALSE,width=8,height=5>>=
posSmoothCover <- round(runmean(posCover, 75, endrule = "constant"))
negSmoothCover <- round(runmean(negCover, 75, endrule = "constant"))
plotCoverageStrands(posSmoothCover,negSmoothCover,"chrI",135000,145000)
@
}
\end{block}
\begin{figure}[ht]
  \centering
  \includegraphics[width=4in,height=2.25in]{cover-smooth-chr1}
\end{figure}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Combining coverage}
\begin{block}{Combining coverage using "parallel" minimums}
{\small
<<combined-coverage>>=
combSmoothCover <- mendoapply(pmin,
                              posSmoothCover,
                              negSmoothCover)
identical(class(posSmoothCover), class(combSmoothCover))
@
}
\end{block}
  \begin{itemize}
    \item The \Rfunction{mendoapply} function defined in
      \Biocpkg{IRanges} packages as a member of the \Rfunction{apply}
      family.
    \begin{itemize}
      \item Performs elementwise operations across multiple
        inputs of the same type.
      \item Returns an object of the same type as the inputs.
    \end{itemize}
    \item The minimum coverage value on either strand can be
      computed using \Rfunction{pmin}.
  \end{itemize}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Plotting combined coverage}
\begin{block}{Plotting chr1, combined strands}
{\scriptsize
<<cover-comb-chr1,fig=TRUE,include=FALSE,prefix=FALSE,width=8,height=5>>=
plotCoverage(combSmoothCover, "chrI", 135000, 145000)
@
}
\end{block}
\begin{figure}[ht]
  \centering
  \includegraphics[width=4in,height=2.5in]{cover-comb-chr1}
\end{figure}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Island selection}
{\scriptsize
<<slice>>=
islands <- slice(combSmoothCover, lower=1)
islandsWithWidePeaks <- islands[viewMaxs(islands) >= 8L &
                                width(islands) >= 500L]
islandsWithWidePeaks
@
}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Common methods for \Rclass{Views} objects}
  \begin{itemize}
    \item Subset via \Rfunction{[}, \Rfunction{[[}, etc.
    \item Manage edge cases via \Rfunction{trim} \& \Rfunction{restrict}
    \item \Rclass{Ranges} operations such as \Rfunction{start},
      \Rfunction{end}, \Rfunction{width}, etc.
    \item Perform within view calculations via \Rfunction{viewSums},
      \Rfunction{viewMins}, \Rfunction{viewMaxs}, \Rfunction{viewWhichMins},
      \Rfunction{viewWhichMaxs}, \Rfunction{viewApply} 
  \end{itemize}
\end{frame}


\section{Long Biological Strings}

\begin{frame}[t,fragile]
  \frametitle{Long biological string framework}
  \begin{block}{Biostrings string types}
{\scriptsize
<<XString-subclasses>>=
library(Biostrings)
names(completeSubclasses(getClass("XString")))
@
}
  \end{block}

  \begin{block}{DNA}
{\scriptsize
<<DNAString>>=
data(yeastSEQCHR1)
c(class(yeastSEQCHR1), nchar(yeastSEQCHR1))
yeast1 <- DNAString(yeastSEQCHR1)
yeast1
IUPAC_CODE_MAP
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{List of strings}
  \begin{block}{Biostrings string list types}
{\footnotesize
<<XStringSet-subclasses>>=
head(names(completeSubclasses(getClass("XStringSet"))), 4)
@
}
  \end{block}

  \begin{block}{DNA strings}
{\footnotesize
<<DNAString>>=
data(srPhiX174)
length(srPhiX174)
head(srPhiX174, 3)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{XString class decomposition}
  \begin{block}{\Rclass{XString} slots}
{\scriptsize
<<GRanges-slots>>=
getSlots("XString")
getSlots("XStringSet")
@
}
  \end{block}

  \begin{block}{Notes}
    \begin{itemize}
      \item \Rfunarg{shared}, \Rfunarg{offset}, \Rfunarg{length},
        \Rfunarg{pool}, and \Rfunarg{ranges} slots regulate
        pass-by-reference semantic.
      \item \Rfunarg{metadata} slot can be used to hold annotation
        information.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{Basic string utilities}
  \begin{block}{Subsequence selection}
    \Rfunction{subseq}, \Rfunction{Views}
  \end{block}

  \begin{block}{Letter frequencies}
    \Rfunction{alphabetFrequency},
    \Rfunction{dinucleotideFrequency},
    \Rfunction{trinucleotideFrequency},
    \Rfunction{oligonucleotideFrequency},
    \Rfunction{letterFrequencyInSlidingView},
    \Rfunction{uniqueLetters}
  \end{block}

  \begin{block}{Letter consensus}
    \Rfunction{consensusMatrix}, \Rfunction{consensusString}
  \end{block}

  \begin{block}{Letter transformation}
    \Rfunction{reverse}, \Rfunction{complement},
    \Rfunction{reverseComplement}, \Rfunction{translate},
    \Rfunction{chartr}
  \end{block}

  \begin{block}{I/O}
    \Rfunction{read.DNAStringSet}, \Rfunction{read.RNAStringSet},
    \Rfunction{read.AAStringSet}, \Rfunction{read.BStringSet},
    \Rfunction{write.XStringSet}, \Rfunction{save.XStringSet}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{String matching/alignment utilities}
  \begin{block}{\Rfunction{matchPDict}}
    \Rfunction{matchPDict}, \Rfunction{countPDict},
    \Rfunction{whichPDict}, \Rfunction{vmatchPDict},
    \Rfunction{vcountPDict}, \Rfunction{vwhichPDict}
  \end{block}

  \begin{block}{\Rfunction{vmatchPattern}}
    \Rfunction{matchPattern}, \Rfunction{countPattern},
    \Rfunction{vmatchPattern}, \Rfunction{vcountPattern},
    \Rfunction{neditStartingAt}, \Rfunction{neditEndingAt},
    \Rfunction{isMatchingStartingAt},
    \Rfunction{isMatchingEndingAt},
    \Rfunction{which.isMatchingStartingAt},
    \Rfunction{which.isMatchingEndingAt}
  \end{block}

  \begin{block}{\Rfunction{pairwiseAlignment}}
    \Rfunction{pairwiseAlignment}, \Rfunction{stringDist}
  \end{block}

  \begin{block}{\Rfunction{matchPWM}}
    \Rfunction{matchPWM}, \Rfunction{countPWM}
  \end{block}

  \begin{block}{OTHER}
    \Rfunction{matchLRPatterns}, \Rfunction{trimLRPatterns},
    \Rfunction{matchProbePair}, \Rfunction{findPalindromes},
    \Rfunction{findComplementedPalindromes}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Letter frequencies}
  \begin{block}{Single-letter frequencies}
{\footnotesize
<<alphabetFrequency>>=
alphabetFrequency(yeast1, baseOnly=TRUE)
@
}
  \end{block}

  \begin{block}{Multi-letter frequencies}
{\footnotesize
<<dinucleotideFrequency>>=
dinucleotideFrequency(yeast1)
head(trinucleotideFrequency(yeast1), 12)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Basic transformations}
  \begin{block}{Standard transformations}
{\scriptsize
\begin{verbatim}
> x
  21-letter "DNAString" instance
seq: TCAACGTTGAATAGCGTACCG
> reverseComplement(x)
  21-letter "DNAString" instance
seq: CGGTACGCTATTCAACGTTGA
> translate(x)
  7-letter "AAString" instance
seq: STLNSVP
\end{verbatim}
}
  \end{block}

  \begin{block}{Bisulfite transformation}
{\scriptsize
\begin{verbatim}
> library(BSgenome.Celegans.UCSC.ce2)
> alphabetFrequency(Celegans$chrII, baseOnly=TRUE)
      A       C       G       T   other 
4878194 2769208 2762193 4869710       3 
> chrIIbis <- chartr("C", "T", Celegans$chrII)
> alphabetFrequency(chrIIbis, baseOnly=TRUE)
      A       C       G       T   other 
4878194       0 2762193 7638918       3
\end{verbatim}
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Letter consensus}
  \begin{block}{Consensus matrix}
{\footnotesize
<<consensusMatrix>>=
snippet <- subseq(head(sort(srPhiX174), 5), 1, 10)
consensusMatrix(snippet, baseOnly=TRUE)
@
}
  \end{block}

  \begin{block}{Consensus string}
{\footnotesize
<<consensusString>>=
consensusString(snippet)
consensusString(snippet, ambiguityMap = "N", threshold = 0.5)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{String matching}
  \begin{block}{Match counting}
{\footnotesize
<<matchPDict>>=
data(phiX174Phage)
genome <- phiX174Phage[["NEB03"]]
negPhiX174 <- reverseComplement(srPhiX174)
posCounts <- countPDict(PDict(srPhiX174), genome)
negCounts <- countPDict(PDict(negPhiX174), genome)
table(posCounts, negCounts)
@
}
  \end{block}

  \begin{block}{Match locations}
{\footnotesize
<<matchPDict>>=
matchPDict(PDict(srPhiX174[posCounts > 0]), genome)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Pairwise alignments}
  \begin{block}{Alignment scores}
{\footnotesize
<<pairwiseAlignments-1>>=
data(phiX174Phage)
posScore <- pairwiseAlignment(srPhiX174, genome,
              type = "global-local", scoreOnly = TRUE)
negScore <- pairwiseAlignment(negPhiX174, genome,
              type = "global-local", scoreOnly = TRUE)
cutoff <- max(pmin.int(posScore, negScore))
@
}
  \end{block}

  \begin{block}{Alignments}
{\footnotesize
<<matchPDict>>=
pairwiseAlignment(srPhiX174[posScore > cutoff], genome,
                  type = "global-local")
@
}
  \end{block}
\end{frame}


\section{Developer's Notes}

\begin{frame}[t]
  \frametitle{Long compressed sequence classes (\Biocpkg{IRanges})}
  \begin{block}{\Rclass{Rle}}
    \begin{itemize}
      \item Compressed atomic vectors
      \item Methods for standard \R{} \Rclass{atomic vector} functions
%      \item Fixed-width running window tools
      \item Concrete class with sub-typing at the slot level
    \end{itemize}
  \end{block}

  \begin{block}{\Rclass{CompressedList}}
    \begin{itemize}
      \item Compressed list of S4 objects
      \item Methods for standard \R{} \Rclass{list} functions
      \item Virtual class with sub-typing at the subclass level
%      \item Has sibling \Rclass{SimpleList} class
    \end{itemize}
  \end{block}

  \begin{block}{\Rclass{IRanges} (as Sequences)}
    \begin{itemize}
      \item \Rfunction{as.integer} coercion
      \item Subscripting via \Rfunction{seqselect}, \Rfunction{window}, and
        \Rfunction{[}
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{Pointer referenced sequence classes}
  \begin{block}{\Rclass{XVector} (\Biocpkg{IRanges})}
    \begin{itemize}
      \item External pointer-based atomic vectors
      \item Virtual class
      \item Concrete subclasses:
      \begin{itemize}
        \item \Rclass{XRaw} -- Underlies \Biocpkg{Biostrings} infrastructure
        \item \Rclass{XInteger} -- Experimental integer vector class
        \item \Rclass{XDouble} -- Experimental real number vector class
      \end{itemize}
    \end{itemize}
  \end{block}

  \begin{block}{\Rclass{XString} (\Biocpkg{Biostrings})}
    \begin{itemize}
      \item Virtual class
      \item Concrete subclasses:
      \begin{itemize}
        \item \Rclass{BString} -- Any ``biological" sequence
        \item \Rclass{DNAString} -- DNA sequence
        \item \Rclass{RNAString} -- RNA sequence
        \item \Rclass{AAString} -- Amino acid sequence
      \end{itemize}
    \end{itemize}
  \end{block}
\end{frame}


\section{Resources}

\begin{frame}
  \frametitle{Resources}
  \Bioconductor{} Web site
  \begin{itemize}
    \item `\Biocpkg{IRanges}', `\Biocpkg{GenomicRanges}', and
      `\Biocpkg{Biostrings}' links.
    \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 \Rcode{help.start()} to view a help browser.
    \item \Rcode{help(package = "Biostrings")}
    \item \Rcode{?findOverlaps}
    \item \Rcode{browseVignettes("GenomicRanges")}
  \end{itemize}
\end{frame}


\end{document}
