Mercurial > repos > ufz > dose_response_analysis_tool
changeset 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 |
files | dose_response.R dose_response.xml test-data/test_summary.csv test-data/test_summary.tsv |
diffstat | 4 files changed, 147 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dose_response.R Mon Jun 10 11:57:52 2024 +0000 @@ -0,0 +1,76 @@ +library(drc) +library(ggplot2) + +fit_models <- function(data, concentration_col, response_col) { + models <- list( + LL.2 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.2(), type = "binomial"), + LL.3 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.3(), type = "binomial"), + LL.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.4(), type = "binomial"), + LL.5 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = LL.5(), type = "binomial"), + W1.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W1.4(), type = "binomial"), + W2.4 = drm(data[[response_col]] ~ data[[concentration_col]], data = data, fct = W2.4(), type = "binomial") + ) + return(models) +} + +select_best_model <- function(models) { + aic_values <- sapply(models, AIC) + best_model_name <- names(which.min(aic_values)) + best_model <- models[[best_model_name]] + return(list(name = best_model_name, model = best_model)) +} + +calculate_ec_values <- function(model) { + ec50 <- ED(model, 50, type = "relative") + ec25 <- ED(model, 25, type = "relative") + ec10 <- ED(model, 10, type = "relative") + return(list(EC50 = ec50, EC25 = ec25, EC10 = ec10)) +} + +plot_dose_response <- function(model, data, ec_values, concentration_col, response_col, plot_file) { + concentration_grid <- seq(min(data[[concentration_col]]), max(data[[concentration_col]]), length.out = 100) + prediction_data <- data.frame(concentration_grid) + colnames(prediction_data) <- concentration_col + predicted_values <- predict(model, newdata = prediction_data, type = "response") + prediction_data$response <- predicted_values + p <- ggplot(data, aes_string(x = concentration_col, y = response_col)) + + geom_point(color = "red") + + geom_line(data = prediction_data, aes_string(x = concentration_col, y = "response"), color = "blue") + + geom_vline(xintercept = ec_values$EC10[1], color = "green", linetype = "dashed") + + geom_vline(xintercept = ec_values$EC50[1], color = "purple", linetype = "dashed") + + labs(title = "Dose-Response Curve", x = "Concentration", y = "Effect") + + theme_minimal() + + theme( + panel.background = element_rect(fill = "white", color = NA), + plot.background = element_rect(fill = "white", color = NA) + ) + + ggsave(filename = plot_file, plot = p, device = "jpg") +} + +dose_response_analysis <- function(data, concentration_col, response_col, plot_file, ec_file) { + models <- fit_models(data, concentration_col, response_col) + best_model_info <- select_best_model(models) + ec_values <- calculate_ec_values(best_model_info$model) + plot_dose_response(best_model_info$model, data, ec_values, concentration_col, response_col, plot_file) + + ec_data <- data.frame( + EC10 = ec_values$EC10[1], + EC25 = ec_values$EC25[1], + EC50 = ec_values$EC50[1] + ) + write.csv(ec_data, ec_file, row.names = FALSE) + + return(list(best_model = best_model_info$name, ec_values = ec_values)) +} + +args <- commandArgs(trailingOnly = TRUE) + +data_file <- args[1] +concentration_col <- args[2] +response_col <- args[3] +plot_file <- args[4] +ec_file <- args[5] + +data <- read.csv(data_file, header = TRUE) +dose_response_analysis(data, concentration_col, response_col, plot_file, ec_file)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dose_response.xml Mon Jun 10 11:57:52 2024 +0000 @@ -0,0 +1,57 @@ +<tool id="dr_curve" name="Dose Response Curve for Toxicological Risk Assessment" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="23.0"> + <description>Toxicity prediction tool - Dose response Curve</description> + <macros> + <token name="@TOOL_VERSION@">3.0_1</token> + <token name="@VERSION_SUFFIX@">0</token> + </macros> + <creator> + <organization name="Helmholtz Centre for Environmental Research - UFZ, Department of Ecotoxicology" + url ="https://www.ufz.de/index.php?en=34241"/> + </creator> + <requirements> + <requirement type="package" version="@TOOL_VERSION@">r-drc</requirement> + <requirement type="package" version="4.3.3">r-base</requirement> + <requirement type="package" version="3.5.1">r-ggplot2</requirement> + </requirements> + <command detect_errors="aggressive"> + <![CDATA[ + Rscript $__tool_directory__/dose_response.R + '$input_csv' + '$concentration_column' + '$response_column' + '$plot_output' + '$ec_output' + ]]> + </command> + <inputs> + <param name="input_csv" type="data" format="tabular" label="Concentration - Response Tabular Input"/> + <param name="concentration_column" type="data_column" data_ref="input_csv" label="Concentration Column" help="Name of the column for concentration values"/> + <param name="response_column" type="data_column" label="Response Column" help="Name of the column for response values"/> + </inputs> + <outputs> + <data name="plot_output" format="jpg" label="Dose Response Plot"/> + <data name="ec_output" format="tabular" label="${tool.name} on ${on_string}: EC Values"/> + </outputs> + <tests> + <test> + <param name="input_csv" value="test_summary.tsv"/> + <param name="concentration_column" value="concentration"/> + <param name="response_column" value="lethal"/> + <output name="plot_output" value="result.jpeg" ftype="jpg"/> + <output name="ec_output" value="results_EC.csv" ftype="csv" /> + </test> + </tests> + <help><![CDATA[ + This tool performs dose-response analysis on the provided CSV file, + generates a dose-response plot, and calculates EC values (EC10, EC25, EC50). + + - `input_csv`: A CSV file containing the dose-response data. + - `concentration_column`: The name of the column in the CSV file that contains the concentration values. + - `response_column`: The name of the column in the CSV file that contains the response values + - `plot_output`: A JPG image file of the dose-response plot. + - `ec_output`: A CSV file containing the calculated EC values. + ]]></help> + <citations> + <citation type="doi">10.1371/journal.pone.0146021</citation> + </citations> +</tool>