comparison dose_response.R @ 3:2aa9da0a84a4 draft default tip

planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/tox_tools/dose_responses commit 707eca86fc2de2e563fb5c89889f54eb13f529d0
author ufz
date Tue, 21 Jan 2025 12:26:00 +0000
parents c122403ac78a
children
comparison
equal deleted inserted replaced
2:c122403ac78a 3:2aa9da0a84a4
24 ec25 <- ED(model, 25, type = "relative") 24 ec25 <- ED(model, 25, type = "relative")
25 ec10 <- ED(model, 10, type = "relative") 25 ec10 <- ED(model, 10, type = "relative")
26 return(list(EC50 = ec50, EC25 = ec25, EC10 = ec10)) 26 return(list(EC50 = ec50, EC25 = ec25, EC10 = ec10))
27 } 27 }
28 28
29 plot_dose_response <- function(model, data, ec_values, concentration_col, response_col, plot_file, compound_name, concentration_unit) { 29 plot_dose_response <- function(model, data, ec_values, concentration_col, response_col, replicate_col, plot_file, compound_name, concentration_unit) {
30 # Generate a grid of concentration values for predictions 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 34
38 prediction_data$lower <- predictions[, 2] 38 prediction_data$lower <- predictions[, 2]
39 prediction_data$upper <- predictions[, 3] 39 prediction_data$upper <- predictions[, 3]
40 40
41 print(prediction_data) 41 print(prediction_data)
42 42
43 data$rep <- factor(data$rep) 43 # Ensure replicate_col is treated as a factor
44 data[[replicate_col]] <- factor(data[[replicate_col]])
44 45
45 # Create the plot 46 # Create the plot
46 p <- ggplot(data, aes_string(x = concentration_col, y = response_col)) + 47 p <- ggplot(data, aes_string(x = concentration_col, y = response_col)) +
47 geom_point(aes(colour = rep)) + # Original data points 48 geom_point(aes_string(colour = replicate_col)) + # Original data points
48 geom_line(data = prediction_data, aes_string(x = "conc", y = "resp"), color = "blue") + # Predicted curve 49 geom_line(data = prediction_data, aes_string(x = concentration_col, y = response_col), 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 50 geom_ribbon(data = prediction_data, aes_string(x = concentration_col, ymin = "lower", ymax = "upper"), alpha = 0.2, fill = "blue") + # Confidence intervals
50 geom_vline(xintercept = ec_values$EC10[1], color = "green", linetype = "dashed") + 51 geom_vline(xintercept = ec_values$EC10[1], color = "green", linetype = "dashed") +
51 geom_vline(xintercept = ec_values$EC50[1], color = "red", linetype = "dashed") + 52 geom_vline(xintercept = ec_values$EC50[1], color = "red", linetype = "dashed") +
52 labs( 53 labs(
53 title = paste(compound_name, "- Dose-Response Curve"), 54 title = paste(compound_name, "- Dose-Response Curve"),
54 x = paste("Dose [", concentration_unit, "]"), 55 x = paste("Dose [", concentration_unit, "]"),
55 y = "Response %" 56 y = "Response %",
57 colour = "Replicates"
56 ) + 58 ) +
57 theme_minimal() + 59 theme_minimal() +
58 theme( 60 theme(
59 panel.background = element_rect(fill = "white", color = NA), 61 panel.background = element_rect(fill = "white", color = NA),
60 plot.background = element_rect(fill = "white", color = NA) 62 plot.background = element_rect(fill = "white", color = NA)
64 jpeg(filename = plot_file, width = 480, height = 480, res = 72) 66 jpeg(filename = plot_file, width = 480, height = 480, res = 72)
65 print(p) 67 print(p)
66 dev.off() 68 dev.off()
67 } 69 }
68 70
69 dose_response_analysis <- function(data, concentration_col, response_col, plot_file, ec_file, compound_name, concentration_unit) { 71 dose_response_analysis <- function(data, concentration_col, response_col, replicate_col, plot_file, ec_file, compound_name, concentration_unit) {
70 # Ensure column names are correctly selected 72 # Ensure column names are correctly selected
71 concentration_col <- colnames(data)[as.integer(concentration_col)] 73 concentration_col <- colnames(data)[as.integer(concentration_col)]
72 response_col <- colnames(data)[as.integer(response_col)] 74 response_col <- colnames(data)[as.integer(response_col)]
75 replicate_col <- colnames(data)[as.integer(replicate_col)]
73 76
74 # Fit models and select the best one 77 # Fit models and select the best one
75 models <- fit_models(data, concentration_col, response_col) 78 models <- fit_models(data, concentration_col, response_col)
76 best_model_info <- select_best_model(models) 79 best_model_info <- select_best_model(models)
77 best_model <- best_model_info$model 80 best_model <- best_model_info$model
79 82
80 # Calculate EC values 83 # Calculate EC values
81 ec_values <- calculate_ec_values(best_model) 84 ec_values <- calculate_ec_values(best_model)
82 85
83 # Plot the dose-response curve 86 # Plot the dose-response curve
84 plot_dose_response(best_model, data, ec_values, concentration_col, response_col, plot_file, compound_name, concentration_unit) 87 plot_dose_response(best_model, data, ec_values, concentration_col, response_col, replicate_col, plot_file, compound_name, concentration_unit)
85 88
86 # Get model summary and AIC value 89 # Get model summary and AIC value
87 model_summary <- summary(best_model) 90 model_summary <- summary(best_model)
88 model_aic <- AIC(best_model) 91 model_aic <- AIC(best_model)
89 92
106 args <- commandArgs(trailingOnly = TRUE) 109 args <- commandArgs(trailingOnly = TRUE)
107 110
108 data_file <- args[1] 111 data_file <- args[1]
109 concentration_col <- args[2] 112 concentration_col <- args[2]
110 response_col <- args[3] 113 response_col <- args[3]
111 plot_file <- args[4] 114 replicate_col <- args[4]
112 ec_file <- args[5] 115 plot_file <- args[5]
113 compound_name <- args[6] 116 ec_file <- args[6]
114 concentration_unit <- args[7] 117 compound_name <- args[7]
118 concentration_unit <- args[8]
115 119
116 data <- read.csv(data_file, header = TRUE, sep = "\t") 120 data <- read.csv(data_file, header = TRUE, sep = "\t")
117 print(data) 121 print(data)
118 dose_response_analysis(data, concentration_col, response_col, plot_file, ec_file, compound_name, concentration_unit) 122 dose_response_analysis(data, concentration_col, response_col, replicate_col, plot_file, ec_file, compound_name, concentration_unit)