Mercurial > repos > ufz > dose_response_analysis_tool
annotate dose_response.R @ 0:082e9d22c38d draft
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
author | ufz |
---|---|
date | Mon, 10 Jun 2024 11:57:52 +0000 |
parents | |
children | 8a1b524ed9d8 |
rev | line source |
---|---|
0
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
1 library(drc) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
2 library(ggplot2) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
3 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
4 fit_models <- function(data, concentration_col, response_col) { |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
5 models <- list( |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
6 LL.2 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.2(), type = "binomial"), |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
7 LL.3 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.3(), type = "binomial"), |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
8 LL.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.4(), type = "binomial"), |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
9 LL.5 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.5(), type = "binomial"), |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
10 W1.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W1.4(), type = "binomial"), |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
11 W2.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W2.4(), type = "binomial") |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
12 ) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
13 return(models) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
14 } |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
15 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
16 select_best_model <- function(models) { |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
17 aic_values <- sapply(models, AIC) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
18 best_model_name <- names(which.min(aic_values)) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
19 best_model <- models[[best_model_name]] |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
20 return(list(name = best_model_name, model = best_model)) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
21 } |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
22 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
23 calculate_ec_values <- function(model) { |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
24 ec50 <- ED(model, 50, type = "relative") |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
25 ec25 <- ED(model, 25, type = "relative") |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
26 ec10 <- ED(model, 10, type = "relative") |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
27 return(list(EC50 = ec50, EC25 = ec25, EC10 = ec10)) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
28 } |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
29 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
30 plot_dose_response <- function(model, data, ec_values, concentration_col, response_col, plot_file) { |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
31 concentration_grid <- seq(min(data[[concentration_col]]), max(data[[concentration_col]]), length.out = 100) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
32 prediction_data <- data.frame(concentration_grid) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
33 colnames(prediction_data) <- concentration_col |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
34 predicted_values <- predict(model, newdata = prediction_data, type = "response") |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
35 prediction_data$response <- predicted_values |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
36 p <- ggplot(data, aes_string(x = concentration_col, y = response_col)) + |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
37 geom_point(color = "red") + |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
38 geom_line(data = prediction_data, aes_string(x = concentration_col, y = "response"), color = "blue") + |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
39 geom_vline(xintercept = ec_values$EC10[1], color = "green", linetype = "dashed") + |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
40 geom_vline(xintercept = ec_values$EC50[1], color = "purple", linetype = "dashed") + |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
41 labs(title = "Dose-Response Curve", x = "Concentration", y = "Effect") + |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
42 theme_minimal() + |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
43 theme( |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
44 panel.background = element_rect(fill = "white", color = NA), |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
45 plot.background = element_rect(fill = "white", color = NA) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
46 ) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
47 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
48 ggsave(filename = plot_file, plot = p, device = "jpg") |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
49 } |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
50 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
51 dose_response_analysis <- function(data, concentration_col, response_col, plot_file, ec_file) { |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
52 models <- fit_models(data, concentration_col, response_col) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
53 best_model_info <- select_best_model(models) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
54 ec_values <- calculate_ec_values(best_model_info$model) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
55 plot_dose_response(best_model_info$model, data, ec_values, concentration_col, response_col, plot_file) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
56 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
57 ec_data <- data.frame( |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
58 EC10 = ec_values$EC10[1], |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
59 EC25 = ec_values$EC25[1], |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
60 EC50 = ec_values$EC50[1] |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
61 ) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
62 write.csv(ec_data, ec_file, row.names = FALSE) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
63 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
64 return(list(best_model = best_model_info$name, ec_values = ec_values)) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
65 } |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
66 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
67 args <- commandArgs(trailingOnly = TRUE) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
68 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
69 data_file <- args[1] |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
70 concentration_col <- args[2] |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
71 response_col <- args[3] |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
72 plot_file <- args[4] |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
73 ec_file <- args[5] |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
74 |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
75 data <- read.csv(data_file, header = TRUE) |
082e9d22c38d
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 3aebdcc7c5b266a30262402934ffaad2a58adbcb
ufz
parents:
diff
changeset
|
76 dose_response_analysis(data, concentration_col, response_col, plot_file, ec_file) |