##' Identify the 12C peak for a compound and calculate its percentage vs all ions
##'
##' @param raw_m Raw data in melted form
##' @export
calc_12C <- function(raw_m, id.cols = vector(0), ion.col = ion.count, ...) {
id.cols <- quos(...) # NOTE THAT THIS IGNORES THE id.cols THAT ARE SET AS TRADITIONAL ARGUMENTS
ion.col.quo <- enquo(ion.col)
if(length(id.cols==0)) {
id.cols = quos(sample, compound, treatment, replicate, sample.type, time) # Note that the default id.cols values are basically a placeholder -
} # ultimately they should be autodetected or specified at the time of import by the user
#p <- ggplot(mtcars, aes(x=mpg, y=cyl)) + geom_point()
#print(p)
# Add a column for the sum of all ion counts for that compound
### Damn that's slow - shold rewrite in dplyr
# raw_ion_sum <- plyr::ddply(raw_m, c("sample", "compound", "treatment", "replicate", "sample.type", "time"), mutate,
# sum.ion.count=sum(ion.count, na.rm=TRUE),
# is.12C = medMz==min(medMz),
# relative.ion.count = ion.count/sum.ion.count)
browser() # read http://dplyr.tidyverse.org/articles/programming.html
raw_ion_sum <- raw_m %>%
#group_by(sample, compound, treatment, replicate, sample.type, time) %>% # THESE ARE HARD-CODED, NEED NOT TO BE
dplyr::group_by(!!!id.cols) %>%
dplyr::mutate(sum.ion.count = sum(!!ion.col.quo, na.rm=TRUE),
is.base.ion = (medMX == min(medMz)), # THESE ARE HARD-CODED, NEED NOT TO BE
relative.ion.count = ion.count / sum.ion.count) # THESE ARE HARD-CODED, NEED NOT TO BE
warning("must fix that column names are hard-coded")
# message("* This algorithm currently identifies 12C peaks as the minimum medMz value for each unique combination of sample and compound. I want to confirm with the chemists that is correct.")
# message("* This calculates relative ion count (i.e., %12C) as the ion count for the 12C divided by the ion count for all isotopomers IN A SINGLE SAMPLE. I'm not sure that is correct")
raw_ion_sum
# # CHeck to see if there's exactly one 12C identified
# c12_check <- ddply(raw_ion_sum, c("compoundId", "sample"), summarise,
# good_cpd=sum(is.12C==1))
# sum(c12_check$good_cpd)==nrow(c12_check) #TRUE! Seems to be working
#
# # Figure out if I'm doing this right
# single_mol <- subset(raw_ion_sum, sample=="X24_154" & compound=="ATP")
#
# isotopomers <- ddply(raw_m, c("compound", "sample"), summarise,
# count=length(ion.count))
#
# subset(isotopomers, count==13) # just glutathione and UDP-D-glucuronate
# subset(raw_m, compound=="glutathione" & sample=="X24_154")
#
# ggplot(single_mol, x=sample, y=relative.ion.count) +
# geom_rug()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.