df_nidulans <- df %>%
  filter(species == "nidulans") %>%
  select(ObjectId, time, Area, Circularity, well, species) %>%
  group_by(ObjectId) %>%
  mutate(
    InitialArea = first(na.omit(Area)),
    InitialCircularity = first(na.omit(Circularity)),
    PercentIncrease = ((Area - InitialArea) / InitialArea) * 100,
    CircularityChange = abs(Circularity - InitialCircularity) / InitialCircularity,
    R = ifelse(CircularityChange < 0.1 & PercentIncrease < 10, 1, 0),
    S = ifelse(CircularityChange < 0.1 & PercentIncrease >= 10, 1, 0),
    G = ifelse(CircularityChange >= 0.1 & PercentIncrease >= 50, 1, 0)
  ) %>%
  mutate(
    R = na_if(R, 0), S = na_if(S, 0), G = na_if(G, 0),
    R = na.locf0(R), S = na.locf0(S), G = na.locf0(G),
    status = case_when(
      G == 1 ~ 2,
      S == 1 & is.na(G) ~ 1,
      TRUE ~ 0
    ),
    S = ifelse(status > 0, 1, 0),
    G = ifelse(status > 1, 1, 0)
  ) %>%
  ungroup() %>%
  mutate(time = as.integer(time / 60) + 1)
summary_nidulans <- df_nidulans %>%
  group_by(well, time, species) %>%
  summarise(
    Total = n_distinct(ObjectId),
    Swelling = sum(S, na.rm = TRUE),
    Germination = sum(G, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  mutate(
    SwellingPercent = Swelling / Total * 100,
    GerminationPercent = Germination / Total * 100
  ) %>%
  mutate(
    Treatment = str_remove(well, "nidulans/")
  )
group1 <- c("C", "C-K", "Cys", "Acy-Cys", "Met-Cys")
group2 <- c("pro", "pre-pro 2 h", "pre-pro 4 h", "pre-pro 6 h", "pre-pro 8 h", "pre-pro 10 h")

treatment_labels <- c(
  "C" = "Control",
  "C-K" = "Killed Spores",
  "Cys" = "Cysteine",
  "Acy-Cys" = "N-acetyl Cys",
  "Met-Cys" = "S-methyl Cys",
  "pro" = "Proline",
  "pre-pro 2 h" = "Pre-Pro 2h + Cys",
  "pre-pro 4 h" = "Pre-Pro 4h + Cys",
  "pre-pro 6 h" = "Pre-Pro 6h + Cys",
  "pre-pro 8 h" = "Pre-Pro 8h + Cys",
  "pre-pro 10 h" = "Pre-Pro 10h + Cys"
)

colors <- c(
  "Control" = "navy", "Killed Spores" = "black", "Cysteine" = "red3",
  "N-acetyl Cys" = "darkorange2", "S-methyl Cys" = "darkgreen", "Proline" = "hotpink",
  "Pre-Pro 2h + Cys" = "#1b9e77", "Pre-Pro 4h + Cys" = "navy",
  "Pre-Pro 6h + Cys" = "gold", "Pre-Pro 8h + Cys" = "pink1",
  "Pre-Pro 10h + Cys" = "skyblue"
)
summary_nidulans %>%
  filter(Treatment %in% group1) %>%
  mutate(Label = treatment_labels[Treatment]) %>%
  ggplot(aes(x = time, y = SwellingPercent, color = Label)) +
  geom_line(linewidth = 1.3) +
  labs(title = "Swelling (%) — Cysteine vs Controls", x = "Time (h)", y = "Swelling (%)", color = "Condition") +
  theme_classic(base_size = 14) +
  scale_color_manual(values = colors) +
  ylim(0, 100) + scale_x_continuous(breaks = seq(0, 14, 2)) +
  theme(legend.position = "bottom")

summary_nidulans %>%
  filter(Treatment %in% group1) %>%
  mutate(Label = treatment_labels[Treatment]) %>%
  ggplot(aes(x = time, y = GerminationPercent, color = Label)) +
  geom_line(linewidth = 1.3) +
  labs(title = "Germination (%) — Cysteine vs Controls", x = "Time (h)", y = "Germination (%)", color = "Condition") +
  theme_classic(base_size = 14) +
  scale_color_manual(values = colors) +
  ylim(0, 100) + scale_x_continuous(breaks = seq(0, 14, 2)) +
  theme(legend.position = "bottom")

summary_nidulans %>%
  filter(Treatment %in% group2) %>%
  mutate(Label = treatment_labels[Treatment]) %>%
  ggplot(aes(x = time, y = SwellingPercent, color = Label)) +
  geom_line(linewidth = 1.3) +
  labs(title = "Swelling (%) — Proline & Pre-Treatments", x = "Time (h)", y = "Swelling (%)", color = "Condition") +
  theme_classic(base_size = 14) +
  scale_color_manual(values = colors) +
  ylim(0, 100) + scale_x_continuous(breaks = seq(0, 14, 2)) +
  theme(legend.position = "bottom")

summary_nidulans %>%
  filter(Treatment %in% group2) %>%
  mutate(Label = treatment_labels[Treatment]) %>%
  ggplot(aes(x = time, y = GerminationPercent, color = Label)) +
  geom_line(linewidth = 1.3) +
  labs(title = "Germination (%) — Proline & Pre-Treatments", x = "Time (h)", y = "Germination (%)", color = "Condition") +
  theme_classic(base_size = 14) +
  scale_color_manual(values = colors) +
  ylim(0, 100) + scale_x_continuous(breaks = seq(0, 14, 2)) +
  theme(legend.position = "bottom")