---
title: "Getting started"
output: 
  rmarkdown::html_vignette:
    toc: true
    df_print: kable
    fig_width: 7
    fig_height: 6
      
vignette: >
  %\VignetteIndexEntry{Getting started}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,comment=NA,R.options=list(width = 95))
```

Here we will show a very basic example how to use grandR to perform kinetic modeling. For much more vignettes, see the [grandR website](https://grandr.erhard-lab.de).

We will use data from [[1]](https://www.nature.com/articles/s41586-021-03610-3). These are SLAM-seq data from multiple time points (1h,2h,3h,4h) after infecting Calu-3 cells with SARS-CoV-2 (or mock as control). 

We first load the grandR package and the read the GRAND-SLAM that is part of the grandR package:

```{r warning=FALSE}
suppressPackageStartupMessages({
    library(grandR)
    library(ggplot2)
    library(patchwork)
})

sars <- ReadGRAND(system.file("extdata", "sars.tsv.gz", package = "grandR"),
                 design=c(Design$Condition,Design$dur.4sU,Design$Replicate))
sars <- Normalize(sars)
sars
```

The GRAND-SLAM output normally contains any gene with at least 1 read, i.e. >30k genes. The data set that is part of grandR has been prefiltered and only consists of 1045 genes. For a complete workflow including filter see the [full vignette](https://grandr.erhard-lab.de/articles/web/kinetic-modeling.html). Note that we also normalized the read counts (by using size factors), which added an additional data "slot". 

We start by creating a plot showing the kinetics for a gene:

```{r fig.alt = "Progressive labeling plot for SRSF6"}
PlotGeneProgressiveTimecourse(sars,"SRSF6",steady.state=list(Mock=TRUE,SARS=FALSE))
```

Note that this automatically fit the kinetic model for this gene, separately for the two conditions. Modeling used the default data slot, which are the size-factor normalized values, as indicated above. By using the `steady.state` parameter, we defined the mock infected control samples to be in steady state, whereas the virus infected samples should not be assumed to be in steady state.

We now fit the kinetic model for all genes:
```{r}
SetParallel(cores = 2)  # increase on your system, or omit the cores = 2 for automatic detection
sars<-FitKinetics(sars,"kinetics",steady.state=list(Mock=TRUE,SARS=FALSE))
```

Modeling results are stored in two "analysis tables":

```{r fig.height=3, fig.width=6, warning=FALSE}
Analyses(sars)
```

We can retrieve this table (for more information, see the [data handling vignette](https://grandr.erhard-lab.de/articles/web/data-matrices-and-analysis-results.html):

```{r}
head(GetAnalysisTable(sars))
```

We can also easily plot the RNA half-lives of mock infected cells against virus infected cells:

```{r fig.height=4, fig.width=4, fig.alt = "Half-lives scatter plot mock vs SARS"}
PlotScatter(sars,`kinetics.Mock.Half-life`,`kinetics.SARS.Half-life`,
            lim=c(0,24),correlation = FormatCorrelation())+
  geom_abline()
```
