---
execute:
message: FALSE
warning: FALSE
---
# Cell Counts {.unnumbered}
```{r}
library (tidyverse)
library (scales)
library (ggtext)
```
## File Paths
```{r}
# Folder paths
MOC_input_path <- "data/input_data/MOC"
CFUs_input_path <- "data/dataframes"
# Create subfolders for output files
dataframes_folder <- "data/dataframes"
if (! file.exists ("data/dataframes" )) {
dir.create ("data/dataframes" , recursive = TRUE )
}
outputs_folder <- "data/outputs/MOC"
if (! file.exists ("data/outputs/MOC" )) {
dir.create ("data/outputs/MOC" , recursive = TRUE )
}
# Load data and metadata
HNOCells <- read_csv (file.path (MOC_input_path, "MOC_HNOBac_2024.csv" ))
CFUs <- read_rds (file.path (CFUs_input_path, "CFU_values.rds" ))
```
## Cells per HNO well
```{r}
HNOCells$ Date <- as.factor (HNOCells$ Date)
HNOCells$ Line <- as.factor (HNOCells$ Line)
HNOCells <- HNOCells %>%
mutate (Line = factor (Line, levels = c ("HNO918" , "HNO204" , "HNO919" )))
```
```{r}
HNOCells_summary <- HNOCells %>%
group_by (Line) %>%
summarize (median_cells = median (Cells_HNO),
Q1 = quantile (Cells_HNO, 0.25 ),
Q3 = quantile (Cells_HNO, 0.75 ),
n = n (),
SE = sd (Cells_HNO)/ sqrt (n),
CI = SE* 1.96 )
```
### Plot
```{r}
boxplot_HNOCells <- ggplot () +
geom_boxplot (data = HNOCells, aes (x = Line, y = Cells_HNO)) +
geom_jitter (data = HNOCells, aes (x = Line, y = Cells_HNO, shape = Line),
fill = "#5b5b5b" , color = "#5b5b5b" , size = 3 , width = 0.15 , alpha = 0.75 ) +
scale_shape_manual (values = c (21 , 22 , 24 )) +
scale_y_log10 (limits = c (100000 , 10000000 ),
labels = trans_format ("log10" , math_format (10 ^ .x))) +
labs (x = "HNO Line" ,
y = "HNO Cells/Well" ) +
theme_bw () +
theme (panel.grid = element_blank (),
text = element_text (size = 20 ),
axis.text.x = element_text (color = "black" ),
axis.text.y = element_text (color = "black" ))
boxplot_HNOCells
```
### Saving files
```{r}
ggsave (boxplot_HNOCells, filename = paste0 (outputs_folder, "/boxplot_HNOCells.png" ), width = 12 , height = 10 )
saveRDS (boxplot_HNOCells, file.path (outputs_folder, paste0 ("boxplot_HNOCells.rds" )))
```
```{r}
# Save data frames as CSV files in the metadata folder
write_csv (HNOCells, file.path (dataframes_folder, "HNOCells_values.csv" ))
# Save data frames as R objects in the metadata folder
saveRDS (HNOCells, file.path (dataframes_folder, "HNOCells_values.rds" ))
# Use this to read the final objects
HNOCells <- readRDS ("data/dataframes/HNOCells_values.rds" )
```
## Multiplicity of Colonization
```{r}
CFU_Inoculum <- CFUs %>%
filter (Time == "0" ) %>%
group_by (Line, bacteria) %>%
summarize (median_CFUs = median (NewCFU))
CFU_Inoculum <- left_join (CFU_Inoculum, HNOCells_summary, by = "Line" )
MOC_Calcs <- CFU_Inoculum %>%
mutate (MOC = (median_CFUs/ median_cells)) %>%
group_by (bacteria) %>%
mutate (avg_MOC = (mean (MOC)))
```
### Plot
```{r}
boxplot_MOC <- ggplot () +
geom_boxplot (data = MOC_Calcs, aes (x = bacteria, y = MOC, fill = bacteria)) +
geom_jitter (data = MOC_Calcs, aes (x = bacteria, y = MOC, shape = Line),
fill = "grey50" , color = "grey30" , size = 2 ,
width = 0.001 , stroke = 0.75 ) +
scale_fill_manual (values = c ("#2e67f2" ,"#AA35E3" ,"#927ed1" )) +
scale_shape_manual (values = c (21 , 22 , 24 )) +
labs (title = " " ,
x = "HNO Line" ,
y = "MOC" ) +
theme_bw () +
theme (panel.grid = element_blank (), text = element_text (size = 30 ),
axis.text.x = element_markdown (), axis.text.y = element_text (color = "black" ))
boxplot_MOC
```
### Saving files
```{r}
ggsave (boxplot_MOC, filename = paste0 (outputs_folder, "/boxplot_MOC.png" ), width = 12 , height = 10 )
saveRDS (boxplot_MOC, file.path (outputs_folder, paste0 ("boxplot_MOC.rds" )))
```
```{r}
# Save data frames as CSV files in the outputs folder
write_csv (MOC_Calcs, file.path (outputs_folder, "MOC_Calcs.csv" ))
# Save data frames as R objects in the outputs folder
saveRDS (MOC_Calcs, file.path (outputs_folder, "MOC_Calcs.rds" ))
# Use this to read the final objects
MOC_Calcs <- readRDS (file.path (outputs_folder, paste0 ("MOC_Calcs.rds" )))
```