---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*.

```{r}
# IMPORTANT


# uncomment this if you clone newest version of TSAR from GitHub
# rm(list = c("merge_TSA", "TSA_average", "TSA_boxplot", "TSA_compare_plot", "TSA_wells_plot"))
# library("devtools")
# load_all('../../TSARnew') # make sure this should be the directory of TSAR source code
# devtools::document()
# devtools::install()


library(TSAR)


control_ID <- "CA121 WT_dH2O"
data("example_normalized_data")



# library('svglite')
library(ggplot2)
library(ggpubr)
library(readxl)
library(stringr)
library(tidyr)
library(dplyr)
library(readr)
```



```{r}



library('svglite')
# names(normalized_tsa_df)[names(normalized_tsa_df) == "RFU"] <- "Normalized"
# TSA_compare_plotx(example_normalized_data,y = "RFU",control_condition = control_ID,show_Tm = T)
TSA_compare_plot(example_normalized_data, control_condition = control_ID,
                           y = "RFU", smoother = "beta",
                           beta_shape = 4, beta_knots_frac = 0.008, show_Tm = TRUE)

# uncomment the following if you want to save the plots


# for (nm in names(plots)) {
#   # If your list contains NULLs (e.g. for unused slots), skip those:
#   if (is.null(plots[[nm]])) next
# 
#   # Construct filename: e.g., "plot_ConditionA.svg"
#   fname <- paste0("plot_", gsub("[^A-Za-z0-9_-]", "_", nm), ".svg")
#   ggsave(filename = fname, plot = plots[[nm]], width = 6, height = 4, units = "in", device = "svg")
# }
```

```{r}

# source("../R/tsa_curves.R")
for(i in 1:condition_IDs(example_normalized_data, n = TRUE)) {
    cond_i <- condition_IDs(example_normalized_data)[i]
    subset_i <- example_normalized_data$condition_ID == cond_i
    subset_i <- example_normalized_data[subset_i, ]
    # plot_i <- TSA_wells_plot(subset_i)
    plot_i <-  TSA_wells_plot(subset_i, y = "RFU", smooth = TRUE,
               smoother = "beta", beta_shape = 4, beta_knots_frac = 0.008)
    
    print(plot_i)
    
    # uncomment the following if you want to save the plots
    # Create a filename for each condition
    # svg_file <- paste0("tsa_plot_", gsub("[^A-Za-z0-9_-]", "_", cond_i), ".svg")
  
    # Save the plot as SVG
    # ggplot2::ggsave(filename = svg_file, plot = plot_i[[1]], device = "svg", width = 6, height = 4, units = "in")
}
```
```{r}
# 1) Compare L2 (RMSE) across GAM and a small Beta grid
# source("../R/averageTSA.R")
res_L2 <- TSA_smoother_diagnostics(
  example_normalized_data,
  y = "RFU",
  metric = "L2",
  # beta_shapes = c(2, 4),
  # beta_fracs  = c(0.008, 0.02)
)
res_L2_df <- as.data.frame(res_L2)
res_L2_df

```
```{r}

```

Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
