Skip to content

Commit

Permalink
Adding Radar analysis and rix function
Browse files Browse the repository at this point in the history
  • Loading branch information
InkaSpy committed Dec 19, 2024
1 parent 27362d0 commit faa11db
Show file tree
Hide file tree
Showing 12 changed files with 3,587 additions and 976 deletions.
1,640 changes: 680 additions & 960 deletions README.md

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions vignettes/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
### File generated by `rix::rix_init()` ###
# 1. Currently, system RStudio does not inherit environmental variables
# defined in `$HOME/.zshrc`, `$HOME/.bashrc` and alike. This is workaround to
# make the path of the nix store and hence basic nix commands available
# in an RStudio session
# 2. For nix-R session, remove `R_LIBS_USER`, system's R user library.`.
# This guarantees no user libraries from the system are loaded and only
# R packages in the Nix store are used. This makes Nix-R behave in pure manner
# at run-time.
{
is_rstudio <- Sys.getenv("RSTUDIO") == "1"
is_nix_r <- nzchar(Sys.getenv("NIX_STORE"))
if (isFALSE(is_nix_r) && isTRUE(is_rstudio)) {
cat("{rix} detected RStudio R session")
old_path <- Sys.getenv("PATH")
nix_path <- "/nix/var/nix/profiles/default/bin"
has_nix_path <- any(grepl(nix_path, old_path))
if (isFALSE(has_nix_path)) {
Sys.setenv(PATH = paste(old_path, nix_path, sep = ":"))
}
rm(old_path, nix_path)
}
if (isTRUE(is_nix_r)) {
install.packages <- function(...) {
stop("You are currently in an R session running from Nix.\n", "Don't install packages using install.packages(),\nadd them to ", "the default.nix file instead.")
}
update.packages <- function(...) {
stop("You are currently in an R session running from Nix.\n", "Don't update packages using update.packages(),\n", "generate a new default.nix with a more recent version of R. ", "If you need bleeding edge packages, read the", "'Understanding the rPackages set release cycle and using ", "bleeding edge packages' vignette.")
}
remove.packages <- function(...) {
stop("You are currently in an R session running from Nix.\n", "Don't remove packages using `remove.packages()``,\ndelete them ", "from the default.nix file instead.")
}
current_paths <- .libPaths()
userlib_paths <- Sys.getenv("R_LIBS_USER")
user_dir <- grep(paste(userlib_paths, collapse = "|"), current_paths, fixed = TRUE)
new_paths <- current_paths[-user_dir]
.libPaths(new_paths)
rm(current_paths, userlib_paths, user_dir, new_paths)
}
rm(is_rstudio, is_nix_r)
}
12 changes: 6 additions & 6 deletions vignettes/Power_analysis_REACTIV.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

## Reproducible environment

To reproduce the power analysis in the publication the following code has to be executed.

In case you want to make sure that you are creating the exact same output as presented here, you need to create the reproducible environment using `rix` and `nix`. Using `rix`, you can run the following function to create your nix shell.
In case you want to make sure that you are creating the exact same output as presented in the publication, you need to create the reproducible environment using `rix` and `nix`. Using `rix`, you can run the following function to create your nix shell.

```{r eval = FALSE}
rix::rix(
r_ver = "latest",
r_pkgs = c("tidyverse", "tidyr", "purrr", "furrr", "gt","easystats","rlang","drc", "DHARMa", "devtools", "pathwork", "rmarkdown"),
r_pkgs = c("tidyverse", "tidyr", "purrr", "furrr", "gt","easystats","rlang", "DHARMa", "devtools", "patchwork", "rmarkdown"),
system_pkgs = NULL,
tex_pkgs = c("amsmath"),
git_pkgs = list(package_name = "xeredar",
rep_url = "https://github.com/basf/xeredar",
repo_url = "https://github.com/basf/xeredar",
commit = "2b85763"),
ide = "rstudio",
project_path = "./vignettes",
overwrite = TRUE
)
```

Once the default.nix file was created in the path you specified in the command above (`project_path`) you need to type `nix-build` into the terminal. Once the shell was built you can open it via `nix-shell`. You can open Rstudio by first running this line in the terminal: `export QT_XCB_GL_INTEGRATION=none` and then running `rstudio`. Rstudio will open and you will have access to all files located at the path you specified in the `rix` function. You can run the script as usual. Through this approach, the code and results are reproducible.

The following sections contain the code that needs to be executed to reproduce the analysis.

## Variance component analysis
### Functions
Expand Down
199 changes: 199 additions & 0 deletions vignettes/Running_RADAR_analysis.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
---
title: "Analysis of all RADAR validation studies"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{User guide}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---


## Reproducible environment

In case you want to make sure that you are creating the exact same output as presented in the publication, you need to create the reproducible environment using `rix` and `nix`. Using `rix`, you can run the following function to create your nix shell.

```{r eval = FALSE}
rix::rix(
r_ver = "latest",
r_pkgs = c("tidyverse", "tidyr", "purrr", "furrr", "gt","easystats","rlang", "DHARMa", "devtools", "patchwork", "rmarkdown"),
system_pkgs = NULL,
tex_pkgs = c("amsmath"),
git_pkgs = list(package_name = "xeredar",
repo_url = "https://github.com/basf/xeredar",
commit = "2b85763"),
ide = "rstudio",
project_path = "./vignettes",
overwrite = TRUE
)
```

Once the default.nix file was created in the path you specified in the command above (`project_path`) you need to type `nix-build` into the terminal. Once the shell was built you can open it via `nix-shell`. You can open Rstudio by first running this line in the terminal: `export QT_XCB_GL_INTEGRATION=none` and then running `rstudio`. Rstudio will open and you will have access to all files located at the path you specified in the `rix` function. You can run the script as usual. Through this approach, the code and results are reproducible.

The following sections contain the code that needs to be executed to reproduce the analysis.


```{r}
# for(i in unique(RADAR_valid_data_table$Compound)){
# for(j in unique(RADAR_valid_data_table$lab)){
# dat <- RADAR_valid_data_table |>
# dplyr::filter(Compound == i & lab == j)
# if(!nrow(dat) == 0){
# RADAR_valid_data[[paste0(i,j)]] <- dat
# } else{
# next
# }
# }
# }
decreasing_alpha <- function(df, vector){
for(i in vector){
result <- xeredar::data_prep(df, row=FALSE, alpha = i, trim=FALSE, outlier = FALSE)
if(is.list(result)){
break
} else{
next
}
}
result[["alpha"]] <- i
return(result)
}
# RADAR_valid_data_table_spiked_unspiked <- purrr::map(RADAR_valid_data, spiked_unspiked)
changing_alpha <- sort(c(c(1,5) %o% 10^-(2:17), 0), decreasing = TRUE)[-1]
check <- function(list_dfs, id){
result <- tryCatch({
decreasing_alpha(list_dfs, changing_alpha)
}, error = function(e) {
list(error = paste("Error xeredar provides an error"))
})
setNames(list(result), id)
}
anovas <- purrr::imap(xeredar::RADAR_valid_data_table_spiked_unspiked, ~ check(.x, .y)) |> purrr::flatten()
check_positive <- function(df){
if(length(df) ==1){
return(NA)}
if(df[["Monotonicity Test"]]$`Pr(>|t|)`[1] >= 0.01 &
df[["Monotonicity Test"]]$`Pr(>|t|)`[2] <= 0.01 ) {
return(df[["Dunnetts"]])
} else if (length(unique(df$WilliamsIncrease$Y.Tilde)) - 1 > floor(nrow(df$WilliamsIncrease)/2) |
length(unique(df$WilliamsDecrease$Y.Tilde)) - 1 > floor(nrow(df$WilliamsDecrease)/2)) {
return(list(WilliamsIncrease = df[["WilliamsIncrease"]], WilliamsDecrease = df[["WilliamsDecrease"]]))
} else{
return(df[["Dunnetts"]])
}
}
resultList <-
anovas_unspiked <- anovas[stringr::str_detect(names(anovas), "Unspiked")]
anovas_spiked<- anovas[!stringr::str_detect(names(anovas), "Unspiked")]
results_Spiked <- purrr::map(anovas_spiked, check_positive)
results_Unspiked <- purrr::map(anovas_unspiked, check_positive)
Williams_Spiked <- !purrr::map_vec(results_Spiked, is.data.frame)
Williams_Unspiked <- !purrr::map_vec(results_Unspiked, is.data.frame)
significances <- function(df, Williams= FALSE){
if(!is.list(df)){
return(NA)}
if(Williams) {
if(TRUE %in% df[["WilliamsIncrease"]]$Sign){
return("sign_Incr")
}
if(TRUE %in% df[["WilliamsDecrease"]]$Sign){
return("sign_Decr")
}
else{
return("Nonsignificant")
}
}
else{
if(df[["adj p"]][length(df[["adj p"]])] < 0.05){
if(df[["Estimate"]][length(df[["Estimate"]])] > 0){
return("sign_Incr")
} else{
return("sign_Decr")
}
} else{
return("Nonsignificant")
}
}
}
significances_Spiked <- purrr::map2(results_Spiked, Williams_Spiked, significances) |>
dplyr::bind_rows(.id = "CompoundLab") |>
tidyr::pivot_longer(
cols = 1:44,
names_to = "CompoundLab",
values_to = "Spiked_sign"
) |>
dplyr::mutate(CompoundLab = stringr::str_remove(CompoundLab,"_Spiked"))
significances_Unspiked <- purrr::map2(results_Unspiked, Williams_Unspiked, significances) |>
dplyr::bind_rows(.id = "CompoundLab") |>
tidyr::pivot_longer(
cols = 1:44,
names_to = "CompoundLab",
values_to = "Unspiked_sign"
) |>
dplyr::mutate(CompoundLab = stringr::str_remove(CompoundLab,"_Unspiked"))
desired_order <- c(
"AmantadineWATCHFR", "AmantadineCEFAS", "AmantadineFIWI", "AmantadineFRAUNH", "AmantidineIDEA",
"LDopaWATCHFR", "ArabinoseWATCHFR", "CefuroximeWATCHFR", "CefuroximeCEFAS", "CefuroximeFIWI",
"CefuroximeFRAUNH", "CefuroximeIDEA", "CromolynWATCHFR", "CromolynCEFAS", "CromolynFIWI",
"CromolynIDEA", "AnastrazoleWATCHFR", "AnastrozoleCEFAS", "AnastrozoleFIWI", "AnastrozoleIDEA",
"ProchlorazWATCHFR", "DutasterideWATCHFR", "CyproteroneWATCHFR", "FenitrothionWATCHFR",
"FenitrothionCEFAS", "FenitrothionFIWI", "FenitrothionIDEA", "EstradiolWATCHFR", "mDHTWATCHFR",
"mDHTCEFAS", "mDHTFIWI", "mDHTFRAUNH", "mDHTIDEA", "TrenboloneWATCHFR", "LinuronWATCHFR",
"LinuronCEFAS", "LinuronFIWI", "LinuronFRAUNH", "LinuronIDEA", "VinclozolinWATCHFR",
"VinclozolinCEFAS", "VinclozolinFIWI", "VinclozolinFRAUNH", "VinclozolinIDEA"
)
Result_Radar_valid <- dplyr::full_join(significances_Spiked, significances_Unspiked) |>
dplyr::mutate(`Known Mode of Action` =
dplyr::case_when(
stringr::str_detect(CompoundLab, "Amantadine|Amantidine|LDopa|Arabinose|Cefuroxime|Cromolyn")~ "no EAS MoA",
stringr::str_detect(CompoundLab, "Anastrozole|Anastrazole") ~ "steroidogenesis (anti-E): aromatase inhibitor (competitive)",
stringr::str_detect(CompoundLab, "Prochloraz") ~ "steroidogenesis (anti-E): aromatase inhibitor (transcriptional)",
stringr::str_detect(CompoundLab, "Dutasteride") ~ "steroidogenesis: 5alpha reductase inhibitor; AR antagonist (high conc.)",
stringr::str_detect(CompoundLab, "Cyproterone|Fenitrothion") ~ "anti-A: AR antagonist",
stringr::str_detect(CompoundLab, "Estradiol") ~ "E: ER agonist; steroidogenesis (anti-A): aromatase inducer",
stringr::str_detect(CompoundLab, "mDHT|Trenbolone") ~ "A: non-aromatizable AR agonist",
stringr::str_detect(CompoundLab, "Linuron") ~ "anti-A: AR antagonist; anti-E",
stringr::str_detect(CompoundLab, "Vinclozolin") ~ "anti-A: AR antagonist; E: weak ER agonist"
),
CompoundLab = factor(CompoundLab, levels = desired_order)) |>
dplyr::arrange(CompoundLab) |>
dplyr::mutate(`Final Conclusion` = c('A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive','active', 'A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive','A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive', 'A(S)-inactive', 'S-active', 'S-active','S-active', 'inactive', 'inactive', 'A-active', 'A-active', 'A-active', 'A-active', 'A-active', 'A-ative','S-active', 'A-active', 'A-active', 'A-active', 'A-active', 'A-active', 'A-active', 'A-active', 'A-active''inactive', 'inactive', 'A-active', 'A-active', 'A-active', 'A-active', 'inactive', 'A-active')) |>
dplyr::relocate(c("CompoundLab", "Known Mode of Action", "Spiked_sign", "Unspiked_sign", "Final conclusion"))
gt::gt(Result_Radar_valid)
45 changes: 36 additions & 9 deletions vignettes/Software_validation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@ vignette: >
%\VignetteEncoding{UTF-8}
---


## Reproducible environment

xeredar comes with a reproducible environment in form of a nix file (default.nix) [info about nix](https://nix.dev/) that was created with the R-package [rix](https://ropensci.github.io/rix/index.html).
In case you want to make sure that you are creating the exact same output as presented in the publication, you need to create the reproducible environment using [`rix`](https://ropensci.github.io/rix/index.html) and [`nix`](https://nixos.org/). Using `rix`, you can run the following function to create your nix shell.

```{r eval = FALSE}
rix::rix(
r_ver = "latest",
r_pkgs = c("tidyverse", "tidyr", "purrr", "furrr", "gt","easystats","rlang", "DHARMa", "devtools", "patchwork", "rmarkdown"),
system_pkgs = NULL,
tex_pkgs = c("amsmath"),
git_pkgs = list(package_name = "xeredar",
repo_url = "https://github.com/basf/xeredar",
commit = "2b85763"),
ide = "rstudio",
project_path = "./vignettes",
overwrite = TRUE
)
```

Once the default.nix file was created in the path you specified in the command above (`project_path`) you need to type `nix-build` into the terminal. Once the shell was built you can open it via `nix-shell`. You can open Rstudio by first running this line in the terminal: `export QT_XCB_GL_INTEGRATION=none` and then running `rstudio`. Rstudio will open and you will have access to all files located at the path you specified in the `rix` function. You can run the script as usual. Through this approach, the code and results are reproducible.

The following sections contain the code that needs to be executed to reproduce the analysis.

When working in the nix-shell that is built from the default.nix file with the command `nix-build`, the exact same results are received as presented in this vignette.

## Code for validation

Expand All @@ -38,9 +58,9 @@ changing_alpha <- c(c(1,5) %o% 10^-(3:17), 0)
decreasing_alpha <- function(df, vector){
for(i in vector){
df <- df |> filter(!is.na(Fluor))
df <- df |> dplyr::filter(!is.na(Fluor))
dat_trim <- xeredar::trim(df)$dat_trim
result <- data_prep(df, row=FALSE, alpha = i, trim=FALSE, outlier = FALSE)
result <- xeredar::data_prep(df, row=FALSE, alpha = i, trim=FALSE, outlier = FALSE)
if(is.list(result)){
break
} else{
Expand All @@ -53,7 +73,7 @@ decreasing_alpha <- function(df, vector){
result_difficult_dfs <- purrr::map(.x=difficult_dfs, .f= decreasing_alpha, vector = changing_alpha)
results_alpha_decrease <- results[map_vec(results, is.list)]
results_alpha_decrease <- results[purrr::map_vec(results, is.list)]
results <- append(results_alpha_decrease, result_difficult_dfs)
check_positive <- function(df){
Expand Down Expand Up @@ -86,10 +106,17 @@ significances <- function(df, Williams= FALSE){
prelim_thyroid_active <- purrr::map2_vec(results_2, Williams, significances)
country_substance_spiked <- names(results_2) |> stringr::str_split("\\_") |> as.data.frame() |> t() |> dplyr::as_tibble() |> dplyr::rename("substance" = "V1", "country" = "V2", "spiked" = "V3") |> dplyr::mutate(Sign = prelim_thyroid_active)
country_substance_spiked <- names(results_2) |>
stringr::str_split("\\_") |>
as.data.frame() |> t() |>
dplyr::as_tibble() |>
dplyr::rename("substance" = "V1", "country" = "V2", "spiked" = "V3") |>
dplyr::mutate(Sign = prelim_thyroid_active)
increase_h12 <- results |> purrr::map_vec(function(x) {(x[["SummaryDF"]]$Mean[nrow(x[["SummaryDF"]])] / x[["SummaryDF"]]$Mean[1])-1 })
increase_h12 <- results |> purrr::map_vec(function(x) {
(x[["SummaryDF"]]$Mean[nrow(x[["SummaryDF"]])] / x[["SummaryDF"]]$Mean[1])-1
})
thyroid_active <- country_substance_spiked |> dplyr::mutate(Increase = increase_h12,
Expand All @@ -100,9 +127,9 @@ thyroid_active <- country_substance_spiked |> dplyr::mutate(Increase = increase_
dplyr::mutate(
country = ifelse(country== "japan" & substance %in% c("e2", "nh3") |
country == "japan2", "japan lab2", country)) |>
pivot_wider(names_from = country, values_from = thyroid_active) |>
tidyr::pivot_wider(names_from = country, values_from = thyroid_active) |>
dplyr::arrange(match(substance, c("t3", "ptu", "t4", "triac", "cefuroxime","linuron", "nh3", "testosterone","e2", "abamectine", "acetone", "isophorone", "metholmyl"))) |>
mutate(`Expected classification`= c("Thyroid active", "Thyroid active", "Thyroid active", "Thyroid active", "Thyroid inactive", "Thyroid active", "Thyroid active", "Unclear", "Thyroid inactive", "Thyroid inactive","Thyroid inactive","Thyroid inactive","Thyroid inactive"))|>
dplyr::mutate(`Expected classification`= c("Thyroid active", "Thyroid active", "Thyroid active", "Thyroid active", "Thyroid inactive", "Thyroid active", "Thyroid active", "Unclear", "Thyroid inactive", "Thyroid inactive","Thyroid inactive","Thyroid inactive","Thyroid inactive"))|>
dplyr::relocate(c(substance,`Expected classification`, france,japan, usa, `japan lab2`, belgium, portugal )) |>
dplyr::rename(USA = usa)
```
Expand Down
5 changes: 4 additions & 1 deletion vignettes/User_guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ knitr::opts_chunk$set(
```

```{r include=FALSE}
library(xeredar)
devtools::load_all(
)
library(knitr)
library(purrr)
library(dplyr)
Expand Down
Loading

0 comments on commit faa11db

Please sign in to comment.