---
title: "A tutorial to run diffdriver"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{A tutorial to run diffdriver}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE, 
  message = FALSE
)

set.seed(0)
```

# Environmental requirement

Need 16G memory to run diffdriver. The place that needs the most memory is the parameter estimation for background mutation rate model.

# Prepare input

Diffdriver requires three datasets from the user: phenotype or context of each individual tumor sample, somatic mutations identified from tumor samples and a list of driver gene names. Diffdriver will test the association between the phenotype/context with selection strength of each provided driver gene.

  * Phenotype/Context

Diffdriver needs phenotype or context of each individual tumor sample. A data frame should provided, in this data frame the first column is sample ID with column name "SampleID", the second column is phenotype or context with the phenotype or context name as column name (note no space or in tab in column names are allowed).  See an example below: 
```{r}
phenof = system.file("extdata/", "example_phenotypes.txt", package = "diffdriver")
pheno <- read.table(phenof, header = T)
head(pheno)
```

  * Driver genes
  
A vector of driver gene names. Diffdriver will test the association for each gene.
```{r}
genef = system.file("extdata", "example_gene.txt", package = "diffdriver")
gene <- read.table(genef, header = F)
head(gene)
```

  * Somatic mutations
  
Diffdriver needs the somatic mutations identified for each tumor samples. Note this should include all somatic mutations identified, not just the ones in selected driver genes. Somatic mutations will be used to estimate background mutation rate and selection strength for selected genes. A data frame should be provided, see below for colnames of this data frame and example mutations. 

```{r}
mutf = system.file("extdata/", "example_mutations.txt", package = "diffdriver")
mut <- read.table(mutf, header = T)
head(mut)
```

In addition to these datasets provided by the user, diffdriver also needs annotation files. See the package installation page for download links to these annotation files. Unless the number of tumor samples or number of mutations is very small, we suggest to use the 96-annotation files. Please download these files to a folder and provide the folder address to diffdriver. 

  * Directory that contains annotation files

```{r, eval=FALSE}
annodir = "/Volumes/Szhao/library/diffdriver_anno/annodir96"
list.files(annodir)
```


# Run diffdriver

To ensure that the numerical results are reproducible, this tutorial uses a fixed random seed.

```{r}
# library(diffdriver)
set.seed(0)
output_dir <- "/tmp/diffdriver_output"
```

With signature adjustment (`BMRmode = "signature"`), which is the default mode, it will need around 15min to estimate parameters in background model. Please use the provided the annotation folder with 96-annotation files when running with the "signature mode".
```{r, eval=FALSE}
library(diffdriver)
res <- diffdriver(gene = gene, mut= mut, pheno = pheno, anno_dir = "/Volumes/Szhao/library/diffdriver_anno/annodir96", k=6, totalnttype = 96, BMRmode = "signature", output_dir = output_dir, output_prefix = "testdiffdriver_sig")
res
```

Without signature adjustment (`BMRmode = "regular"`), it will need around 15min to estimate parameters in background model. You can use the provided the annotation folder with 9-annotation files or 96-annotation files. The example below uses 96-annotation files. When total number of mutations is low, one should use 9-annotation files.
```{r, eval=FALSE}
res <- diffdriver(gene = gene, mut= mut, pheno = pheno,  anno_dir = "/Volumes/Szhao/library/diffdriver_anno/annodir96", totalnttype = 96, BMRmode = "regular", output_dir = output_dir, output_prefix = "testdiffdriver_reg")
res
```

# Plot specific genes

To visualize the data for specific genes, diffdriver has a plotting function: 
```{r plot, eval=FALSE}
diffdriver::plot_mut(gene_name = "PIK3CA", mut= mut, pheno = pheno, totalnttype = 9, anno_dir = "/Volumes/Szhao/library/diffdriver_anno/annodir9")
```

```{r}
sessionInfo()
```
