-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:saezlab/cosmosR
- Loading branch information
Showing
17 changed files
with
854 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#' Format Ligand-Receptor Resource | ||
#' | ||
#' This function formats a ligand-receptor resource by creating a gene set | ||
#' with source-target pairs, converting it to a long format, and adding | ||
#' default values for 'mor' and 'likelihood'. | ||
#' | ||
#' @param ligrec_ressource A data frame representing the ligand-receptor resource with columns for source and target gene symbols. | ||
#' | ||
#' @return A data frame containing the formatted ligand-receptor gene set with columns: | ||
#' \item{gene}{The gene symbol from the ligand-receptor pairs.} | ||
#' \item{set}{The set identifier combining source and target gene symbols.} | ||
#' \item{mor}{Default value set to 1 for all entries.} | ||
#' \item{likelihood}{Default value set to 1 for all entries.} | ||
#' | ||
#' @examples | ||
#' # Create a sample ligand-receptor resource | ||
#' ligrec_ressource <- data.frame(source_genesymbol = c("L1", "L2"), | ||
#' target_genesymbol = c("R1", "R2")) | ||
#' | ||
#' # Format the ligand-receptor resource | ||
#' formatted_geneset <- format_LR_ressource(ligrec_ressource) | ||
#' | ||
#' @export | ||
format_LR_ressource <- function(ligrec_ressource) | ||
{ | ||
ligrec_geneset <- ligrec_ressource[,c("source_genesymbol","target_genesymbol")] | ||
ligrec_geneset$set <- paste(ligrec_geneset$source_genesymbol, ligrec_geneset$target_genesymbol, sep = "___") | ||
ligrec_geneset <- reshape2::melt(ligrec_geneset, id.vars = "set")[,c(3,1)] | ||
names(ligrec_geneset)[1] <- "gene" | ||
ligrec_geneset$mor <- 1 | ||
ligrec_geneset$likelihood <- 1 | ||
ligrec_geneset <- distinct(ligrec_geneset) | ||
|
||
return(ligrec_geneset) | ||
} | ||
|
||
#' Convert ULM Results to Wide Format | ||
#' | ||
#' This function converts the results from a ULM analysis to a wide format data frame. | ||
#' The input is a data frame with columns for source, condition, and score. The output | ||
#' is a data frame where each row represents a source and each column represents a condition, | ||
#' with the corresponding scores as values. | ||
#' | ||
#' @param ulm_result A data frame representing the ULM results with columns: source, condition, and score. | ||
#' | ||
#' @return A data frame in wide format where each row is a source and each column is a condition. | ||
#' | ||
#' @examples | ||
#' # Create a sample ULM result | ||
#' ulm_result <- data.frame(source = c("A", "A", "B", "B"), | ||
#' condition = c("cond1", "cond2", "cond1", "cond2"), | ||
#' score = c(0.5, 0.8, 0.3, 0.7)) | ||
#' | ||
#' # Convert to wide format | ||
#' wide_ulm_result <- wide_ulm_res(ulm_result) | ||
#' | ||
#' @export | ||
wide_ulm_res <- function(ulm_result) | ||
{ | ||
ulm_result_df <- reshape2::dcast(ulm_result, formula = source~condition, value.var = "score") | ||
row.names(ulm_result_df) <- ulm_result_df$source | ||
ulm_result_df <- ulm_result_df[,-1] | ||
|
||
return(ulm_result_df) | ||
} | ||
|
||
#' Translate Column Using HMDB Mapper | ||
#' | ||
#' This function translates the values in a column using a provided Human Metabolome Database (HMDB) mapper vector. | ||
#' It modifies the input values by replacing certain prefixes and suffixes according to specific rules. | ||
#' | ||
#' @param my_column A vector of values to be translated. | ||
#' @param HMDB_mapper_vec A named vector where the names are the original identifiers and the values are the corresponding HMDB identifiers. | ||
#' | ||
#' @return A vector with the translated values. | ||
#' | ||
#' @examples | ||
#' # Create a sample column and HMDB mapper vector | ||
#' my_column <- c("Metab__1234_a", "Gene5678_b", "Metab__91011_c") | ||
#' HMDB_mapper_vec <- c("1234" = "HMDB00001", "5678" = "HMDB00002", "91011" = "HMDB00003") | ||
#' | ||
#' # Translate the column | ||
#' translated_column <- translate_column_HMDB(my_column, HMDB_mapper_vec) | ||
#' | ||
#' @export | ||
translate_column_HMDB <- function(my_column, HMDB_mapper_vec) | ||
{ | ||
return(sapply(my_column, function(x, HMDB_mapper_vec) { | ||
x <- gsub("Metab__", "", x) | ||
x <- gsub("^Gene", "Enzyme", x) | ||
suffixe <- stringr::str_extract(x, "_[a-z]$") | ||
x <- gsub("_[a-z]$", "", x) | ||
if (x %in% names(HMDB_mapper_vec)) { | ||
x <- HMDB_mapper_vec[x] | ||
x <- paste("Metab__", x, sep = "") | ||
} | ||
if (!is.na(suffixe)) { | ||
x <- paste(x, suffixe, sep = "") | ||
} | ||
return(x) | ||
}, HMDB_mapper_vec = HMDB_mapper_vec)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#' Create Heatmap Color Palette | ||
#' | ||
#' This function generates a color palette suitable for heatmaps based on the values in a matrix. It uses the `createLinearColors` function to generate separate color gradients for positive and negative values. | ||
#' | ||
#' @param my_matrix A numeric matrix for which the heatmap color palette is to be generated. | ||
#' | ||
#' @return A character vector of colors representing the heatmap color palette based on the input matrix values. | ||
#' | ||
#' @examples | ||
#' # Create a sample matrix | ||
#' my_matrix <- matrix(c(-3, -1, 0, 1, 3), nrow = 1) | ||
#' | ||
#' # Generate heatmap color palette | ||
#' heatmap_palette <- make_heatmap_color_palette(my_matrix) | ||
#' | ||
#' @export | ||
make_heatmap_color_palette <- function(my_matrix) | ||
{ | ||
t <- as.vector(t(my_matrix)) | ||
palette1 <- createLinearColors(t[t < 0],withZero = F , maximum = abs(min(t,na.rm = T)) * 10) | ||
palette2 <- createLinearColors(t[t > 0],withZero = F , maximum = abs(max(t,na.rm = T)) * 10) | ||
palette <- c(palette1, palette2) | ||
} | ||
|
||
#' Create Linear Colors Based on Numeric Input | ||
#' | ||
#' This function generates a gradient of colors based on the provided numeric values. The colors can be adjusted to include zero and are configurable with a specified maximum and custom color palette. | ||
#' | ||
#' @param numbers A numeric vector for which the color gradient is to be generated. | ||
#' @param withZero A logical value indicating whether zero should be included in the color gradient. Default is TRUE. | ||
#' @param maximum An integer specifying the maximum number of colors to be generated in the gradient. Default is 100. | ||
#' @param my_colors A character vector of length three specifying the colors to be used in the gradient. Default is c("royalblue3", "white", "red"). | ||
#' | ||
#' @return A character vector of colors representing the gradient based on the input numeric values. | ||
#' | ||
#' @examples | ||
#' # Generate colors for a set of numbers including zero | ||
#' numbers <- c(-50, -20, 0, 20, 50) | ||
#' colors <- createLinearColors(numbers, withZero = TRUE, maximum = 100) | ||
#' | ||
#' @export | ||
createLinearColors <- function(numbers, withZero = T, maximum = 100, my_colors = c("royalblue3","white","red")) | ||
{ | ||
if (min(numbers, na.rm = T) > 0) | ||
{ | ||
if(withZero) | ||
{ | ||
numbers <- c(0,numbers) | ||
} | ||
myPalette <- colorRampPalette(my_colors[c(2,3)]) | ||
myColors <- myPalette(maximum) | ||
} | ||
else | ||
{ | ||
if (max(numbers, na.rm = T) < 0) | ||
{ | ||
if(withZero) | ||
{ | ||
numbers <- c(0,numbers) | ||
} | ||
myPalette <- colorRampPalette(my_colors[c(1,2)]) | ||
myColors <- myPalette(maximum) | ||
} | ||
else | ||
{ | ||
myPalette_pos <- colorRampPalette(c("white","red")) | ||
myPalette_neg <- colorRampPalette(c("royalblue3","white")) | ||
npos <- length(numbers[numbers >= 0]) + 1 | ||
nneg <- length(numbers[numbers <= 0]) + 1 | ||
|
||
myColors_pos <- myPalette_pos(npos) | ||
myColors_neg <- myPalette_neg(nneg) | ||
|
||
#print(myColors_neg) | ||
#print(myColors_pos) | ||
|
||
myColors <- c(myColors_neg[-(nneg)], myColors_pos[-1]) | ||
} | ||
} | ||
return(myColors) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.