%\VignetteDepends{IRanges, GenomicRanges, Biostrings, BSgenome, BSgenome.Scerevisiae.UCSC.sacCer2, GenomicFeatures, yeast2.db, yeast2probe}
%\VignetteIndexEntry{Genomic Features and Sequences in Bioconductor}
%\VignettePackage{HTSandGeneCentricLabs}

\documentclass[9pt]{beamer}

\usepackage{BioconductorSlides}
\usepackage{underscore}
\SweaveOpts{keep.source=TRUE}

\title{Genomic Features and Sequences in \Bioconductor{}}
\author{Herv\'e Pag\`es}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
\date{July 29, 2010}

\begin{document}

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

%% use [fragile] in all frames with Sweave code chunks

\frame{\titlepage}

\section[Outline]{}
\frame{\tableofcontents}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Combining the tools}
  
\begin{frame}[fragile]
  \frametitle{Available data}
  \begin{block}{Relational annotations}
    \begin{itemize}
      \item User-made transcript centric annotations:
            \Rfunction{makeTranscriptDbFromUCSC} and
            \Rfunction{makeTranscriptDbFromBiomart} functions
            in \Biocpkg{GenomicFeatures}.
      \item Platform-specific annotation packages.
            E.g. \Biocpkg{yeast2.db}.
    \end{itemize}
  \end{block}
  \begin{block}{Sequences}
    \begin{itemize}
      \item Platform-specific probe packages.
            E.g. \Biocpkg{yeast2probe}.
      \item Full genome sequences aka \textit{BSgenome data packages}.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available data: Platform-specific annotation packages}
  \begin{block}{Mappings}
{\small
<<yeast2.db>>=
library(yeast2.db)
ls('package:yeast2.db')
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available data: Platform-specific annotation packages}
  \begin{block}{Left/Right keys}
{\small
<<left-right-keys>>=
Lkeys(yeast2ENSEMBL)[1:5]
Rkeys(yeast2ENSEMBL)[1:5]
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available data: Platform-specific annotation packages}
  \begin{block}{Mapped/unmapped keys}
{\small
<<mapped-unmapped-keys>>=
mget(c("1769308_at", "1769309_at"), yeast2ENSEMBL)
mappedLkeys(yeast2ENSEMBL)[1:5]
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available data: Platform-specific probe packages}
  \begin{block}{}
{\small
<<yeast2probe>>=
library(yeast2probe)
yeast2probe
dim(yeast2probe)
colnames(yeast2probe)
yeast2probe$sequence[1:5]
yeast2probe$Probe.Set.Name[1:5]
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available data: BSgenome data packages}
  \begin{block}{}
    \begin{itemize}
      \item One genome per package.
      \item Full genome sequences stored in Biostrings containers.
      \item 14 organisms / 22 packages in the current release (BioC 2.6).
      \item Most (but not all) packages contain sequences with builtin masks.
      \item Naming convention: BSgenome.\textit{Organism}.\textit{Provider}.\textit{BuildVersion}
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available data: available.genomes()}
  \begin{block}{}
    Use the \Rfunction{available.genomes} function (from the
    \Biocpkg{BSgenome} software package) to get the list:
{\small
<<available.genomes>>=
library(BSgenome)
available.genomes()
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available data: Making your own BSgenome data package}
  \begin{block}{}
    It's easy to make your own package if your organism is not supported.
    This is documented in the BSgenomeForge vignette in the
    \Biocpkg{BSgenome} software package.
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Available software}
  \begin{block}{Range manipulation}
    \begin{itemize}
      \item \Biocpkg{IRanges}
      \item \Biocpkg{GenomicRanges}
    \end{itemize}
  \end{block}
  \begin{block}{Creation and manipulation of transcript centric annotations}
    \begin{itemize}
      \item \Biocpkg{GenomicFeatures}
    \end{itemize}
  \end{block}
  \begin{block}{Sequence manipulation}
    \begin{itemize}
      \item \Biocpkg{Biostrings}
      \item \Biocpkg{BSgenome}
    \end{itemize}
  \end{block}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{USE CASE I: Confirmation of the GT-AG rule for Yeast}

\begin{frame}[fragile]
  \frametitle{The GT-AG rule}
  \begin{block}{}
    "Nearly all eukaryotic nuclear introns begin with the nucleotide
    sequence GT, and end with AG."

    See \url{http://en.wikipedia.org/wiki/Intron} for more info.
  \end{block}
  \begin{block}{}
    Our use case is to confirm the GT-AG rule for Yeast.
    We choose to use the sacCer2 reference genome from UCSC for this.
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{What we will use}
  \begin{block}{Reference genome (sequences)}
    The \Biocpkg{BSgenome.Scerevisiae.UCSC.sacCer2} package.
  \end{block}
  \begin{block}{From \Biocpkg{GenomicFeatures}}
    \begin{itemize}
      \item The \Rfunction{makeTranscriptDbFromUCSC} function
            to make the \Rclass{TranscriptDb} object from the sacCer2
            genome.
      \item The \Rfunction{intronsByTranscript} function
            to extract the intron ranges from this \Rclass{TranscriptDb}
            object.
    \end{itemize}
  \end{block}
  \begin{block}{From \Biocpkg{BSgenome}}
    \begin{itemize}
      \item The \Rfunction{getSeq} function to extract the intron
            sequences from \Biocpkg{BSgenome.Scerevisiae.UCSC.sacCer2}.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{What we will use (continued)}
  \begin{block}{From \Biocpkg{Biostrings}}
    \begin{itemize}
      \item The \Rfunction{consensusMatrix} and \Rfunction{consensusString}
            functions.
      \item The \Rfunction{narrow} method for \Rclass{DNAStringSet}
            objects to cut the region of interest from our intron
            sequences.
    \end{itemize}
  \end{block}
  \begin{block}{Plus...}
    \begin{itemize}
      \item all the things we are going to use implicitely like the
            \Rclass{GRanges}, \Rclass{GRangesList},
            \Rclass{DNAStringSet} and \Rclass{BSgenome} containers,
            and more...
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Make the TranscriptDb object for sacCer2}
  \begin{block}{}
{\small
<<supportedUCSCtables>>=
library(GenomicFeatures)
head(supportedUCSCtables())
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Make the TranscriptDb object for sacCer2 (continued)}
  \begin{block}{}
{\small
<<supportedUCSCtables_2>>=
rownames(supportedUCSCtables())
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Make the TranscriptDb object for sacCer2 (continued)}
  \begin{block}{}
{\small
<<make_txdb1>>=
txdb1 <- makeTranscriptDbFromUCSC(genome="sacCer2", tablename="sgdGene")
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Make the TranscriptDb object for sacCer2 (continued)}
  \begin{block}{}
{\small
<<show_txdb1>>=
txdb1
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Extract the introns ranges}
  \begin{block}{}
{\small
<<intronsByTranscript>>=
introns <- intronsByTranscript(txdb1)
introns
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A quick look at this GRangesList object}
  \begin{block}{Nb of introns per transcript}
{\small
<<GRangesList_quick_look1>>=
table(elementLengths(introns))
@
}
  \end{block}
  \begin{block}{Total nb of introns}
{\small
<<GRangesList_quick_look2>>=
sum(elementLengths(introns))
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{From GRangesList to GRanges (unlist)}
  \begin{block}{}
{\small
<<unlist>>=
introns <- unlist(introns)
introns
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Load the sacCer2 genome}
  It's important to use the same reference genome as for
  the \Rclass{TranscriptDb} object.
  \begin{block}{}
{\small
<<load_Scerevisiae>>=
library(BSgenome.Scerevisiae.UCSC.sacCer2)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A quick look at the sacCer2 genome}
  \begin{block}{}
{\small
<<Scerevisiae_quick_look>>=
Scerevisiae
Scerevisiae$chrI
seqlengths(Scerevisiae)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A quick look at the sacCer2 genome (continued)}
  \begin{block}{}
{\small
<<Scerevisiae_quick_look_2>>=
seqlengths(Scerevisiae)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Extract the intron sequences}
  \begin{block}{}
{\small
<<getSeq>>=
intron_seqs <- getSeq(Scerevisiae, introns, as.character=FALSE)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A quick look at this DNAStringSet object}
  \begin{block}{}
{\small
<<DNAStringSet>>=
intron_seqs
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Look at the first 2 bases of each intron}
  \begin{block}{}
{\small
<<narrow_error, eval=FALSE>>=
narrow(intron_seqs, end=2)  # error!
@
<<drop_very_short_introns>>=
table(width(intron_seqs))
intron_seqs <- intron_seqs[width(intron_seqs) >= 4]
intron_seqs
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Look at the first 2 bases of each intron (continued)}
  \begin{block}{}
{\small
<<narrow_OK>>=
narrow(intron_seqs, end=2)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Consensus matrix}
  \begin{block}{}
{\small
<<consensusMatrix>>=
consensusMatrix(narrow(intron_seqs, end=2), baseOnly=TRUE)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Consensus string}
  \begin{block}{}
{\small
<<consensusString>>=
consensusString(narrow(intron_seqs, end=4))
@
}
  \end{block}
  Looking at the last 2 bases of each intron is left as an exercise.
  Tip: \Rfunction{narrow} accepts negative start values (they are counted
  from the end of each sequence).
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{USE CASE II: Extract the Yeast transcriptome and translate it}

\begin{frame}[fragile]
  \frametitle{Goal}
  \begin{block}{}
    In this use case, we want to extract the Yeast transcriptome based
    on the transcripts information stored in our \Rclass{TranscriptDb}
    object.
  \end{block}
  \begin{block}{}
    Again, we must be careful to use the reference genome that matches
    exactly our \Rclass{TranscriptDb} object.
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{What we will use}
  \begin{block}{Reference genome (sequences)}
    The \Biocpkg{BSgenome.Scerevisiae.UCSC.sacCer2} package.
  \end{block}
  \begin{block}{From \Biocpkg{GenomicFeatures}}
    \begin{itemize}
      \item The \Rfunction{makeTranscriptDbFromUCSC} function
            to make the \Rclass{TranscriptDb} object from the sacCer2
            genome.
      \item The \Rfunction{extractTranscriptsFromGenome} function
            to extract the transcriptome.
      \item The \Rfunction{cdsBy} function
            to extract the CDS ranges (grouped by transcripts)
            from this \Rclass{TranscriptDb} object.
    \end{itemize}
  \end{block}
  \begin{block}{From \Biocpkg{Biostrings}}
    \begin{itemize}
      \item The \Rfunction{translate} function to translate the
            transcriptome.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Extract the full transcriptome}
  We asume we've already made the txdb1 object and loaded
  the sacCer2 genome like in USE CASE I.
  \begin{block}{}
{\small
<<extractTranscriptsFromGenome>>=
tx_seqs <- extractTranscriptsFromGenome(Scerevisiae, txdb1)
tx_seqs
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 1st attempt}
  \begin{block}{}
{\small
<<translate_tx_seqs>>=
translate(tx_seqs)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 1st attempt (continued)}
  \begin{block}{}
{\small
<<translate_warnings>>=
warnings()
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 1st attempt (continued)}
  \begin{block}{}
{\small
<<looking_at_stop_codon>>=
narrow(translate(tx_seqs), start=-5)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 1st attempt (continued)}
  \begin{block}{}
{\small
<<looking_at_stop_codon_2>>=
consensusMatrix(narrow(translate(tx_seqs), start=-5))
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Extract the translated part of the transcriptome}
  \begin{block}{}
{\small
<<cdsBy>>=
cds <- cdsBy(txdb1)
cds
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Extract the translated part of the transcriptome (continued)}
  \begin{block}{}
{\small
<<cds_seqs>>=
cds_seqs <- extractTranscriptsFromGenome(Scerevisiae, cds)
cds_seqs
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 2nd attempt}
  \begin{block}{}
{\small
<<translate_cds_seqs>>=
translate(cds_seqs)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 2nd attempt (continued)}
  \begin{block}{}
{\small
<<translate_cds_seqs_2>>=
narrow(translate(cds_seqs), start=-5)
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 2nd attempt (continued)}
  \begin{block}{}
{\small
<<translate_cds_seqs_3>>=
consensusMatrix(narrow(translate(cds_seqs), start=-5))
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Translation - 2nd attempt (continued)}
  As an extra sanity check, we use the \Rfunction{vcountPattern}
  function from the \Biocpkg{Biostrings} package to count the number
  of * in each translated transcript.
  \begin{block}{}
{\small
<<translate_cds_seqs_4>>=
table(vcountPattern("*", translate(cds_seqs)))
@
}
  \end{block}
  Things still don't look completely right :-/
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{USE CASE III: Remap probeset ids to their corresponding genes
         using sequence matching}

\begin{frame}[fragile]
  \frametitle{Motivation}
  \begin{block}{}
    The mapping from probeset ids to gene ids provided by the microarray
    manufacturer may not always be accurate.
    In this use case, we will compute this mapping using pattern matching
    facilities available in the \Biocpkg{Biostrings} package to match
    the probe sequences against the transcriptome. Then we show how
    to infer the mapping from probeset ids to transcript ids from the
    result of this matching.
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{What we will use}
  \begin{block}{Probe sequences for the Yeast Genome 2.0 Array}
    The \Biocpkg{yeast2probe} package.
  \end{block}
  \begin{block}{Reference genome (sequences)}
    The \Biocpkg{BSgenome.Scerevisiae.UCSC.sacCer2} package.
  \end{block}
  \begin{block}{From \Biocpkg{GenomicFeatures}}
    \begin{itemize}
      \item The \Rfunction{makeTranscriptDbFromUCSC} function
            to make the \Rclass{TranscriptDb} object from the sacCer2
            genome.
      \item The \Rfunction{extractTranscriptsFromGenome} function
            to extract the transcriptome.
    \end{itemize}
  \end{block}
  \begin{block}{From \Biocpkg{Biostrings}}
    \begin{itemize}
      \item The \Rfunction{DNAStringSet} and \Rfunction{PDict} constructors.
      \item The \Rfunction{vwhichPDict} function to find which probes hit
            each transcript.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{yeast2.db}
  \begin{block}{}
  But first we have a quick look at the \Biocpkg{yeast2.db} package.
{\small
<<yeast2.db>>=
library(yeast2.db)  # Affymetrix Yeast Genome 2.0 Array
ls('package:yeast2.db')
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{yeast2.db (continued)}
  \begin{block}{}
  A sanity check:
{\small
<<Rkeys_yeast2ENSEMBL>>=
all(Rkeys(yeast2ENSEMBL) %in% names(tx_seqs)) 
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Matching yeast2probe}
  \begin{block}{}
{\small
<<>>=
library(yeast2probe)
yeast2_dict <- DNAStringSet(yeast2probe)
yeast2_dict
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Matching yeast2probe (continued)}
  \begin{block}{}
{\small
<<>>=
yeast2_pdict <- PDict(yeast2_dict)
yeast2_pdict
tx2probes <- vwhichPDict(yeast2_pdict, tx_seqs)
@
}
  \end{block}
\end{frame}

\end{document}
