---
title: "Sample modifiers in pepr: append"
author: "Michal Stolarczyk"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Sample modifiers in pepr: append}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

# Learn append sample modifier in `pepr`

This vignette will show you how and why to use the append functionality of the `pepr` package. 

 - basic information about this concept on the [specification website](http://pep.databio.org/en/2.0.0/specification/#sample_modifiersappend).

## Problem/Goal
The example below demonstrates how to use the constant attributes to **define the samples attributes in the `read_type` column** of the `sample_table.csv` file. This functionality is extremely useful when there are many samples that are characterized by identical values of certain attribute (here: value `SINGLE` in `read_type` attribute). Please consider the example below for reference:

```{r ,echo=FALSE,warning=FALSE,message=FALSE}
branch = "master"
library(knitr)
library(pepr)
sampleAnnotation = system.file(
"extdata",
paste0("example_peps-", branch),
"example_append",
"sample_table_pre.csv",
package = "pepr"
)
sampleAnnotationDF = read.table(sampleAnnotation, sep = ",", header = T)
knitr::kable(sampleAnnotationDF, format = "html") 
```

----

## Solution
As the name suggests the attributes in the specified attributes (here: `read_type`) can be defined as constant ones. The way how this process is carried out is indicated explicitly in the `project_config.yaml` file (presented below). The name of the column is determined in the `sample_modifiers.append` key-value pair. Note that definition of more than one constant attribute is possible.


```{r, echo=FALSE,message=TRUE,collapse=TRUE,comment=" "}
projectConfig = system.file(
"extdata",
paste0("example_peps-", branch),
"example_append",
"project_config.yaml",
package = "pepr"
)
.printNestedList(yaml::read_yaml(projectConfig))
```


Let's introduce a few modifications to the original `sample_table.csv` file to use the `sample_modifiers.append` section of the config. Simply skip the attributes that are set constant and let the `pepr` do the work for you.

```{r ,echo=FALSE,warning=FALSE}
sampleAnnotation = system.file(
  "extdata",
  paste0("example_peps-", branch),
  "example_append",
  "sample_table.csv",
  package = "pepr"
  )
  sampleAnnotationDF = read.table(sampleAnnotation, sep = ",", header = T)
  kable(sampleAnnotationDF, format = "html") 
```

----

## Code

Read in the project metadata by specifying the path to the `project_config.yaml`:

```{r}
projectConfig = system.file(
  "extdata",
  paste0("example_peps-", branch), 
  "example_append", 
  "project_config.yaml", 
  package = "pepr"
)
p = Project(projectConfig)
```

And inspect it:

```{r}
sampleTable(p)
```

As you can see, the resulting samples are annotated the same way as if they were read from the original annotations file with attributes in the last column manually determined.

What is more, the `p` object consists of all the information from the project config file (`project_config.yaml`). Run the following line to explore it:

```{r}
config(p)
```
