IC50 Analysis

Abraheem ‘Abe’ Khouqeer

2024-12-16

Dose Response Analysis Function

The following R function processes two experiments, combines the data, fits a dose-response model, and outputs the fold change and p-values.

Function Inputs

The function requires the following inputs:

  1. experiment1: A dataframe representing the first experiment.
  2. experiment2: A dataframe representing the second experiment.
  3. experiment1_name: A string for the name of the first experiment.
  4. experiment2_name: A string for the name of the second experiment.

Each experiment dataframe should have rows corresponding to replicates (e.g., triplicates) and columns corresponding to dose responses.

Example Input

Below is an example structure for the input dataframe after normalizing to the 0 dose column:

control_data

H G F E D C B A
1 1 0.9603120 0.9931081 0.9992870 0.9048200 0.4503087 0.3642784 0.3594065
2 1 0.9330736 1.0045741 0.9629257 0.9163420 0.5403023 0.3635973 0.3534861
3 1 0.9605534 1.0045047 0.9805202 0.8912783 0.4923075 0.3676367 0.3529051

Each column represents a dose concentration, and each row represents a replicate.

dose_response_analysis <- function(experiment1, experiment2, experiment1_name, experiment2_name) {
  library(tidyr)
  library(drc)
  
  # Define the dose concentrations
  concentrations <- concentrations
  
  # Function to process a single experiment
  process_single_experiment <- function(exp, exp_name) {
    # Add concentrations as a new row
    exp <- rbind(exp, concentrations)
    # Add the experiment name as another row
    exp <- rbind(exp, rep(exp_name, ncol(exp)))
    return(exp)
  }
  
  # Process each experiment
  experiment1 <- process_single_experiment(experiment1, experiment1_name)
  experiment2 <- process_single_experiment(experiment2, experiment2_name)
  
  # Combine the two experiments
  combined <- cbind(experiment1, experiment2)
  
  # Convert to long format
  combined_long <- as.data.frame(t(combined))  # Transpose for easier manipulation
  colnames(combined_long) <- c(paste0("Rep_", 1:(ncol(combined_long) - 2)), "Concentration", "Experiment")
  combined_long <- pivot_longer(combined_long, cols = starts_with("Rep_"), names_to = "Replicate", values_to = "Viability")
  
  # Ensure numeric types for Viability and Concentration
  combined_long$Viability <- as.numeric(combined_long$Viability)
  combined_long$Concentration <- as.numeric(combined_long$Concentration)
  print(combined_long)
  
  # Fit the dose-response model
  drm_model <- drm(
    Viability ~ Concentration, Experiment, data = combined_long,
    fct = LL.4(names = c("hill", "min_value", "max_value", "ic_50"),
               fixed = c(NA, NA, NA, NA))
  )
  plot(drm_model)
  
  # Compare parameters using compParm
  comparison_results <- compParm(drm_model, "ic_50", operator = "/")
  
  # Extract the fold change and p-values
  fold_changes <- comparison_results[, "Estimate"]  # Extract fold changes (ratios)
  p_values <- comparison_results[, "p-value"]       # Extract p-values
  
  # Create a dataframe for the results
  output_df <- data.frame(
    Comparison = rownames(comparison_results),
    Fold_Change = fold_changes,
    p_value = p_values
  )
  
  return(output_df)
}