---
title: "Reproducibility in Microbiome Data Analysis"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Reproducibility in Microbiome Data Analysis}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

<style>
body {
text-align: justify}
</style>

Reproducibility is a crucial aspect of data analysis, particularly in the 
context of microbiome data. The ability to consistently replicate an analysis 
and obtain the same results is essential for ensuring the reliability of 
findings and facilitating scientific collaboration.

The `dar` package includes two key functions, `export_steps` and 
`import_steps`, which promote reproducibility in microbiome data analysis. 
These functions allow you to export the steps of a recipe to a JSON file and 
then import those steps to reproduce the analysis in a different environment.

## Exporting Steps of a Recipe

The `export_steps` function facilitates the export of a recipe's steps to a 
JSON file. This is useful for documenting and sharing the parameters used in 
the analysis.

Here's an example of how to use the `export_steps` function:

```{r}
library(dar)
data(metaHIV_phy)

# Create a recipe with steps
rec <- 
  recipe(metaHIV_phy, "RiskGroup2", "Species") |>
  step_subset_taxa(tax_level = "Kingdom", taxa = c("Bacteria", "Archaea")) |>
  step_filter_taxa(.f = "function(x) sum(x > 0) >= (0.3 * length(x))") |>
  step_maaslin()

# Export the steps to a JSON file
out_file <- tempfile(fileext = ".json")
export_steps(rec, out_file)
```

In this example, a recipe with multiple steps is created, and then the steps 
are exported to a JSON file using the `export_steps` function.

## Importing Steps from a JSON File

The `import_steps` function allows you to import steps from a JSON file and 
add them to an existing recipe. This is useful when you want to reuse a 
previously saved set of steps or incorporate steps from another recipe into 
your current analysis.

Here's an example of how to use the `import_steps` function:

```{r}
# Initialize a recipe with a phyloseq object
rec <- recipe(metaHIV_phy, "RiskGroup2", "Species")

# Import the steps from a JSON file
json_file <- out_file
rec <- import_steps(rec, json_file)
rec
```

In this example, an empty recipe is initialized, and then the steps are 
imported from a JSON file using the `import_steps` function. The imported steps 
are added to the existing recipe.

Once the recipe is imported, we can choose to add more steps or execute the 
code using the `prep` function. In this case, we choose to execute `prep` 
directly.

```{r}
## Execute
da_results <- prep(rec, parallel = FALSE) |> bake()
da_results
```

## Limitations and Considerations

It's important to note some limitations and considerations when using the 
`export_steps` and `import_steps` functions:

- The JSON files generated by `export_steps` only contain the parameters of the 
recipe steps and bakes, not the original data used in the analysis. Therefore, 
when importing the steps from a JSON file, ensure that you have access to the 
same data that was originally used.

- The `export_steps` and `import_steps` functions are specific to the `dar` 
package and are designed for use in microbiome data analysis. They are not 
applicable to other types of analyses or packages.

- When importing steps from a JSON file, it's important to check if the file 
contains "bake" steps. If so, the recipe will be automatically prepared after 
importing the steps. This may have implications for runtime and resource 
requirements of the analysis.

- Make sure you have the correct versions of the dependencies of the `dar` 
package when exporting and importing recipe steps. Updates in dependencies can 
affect the compatibility and reproducibility of the analyses.

## Conclusion

Reproducibility is essential in microbiome data analysis, and the `dar` package 
facilitates this aspect by providing the `export_steps` and `import_steps` 
functions. These functions allow you to export the steps of a recipe to a JSON 
file and then import them to reproduce the analysis in a different environment. 
With these tools, you can effectively document and share your analyses, 
increasing transparency and the reliability of your results.

## Session info

```{r}
devtools::session_info()
```