#' @title Normalize signal by... #' #' @description This function helps compensate for differences in staining ("collected"), imaging parameters ("imaged"), or parents ("pair") #' @param data a tibble containing the standard metadata + individual fish as the columns and brain region names as the rows. #' #' If you use the output from tidyImportedData or tidyImportedDataUnderscore you should be all set. #' @param byColumn This is the column that the function will group and normalize by. Essentially, every fish that #' #' has the same value for that column will be averaged together, then all the signal values for each fish #' #' will be divided by the average overall signal for the group to which it belongs. #' @export #' @examples normalizeSignalBy <- function(data, by_column) { #by_column should be "collected" or "imaged" or "pair" #note: I tested this function against my original script output-- outputs were identical # I tested my original script output against an Excel spreadsheet where I did all the manipulations as I intended studyDesign <- data[, 1:7] dataOnly <- data[,8:length(data)] brainRegions <- colnames(dataOnly) numericDataOnly <- dataOnly %>% mutate_all(as.numeric) numericDataOnly$avgSig <- rowMeans(numericDataOnly) data2 <- cbind(studyDesign, numericDataOnly) if (by_column == "collected") { data2 <- data2 %>% group_by(collected) meanSigByGrouped <- summarise(data2, mean(avgSig)) obj <- left_join(data2, meanSigByGrouped, by = "collected") } else if (by_column == "imaged") { data2 <- data2 %>% group_by(imaged) meanSigByGrouped <- summarise(data2, mean(avgSig)) obj <- left_join(data2, meanSigByGrouped, by = "imaged") } else if (by_column == "pair") { data2 <- data2 %>% group_by(pair) meanSigByGrouped <- summarise(data2, mean(avgSig)) obj <- left_join(data2, meanSigByGrouped, by = "pair") } else { print("Please enter by_column = collected or imaged or pair (in quotes)") } obj2 <- obj[,8:length(obj)] obj3 <- obj2 %>% sapply(`/`, obj[,length(obj)]) obj3[is.na(obj3)] = 0 obj3 <- as_tibble(obj3) obj4 <- obj3[1:(length(obj3)-2)] colnames(obj4) <- brainRegions obj4 <- cbind(studyDesign,obj4) obj4 <- as_tibble(obj4) return(obj4) write_csv(obj4, "normalizedSignal_Test1_210518.csv") } #### testing the function #setwd("C:/Users/Joe/Documents/MATLAB/WholeBrainExpression") #original_raw_data <- read_csv("data/AllFullBrains_Together_210412.csv") #tidiedData <- tidyImportedDataUnderscore(original_raw_data) #studyDesign <- tidiedData[, 1:7] # dataOnly <- tidiedData[,8:length(tidiedData)] # # numericDataOnly <- dataOnly %>% # mutate_all(as.numeric) # # numericData <- cbind(studyDesign, numericDataOnly) # # result <- normalizeSignalBy(numericData, by_column = "collected") # # write_csv(result, "normalizedSignal_Test3Function_210518.csv")