#'@title Export Strongest Correlated Regions #' #' @description This function identifies a user-specified number of regions where expression correlates most #' strongly and positively to a user-specified brain region. #' If there are any brain regions where expression correlates negatively with expression in that region, this #' function also finds and ranks those in a separate column. #' This function then exports a .csv file that can be fed into the Matlab function CustomBrainRegionStack #' so that the anatomical organization of these brain regions can be visualized in 3D. Regions with positive #' scores will be colored green while regions with negative scores will be purple. #' This package requres you to have the janitor, readxl, and tidyverse packages loaded. #' @param corr_object This is the unmodified output of your cor() function. #' @param brain_region This is the brain region you want to examine. Be sure to enter the region name exactly #' as it appears in your correlation object. For example, type out rhombencephalon_qrfp_neuron_cluster_sparse. #' @param num2show This is the max number of regions in EACH color you want displayed on the graph. You may #' get fewer (for example if there are no regions negatively correlated with your region of choice) #' @param filtered this is whether you filtered out brain regions due to alignment problems. Enter "none" if #' you filtered out no regions (your corr matrix is 293 x 293). Enter "spinalcord" if you filtered #' out the spinal cord (your corr matrix is 275 x 275). Enter "rhomb7" if you filtered out everything caudal #' to rhombomere 7 (your corr matrix is 254 x 254). #' @export #' @examples exportStrongestCorrelatedRegions <- function(corr_object, brain_region, num2show, filtered) { num2show = as.numeric(num2show) # importing correct list of brain regions if (filtered == "rhomb7") { regions = read_xlsx("rhomb7_filtered_regions_underscores.xlsx", col_names = c("ROIname")) } else if (filtered == "spinalcord") { regions = read_xlsx("spinalcord_filtered_regions_underscores.xlsx", col_names = c("ROIname")) } else if (filtered == "none") { regions = read_xlsx("unfiltered_regions_underscores.xlsx", col_names = c("ROIname")) } else { print("For the 'filtered' parameter, please enter 'none' or 'spinalcord' or 'rhomb7'.") } corr_wholebrain_tib <- as_tibble(corr_object) myROI_corr <- corr_wholebrain_tib %>% select(brain_region) myROI_corr_regions <- cbind(regions, myROI_corr) colnames(myROI_corr_regions) <- c("ROIname","corr") myROI_corr_ranked <- myROI_corr_regions %>% arrange(desc(corr)) colnames(myROI_corr_ranked) <- c("ROIname","corr") top_ranked_regions <- slice_head(myROI_corr_ranked, n = (num2show+1)) %>% #num2show +1 because top region will be the ROI itself filter(corr< .999999999) #filter out the brain region itself, which will have a correlation of 1.0 bottom_ranked_regions <- slice_tail(myROI_corr_ranked, n = num2show) bottom_regions_filtered <- bottom_ranked_regions %>% # only include bottom regions if their scores are actually <0 (anticorrelated) filter(corr < 0) %>% mutate(corr = abs(corr)) #...but Matlab needs positive numbers to graph brain_reg_columnar <- read_csv("293_brain_regions_formatted.csv") # THIS ONLY IS COMPATIBLE WITH THE MATLAB PROGRAM IF YOU USE THE FULL 293 BRAIN REGIONS colnames(brain_reg_columnar) <- "ROIname" posJoin <- left_join(brain_reg_columnar, top_ranked_regions) # fills in correlations at corresponding brain regions posJoin[is.na(posJoin)] = 0 # replaces NAs with 0s colnames(posJoin) <- c("ROIname","PosCorr") negJoin <- left_join(brain_reg_columnar, bottom_regions_filtered, by = "ROIname") # fills in scores at corresponding brain regions negJoin[is.na(negJoin)] = 0 # replaces NAs with 0s colnames(negJoin) <- c("ROIname","NegCorr") posJoin$NegCorr <- negJoin$NegCorr #adds negative value column to main object correlations <- posJoin %>% select(PosCorr,NegCorr) # gets rid of ROI names because they make Matlab unhappy reg_name <- as.character(brain_region) filename <- paste(reg_name, "Corr.csv", sep = "") #descriptive filename write_csv(correlations, filename) #write csv all_ranked_regions <- rbind(top_ranked_regions, bottom_ranked_regions) return(all_ranked_regions) }