-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug fixes and added heatmap sorting fn -- closes #109
- Loading branch information
Showing
6 changed files
with
90 additions
and
4 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
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,48 @@ | ||
#' Generate a table of fitted values and celltype metadata for genes of interest. | ||
#' | ||
#' @name sortGenesHeatmap | ||
#' @author Jack Leary | ||
#' @import magrittr | ||
#' @importFrom purrr map reduce | ||
#' @importFrom dplyr filter distinct select with_groups summarise arrange pull | ||
#' @description Sort genes such that genes with peak expression occurring earlier in pseudotime are first, and vice versa for genes with late peak expression. Useful for ordering genes in order to create heatmaps of expression cascades. | ||
#' @param heatmap.mat A matrix of raw or smoothed expression values with genes as columns and cells as rows. Defaults to NULL. | ||
#' @param pt.vec A numeric vector of pseudotime values for each cell i.e., for each row in the heatmap matrix. Defaults to NULL. | ||
#' @return A character vector of genes sorted by their peak expression values over pseudotime. | ||
#' @export | ||
#' @examples | ||
#' \dontrun{ | ||
#' smoothed_counts <- smoothedCountsMatrix(gene_stats, pt = pt_df) | ||
#' sortGenesheatmap(heatmap.mat = smoothed_counts$Lineage_A, | ||
#' pt.vec = pt_df[!is.na(pt_df$Lineage_A), ]$Lineage_A) | ||
#' } | ||
|
||
sortGenesHeatmap <- function(heatmap.mat = NULL, pt.vec = NULL) { | ||
# check inputs | ||
if (!inherits(heatmap.mat, "matrix")) { | ||
heatmap.mat <- try(as.matrix(heatmap.mat), silent = TRUE) | ||
if (inherits(heatmap.mat, "try-error")) { | ||
stop("heatmap.mat must be coerceable to a matrix.") | ||
} | ||
} | ||
if (!is.numeric(pt.vec) || any(is.na(pt.vec))) { stop("pt.vec must be a numeric vector with no NA values.") } | ||
|
||
# identify point at which peak expression occurs for each gene across pseudotime | ||
gene_peak_order <- purrr::map(seq(ncol(heatmap.mat)), \(x) { | ||
data.frame(gene = colnames(heatmap.mat)[x], | ||
pt = pt.vec, | ||
mRNA = heatmap.mat[, x]) %>% | ||
dplyr::filter(mRNA == max(mRNA)) %>% | ||
dplyr::distinct() %>% | ||
dplyr::select(gene, | ||
pt, | ||
mRNA) | ||
}) %>% | ||
purrr::reduce(rbind) %>% | ||
dplyr::with_groups(gene, | ||
dplyr::summarise, | ||
mu = mean(pt)) %>% | ||
dplyr::arrange(mu) %>% | ||
dplyr::pull(gene) | ||
return(gene_peak_order) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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