%\VignetteDepends{org.Hs.eg.db,hgu95av2.db,GO.db,RSQLite}
%\VignetteIndexEntry{Using Annotations in Bioconductor}
%\VignettePackage{HTSandGeneCentricLabs}

\documentclass{beamer}

\usepackage{Sweave}
\usepackage{color}
\usepackage{graphics}
\usepackage{Bioconductor}

\SweaveOpts{keep.source=TRUE}

\title{Using Annotations in Bioconductor}
\author{Marc Carlson}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
\date{July 30, 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{Bioconductor Annotation Packages} 
  
\begin{frame}[fragile]
  \frametitle{Bioconductor annotation packages}
  Major types of annotation in Bioconductor.
  
  \structure{AnnotationDbi} packages:
  \begin{itemize}
    \item Organism level: \structure{org.Mm.eg.db}.
    \item Platform level: \structure{hgu133plus2.db}. 
    \item System-biology level: \structure{GO.db} or \structure{KEGG.db}.
    \item Transcript centric annotations: \structure{GenomicFeatures}.
  \end{itemize}
  
  \structure{biomaRt}: 
    \begin{itemize}
      \item Query web-based `biomart' resource for genes, sequence,
           SNPs, and etc. 
    \end{itemize}
  
  Other packages: 
    \begin{itemize}
      \item \structure{rtracklayer} -- export to UCSC web browsers.
    \end{itemize}
\end{frame}




\section{AnnotationDbi}

\begin{frame}[fragile]
  \frametitle{AnnotationDbi}
  AnnotationDbi is a software package that enables the package annotations:
\begin{itemize}
  \item Each supported package contains a database.
  \item AnnotationDbi allows access to that data via Bimap objects.
  \item Some databases depend on the databases in other packages.
\end{itemize}
\end{frame}



\begin{frame}[fragile]
  \frametitle{Organism-level annotation}
  There are a number of organism annotation packages with names starting with
  org, e.g., \structure{org.Hs.eg.db} -- genome-wide annotation for human.
{\small
<<organnotation, results=hide>>=
library(org.Hs.eg.db)
org.Hs.eg()
org.Hs.eg_dbInfo()
org.Hs.egGENENAME
org.Hs.eg_dbschema()
@   
}
\end{frame}



\begin{frame}[fragile]
  \frametitle{platform based packages (chip packages)}
  There are a number of platform or chip specific annotation packages
  named after their respective platforms, e.g. \structure{hgu95av2.db} -
  annotations for the hgu95av2 Affymetrix platform.
\begin{itemize}
  \item These packages appear to contain a lot of data but it's an illusion.
\end{itemize}
{\small
<<platannotation, results=hide>>=
library(hgu95av2.db)
hgu95av2()
hgu95av2_dbInfo()
hgu95av2GENENAME
hgu95av2_dbschema()
@   
}
\end{frame}




\begin{frame}[fragile]
  \frametitle{Kinds of annotation}
  What can you hope to extract from an annotation package? 
  
  \begin{itemize}
    \item GO IDs: \structure{GO}
    \item KEGG pathway IDs: \structure{PATH}
    \item Gene Symbols: \structure{SYMBOL}
    \item Chromosome start and stop locs: \structure{CHRLOC} and \structure{CHRLOCEND}
    \item Alternate Gene Symbols: \structure{ALIAS}
    \item Associated Pubmed IDs: \structure{PMID}
    \item RefSeq IDs: \structure{REFSEQ}
    \item Unigene IDs: \structure{UNIGENE}
    \item PFAM IDs: \structure{PFAM}
    \item Prosite IDs: \structure{PROSITE}
    \item ENSEMBL IDs: \structure{ENSEMBL}
  \end{itemize}  
\end{frame}



\section{AnnotationDbi Basics}

\begin{frame}[fragile]
  \frametitle{Basic Bimap structure and getters}
  Bimaps create a mapping from one set of keys to another.  And they can easily be searched.
\begin{itemize}
  \item \Rfunction{toTable}: converts a Bimap to a \structure{data.frame}
  \item \Rfunction{get}: pulls data from a Bimap
  \item \Rfunction{mget}: pulls data from a Bimap for multiple things at once
\end{itemize}
{\small
<<basic bimap1, results=hide>>=
head(toTable(hgu95av2SYMBOL))
get("38187_at",hgu95av2SYMBOL)
mget(c("38912_at","38187_at"),hgu95av2SYMBOL,ifnotfound=NA)
@   
}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Reversing and subsetting Bimaps}
  Bimaps can also be reversed and subsetted:
\begin{itemize}
  \item \Rfunction{revmap}: reverses a Bimap
  \item \Rfunction{[[},\Rfunction{[}: Bimaps are subsettable.
\end{itemize}
{\small
<<basic bimap2, results=hide>>=
##revmap
mget(c("NAT1","NAT2"),revmap(hgu95av2SYMBOL),ifnotfound=NA)
##subsetting
head(toTable(hgu95av2SYMBOL[1:3]))
hgu95av2SYMBOL[["1000_at"]]
revmap(hgu95av2SYMBOL)[["MAPK3"]]
##Or you can combine things
toTable(hgu95av2SYMBOL[c("38912_at","38187_at")])
@   
}
\end{frame}



%EXERCISE 1
\begin{frame}[fragile]
  \frametitle{Annotation exercise 1}
  Find the gene symbol, chromosome position and KEGG pathway ID for "1003\_s\_at".
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 1 solution}
{\small
<<exer1, results=hide>>=
library(hgu95av2.db)
get("1003_s_at",hgu95av2SYMBOL) 
get("1003_s_at",hgu95av2CHRLOC) 
get("1003_s_at",hgu95av2PATH) 

##OR if you like data frames:
toTable(hgu95av2SYMBOL["1003_s_at"])
toTable(hgu95av2CHRLOC["1003_s_at"])
toTable(hgu95av2PATH["1003_s_at"])
@
}
\end{frame}





\begin{frame}[fragile]
  \frametitle{Bimap keys}
  Bimaps create a mapping from one set of keys to another.  Some important methods include:
\begin{itemize}
  \item \Rfunction{keys}: centralID for the package (directional)
  \item \Rfunction{Lkeys}: centralID for the package (probe ID or gene ID)
  \item \Rfunction{Rkeys}: centralID for the package (attached data)
\end{itemize}
{\small
<<keys1 bimap, results=hide>>=
keys(hgu95av2SYMBOL[1:4])
Lkeys(hgu95av2SYMBOL[1:4])
Rkeys(hgu95av2SYMBOL)[1:4]
@   
}
\end{frame}


\begin{frame}[fragile]
  \frametitle{More Bimap structure}
  Not all keys have a partner (or are mapped)
\begin{itemize}
  \item \Rfunction{mappedkeys}: which of the key are mapped (directional)
  \item \Rfunction{mappedLkeys} \Rfunction{mappedRkeys}: which keys are mapped (absolute reference)
  \item \Rfunction{count.mappedkeys}: Number of mapped keys (directional)
  \item \Rfunction{count.mappedLkeys},\Rfunction{count.mappedRkeys}: Number of mapped keys (absolute)
\end{itemize}
{\small
<<keys2 Bimap, results=hide>>=
mappedkeys(hgu95av2SYMBOL[1:10])
mappedLkeys(hgu95av2SYMBOL[1:10])
mappedRkeys(hgu95av2SYMBOL[1:10])
count.mappedkeys(hgu95av2SYMBOL[1:100])
count.mappedLkeys(hgu95av2SYMBOL[1:100])
count.mappedRkeys(hgu95av2SYMBOL[1:100])
@   
}
\end{frame}



\begin{frame}[fragile]
  \frametitle{Bimap Conversions}
  How to handle conversions from Bimaps to lists
\begin{itemize}
  \item \Rfunction{as.list}: converts a Bimap to a \structure{list}
  \item \Rfunction{unlist2}: unlists a \structure{list} minus the name-mangling.
\end{itemize}
{\small
<<Bimap conversion, results=hide>>=
as.list(hgu95av2SYMBOL[c("38912_at","38187_at")])
unlist(as.list(hgu95av2SYMBOL[c("38912_at","38187_at")]))
unlist2(as.list(hgu95av2SYMBOL[c("38912_at","38187_at")]))

##but what happens when there are 
##repeating values for the left key?
unlist(as.list(revmap(hgu95av2SYMBOL)[c("STAT1","PTGER3")]))   

##unlist2 can help with this
unlist2(as.list(revmap(hgu95av2SYMBOL)[c("STAT1","PTGER3")]))   
@   
}
\end{frame}



%EXERCISE 2
\begin{frame}[fragile]
  \frametitle{Annotation exercise 2}
  Gene symbols are often recycled by other genes making them a poor
  choice for identifiers.  Using what you have learned, the SYMBOL
  Bimap, along with the \Rfunction{lapply}, \Rfunction{length} and
  \Rfunction{sort} functions, determine which gene symbols in hgu95av2
  are the worst offenders.
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 2 solution}
{\small
<<exer2, results=hide>>=
badRank <- lapply(as.list(revmap(hgu95av2SYMBOL)), length)
tail(sort(unlist(badRank)))
@
}
\end{frame}



\begin{frame}[fragile]
  \frametitle{toggleProbes}
  How to hide/unhide ambiguous probes.
\begin{itemize}
  \item \Rfunction{toggleProbes}: hides or displays the probes that have multiple mappings to genes.
\end{itemize}
{\small
<<toggleProbes, results=hide>>=
## How many probes?
dim(hgu95av2ENTREZID)
## Make a mapping with multiple probes exposed 
multi <- toggleProbes(hgu95av2ENTREZID, "all")
## How many probes?
dim(multi)

## Make a mapping with ONLY multiple probes exposed 
multiOnly <- toggleProbes(multi, "multiple")
## How many probes?
dim(multiOnly)

## Then make a mapping with ONLY single mapping probes
singleOnly <- toggleProbes(multiOnly, "single")
## How many probes?
dim(singleOnly)  
@   
}
\end{frame}



%EXERCISE 3
\begin{frame}[fragile]
  \frametitle{Annotation exercise 3}
  Using the knowledge that Entrez IDs are good IDs that can be used to
  define genes uniquely, find the probe that maps to the largest
  number of different genes on hgu95av2.
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 3 solution}
{\small
<<exer3, results=hide>>=
mult <- toggleProbes(hgu95av2ENTREZID, "multi")
dim(mult)
multRank <- lapply(as.list(mult), length)
tail(sort(unlist(multRank)))
@
}
\end{frame}





\section{Working with GO.db}


\begin{frame}[fragile]
  \frametitle{GO}
  Some important considerations about the Gene Ontology
\begin{itemize}
  \item GO is actually 3 ontologies (CC, BP and MF)
  \item Each ontology is a directed acyclic graph.
  \item The structure of GO is maintained separarately from the genes
    that these GO IDs are usually used to annotate.
\end{itemize}

\end{frame}




\begin{frame}[fragile]
  \frametitle{GO to gene mappings are stored in other packages}
  Mapping Entrez IDs to GO
  \begin{itemize}
    \item Each ENTREZ ID is associated with up to three GO categories.
    \item The objects returned from an ordinary GO mapping are complex.
  \end{itemize}
{\small
<<entrez to GO, results=hide>>=
go <- org.Hs.egGO[["1000"]]
length(go)
go[[2]]$GOID
go[[2]]$Ontology
@ 
}
\end{frame}


%TODO: give people a heads up on sapply(GOids,names)
%EXERCISE 4
\begin{frame}[fragile]
  \frametitle{Annotation exercise 4}
  Use what you have learned to write a function that gets the
  GOIDs for a particular entrez gene ID, and then returns only their
  GOID as a named vector.  Use \Rfunction{lapply} and \Rfunction(names).
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 4 solution}
{\small
<<exer4, results=hide>>=
##get GOIDs from Hs package.
getGOIDs <- function(ids){
    require(org.Hs.eg.db)
    GOs = mget(ids, org.Hs.egGO, ifnotfound=NA)
    unlist2(lapply(GOs,names))
}
##usage example:
getGOIDs(c("1","10"))
@
}
\end{frame}




\begin{frame}[fragile]
  \frametitle{Working with GO.db}
  \begin{itemize}
    \item Encodes the hierarchical structure of GO terms.
    \item The mapping between \texttt{GO} terms and individual genes
      is maintained in the GO mappings from the other packages.
    \item the difference between children and offspring is how many
      generations are represented.  Children only nets you one step down 
      the graph.
  \end{itemize}
{\small  
<<working with GO, results=hide>>=
library(GO.db)
ls("package:GO.db")
## find children
as.list(GOMFCHILDREN["GO:0008094"])
## all the descendants (children, grandchildren, and so on)
as.list(GOMFOFFSPRING["GO:0008094"])
@ 
}  
\end{frame}



\begin{frame}[fragile]
  \frametitle{GO helper methods}
  Using the GO helper methods
  \begin{itemize}
    \item The GO terms are described in detail in the \structure{GOTERM} 
      mapping.
    \item The objects returned by GO.db are GOTerms objects, which can
      make use of helper methods like \Rfunction{GOID}, \Rfunction{Term},
      \Rfunction{Ontology} and \Rfunction{Definition} to retrieve various
      details.
    \item You can also pass GOIDs to these helper methods.
  \end{itemize}
{\small
<<GOTERMS, results=hide>>=
##Mapping a GOTerms object
go <- GOTERM[1]
GOID(go)
Term(go)

##OR you can supply GO IDs
id = c("GO:0007155","GO:0007156")
GOID(id)
Term(id)
Ontology(id)
Definition(id)
@ 
}
\end{frame}


%EXERCISE 5
\begin{frame}[fragile]
  \frametitle{Annotation exercise 5}
  Use what they have learned to write a function that calls your
  previous function so that it can get GOIDs and then returns the GO
  definitions.
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 5 solution}
{\small
<<exer5, results=hide>>=
##get GOIDs from Hs package.
library(GO.db)
getGODefs <- function(ids){
    GOids <- getGOIDs(ids)
    defs <- Definition(GOids)
    names(defs) <- names(GOids)
    defs
}
##usage example:
getGODefs(c("1","10"))
@
}
\end{frame}









%% Below is an example to create a slide as well as other features of
%% beamer-style vignette such as block and columns. If your slides
%% contain R chunck code, alway use \begain{frame}[fragile] to begin a slide
%% frame. Otherwise, you can use us \begin{frame}.
%% One more thing, \section{} allows the beamer-style vignette to create a
%% table of content, which will be placed at the first slide. 
% \section{AnnotationDbi}
% \begin{frame}[fragile]
%   \frametitle{haha}
%     An example of creating block 
%     \begin{block}{I am a block}
%       content of the block
%     \end{block}    
    
%     An example of creating columns
%     \begin{columns}
%       \begin{column}{0.5\textwidth}
%         column 1
%       \end{column}
%       \begin{column}{0.5\textwidth}
%         column 2
%       \end{column}
%     \end{columns}
    
% \end{frame}


%% \end{document}







%-----------------------------------------------
%-----------------------------------------------
\section{SQL databases}



%-----------------------------------------------
\begin{frame}{Relational Databases}
\begin{block}{Relational database basics}
  \begin{itemize}
    \item Data stored in \textit{tables}
    \item Tables related through \textit{keys}
    \item Relational model called a \textit{schema}
    \item Tables designed to avoid redundancy
  \end{itemize}    
\end{block}
\begin{block}{Beneficial uses by R packages}
  \begin{itemize}
    \item Out-of-memory data storage
    \item Fast access to data subsets
    \item Databases accessible by other software
  \end{itemize}
\end{block}    
\end{frame}



%-----------------------------------------------
\begin{frame}{Uses of Relational Databases in Bioconductor}
\begin{block}{Annotation packages}
  \begin{itemize}
    \item Organism, genome (e.g. \Rpackage{org.Hs.eg.db})
    \item Microarray platforms (e.g. \Rpackage{hgu95av2.db})
    \item Homology (e.g. \Rpackage{hom.Hs.inp.db})
  \end{itemize}    
\end{block}
\begin{block}{Software packages}
  \begin{itemize}
    \item Transcript annotations (e.g. \Rpackage{GenomicFeatures})
    \item NGS experiments (e.g. \Rpackage{Genominator})
    \item Annotation infrastructure (e.g. \Rpackage{AnnotationDbi})
  \end{itemize}
\end{block}    
\end{frame}




%% TODO: replace this with an org schema or a platform schema (or one of ea.)
%-----------------------------------------------
\begin{frame}{A example database schema}
  \begin{figure}
    \includegraphics[width=4.25in]{database/transcriptschema.png}
  \end{figure}
\end{frame}



%-----------------------------------------------
%-----------------------------------------------
%% I should probably show people how to see the schemas before I get
%% to this next part...  OR I can show them this next.

\section{Basic SQL}
%% Slides to teach some basic SQL so that people can follow along (10 slides)

\begin{frame}{SQL in 3 slides}
  Structured Query Language (SQL) is the most common language
  for interacting with relational databases.
\end{frame}

%-----------------------------------------------
\begin{frame}[fragile, shrink=15]{Database Retrieval}
\begin{block}{Single table selections}
  \begin{verbatim}
    SELECT * FROM gene;
    SELECT gene_id, gene._tx_id FROM gene;

    SELECT * FROM gene WHERE _tx_id=49245;
    SELECT * FROM transcript WHERE tx_name LIKE '%oap.1'; 
  \end{verbatim}
\end{block}
\begin{block}{Inner joins}
  \begin{verbatim}
    SELECT gene.gene_id,transcript._tx_id
      FROM gene, transcript
      WHERE gene._tx_id=transcript._tx_id;

    SELECT g.gene_id,t._tx_id 
      FROM gene AS g, transcript AS t
      WHERE g._tx_id=t._tx_id
      AND t._tx_id > 10;
  \end{verbatim}
\end{block}
\end{frame}

%-----------------------------------------------
\begin{frame}[fragile, shrink=15]{Database Modifications}
\begin{block}{CREATE TABLE}
  \begin{verbatim}
    CREATE TABLE foo (
      id INTEGER,
      string TEXT
    );
  \end{verbatim}
\end{block}
\begin{block}{INSERT}
  \begin{verbatim}
    INSERT INTO foo (id, string) VALUES (1,"bar");
  \end{verbatim}
\end{block}
\begin{block}{CREATE INDEX}
  \begin{verbatim}
    CREATE INDEX fooInd1 ON foo(id);
  \end{verbatim}
\end{block}
\end{frame}



% DEMO: demonstrate the SQL statements on the test DB.





%-----------------------------------------------
%-----------------------------------------------
\section{Using SQL from within R}
%% Slides to introduce DBI, RSQLite and how these are used to: 1)
%% build DBs, and 2) access the details of this database.(10-15 slides)


%-----------------------------------------------
\begin{frame}[fragile]{The \Rpackage{DBI} package}
  \begin{itemize}
    \item Provides a nice generic access to databases in R
    \item Many of the functions are convenient and simple to use
  \end{itemize}  
\end{frame}


%-----------------------------------------------
\begin{frame}[fragile]{Some popular DBI functions}
<<driverUsageAndListTables>>=
library(RSQLite) #loads DBI too, (but we need both)
drv <- dbDriver("SQLite")
con <- dbConnect(drv, dbname=system.file("extdata", 
                 "mm9KG.sqlite", package="HTSandGeneCentricLabs"))
dbListTables(con)
dbListFields(con,"transcript")
@ 
\end{frame}


%-----------------------------------------------
\begin{frame}[fragile]{The dbGetQuery approach}
<<dbGetQuery>>=
dbGetQuery(con, "SELECT * FROM transcript LIMIT 3")
@ 
\end{frame}


%-----------------------------------------------
\begin{frame}[fragile]{The dbSendQuery approach}
If you use result sets, you also need to put them away
<<dbSendQuery>>=
res <- dbSendQuery(con, "SELECT * FROM transcript")
fetch(res, n= 3)
dbClearResult(res)
@ 
Calling fetch again will get the next three results.
This allows for simple iteration.
\end{frame}


%-----------------------------------------------
%EXERCISE 6
\begin{frame}[fragile]{Exercise 6}
  Connect to the database in these slides and use the schema diagrammed
  to select the exons from the minus strand of chromosome 9.
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 6 solution}
{\small
<<exercise6,results=hide>>=
library(RSQLite)
drv <- dbDriver("SQLite")
con <- dbConnect(drv, dbname=system.file("extdata", 
                 "mm9KG.sqlite", package="HTSandGeneCentricLabs"))
sql <- "SELECT * FROM exon WHERE exon.exon_strand='-' 
        AND exon.exon_chrom='chr9'"
res <- dbGetQuery(con, sql)
@ 
}
\end{frame}




%-----------------------------------------------
\begin{frame}[fragile,shrink=15]{Setting up a new DB}
  First, lets close the connection to our other DB:
<<dbDisconnect>>=
   dbDisconnect(con)
@ 
  Then lets make a new database.
  Notice that we specify the database name with "dbname" 
  This allows it to be written to disc instead of just memory.
<<makeNewDb>>=
drv <- dbDriver("SQLite")
con <- dbConnect(drv, dbname="myNewDb.sqlite")
@ 
Once you have this, you may want to make a new table
<<CreateNewTable>>=
dbGetQuery(con, "CREATE Table foo (id INTEGER, string TEXT)")
@ 
\end{frame}


% -----------------------------------------------
%EXERCISE 7
\begin{frame}[fragile]{Exercise 7}
  Create a database and then put a table in it called genePheno to store the genes mutated and a phenotypes associated with each.  Plan for genePheno to hold the following gene IDs and phenotypes (as a toy example):
\begin{verbatim}
data = data.frame(id=c(69773,20586,258822,18315),
                  string=c("Dead",
                           "Alive",
                           "Dead",
                           "Alive"),
                  stringsAsFactors=FALSE)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 7 solution}
{\small
<<exercise7,results=hide>>=
drv <- dbDriver("SQLite")
dbcon <- dbConnect(drv, dbname="myExDb.sqlite")
cval <- dbGetQuery(dbcon, 
                   "CREATE Table genePheno 
                    (id INTEGER, string TEXT)")
@ 
}
\end{frame}


%-----------------------------------------------
\begin{frame}[fragile]{The \Rpackage{RSQLite} package}
  \begin{itemize}
    \item Provides SQLite access for R
    \item Much better support for complex inserts
  \end{itemize}  
\end{frame}




%-----------------------------------------------
\begin{frame}[fragile]{Prepared queries}
<<LabelledPreparedQueries>>=
data <- data.frame(c(226089,66745),
                  c("C030046E11Rik","Trpd52l3"), 
                  stringsAsFactors=FALSE)
names(data) <- c("id","string")
sql <- "INSERT INTO foo VALUES ($id, $string)"
dbBeginTransaction(con)
dbGetPreparedQuery(con, sql, bind.data = data)
dbCommit(con)
@ 
Notice that we want strings instead of factors in our data.frame
\end{frame}




% -----------------------------------------------
%EXERCISE 8
\begin{frame}[fragile]{Exercise 8}
  Now take a moment to insert that data into your database.
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 8 solution}
{\small
<<exercise8,results=hide>>=
data <- data.frame(id=c(69773,20586,258822,18315),
                   string=c("Dead",
                            "Alive",
                            "Dead",
                            "Alive"),
                   stringsAsFactors=FALSE)
sql <- "INSERT INTO genePheno VALUES ($id,$string)"
bval <- dbBeginTransaction(dbcon)
gval <- dbGetPreparedQuery(dbcon, sql, bind.data = data)
cval <- dbCommit(dbcon)
@ 
}
\end{frame}




%-----------------------------------------------
\begin{frame}[fragile]{in SQLite, you can ATTACH Dbs}
  The SQL what we want looks quite simple:
  \begin{verbatim}
    ATTACH "mm9KG.sqlite" AS db;
  \end{verbatim}  
  So we just need to do something like this:
<<ATTACH>>=
db <- system.file("extdata", "mm9KG.sqlite", 
                  package="HTSandGeneCentricLabs")
dbGetQuery(con, sprintf("ATTACH '%s' AS db",db))
@ 
\end{frame}



%-----------------------------------------------
\begin{frame}[fragile, shrink=15]{You can join across attached Dbs}
  The SQL this looks like:
  \begin{verbatim}
    SELECT * FROM db.gene AS dbg, foo AS f 
    WHERE dbg.gene_id=f.id;
  \end{verbatim}  
  Then in R:
<<ATTACHJoin>>=
  sql <- "SELECT * FROM db.gene AS dbg, 
          foo AS f WHERE dbg.gene_id=f.id"
  res <- dbGetQuery(con, sql)
  res
@ 
\end{frame}




% -----------------------------------------------
%EXERCISE 9
\begin{frame}[fragile, shrink=15]{Exercise 9}
  Now create a cross join to your database and extract the \_tx\_id 's from the gene table there using your gene IDs as a foreign key.
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 9 solution}
{\small
<<exercise9,results=hide>>=
  #1st attach
  db <- system.file("extdata", "mm9KG.sqlite", 
                    package="HTSandGeneCentricLabs")
  att <- dbGetQuery(dbcon, sprintf("ATTACH '%s' AS db",db))
  #then select
  sql <- "SELECT * FROM db.gene AS dbg, 
          genePheno AS gp WHERE dbg.gene_id=gp.id"
  res <- dbGetQuery(dbcon, sql)
@ 
}
\end{frame}




%-----------------------------------------------
%EXERCISE 10
\begin{frame}[fragile, shrink=15]{Exercise 10}
 Now conect your cross join to the transcript table in the database and extract the fields from that table while still using your gene IDs as a foreign key.
\end{frame}

\begin{frame}[fragile]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Annotation exercise 10 solution}
{\small
<<exercise10,results=hide>>=
  sql <- "SELECT * FROM db.gene AS dbg, 
          genePheno AS gp, transcript AS t 
          WHERE dbg.gene_id=gp.id
          AND dbg._tx_id=t._tx_id"
  res <- dbGetQuery(dbcon, sql)
@ 
}
\end{frame}



%% TODO: add a section demonstrating how to use SQLForge to make custom platform DBs?  
%% And add a slide that mentions there is a vignette for SQLForge and a more custom option as well.













 
 













%%%% Island of lost slides below:

%% TODO discuss dbname=:memory: (default)
%% TODO discuss RSQLites SQLite() and SQLITE_RO() modes etc.

%% TODO: discuss blobs???
% \begin{frame}[fragile]{handling blobs}
% <<code-block>>=
% @ 
% \end{frame}


%% parallel R on database access- should work now, but benefits for annotation packages are held back (I think) due to caching mechanism.  So instead use direct SQL queries.  So make some slides to discuss that:


% \begin{frame}[fragile]{An overview of the GenomicFeatures package}
% <<code-block>>=
% ## This example ONLY shows improvement for multicore the FIRST time its run...
% ## suspect there is some caching going on...
% library(multicore)
% library(org.Hs.eg.db)
% x <- list(org.Hs.egALIAS2EG, org.Hs.egCHRLOCEND)
% system.time(lapply(x, toTable))
% system.time(mclapply(x, toTable))
% @ 
% \end{frame}


%% mcapply seems to STILL be slower than lapply()...
% \begin{frame}[fragile]{TEST}
% <<code-block>>=
% library(DBI)
% library(RSQLite)
% x = list("genes", "kegg", "pfam", "chromosomes", "accessions", "go_cc_all")
% fun <- function(table){
%   con <- dbConnect(SQLite(), dbname = system.file("extdata","org.Hs.eg.sqlite",package="org.Hs.eg.db") ) 
%   res = sqliteExecStatement(con, paste("SELECT * FROM ",table,sep=""))
%   ans = fetch(res)
%   sqliteCloseResult(res)
% }
% res <- lapply(x, fun)
% head(res[[1]])
% head(res[[2]])

% system.time(lapply(x, fun))
% system.time(mclapply(x, fun))

% @ 
% \end{frame}



% \section{Resources}

% \begin{frame}[fragile]{Resources}
%   %% Include key resources -- urls, books, ...
% \end{frame}







<<cleanup,echo=FALSE>>=
unlink("myNewDb.sqlite")
unlink("myExDb.sqlite")
@ 


\end{document}
