## ---- additional functions
normSL <- function(tp, groupBy=NULL){
# suppressPackageStartupMessages({
# library(dplyr)
# library(data.table)
# })
# Create an expression that will be evaluated to dynamically
# group data based on user provided grouping factors. Default's
# to Sample -- every replicate.
tp <- ungroup(as.data.frame(tp))
cmd <- paste0("group_by(tp, ",paste(groupBy,collapse=", "),")")
# Calculate column sums for grouped samples.
# FIXME: if .groups is set to 'drop' to avoid Warning msg, then error?
data_list <- eval(parse(text=cmd)) %>%
summarize(Total=sum(Intensity,na.rm=TRUE),.groups="drop") %>%
group_split()
# Calculate normalization factors.
data_SL <- lapply(data_list,function(x) {
x$"Mean(Total)" <- mean(x$Total,na.rm=TRUE)
x$NormFactor <- x$"Mean(Total)"/x$Total
x$Norm <- x$Total*x$NormFactor
return(x) }) %>% bind_rows()
# Collect normalization factors as named vector.
SL_factors <- data_SL$NormFactor
names(SL_factors) <- as.character(data_SL[["Sample"]])
# Normalize measurements by sample factors.
tp$Intensity <- tp$Intensity * SL_factors[as.character(tp[["Sample"]])]
return(as.data.table(tp))
} #EOF
normSP <- function(tp, pool){
# perform normalization to pooled QC samples
# store a copy of the data
tp <- ungroup(tp)
tp_copy <- tp
# group pooled together
tp$Group <- as.numeric(grepl(paste(pool,collapse="|"),tp$Sample))
tp_list <- tp %>% group_by(Protein,Group) %>%
dplyr::summarize(Mean_Intensity=mean(Intensity,na.rm=TRUE),
n = length(Intensity), .groups="drop") %>%
as.data.table() %>%
arrange(Protein,Group) %>%
group_by(Protein) %>% group_split()
# loop to calculate normalization factors
new_list <- list()
for (i in 1:length(tp_list)){
x <- tp_list[[i]]
x$NormFactor <- c(x$Mean_Intensity[2]/x$Mean_Intensity[1],1)
x$Norm_Mean_Intensity <- x$Mean_Intensity * x$NormFactor
new_list[[i]] <- x
}
tp_list <- new_list
# collect in a df
df <- do.call(rbind,tp_list) %>%
dplyr::select(Protein,Group,NormFactor)
# merge with input data
tp_norm <- left_join(tp,df,by=c("Protein","Group"))
# Perform normalization step.
tp_norm$Intensity <- tp_norm$Intensity * tp_norm$NormFactor
tp_norm <- tp_norm %>% dplyr::select(colnames(tp_copy))
tp_norm <- as.data.table(tp_norm)
# return the normalized data
return(tp_norm)
} #EOF
normSP2 <- function(tp, pool){
# perform normalization to pooled QC samples
# store a copy of the data
tp <- ungroup(tp)
tp_copy <- tp
# group pooled together
tp$Group <- as.numeric(grepl(paste(pool,collapse="|"),tp$Condition))
tp_list <- tp %>% group_by(Protein,Group) %>%
dplyr::summarize(Mean_Intensity=mean(Intensity,na.rm=TRUE),
n = length(Intensity), .groups="drop") %>%
as.data.table() %>%
arrange(Protein,Group) %>%
group_by(Protein) %>% group_split()
# loop to calculate normalization factors
new_list <- list()
for (i in 1:length(tp_list)){
x <- tp_list[[i]]
x$NormFactor <- c(x$Mean_Intensity[2]/x$Mean_Intensity[1],1)
x$Norm_Mean_Intensity <- x$Mean_Intensity * x$NormFactor
new_list[[i]] <- x
}
tp_list <- new_list
# collect in a df
df <- do.call(rbind,tp_list) %>%
dplyr::select(Protein,Group,NormFactor)
# merge with input data
tp_norm <- left_join(tp,df,by=c("Protein","Group"))
# Perform normalization step.
tp_norm$Intensity <- tp_norm$Intensity * tp_norm$NormFactor
tp_norm <- tp_norm %>% dplyr::select(colnames(tp_copy))
tp_norm <- as.data.table(tp_norm)
# return the normalized data
return(tp_norm)
} #EOF
write_excel <- function(mydata, myfile, rowNames = FALSE, colNames = TRUE, ...) {
# imports
suppressPackageStartupMessages({
})
# check that input data is a list
if ("list" %in% class(mydata)) {
mylist <- mydata
} else {
# Coerce to list
mylist <- list(mydata)
}
# Insure there are names.
if (is.null(names(mylist))) {
names(mylist) <- paste("Sheet", c(1:length(mylist)))
}
wb <- openxlsx::createWorkbook()
# Loop to add a worksheets:
for (i in 1:length(mylist)) {
df <- as.data.frame(mylist[[i]])
openxlsx::addWorksheet(wb, sheetName = names(mylist[i]))
openxlsx::writeData(wb,
sheet = i, df,
rowNames = rowNames, colNames = colNames, ...
)
}
# Save workbook.
openxlsx::saveWorkbook(wb, myfile, overwrite = TRUE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.