```{r setupx, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r libraries, message=FALSE}
library(CompoundDb)
library(MetaboCoreUtils)
```

```{r nl}
nl <- data.frame(rbind(
  c("[M+H-H2O]+", calculateMass("H2O"), 1),
  c("[M+H-2(H2O)]+", calculateMass("H2OH2O"), 1),
  c("[M+H-H2O-NH3]+", calculateMass("H2ONH3"), 1),
  c("[M+H-H2O-CO]+", calculateMass("H2OCO"), 1),
  c("[M+H-NH3]+", calculateMass("NH3"), 1),
  c("[M+H-C3H9N]+", calculateMass("C3H9N"), 1),
  c("[M+H-Glu]+", calculateMass(subtractElements("C5H9NO4", "H2O")), 1),
  c("[M+H-Gly]+", calculateMass(subtractElements("C2H5NO2", "H2O")), 1),
  c("[M+H-Gly-H2O]+", calculateMass("C2H5NO2"), 1),
  
  c("[M-H-NH3]-", calculateMass("NH3"), 0),
  c("[M-H-H2O]-", calculateMass("H2O"), 0),
  c("[M-H-CO2]-", calculateMass("CO2"), 0),
  c("[M-H-C2H2O]-", calculateMass("C2H2O"), 0),
  c("[M-H-C2H4O]-", calculateMass("C2H4O"), 0),
  c("[M-H-C2H4O2]-", calculateMass("C2H4O2"), 0),
  c("[M-H-CONH]-", calculateMass("CONH"), 0),
  c("[M-H-C3H9N]-", calculateMass("C3H9N"), 0)
))

ions <- data.frame(rbind(
  c("[Asp+H]+", mass2mz(calculateMass("C4H7NO4"), "[M+H]+"), 1),
  c("[Glu+H]+", mass2mz(calculateMass("C5H10N2O3"), "[M+H]+"), 1),
  c("[Glu+H-NH3]+", mass2mz(calculateMass(subtractElements("C5H10N2O3", "NH3")), "[M+H]+"), 1),
  c("[(i)Leu+H]+", mass2mz(calculateMass("C6H13NO2"), "[M+H]+"), 1),
  c("[(i)Leu+H-H2O-CO]+", mass2mz(calculateMass(subtractElements("C6H13NO2", "H2OCO")), "[M+H]+"), 1),
  c("[Phe+H]+", mass2mz(calculateMass("C9H11NO2"), "[M+H]+"), 1),
  c("[Phe+H-H2O-CO]+", mass2mz(calculateMass(subtractElements("C9H11NO2", "H2OCO")), "[M+H]+"), 1),
  c("[Pro+H]+", mass2mz(calculateMass("C5H9NO2"), "[M+H]+"), 1),
  c("[Tyr+H]+", mass2mz(calculateMass("C9H11NO3"), "[M+H]+"), 1),
  c("[Tyr+H-H2O-CO]+", mass2mz(calculateMass(subtractElements("C9H11NO3", "H2OCO")), "[M+H]+"), 1),
  c("[Val+H-H2O-CO]+", mass2mz(calculateMass(subtractElements("C5H11NO2", "H2OCO")), "[M+H]+"), 1),
  c("Flavonoid 1,3A+", mass2mz(calculateMass("C7H4O4"), "[M+H]+"), 1),
  
  c("[Ala-H]-", mass2mz(calculateMass("C3H7NO2"), "[M-H]-"), 0),
  c("[Glu-H-NH3]-", mass2mz(calculateMass(subtractElements("C5H10N2O3", "NH3")), "[M-H]-"), 0),
  c("[Leu-H]-", mass2mz(calculateMass("C6H13NO2"), "[M-H]-"), 0),
  c("[Phe-H]-", mass2mz(calculateMass("C9H11NO2"), "[M-H]-"), 0),
  c("[Phe-H-NH3]-", mass2mz(calculateMass(subtractElements("C9H11NO2", "NH3")), "[M-H]-"), 0),
  c("[Pro-H]-", mass2mz(calculateMass("C5H9NO2"), "[M-H]-"), 0),
  c("[Pro-H-H2O]-", mass2mz(calculateMass(subtractElements("C5H9NO2", "H2O")), "[M-H]-"), 0),
  c("[Tyr-H]-", mass2mz(calculateMass("C9H11NO3"), "[M-H]-"), 0),
  c("[Tyr-H-NH3]-", mass2mz(calculateMass(subtractElements("C9H11NO3", "NH3")), "[M-H]-"), 0),
  c("[Val-H]-", mass2mz(calculateMass("C5H11NO2"), "[M-H]-"), 0)
))

colnames(nl) <- colnames(ions) <- c("name", "mass", "polarity")
nl$mass <- as.numeric(nl$mass)
ions$mass <- as.numeric(ions$mass)
```

```{r x}
cdb <- CompDb("CompDb_inhouse_0.sqlite", flags = RSQLite::SQLITE_RW)
sps <- Spectra::Spectra(cdb)
cmps <- compounds(cdb, c("compound_id", "name", "formula", "exactmass"))

m <- unique(sps$compound_id)

i <- which(m == "M0047")
#for(i in seq(length(m))){
#  if(!any(grepl(m[i], list.files("molecule/")))){
rmarkdown::render("M0000.Rmd", 
                  params = list(metabolite = m[i]),
                  output_file = paste0("molecule/", m[i]))
#  }
#}
```

