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