annotate dose_response.R @ 1:8a1b524ed9d8 draft

planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
author ufz
date Tue, 08 Oct 2024 12:41:07 +0000
parents 082e9d22c38d
children c122403ac78a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1
8a1b524ed9d8 planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
ufz
parents: 0
diff changeset
48 jpeg(filename = plot_file)
8a1b524ed9d8 planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
ufz
parents: 0
diff changeset
49 print(p)
8a1b524ed9d8 planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
ufz
parents: 0
diff changeset
50 dev.off()
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
51 }
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
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 dose_response_analysis <- function(data, concentration_col, response_col, plot_file, ec_file) {
1
8a1b524ed9d8 planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
ufz
parents: 0
diff changeset
54 concentration_col <- colnames(data)[as.integer(concentration_col)]
8a1b524ed9d8 planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
ufz
parents: 0
diff changeset
55 response_col <- colnames(data)[as.integer(response_col)]
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
56 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
57 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
58 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
59 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
60
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 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
62 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
63 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
64 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
65 )
1
8a1b524ed9d8 planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
ufz
parents: 0
diff changeset
66 write.table(ec_data, ec_file, sep = "\t", row.names = FALSE, col.names = TRUE, quote = FALSE)
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
67
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 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
69 }
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
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 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
72
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 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
74 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
75 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
76 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
77 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
78
1
8a1b524ed9d8 planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit dca5f947ae4c9697ac0cfce0b313170b541124e5
ufz
parents: 0
diff changeset
79 data <- read.csv(data_file, header = TRUE, sep = "\t")
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
80 dose_response_analysis(data, concentration_col, response_col, plot_file, ec_file)