comparison dose_response.R @ 2:c122403ac78a draft

planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tools/tox_tools/baseline_calculator commit 61a3d9a20a9a90d551dd5f7503be781dc28f4b75
author ufz
date Wed, 18 Dec 2024 09:11:40 +0000
parents 8a1b524ed9d8
children 2aa9da0a84a4
comparison
equal deleted inserted replaced
1:8a1b524ed9d8 2:c122403ac78a
2 library(ggplot2) 2 library(ggplot2)
3 3
4 fit_models <- function(data, concentration_col, response_col) { 4 fit_models <- function(data, concentration_col, response_col) {
5 models <- list( 5 models <- list(
6 LL.2 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.2(), type = "binomial"), 6 LL.2 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.2(), type = "binomial"),
7 LL.3 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.3(), type = "binomial"),
8 LL.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.4(), type = "binomial"), 7 LL.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.4(), type = "binomial"),
9 LL.5 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.5(), type = "binomial"),
10 W1.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W1.4(), type = "binomial"), 8 W1.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W1.4(), type = "binomial"),
11 W2.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W2.4(), type = "binomial") 9 W2.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W2.4(), type = "binomial"),
10 BC.5 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = BC.5(), type = "binomial")
12 ) 11 )
13 return(models) 12 return(models)
14 } 13 }
15 14
16 select_best_model <- function(models) { 15 select_best_model <- function(models) {
25 ec25 <- ED(model, 25, type = "relative") 24 ec25 <- ED(model, 25, type = "relative")
26 ec10 <- ED(model, 10, type = "relative") 25 ec10 <- ED(model, 10, type = "relative")
27 return(list(EC50 = ec50, EC25 = ec25, EC10 = ec10)) 26 return(list(EC50 = ec50, EC25 = ec25, EC10 = ec10))
28 } 27 }
29 28
30 plot_dose_response <- function(model, data, ec_values, concentration_col, response_col, plot_file) { 29 plot_dose_response <- function(model, data, ec_values, concentration_col, response_col, plot_file, compound_name, concentration_unit) {
30 # Generate a grid of concentration values for predictions
31 concentration_grid <- seq(min(data[[concentration_col]]), max(data[[concentration_col]]), length.out = 100) 31 concentration_grid <- seq(min(data[[concentration_col]]), max(data[[concentration_col]]), length.out = 100)
32 prediction_data <- data.frame(concentration_grid) 32 prediction_data <- data.frame(concentration_grid)
33 colnames(prediction_data) <- concentration_col 33 colnames(prediction_data) <- concentration_col
34 predicted_values <- predict(model, newdata = prediction_data, type = "response") 34
35 prediction_data$response <- predicted_values 35 # Compute predictions with confidence intervals
36 predictions <- predict(model, newdata = prediction_data, type = "response", interval = "confidence")
37 prediction_data$resp <- predictions[, 1]
38 prediction_data$lower <- predictions[, 2]
39 prediction_data$upper <- predictions[, 3]
40
41 print(prediction_data)
42
43 data$rep <- factor(data$rep)
44
45 # Create the plot
36 p <- ggplot(data, aes_string(x = concentration_col, y = response_col)) + 46 p <- ggplot(data, aes_string(x = concentration_col, y = response_col)) +
37 geom_point(color = "red") + 47 geom_point(aes(colour = rep)) + # Original data points
38 geom_line(data = prediction_data, aes_string(x = concentration_col, y = "response"), color = "blue") + 48 geom_line(data = prediction_data, aes_string(x = "conc", y = "resp"), color = "blue") + # Predicted curve
49 geom_ribbon(data = prediction_data, aes_string(x = "conc", ymin = "lower", ymax = "upper"), alpha = 0.2, fill = "blue") + # Confidence intervals
39 geom_vline(xintercept = ec_values$EC10[1], color = "green", linetype = "dashed") + 50 geom_vline(xintercept = ec_values$EC10[1], color = "green", linetype = "dashed") +
40 geom_vline(xintercept = ec_values$EC50[1], color = "purple", linetype = "dashed") + 51 geom_vline(xintercept = ec_values$EC50[1], color = "red", linetype = "dashed") +
41 labs(title = "Dose-Response Curve", x = "Concentration", y = "Effect") + 52 labs(
53 title = paste(compound_name, "- Dose-Response Curve"),
54 x = paste("Dose [", concentration_unit, "]"),
55 y = "Response %"
56 ) +
42 theme_minimal() + 57 theme_minimal() +
43 theme( 58 theme(
44 panel.background = element_rect(fill = "white", color = NA), 59 panel.background = element_rect(fill = "white", color = NA),
45 plot.background = element_rect(fill = "white", color = NA) 60 plot.background = element_rect(fill = "white", color = NA)
46 ) 61 )
47 62
48 jpeg(filename = plot_file) 63 # Save the plot to a file
64 jpeg(filename = plot_file, width = 480, height = 480, res = 72)
49 print(p) 65 print(p)
50 dev.off() 66 dev.off()
51 } 67 }
52 68
53 dose_response_analysis <- function(data, concentration_col, response_col, plot_file, ec_file) { 69 dose_response_analysis <- function(data, concentration_col, response_col, plot_file, ec_file, compound_name, concentration_unit) {
70 # Ensure column names are correctly selected
54 concentration_col <- colnames(data)[as.integer(concentration_col)] 71 concentration_col <- colnames(data)[as.integer(concentration_col)]
55 response_col <- colnames(data)[as.integer(response_col)] 72 response_col <- colnames(data)[as.integer(response_col)]
73
74 # Fit models and select the best one
56 models <- fit_models(data, concentration_col, response_col) 75 models <- fit_models(data, concentration_col, response_col)
57 best_model_info <- select_best_model(models) 76 best_model_info <- select_best_model(models)
58 ec_values <- calculate_ec_values(best_model_info$model) 77 best_model <- best_model_info$model
59 plot_dose_response(best_model_info$model, data, ec_values, concentration_col, response_col, plot_file) 78 best_model_name <- best_model_info$name
60 79
80 # Calculate EC values
81 ec_values <- calculate_ec_values(best_model)
82
83 # Plot the dose-response curve
84 plot_dose_response(best_model, data, ec_values, concentration_col, response_col, plot_file, compound_name, concentration_unit)
85
86 # Get model summary and AIC value
87 model_summary <- summary(best_model)
88 model_aic <- AIC(best_model)
89
90 # Prepare EC values data frame with additional information
61 ec_data <- data.frame( 91 ec_data <- data.frame(
62 EC10 = ec_values$EC10[1], 92 Metric = c("chemical_name", "EC10", "EC25", "EC50", "AIC"),
63 EC25 = ec_values$EC25[1], 93 Value = c(compound_name, ec_values$EC10[1], ec_values$EC25[1], ec_values$EC50[1], model_aic)
64 EC50 = ec_values$EC50[1]
65 ) 94 )
95
96 # Write EC values, AIC, and model summary to the output file
66 write.table(ec_data, ec_file, sep = "\t", row.names = FALSE, col.names = TRUE, quote = FALSE) 97 write.table(ec_data, ec_file, sep = "\t", row.names = FALSE, col.names = TRUE, quote = FALSE)
67 98
68 return(list(best_model = best_model_info$name, ec_values = ec_values)) 99 # Append the model summary to the file
100 cat("\nModel Summary:\n", file = ec_file, append = TRUE)
101 capture.output(model_summary, file = ec_file, append = TRUE)
102
103 return(list(best_model = best_model_name, ec_values = ec_values))
69 } 104 }
70 105
71 args <- commandArgs(trailingOnly = TRUE) 106 args <- commandArgs(trailingOnly = TRUE)
72 107
73 data_file <- args[1] 108 data_file <- args[1]
74 concentration_col <- args[2] 109 concentration_col <- args[2]
75 response_col <- args[3] 110 response_col <- args[3]
76 plot_file <- args[4] 111 plot_file <- args[4]
77 ec_file <- args[5] 112 ec_file <- args[5]
113 compound_name <- args[6]
114 concentration_unit <- args[7]
78 115
79 data <- read.csv(data_file, header = TRUE, sep = "\t") 116 data <- read.csv(data_file, header = TRUE, sep = "\t")
80 dose_response_analysis(data, concentration_col, response_col, plot_file, ec_file) 117 print(data)
118 dose_response_analysis(data, concentration_col, response_col, plot_file, ec_file, compound_name, concentration_unit)