## Setup

Restore the [renv](https://rstudio.github.io/renv/) to install all of the packages as declared in the lockfile, ie. get all the software needed for this template

```{r}
renv::restore()
```

## Sanity check config/metadata

*This will check for some common issues in the input configuration/metadata files. Please address any issues flagged in the output html file (./sanity_check_inputs/sanity_check_config_metadata.html) before continuing.*

```{r}
rmarkdown::render("./sanity_check_inputs/sanity_check_config_metadata.Rmd")
```

## Sanity check raw count data

*This will check for some common issues in the input raw count data. Please address any issues flagged in the output html file (./sanity_check_inputs/sanity_check_raw_counts.html) before continuing.*

```{r}
rmarkdown::render("./sanity_check_inputs/sanity_check_raw_counts.Rmd")
```

## Summarise the read counts/mapping rates for both pipelines

*This will output a csv file with various mapping/read count metrics for all samples from the smrnaseq/excerpt pipeline outputs as well as the raw fastq files (./mapping_rates/mapping_rates.csv)*

```{bash}
bash mapping_rates/extract_mapping_info.sh
```

## Sequencing read QC

*This will output an interactive html document exploring the raw sequencing read count and number of reads used for alignment (./sequencing_read_QC/sequencing_read_QC.html)*

```{r}
rmarkdown::render("./sequencing_read_QC/sequencing_read_QC.Rmd")
```

## Prepare count data

*This will output a csv file with raw, counts per million and log counts per million for all samples and RNA species (./prepare_counts/counts.csv)*

```{r}
rmarkdown::render("./prepare_counts/prepare_counts.Rmd")
```

## RNA species composition plotting

*This will output two interactive html document exploring the RNA species composition (./rna_species_composition/rna_species_composition_treatments.html and ./rna_species_composition/rna_species_composition_samples.html)*

```{r}
rmarkdown::render("./rna_species_composition/rna_species_composition_treatments.Rmd")
rmarkdown::render("./rna_species_composition/rna_species_composition_samples.Rmd")
```

## MDS plots

*This will output an interactive html document exploring the multidimensional scaling (MDS) of the RNA's (./mds/mds.html) as well as csv files with raw mds data (./mds/mds_results/)*

```{r}
rmarkdown::render("./mds/mds.Rmd")
```

## PCA plots

Data prep for app

*This will output several csv files that are used in the RShiny PCA app (for example: ./pca/pca_results/individuals_1_2.csv, ./pca/pca_results/variables_1_2.csv and ./pca/pca_results/scree.csv)*

```{r}
base::source("./pca/data_prep_for_app.R")
```

Run the RShiny app locally or deploy to a shinyapps IO account (which will allow you to link the app in a github page), see the server and ui scripts that comprise the app at `./pca/server.R` and `./pca/ui.R` respectively

## Differential expression analysis

*This will output interactive html documents exploring the differential expression analysis of the RNA's as well as csv files with all raw differential expression results data for both differential expression methods (./diff_expression/diff_expr_results/all_diff_expr_results.tsv) and all raw differential expression results data for limma/voom and deseq2 differential expression results separately (./diff_expression/diff_expr_results/all_limma_voom_diff_expr_results.tsv, ./diff_expression/diff_expr_results/all_deseq_diff_expr_results.tsv respectively). Also output are significant RNA's that meet the user specified log fold change cutoff and have an adjusted p-value smaller than 0.01, 0.05 and 0.1 (./diff_expression/diff_expr_results/sig_diff_expr_results_1.tsv, ./diff_expression/diff_expr_results/sig_diff_expr_results_5.tsv, ./diff_expression/diff_expr_results/sig_diff_expr_results_10.tsv respectively)*

### limma/voom

```{r}
rmarkdown::render("./diff_expression/diff_expression_limma_voom.Rmd")
rmarkdown::render("./diff_expression/diff_expression_limma_voom_volcano.Rmd")
rmarkdown::render("./diff_expression/diff_expression_limma_voom_results.Rmd")
```

### deseq2

```{r}
rmarkdown::render("./diff_expression/diff_expression_deseq.Rmd")
rmarkdown::render("./diff_expression/diff_expression_deseq_ma.Rmd")
rmarkdown::render("./diff_expression/diff_expression_deseq_volcano.Rmd")
rmarkdown::render("./diff_expression/diff_expression_deseq_results.Rmd")
```

### Both differential expression methods

```{r}
rmarkdown::render("./diff_expression/diff_expression_all_results.Rmd")
```

## Heatmaps

*This will output an interactive html document with heatmaps (./heatmaps/heatmaps.html)*

```{r}
rmarkdown::render("./heatmaps/heatmaps.Rmd")
```

## Presence/absence

*This will output an interactive html document exploring the presence/absence of the RNA's between treatment groups (./presence_absence/presence_absence.html)*

```{r}
rmarkdown::render("./presence_absence/presence_absence.Rmd")
```

## Expression plots

Data prep for app

*This will output an sqlite database and several csv files that are used in the RShiny PCA app (for example: ./expression_plotting/expr_plotting_results/master-count.sqlite and ./expression_plotting/expr_plotting_results/master_diff_expr_data.csv). It will also output a version of the config file from ./config/config.yaml that is ready to be used by the app (./expression_plotting/expr_plotting_results/config.yaml)*

```{r}
base::source("./expression_plotting/data_prep_for_app.R")
```

Run the RShiny app locally or deploy to a shinyapps IO account (which will allow you to link the app in a github page), see the server and ui scripts that comprise the app at `./expression_plotting/server.R` and `./expression_plotting/ui.R` respectively

## Save session info

*This will record the R version and packages you're using for reproducibility purposes (./R_version_info.txt and ./R_session_info.txt)*

```{r, results = "hide"}
# save session info
base::writeLines(utils::capture.output(base::R.Version()), "R_version_info.txt")
base::writeLines(utils::capture.output(utils::sessionInfo()), "R_session_info.txt")
```
