---
title: "Simple Workflow by systemPipeR" 
author: "Author: Daniela Cassol, Le Zhang, and Thomas Girke"
date: "Last update: `r format(Sys.time(), '%d %B, %Y')`" 
output:
  BiocStyle::html_document:
    toc_float: true
    code_folding: show
package: systemPipeR
vignette: |
  %\VignetteEncoding{UTF-8}
  %\VignetteIndexEntry{Workflow example}
  %\VignetteEngine{knitr::rmarkdown}
fontsize: 14pt
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Build the Workflow 

## Export dataset to file

- Add first step as `LineWise`

```{r export_iris, eval=TRUE, spr='r'}
mapply(
  function(x, y) write.csv(x, y),
  split(iris, factor(iris$Species)),
  file.path("results", paste0(names(split(iris, factor(iris$Species))), ".csv"))
)
```

## Compress data

- Adding the second step, as `SYSargs2` 

```{r gzip, eval=TRUE, spr='sysargs', spr.dep='export_iris'}
appendStep(sal) <- SYSargsList(step_name = "gzip", 
                      targets="targets.txt", dir=TRUE,
                      wf_file="gunzip/workflow_gzip.cwl", input_file="gunzip/gzip.yml",
                      dir_path = "param/cwl",
                      inputvars = c(FileName = "_FILE_PATH_", SampleName = "_SampleName_"), 
                      dependency = "export_iris")
```

## Decompress data

```{r gunzip, eval=TRUE, spr='sysargs', spr.dep='gzip'}
appendStep(sal) <- SYSargsList(step_name = "gunzip", 
                      targets = "gzip", dir = TRUE,
                      wf_file = "gunzip/workflow_gunzip.cwl", input_file = "gunzip/gunzip.yml",
                      dir_path = "param/cwl",
                      inputvars = c(gzip_file = "_FILE_PATH_", SampleName = "_SampleName_"), 
                       rm_targets_col = "FileName", 
                      dependency = "gzip")
```

## Import data to R and perform statistical analysis and visualization 

```{r stats, eval=TRUE, spr='r', spr.dep='gunzip'}
df <- lapply(getColumn(sal, step="gunzip", 'outfiles'), function(x) read.delim(x, sep=",")[-1])
df <- do.call(rbind, df)

stats <- data.frame(cbind(mean=apply(df[,1:4], 2, mean),
               sd=apply(df[,1:4], 2, sd)))
stats$species <- rownames(stats)

plot <- ggplot2::ggplot(stats, ggplot2::aes(x=species, y=mean, fill=species)) + 
  ggplot2::geom_bar(stat = "identity", color="black", position=ggplot2::position_dodge()) +
  ggplot2::geom_errorbar(ggplot2::aes(ymin=mean-sd, ymax=mean+sd), width=.2, position=ggplot2::position_dodge(.9)) 
```


