%\VignetteIndexEntry{RmiR.Hs.miRNA Vignette}
%\VignetteDepends{RmiR.Hs.miRNA, hgug4112a.db}
%\VignetteKeywords{miRNA target database.}
%\VignettePackage{RmiR.Hs.miRNA}

\documentclass[12pt]{article}
\usepackage{hyperref}
\usepackage{subfigure}

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Robject}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}

\textwidth=6.2in
\textheight=8.5in
\oddsidemargin=0.2in
\evensidemargin=0.2in
\headheight=0in
\headsep=0in

\author{Francesco Favero\footnote{favero.francesco@gmail.com}}

\begin{document}
\title{RmiR.Hs.miRNA package vignette}
\maketitle

\tableofcontents

\section{Introduction}
RmiR.Hs.miRNA is an R package which includes various databases of miRNA targets:
\begin{itemize}
\item{\href{http://microrna.sanger.ac.uk/targets/v5/}{mirBase}}
\item{\href{http://www.targetscan.org/}{targetScan}}
\item{miRanda from \href{http://www.microrna.org/microrna/}{microrna.org}}
\item{tarBase from \href{http://diana.cslab.ece.ntua.gr/tarbase/}{Diana Labs}}
\item{mirTarget2 from \href{http://mirdb.org/miRDB/download.html}{mirDB}}
\item{\href{http://pictar.mdc-berlin.de/}{picTar}}
\end{itemize}

\par With the package it is possible to evaluate or comapre different miRNA target database or also retrieve the targets or the miRNAs, given  a list of miRNAs or a list of genes respectively.  

\section{Querying and evaluating a miRNA target database}
The miRNA targets databases are included in an SQLite object. We can browse and inspect them directly in an R environment:
<<Setup>>=
library(RmiR.Hs.miRNA)
dbListTables(RmiR.Hs.miRNA_dbconn())
@
We should make a SQL query to have the desired results only:
<<SQL>>=
dbGetQuery(
RmiR.Hs.miRNA_dbconn(), "SELECT * FROM tarbase WHERE mature_miRNA='hsa-miR-21'")
@

Every query gives a mature\_miRNA column with the microRNA name and a gene\_id column with the entrez gene id of the target. There could be also other additional columns useful for further investigation. These columns depend on the database. For example, in TarBase we have the PubMed ID of the article which proves the relation between the miRNA and its target, in TargetScan there are the start and the end point of the miRNA seed in the gene, and so on.
\par To evaluate the consistency of a database we can visualize two properties of the miRNA/Target relationship; the \textit{multiplicity} and the \textit{cooperativity} :

<<label=mult_tarbase, include=FALSE, echo=FALSE, eval=FALSE>>=
tarbase <-dbReadTable(RmiR.Hs.miRNA_dbconn(), "tarbase")[, 1:2]
tarb_mir <- sort(table(tarbase$mature_miRNA), decreasing=T)
plot(x=log2(c(1:length(tarb_mir))), y=tarb_mir,
     ylab="miRNA targets", xlab="log2 (rank of miRNA)")
@
<<label=coop_tarbase, include=FALSE, echo=FALSE, eval=FALSE>>=
tarb_gene <- sort(table(tarbase$gene_id), decreasing=T)
plot(x=log2(c(1:length(tarb_gene))), y=tarb_gene,
     ylab="target sites", xlab="log2 (rank of genes)")
@

\setkeys{Gin}{width=0.47\textwidth}
\begin{figure}[htbp]
\centering
\subfigure[Multiplicity of miRNA in TarBase.]{
\includegraphics{RmiRdb-fig1a.pdf}
\label{fig:1a}
}
\subfigure[Cooperativity of miRNA in TarBase.]{
\includegraphics{RmiRdb-fig1b.pdf}
\label{fig:1b}
}
\caption{Plot of the multiplicity and cooperativity generated with the TarBase database.}
\label{fig:one}
\end{figure}
<<label=plot1, fig=FALSE, eval=FALSE>>=
<<mult_tarbase>>
<<coop_tarbase>>
@
<<label=mult_targetscan, include=FALSE, echo=FALSE, eval=FALSE>>=
targetscan <-dbReadTable(RmiR.Hs.miRNA_dbconn(), "targetscan")[, 1:2]
targ_mir <- sort(table(targetscan$mature_miRNA), decreasing=T)
plot(x=log2(c(1:length(targ_mir))), y=targ_mir,
     ylab="miRNA targets", xlab="log2 (rank of miRNA)")
@
<<label=coop_targetscan, include=FALSE, echo=FALSE, eval=FALSE>>=
targ_gene <- sort(table(targetscan$gene_id), decreasing=T)
plot(x=log2(c(1:length(targ_gene))), y=targ_gene,
ylab="target sites", xlab="log2 (rank of genes)")
@

\setkeys{Gin}{width=0.47\textwidth}
\begin{figure}[htbp]
\centering
\subfigure[Multiplicity of miRNA in TargetScan.]{
\includegraphics{RmiRdb-fig2a.pdf}
\label{fig:2a}
}
\subfigure[Cooperativity of miRNA in TargetScan.]{
\includegraphics{RmiRdb-fig2b.pdf}
\label{fig:2b}
}
\caption{Plot of the multiplicity and cooperativity generated with the TargetScan database.}
\label{fig:two}
\end{figure}
<<label=plot2, fig=FALSE, eval=FALSE>>=
<<mult_targetscan>>
<<coop_targetscan>>
@

From the graphs we can see that for some miRNAs the number of predicted targets is huge ( Fig. \ref{fig:2a} ) compared with the number of experimentally validated targets ( Fig. \ref{fig:1a} ). 
\par For a predicted database we note how miRNA have a cooperative control for a lot of gene targets ( Fig. \ref{fig:2b} ), when in the TarBase database many gene targets do not have more than four target sites ( Fig. \ref{fig:1b} ).

\subsection{Find a list of miRNAs or targets}
In general the result of an analysis is a list of genes or microRNAs. A nice continuation it is to look for interesting miRNAs or gene targets matching the results.
<<lists>>=
mirna <- c("hsa-miR-148b", "hsa-miR-27b", "hsa-miR-25","hsa-miR-181a",
	   "hsa-miR-27a", "hsa-miR-7", "hsa-miR-32", "hsa-miR-32",
	   "hsa-miR-7")
genes <- c("A_23_P171258", "A_23_P150053", "A_23_P150053", "A_23_P150053",
	   "A_23_P202435", "A_24_P90097", "A_23_P127948")
@
We have created a list of miRNA and a list of genes, we use the table of \Robject{targetscan} we created in the previous example, to look for the information we need:
<<label=evalDBs, echo=FALSE, eval=TRUE>>=
targetscan <-dbReadTable(RmiR.Hs.miRNA_dbconn(), "targetscan")[, 1:2]
@
<<simpleList>>=
mirs <-  targetscan[targetscan$mature_miRNA%in%mirna, ]
nrow(mirs)
mirs[1:10,]
library(hgug4112a.db)
targs <- targetscan[targetscan$gene_id%in%mget(genes, hgug4112aENTREZID), ]
nrow(targs)
targs[1:10, ]
@

\end{document}
