Commit b1f58e00 authored by Jun Zhao's avatar Jun Zhao
Browse files

subset input based on labels

parent 01757fd3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -61,7 +61,8 @@ SeuratMarkerFinder <- function(
addDAslot <- function(object, da.regions, da.slot = "da", set.ident = F){
  seurat.version <- substr(packageVersion("Seurat"),1,1)
  if(seurat.version == "3" | seurat.version == "4"){
    object@meta.data[,da.slot] <- da.regions$da.region.label
    object@meta.data[,da.slot] <- NA
    object@meta.data[,da.slot][da.regions$cell.idx] <- da.regions$da.region.label
    if(set.ident){
      Idents(object) <- object@meta.data[,da.slot]
    }
+12 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#'
#' @return a list of results
#' \describe{
#'   \item{cell.idx}{index of cells used in DA calculation}
#'   \item{da.ratio}{score vector for each cell}
#'   \item{da.pred}{(mean) prediction from the logistic regression}
#'   \item{da.up}{index for DA cells more abundant in condition of labels.2}
@@ -53,8 +54,15 @@ getDAcells <- function(
     !inherits(labels.1, "character") | !inherits(labels.2, "character")){
    stop("Input parameters cell.labels, labels.1 and labels.2 must be character")
  }

  # subset input data if just using a subset of input cells
  if(length(setdiff(cell.labels, c(labels.1, labels.2))) > 0){
    stop("Input parameter cell.labels contain labels not from labels.1 or labels.2")
    warning("Input parameter cell.labels contain labels not from labels.1 or labels.2, subsetting...")
    cell.idx <- which(cell.labels %in% c(labels.1, labels.2))
    X <- X[cell.idx,]
    cell.labels <- cell.labels[cell.idx]
  } else {
    cell.idx <- seq_len(length(cell.labels))
  }
  n.cells <- length(cell.labels)

@@ -168,6 +176,7 @@ getDAcells <- function(
    X.pred.plot <- NULL
    X.da.cells.plot <- NULL
  } else if(do.plot & !is.null(plot.embedding)){
    plot.embedding <- plot.embedding[cell.idx,]
    X.pred.plot <- plotCellScore(
      X = plot.embedding, score = X.pred, size = size
    ) + theme(legend.title = element_blank())
@@ -201,6 +210,7 @@ getDAcells <- function(
    ))
  } else {
    return(list(
      cell.idx = cell.idx,
      da.ratio = X.knn.ratio,
      da.pred = X.pred,
      rand.pred = X.random.pred.list,
@@ -306,6 +316,7 @@ updateDAcells <- function(
    X.pred.plot <- NULL
    X.da.cells.plot <- NULL
  } else if(do.plot & !is.null(plot.embedding)){
    plot.embedding <- plot.embedding[X$cell.idx,]
    X.pred.plot <- plotCellScore(
      X = plot.embedding, score = X.pred, size = size
    ) + theme(legend.title = element_blank())
+17 −6
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#'
#' @return a list of results
#' \describe{
#'   \item{cell.idx}{index of cells used in DA calculation}
#'   \item{da.region.label}{DA region label for each cell from the whole dataset,
#'   '0' represents non-DA cells.}
#'   \item{DA.stat}{a table showing DA score and p-value for each DA region}
@@ -43,24 +44,32 @@ getDAregion <- function(
    cat("Turning X to a matrix.\n")
    X <- as.matrix(X)
  }
  n.cells <- nrow(X)
  n.dims <- ncol(X)
  if(is.null(rownames(X))){
    rownames(X) <- paste("C",c(1:n.cells), sep = "")
  }

  # check label input
  if(!inherits(cell.labels, "character") |
     !inherits(labels.1, "character") | !inherits(labels.2, "character")){
    stop("Input parameters cell.labels, labels.1 and labels.2 must be character")
  }
  if(length(setdiff(cell.labels, c(labels.1, labels.2))) > 0){
    stop("Input parameter cell.labels contain labels not from labels.1 or labels.2")
    warning("Input parameter cell.labels contain labels not from labels.1 or labels.2, subsetting...")
    cell.idx <- which(cell.labels %in% c(labels.1, labels.2))
    X <- X[cell.idx,]
    cell.labels <- cell.labels[cell.idx]
  } else {
    cell.idx <- seq_len(length(cell.labels))
  }

  if(is.null(min.cell)){
    min.cell <- as.integer(colnames(da.cells$da.ratio)[1])
    cat("Using min.cell = ", min.cell, "\n", sep = "")
  }

  n.cells <- nrow(X)
  n.dims <- ncol(X)
  if(is.null(rownames(X))){
    rownames(X) <- paste("C",c(1:n.cells), sep = "")
  }

  seurat.version <- substr(packageVersion("Seurat"),1,1)
  if(seurat.version == "3" | seurat.version == "4"){
    X.S <- CreateSeuratObject(counts = t(X))
@@ -146,6 +155,7 @@ getDAregion <- function(
    warning("plot.embedding must be provided by user if do.plot = T")
    X.region.plot <- NULL
  } else if(do.plot & !is.null(plot.embedding)){
    plot.embedding <- plot.embedding[cell.idx,]
    X.da.label <- da.region.label
    X.da.order <- order(X.da.label, decreasing = F)
    X.region.plot <- plotCellLabel(
@@ -157,6 +167,7 @@ getDAregion <- function(
  }

  return(list(
    cell.idx = cell.idx,
    da.region.label = da.region.label,
    DA.stat = X.da.stat,
    da.region.plot = X.region.plot
+1 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ default True}
\value{
a list of results
\describe{
  \item{cell.idx}{index of cells used in DA calculation}
  \item{da.ratio}{score vector for each cell}
  \item{da.pred}{(mean) prediction from the logistic regression}
  \item{da.up}{index for DA cells more abundant in condition of labels.2}
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ getDAregion(X, da.cells, cell.labels, labels.1, labels.2,
\value{
a list of results
\describe{
  \item{cell.idx}{index of cells used in DA calculation}
  \item{da.region.label}{DA region label for each cell from the whole dataset,
  '0' represents non-DA cells.}
  \item{DA.stat}{a table showing DA score and p-value for each DA region}