%\VignetteIndexEntry{Using samtools C libraries}
%\VignetteDepends{}
%\VignetteKeywords{Short read, I/0, samtools}
%\VignettePackage{Rsamtools}
\documentclass[]{article}

\usepackage{times}
\usepackage{hyperref}

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Robject}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}
\newcommand{\Rmethod}[1]{{\texttt{#1}}}
\newcommand{\Rfunarg}[1]{{\texttt{#1}}}
\newcommand{\Rclass}[1]{{\textit{#1}}}
\newcommand{\Rcode}[1]{{\texttt{#1}}}

\newcommand{\software}[1]{\textsf{#1}}
\newcommand{\R}{\software{R}}
\newcommand{\Rsamtools}{\Rpackage{Rsamtools}}
\newcommand{\bam}{\texttt{BAM}}
\newcommand{\Bioconductor}{\software{Bioconductor}}

\title{Using samtools C libraries with \Rsamtools}
\author{Martin Morgan}
\date{Modified: 21 March, 2011. Compiled: \today}

\begin{document}

\maketitle

This document is meant for package developers wanting to usethe
\href{http://samtools.sourceforge.net/}{samtools} C libraries provided
by \Rsamtools{}. The instructions here are based on the `Writing R
Extensions' manual associated with R-2.13; consult the current version
of the manual for up-to-date instructions.

\section{Background}

\Rsamtools{} arranges to install static versions of the \verb|libbam|
and \verb|libbcf| libraries. The idea is that other packages can use
these to implement C functionality that uses these libraries. This
means that the samtools libraries are available in a consistent
version to users across platforms, without requiring installation of
additional software.

\Rsamtools{} takes the following approach. On installation,
\Rsamtools{} contains a snapshot of the samtools library source code
under \verb|src/samtools|. \Rsamtools{} makes static version(s) of the
samtools libraries \verb|libbam.a| and \verb|libbcf.a|. These static
libraries are specific to the operating system on which \Rsamtools{}
is being installed, are found under \verb|usrlib${R_ARCH}| in the user
library location specified by the mechanism (e.g.,
\Rfunction{biocLite}, \Rfunction{install.packages}) used to install
\Rsamtools. At the same time, \Rsamtools{} copies headers required to
use the library to the location \verb|include/samtools|.

\section{Use}

To use these libraries, the third party package developer needs to (1)
discover the appropriate header files when their package is built, and
(2) locate the static library when their package is used.

To discover appropriate header files at package installation time, add
\begin{verbatim}
LinkingTo: Rsamtools
\end{verbatim}
to the \verb|DESCRIPTION| file, and reference the relevant include
files as, for instance,
\begin{verbatim}
#include "samtools/bam.h"
\end{verbatim}
The content of the include files can be found in the \Rsamtools{}
source (under \verb|src/samtools|) or at their installed location.

Linking to the static libraries is accomplished by including the
following code in \verb|src/Makevars|:
\begin{verbatim}
SAMVARS=$(shell \
  echo 'cat(system.file("usretc", .Platform[["r_arch"]], \
              "Rsamtools.mk", package="Rsamtools", mustWork=TRUE))' |\
      "${R_HOME}/bin/R" --vanilla --slave)
include $(SAMVARS)
\end{verbatim}
This updates the environment variables \verb|$PKG_CPPFLAGS| and
\verb|$PKG_LIBS|; if your \verb|Makefile| modies these also, be sure
to use syntax like \verb|PKG_LIBS+=-l...|.

\end{document}
