## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

## ----load-data----------------------------------------------------------------
library(bsocialv2)

# Read the consortia presence/absence matrix
consortia <- read.csv(
  system.file("extdata", "consortia.csv", package = "bsocialv2")
)
if (!"Consortia" %in% colnames(consortia)) colnames(consortia)[1] <- "Consortia"

# Read the curated (pre-processed) growth parameters
curated <- read.csv(
  system.file("extdata", "curated_MTBE.csv", package = "bsocialv2")
)
if (!"Consortia" %in% colnames(curated)) colnames(curated)[1] <- "Consortia"

head(consortia)
head(curated)

## ----create-object------------------------------------------------------------
obj <- new("bsocial")
obj@id_proyecto <- "MTBE_example"

# Strain names are all columns in consortia except "Consortia"
obj@cepas_seleccionadas <- setdiff(colnames(consortia), "Consortia")

# Populate the raw-data list
obj@datos_crudos <- list(
  consortia = consortia,
  curated   = curated
)

## ----transform-curated--------------------------------------------------------
obj <- transform_curated_data(obj)
head(obj@datos_procesados)

## ----analyze-growth-----------------------------------------------------------
obj <- analyze_growth(obj)

# View the scatter plot
obj@graficos$growth_scatter

# Top 10 consortia by number of generations
obj@resultados_analisis$best_10_ngen

## ----analyze-social-----------------------------------------------------------
obj <- analyze_social_behavior(obj)

# Fitness boxplots (NGen and GR)
obj@resultados_analisis$social_behavior$social_generations_plot
obj@resultados_analisis$social_behavior$social_gr_plot

## ----summarize-social---------------------------------------------------------
obj <- summarize_social_behavior(obj)

# Classification based on number of generations
obj@resultados_analisis$summary_gen

# Classification based on growth rate
obj@resultados_analisis$summary_gr

## ----analyze-diversity--------------------------------------------------------
obj <- analyze_diversity(obj)

# Diversity effect on fitness (NGen)
obj@graficos$diversity_gen_plot

# Diversity effect on fitness (GR)
obj@graficos$diversity_gr_plot

## ----analyze-stability--------------------------------------------------------
obj <- analyze_stability(obj)

# Stability violin plots
obj@graficos$stability_ngen_plot
obj@graficos$stability_gr_plot

## ----analyze-biofilm----------------------------------------------------------
obj <- analyze_biofilm_sequence(obj)

# The paths are stored as named lists
str(obj@resultados_analisis$biofilm_gen_paths, max.level = 2)

# Plot the assembly graph (base R plot)
obj@graficos$biofilm_gen_plot_func()

## ----raw-workflow, eval = FALSE-----------------------------------------------
#  library(bsocialv2)
#  library(readr)
#  
#  obj <- new("bsocial")
#  obj@id_proyecto <- "raw_example"
#  
#  # Read consortia matrix
#  consortia <- read.csv(
#    system.file("extdata", "consortia.csv", package = "bsocialv2")
#  )
#  if (!"Consortia" %in% colnames(consortia)) colnames(consortia)[1] <- "Consortia"
#  
#  obj@cepas_seleccionadas <- setdiff(colnames(consortia), "Consortia")
#  
#  # Read plate CSVs
#  plate_files <- paste0("plate", 1:6, ".csv")
#  plates <- lapply(plate_files, function(f) {
#    read_csv(system.file("extdata", f, package = "bsocialv2"), show_col_types = FALSE)
#  })
#  
#  obj@datos_crudos <- list(
#    consortia = consortia,
#    plates    = plates,
#    type      = "raw"
#  )
#  
#  # Step 1: Preprocess raw data (background correction + replicate aggregation)
#  #   groups    - integer vector assigning plates to replicate groups
#  #   bg_type   - "blank" (subtract blank well) or "threshold" (subtract OD value)
#  #   bg_param  - blank well ID (for "blank") or numeric threshold (for "threshold")
#  obj <- transform_raw_data(obj,
#    groups   = c(1, 1, 2, 2, 3, 3),
#    bg_type  = "blank",
#    bg_param = "BLANK"
#  )
#  
#  # Step 2: Calculate growth parameters
#  obj <- calculate_growth_params(obj, method = "growthcurver")
#  
#  # Optional: visualize processed curves
#  obj <- plot_processed_curves(obj)
#  obj@graficos$processed_curves_plot
#  
#  # Step 3 onwards: same pipeline as curated workflow
#  obj <- analyze_growth(obj)
#  obj <- analyze_social_behavior(obj)
#  obj <- summarize_social_behavior(obj)
#  obj <- analyze_diversity(obj)
#  obj <- analyze_stability(obj)
#  obj <- analyze_biofilm_sequence(obj)

