Skip to content

Commit

Permalink
finally fixed sorting fn
Browse files Browse the repository at this point in the history
  • Loading branch information
jr-leary7 committed Apr 7, 2024
1 parent cde9bf4 commit f89d61e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions R/sortObservations.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @details
#' \itemize{
#' \item If the input is a matrix, it is assumed that the columns are cells - and are named as such - and the rows are genes.
#' \item If the input is a Seurat object, sorting requires converting to \code{SingleCellExperiment} object first, then ordering, then converting back toa \code{Seurat} object. Some information might be lost, so it is recommended not to overwrite your original \code{Seurat} object.
#' }
#' @export
#' @examples
Expand All @@ -27,17 +28,23 @@ sortObservations <- function(expr.mat = NULL,
if (is.null(expr.mat) || is.null(pt) || is.null(id.vec)) { stop("You forgot some inputs to sortObservations().") }
if (any(is.na(id.vec))) { stop("The subject ID vector must not contain any NA values.") }
# create table with subject ID and pseudotime
subj_df <- data.frame(ID = id.vec,
PT = pt.vec,
Cell = colnames(expr.mat))
subj_df <- data.frame(Cell = colnames(expr.mat),
ID = id.vec,
PT = pt.vec)
# arrange by subject ID then pseudotime
subj_df <- dplyr::arrange(subj_df,
ID,
PT)
# sort object by cells
expr.mat <- expr.mat[, subj_df$Cell]
if (!is.unsorted(colnames(expr.mat))) {
stop("Sorting has failed.")
if (inherits(expr.mat, "SingleCellExperiment") || inherits(expr.mat, "matrix") || inherits(expr.mat, "dgCMatrix")) {
expr.mat <- expr.mat[, subj_df$Cell]
} else if (inherits(expr.mat, "Seurat")) {
warning("Ordering a Seurat object requires conversion to SingleCellExperiment, and some information might be lost.")
sce <- Seurat::as.SingleCellExperiment(expr.mat)
sce <- sce[, subj_df$Cell]
expr.mat <- Seurat::as.Seurat(sce)
} else {
stop("Unrecognized input class.")
}
return(expr.mat)
}
1 change: 1 addition & 0 deletions man/sortObservations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f89d61e

Please sign in to comment.