This is the plot before peak calling

library(dplyr)
## Warning: replacing previous import 'ellipsis::check_dots_unnamed' by
## 'rlang::check_dots_unnamed' when loading 'tibble'
## Warning: replacing previous import 'ellipsis::check_dots_used' by
## 'rlang::check_dots_used' when loading 'tibble'
## Warning: replacing previous import 'ellipsis::check_dots_empty' by
## 'rlang::check_dots_empty' when loading 'tibble'
## Warning: replacing previous import 'ellipsis::check_dots_unnamed' by
## 'rlang::check_dots_unnamed' when loading 'pillar'
## Warning: replacing previous import 'ellipsis::check_dots_used' by
## 'rlang::check_dots_used' when loading 'pillar'
## Warning: replacing previous import 'ellipsis::check_dots_empty' by
## 'rlang::check_dots_empty' when loading 'pillar'
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
# 6. Compare the aligned spectrum -------------------------------------------------
setwd('/Users/anjideng1/Desktop/MALDI/')

## Read the original file
table <- NULL
for (weekid in c('wk2', 'wk5')){
  tablei <- read.csv(paste0("output_Maaike/mean_spec_pos_",weekid,".csv"), header = T)
  colnames(tablei) <- NULL
  table <- rbind(table, data.frame(tablei, week = weekid))
}

## Read the cluster file
table3 <- NULL
for (weekid in c('wk2', 'wk5')){
  tablei <- read.csv(paste0("output_Maaike/final_combine_ranges.csv"), header = F)
  names(tablei) <- c("start", "end")
  ylevel <- min(table[table$week == weekid, "X2"]) - 0.05
  table3 <- rbind(table3, data.frame(tablei, week = weekid, ylevel = ylevel))
}
table$weeks = factor(table$week, levels = c('wk2', 'wk5'), labels = c('wk2', 'wk5'))
table3$weeks = factor(table3$week, levels = c('wk2', 'wk5'), labels = c("2 wks", "5 wks"))

table <- table %>%
  mutate(intensity = X2) %>%
  mutate(mzvalues = X1)
# pdf(file = paste0("output/explore/linetwoweeks.pdf"),height = 8, width = 12)
color_palatte <- c("#CAE7B9", "#F3DE8A",  "#EB9486", "#7E7F9A", "#565F41", "#D1D1D1","#6D1A9C","#15821E","#3A84E6","#997273")
strip_color <- "#0A1D37"
# table2 <- table2 %>%
#   mutate(intensity = X3) %>%
#   mutate(mzvalues = X2)

lineplot <- ggplot(table) + 
  geom_line(aes(y=intensity, x=mzvalues, color = weeks), size = 0.3) + 
  # geom_point(aes(y=intensity, x=mzvalues, color = weeks), size = 0.6, data = table2) + 
  # geom_segment(aes(x = start, y = ylevel, xend = end, yend = ylevel), size = 0.6,  data = table) +
  scale_color_manual(values=color_palatte) + 
  labs(x = "m/z values", y = "intensity") + 
  facet_grid(weeks~., scales = "free_y") + 
  # coord_cartesian(ylim = c(0.2, 0.8)) +
  # geom_vline(xintercept=c(21, 25, 50, 188, 250, 440, 600, 880), linetype=2, color = "#B3B6B7", size = 0.3)+
  theme_bw()  +
  scale_y_log10() +
  theme(text = element_text(size=16, family="URWHelvetica"),  axis.text = element_text(size = 16, family="URWHelvetica"), panel.spacing = unit(1, "lines") ) +
  theme(strip.background = element_rect(fill=strip_color,color=strip_color))+ # #535b44
  theme(strip.text = element_text(colour = 'white')) +
  theme(panel.border = element_rect(colour = strip_color), legend.position = "none")

plotly::ggplotly(lineplot)
# 5. Recover the original - single spectrum -------------------------------------------------
setwd('/Users/anjideng1/Desktop/MALDI/')

## Read the original file
table <- NULL
for (weekid in c('wk2', 'wk5')){
  tablei <- read.csv(paste0("output_Maaike/single_",weekid,".csv"), header = T)
  colnames(tablei) <- NULL
  table <- rbind(table, data.frame(tablei, week = weekid))
}

## Read the cluster file
table3 <- NULL
for (weekid in c('wk2', 'wk5')){
  tablei <- read.csv(paste0("output_Maaike/final_combine_ranges.csv"), header = F)
  names(tablei) <- c("start", "end")
  ylevel <- min(table[table$week == weekid, "X3"]) - 0.05
  table3 <- rbind(table3, data.frame(tablei, week = weekid, ylevel = ylevel))
}
table$weeks = factor(table$week, levels = c('wk2', 'wk5'), labels = c("2 wks", "5 wks"))
# table2$weeks = factor(table2$week, levels = c(4:3), labels = c("2 wks", "5 wks"))
table3$weeks = factor(table3$week, levels = c('wk2', 'wk5'), labels = c("2 wks", "5 wks"))
# pdf(file = paste0("output/explore/linetwoweeks.pdf"),height = 8, width = 12)
color_palatte <- c("#CAE7B9", "#F3DE8A",  "#EB9486", "#7E7F9A", "#565F41", "#D1D1D1","#6D1A9C","#15821E","#3A84E6","#997273")
strip_color <- "#0A1D37"
table <- table %>%
  mutate(intensity = X3) %>%
  mutate(mzvalues = X2)
# table2 <- table2 %>%
#   mutate(intensity = X3) %>%
#   mutate(mzvalues = X2)

lineplot <- ggplot(table) + 
  geom_line(aes(y=intensity, x=mzvalues, color = weeks), size = 0.3) + 
  # geom_point(aes(y=intensity, x=mzvalues, color = weeks), size = 0.6, data = table2) + 
  geom_segment(aes(x = start, y = ylevel, xend = end, yend = ylevel), size = 0.6,  data = table3) +
  scale_color_manual(values=color_palatte) + 
  labs(x = "m/z values", y = "intensity") + 
  facet_grid(weeks~., scales = "free_y") + 
  # coord_cartesian(ylim = c(0.2, 0.8)) +
  # geom_vline(xintercept=c(21, 25, 50, 188, 250, 440, 600, 880), linetype=2, color = "#B3B6B7", size = 0.3)+
  theme_bw()  +
  scale_y_log10() +
  theme(text = element_text(size=16, family="URWHelvetica"),  axis.text = element_text(size = 16, family="URWHelvetica"), panel.spacing = unit(1, "lines") ) +
  theme(strip.background = element_rect(fill=strip_color,color=strip_color))+ # #535b44
  theme(strip.text = element_text(colour = 'white')) +
  theme(panel.border = element_rect(colour = strip_color), legend.position = "none")

plotly::ggplotly(lineplot)