---
title: "qPCRtools introduction"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{qPCRtools introduction}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
bibliography: lib.bib
csl: chinese-gb7714-2005-numeric.csl
---

## Install

```{r eval=FALSE}
install.packages("qPCRtools")
```

## Calculate volume for reverse transcription

The first step of qPCR is usually the preparation of cDNA. We need to calculate the column of RNA for reverse transcription to cDNA. So, if we have the concentration of RNA, we can use the function `CalRTable` to do that. The function have three parameters:

-   `data`: The table of RNA concentration. The unit of concentration is ng/μl. The demo data can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/crtv.data.txt).
-   `template`: The table of reagent for reverse transcription. The demo data can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/crtv.template.txt). The column `all` that must be in this `data.frame` is the total volume for 1 μg RNA.
-   `rna_weight`: The mass of RNA. The unit is μg. The default value is 2.

```{r echo=TRUE}
library(magrittr)

df.1.path <- system.file("examples", "crtv.data.txt", package = "qPCRtools")
df.2.path <- system.file("examples", "crtv.template.txt", package = "qPCRtools")
df.1 <- read.table(df.1.path, sep = "\t", header = TRUE)
df.2 <- read.table(df.2.path, sep = "\t", header = TRUE)
result <- qPCRtools::CalRTable(data = df.1, template = df.2, rna_weight = 2)

result %>% 
  dplyr::slice(1:6) %>% 
  kableExtra::kable(format = "html") %>% 
  kableExtra::kable_styling("striped")
```

## Calculate standard curve

The function can calculate the standard curve. At the same time, it can get the amplification efficiency of primer(s). Based on the amplification efficiency, we can know which method can be used to calculate the expression level. The function has 6 parameters:

-   `cq_table`: The table of Cq. It must contain at least two columns:One `Position` and `Cq`. The demo data can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/calsc.cq.txt).
-   `concen_table`: The table of gene(s) and concentration. It must contain at least three columns: `Position`, `Gene` and `Conc`. The demo data can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/calsc.info.txt).
-   `lowest_concen`: The lowest concentration used to calculate the standard curve.
-   `highest_concen`: The highest concentration used to calculate the standard curve.
-   `dilution`: The dilution factor of cDNA template. The default value is 4.
-   `by_mean`: Calculate the standard curve by average data or the full data. The default value is `TRUE`.

```{r echo=TRUE}
library(qPCRtools)

df.1.path <- system.file("examples", "calsc.cq.txt", package = "qPCRtools")
df.2.path <- system.file("examples", "calsc.info.txt", package = "qPCRtools")
df.1 <- read.table(df.1.path, header = TRUE)
df.2 <- read.table(df.2.path, header = TRUE)
qPCRtools::CalCurve(
                    cq_table = df.1,
                    concen_table = df.2,
                    lowest_concen = 4,
                    highest_concen = 4096,
                    dilution = 4,
                    by_mean = TRUE
                  ) -> p

p[["table"]] %>% 
  dplyr::slice(1:6) %>% 
  kableExtra::kable(format = "html") %>% 
  kableExtra::kable_styling("striped")

p[["figure"]]

```

## Calculate expression using standard curve
After we calculated the standard curve, we can use the standard curve to calculate the expression level of genes. In `qPCRtools`, function `CalExpCurve` can get the expression using standard curve. There are several parameters in this function:
- `cq_table`: The table of Cq. It must contain at least two columns:One `Position` and `Cq`. The demo data can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/cal.exp.curve.cq.csv).
- `curve_table`: The table of standard curve calculated by `CalCurve`.
- `design_table`: The design information including three columns: `Position`,	`Treatment` and	`Gene`. The demo table can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/cal.exp.curve.design.txt). 
- `correction`: Expression level is corrected or not with internal reference genes. The default value is `TRUE`.
- `ref_gene`: The name of reference gene.
- `stat_method`: The method used to calculate differential expression of genes. If we want to calculate the difference between target group and reference group, one of `t.test` or `wilcox.test` can be used. `anova` is for all groups. The default value is `t.test`. 
- `ref_group`: The name of reference group. If `stat_method` is `t.test` or `wilcox.test`, the function need a `ref_group`.
- `fig_type`: The type of figure, `box` or `bar`. `box` represents `boxplot`. `bar` represents `barplot`. The default value is `box`.
- `fig_ncol`: The column of figure. The default value is `NULL`.



```{r}
df1.path <- system.file("examples", "cal.exp.curve.cq.txt", package = "qPCRtools")
df2.path <- system.file("examples", "cal.expre.curve.sdc.txt", package = "qPCRtools")
df3.path <- system.file("examples", "cal.exp.curve.design.txt", package = "qPCRtools")

cq_table <- read.table(df1.path, header = TRUE)
curve_table <- read.table(df2.path, sep = "\t", header = TRUE)
design_table <- read.table(df3.path, header = TRUE)

qPCRtools::CalExpCurve(
                      cq_table,
                      curve_table,
                      design_table,
                      correction = TRUE,
                      ref_gene = "OsUBQ",
                      stat_method = "t.test",
                      ref_group = "CK",
                      fig_type = "box",
                      fig_ncol = NULL) -> res

res[["table"]] %>% 
  dplyr::slice(1:6) %>% 
  kableExtra::kable(format = "html") %>% 
  kableExtra::kable_styling("striped")
res[["figure"]]
```

## Calculate expression using 2-ΔΔCt
$2^{-{Δ}{Δ}{C_t }} $is a widely used method to calculate qPCR data[@livak2001analysis]. Our function `CalExp2ddCt` can do it. Seven parameters are required for this function:
- `cq_table`: The demo file can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/ddct.cq.txt).
- `design_table`: The demo data can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/ddct.design.txt).
Other parameters are same as the function `CalExpCurve`.
- `ref_gene`: The name of reference gene.
- `ref_group`: The name of reference group. If `stat_method` is `t.test` or `wilcox.test`, the function need a `ref_group`.
- `stat_method`: The method used to calculate differential expression of genes. If we want to calculate the difference between target group and reference group, one of `t.test` or `wilcox.test` can be used. `anova` is for all groups. The default value is `t.test`. 
- `fig_type`: The type of figure, `box` or `bar`. `box` represents `boxplot`. `bar` represents `barplot`. The default value is `box`.
- `fig_ncol`: The column of figure. The default value is `NULL`.

```{r echo=TRUE}
df1.path <- system.file("examples", "ddct.cq.txt", package = "qPCRtools")
df2.path <- system.file("examples", "ddct.design.txt", package = "qPCRtools")

cq_table <- read.table(df1.path, header = TRUE)
design_table <- read.table(df2.path, header = TRUE)

qPCRtools::CalExp2ddCt(cq_table,
                       design_table,
                       ref_gene = "OsUBQ",
                       ref_group = "CK",
                       stat_method = "t.test",
                       fig_type = "bar",
                       fig_ncol = NULL) -> res

res[["table"]] %>% 
  dplyr::slice(1:6) %>% 
  kableExtra::kable(format = "html") %>% 
  kableExtra::kable_styling("striped")

res[["figure"]]
```

## Calculate expression using RqPCR
The method from [SATQPCR](http://satqpcr.sophia.inra.fr/cgi/home.cgi) can identify the most stable reference genes (REF) across biological replicates and technical replicates[@rancurel2019satqpcr]. Our package provides a function, `CalExpRqPCR`, to achieve it. In the `design_table`, `BioRep`,	`TechRep` and `Eff` are required. `BioRep` is the `biological replicates`. `TechRep` is the `technical replicates`. `Eff` is the amplification efficiency of genes. 

- The `cq_table` can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/cal.expre.rqpcr.cq.txt). 
- The `design,table` can be found at [GitHub](https://github.com/lixiang117423/qPCRtools/blob/main/CRAN/qPCRtools/inst/examples/cal.expre.rqpcr.design.txt). If user want to give reference gene, `ref_gene` can be used (The default is `NULL`).
- `ref_gene`: The name of reference gene.
- `ref_group`: The name of reference group. If `stat_method` is `t.test` or `wilcox.test`, the function need a `ref_group`.
- `stat_method`: The method used to calculate differential expression of genes. If we want to calculate the difference between target group and reference group, one of `t.test` or `wilcox.test` can be used. `anova` is for all groups. The default value is `t.test`. 
- `fig_type`: The type of figure, `box` or `bar`. `box` represents `boxplot`. `bar` represents `barplot`. The default value is `box`.
- `fig_ncol`: The column of figure. The default value is `NULL`.

```{r echo=TRUE}
df1.path <- system.file("examples", "cal.expre.rqpcr.cq.txt", package = "qPCRtools")
df2.path <- system.file("examples", "cal.expre.rqpcr.design.txt", package = "qPCRtools")

cq_table <- read.table(df1.path, header = TRUE)
design_table <- read.table(df2.path, header = TRUE)

qPCRtools::CalExpRqPCR(cq_table,
                       design_table,
                       ref_gene = NULL,
                       ref_group = "CK",
                       stat_method = "t.test",
                       fig_type = "bar",
                       fig_ncol = NULL
                       ) -> res

res[["table"]] %>% 
  dplyr::slice(1:6) %>% 
  kableExtra::kable(format = "html") %>% 
  kableExtra::kable_styling("striped")

res[["figure"]]
```

## References
