From b03527c0565cb4da474e355c97b94d92ebed593e Mon Sep 17 00:00:00 2001 From: Junchen Yang Date: Tue, 16 Jul 2024 21:50:15 -0400 Subject: [PATCH 1/4] fix Seurat version check bug --- R/RunCellToCell.R | 2 +- R/RunCellToCellSpatial.R | 2 +- R/RunCellToNeighborhood.R | 2 +- R/RunCellToSystem.R | 2 +- R/RunNeighborhoodToCell.R | 2 +- R/RunSystemToCell.R | 2 +- R/utils.R | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/RunCellToCell.R b/R/RunCellToCell.R index 29b1478..91c6fe4 100644 --- a/R/RunCellToCell.R +++ b/R/RunCellToCell.R @@ -109,7 +109,7 @@ RunCellToCell <- function(sys.small, #Use this matrix to create a Seurat object: demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'CellToCell') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot - if(SeuratObject::Version(demo) >= 5){ + if(SeuratObject::Version(demo) >= "5.0.0"){ demo <- NormalizeData(demo,assay = "CellToCell") # Seura Object need to be >= 5.0.1 demo@assays$CellToCell@layers$data <- demo@assays$CellToCell@layers$counts # Seura Object need to be>= 5.0.1 diff --git a/R/RunCellToCellSpatial.R b/R/RunCellToCellSpatial.R index 9230ddd..88d7844 100644 --- a/R/RunCellToCellSpatial.R +++ b/R/RunCellToCellSpatial.R @@ -62,7 +62,7 @@ RunCellToCellSpatial <- function(sys.small, demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'CellToCellSpatial') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot - if(SeuratObject::Version(demo) >= 5){ + if(SeuratObject::Version(demo) >= "5.0.0"){ demo <- NormalizeData(demo,assay = "CellToCellSpatial") # Seura Object need to be >= 5.0.1 demo@assays$CellToCellSpatial@layers$data <- demo@assays$CellToCellSpatial@layers$counts # Seura Object need to be >= 5.0.1 diff --git a/R/RunCellToNeighborhood.R b/R/RunCellToNeighborhood.R index 9779572..2b5a76b 100644 --- a/R/RunCellToNeighborhood.R +++ b/R/RunCellToNeighborhood.R @@ -68,7 +68,7 @@ RunCellToNeighborhood <- function(sys.small, # Use this matrix to create a Seurat object: demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'CellToNeighborhood') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot - if(SeuratObject::Version(demo) >= 5){ + if(SeuratObject::Version(demo) >= "5.0.0"){ demo <- NormalizeData(demo,assay = "CellToNeighborhood") # Seura Object need to be >= 5.0.1 demo@assays$CellToNeighborhood@layers$data <- demo@assays$CellToNeighborhood@layers$counts # Seura Object need to be >= 5.0.1 diff --git a/R/RunCellToSystem.R b/R/RunCellToSystem.R index 3ca87c8..5b49ac7 100644 --- a/R/RunCellToSystem.R +++ b/R/RunCellToSystem.R @@ -83,7 +83,7 @@ RunCellToSystem <- function(sys.small, # Use this matrix to create a Seurat object: demo <- Seurat::CreateSeuratObject(counts = as.matrix(sc.connectome),assay = 'CellToSystem') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot - if(SeuratObject::Version(demo) >= 5){ + if(SeuratObject::Version(demo) >= "5.0.0"){ demo <- NormalizeData(demo,assay = "CellToSystem") # Seura Object need to be >= 5.0.1 demo@assays$CellToSystem@layers$data <- demo@assays$CellToSystem@layers$counts # Seura Object need to be >= 5.0.1 diff --git a/R/RunNeighborhoodToCell.R b/R/RunNeighborhoodToCell.R index 183198b..d082ed7 100644 --- a/R/RunNeighborhoodToCell.R +++ b/R/RunNeighborhoodToCell.R @@ -68,7 +68,7 @@ RunNeighborhoodToCell <- function(sys.small, # Use this matrix to create a Seurat object: demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'NeighborhoodToCell') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot - if(SeuratObject::Version(demo) >= 5){ + if(SeuratObject::Version(demo) >= "5.0.0"){ demo <- NormalizeData(demo,assay = "NeighborhoodToCell") # Seura Object need to be >= 5.0.1 demo@assays$NeighborhoodToCell@layers$data <- demo@assays$NeighborhoodToCell@layers$counts # Seura Object need to be >= 5.0.1 diff --git a/R/RunSystemToCell.R b/R/RunSystemToCell.R index e69abab..bdf347b 100644 --- a/R/RunSystemToCell.R +++ b/R/RunSystemToCell.R @@ -81,7 +81,7 @@ RunSystemToCell <- function(sys.small, #Use this matrix to create a Seurat object: demo <- Seurat::CreateSeuratObject(counts = as.matrix(sc.connectome),assay = 'SystemToCell') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot - if(SeuratObject::Version(demo) >= 5){ + if(SeuratObject::Version(demo) >= "5.0.0"){ demo <- NormalizeData(demo,assay = "SystemToCell") # Seurat Object need to be >= 5.0.1 demo@assays$SystemToCell@layers$data <- demo@assays$SystemToCell@layers$counts # Seurat Object need to be >= 5.0.1 diff --git a/R/utils.R b/R/utils.R index 11b3b02..63d9d78 100644 --- a/R/utils.R +++ b/R/utils.R @@ -7,7 +7,7 @@ #' @return the specified assay data #' @export getSeuratAssay <- function(object,assay,slot){ - if(SeuratObject::Version(object) >= 5) return(Seurat::GetAssayData(object,assay=assay,layer=slot)) + if(SeuratObject::Version(object) >= "5.0.0") return(Seurat::GetAssayData(object,assay=assay,layer=slot)) else return(Seurat::GetAssayData(object,assay=assay,slot=slot)) } From 177ab83dd6e453e01c7e9749596f092acf71b864 Mon Sep 17 00:00:00 2001 From: Junchen Yang Date: Tue, 16 Jul 2024 22:50:08 -0400 Subject: [PATCH 2/4] prevent potential bugs of AddMetaData in RunNeigh2Cell and RunCell2Neigh --- R/RunCellToNeighborhood.R | 10 +++++----- R/RunNeighborhoodToCell.R | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/R/RunCellToNeighborhood.R b/R/RunCellToNeighborhood.R index 2b5a76b..b0930bb 100644 --- a/R/RunCellToNeighborhood.R +++ b/R/RunCellToNeighborhood.R @@ -63,7 +63,7 @@ RunCellToNeighborhood <- function(sys.small, # Label columns properly barcodes <- colnames(scc) - colnames(scc) <- paste(colnames(scc),'Neighborhood',sep = '—') + colnames(scc) <- paste(barcodes,'Neighborhood',sep = '—') # Use this matrix to create a Seurat object: demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'CellToNeighborhood') @@ -75,12 +75,12 @@ RunCellToNeighborhood <- function(sys.small, } # Add metadata based on ident slot - demo <- Seurat::AddMetaData(demo,metadata = barcodes,col.name = 'SendingCell') # bug fix: add the Neighborhood - prefix - sending_type.meta <- data.frame(Seurat::Idents(sys.small)[barcodes]) - rownames(sending_type.meta) <- paste(rownames(sending_type.meta),"Neighborhood",sep = '—') + sending_type.meta <- data.frame(SendingCell = barcodes, + SendingType = Seurat::Idents(sys.small)[barcodes], + row.names = paste(barcodes,"Neighborhood",sep = '—')) - demo <- Seurat::AddMetaData(demo,metadata = sending_type.meta,col.name = 'SendingType') + demo <- Seurat::AddMetaData(demo,metadata = sending_type.meta,col.name = c("SendingCell","SendingType")) # Gather and assemble additional metadata if (!is.null(meta.data.to.map)){ diff --git a/R/RunNeighborhoodToCell.R b/R/RunNeighborhoodToCell.R index d082ed7..2e70f1b 100644 --- a/R/RunNeighborhoodToCell.R +++ b/R/RunNeighborhoodToCell.R @@ -63,7 +63,7 @@ RunNeighborhoodToCell <- function(sys.small, # Label columns properly barcodes <- colnames(scc) - colnames(scc) <- paste('Neighborhood',colnames(scc),sep = '—') + colnames(scc) <- paste('Neighborhood',barcodes,sep = '—') # Use this matrix to create a Seurat object: demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'NeighborhoodToCell') @@ -75,12 +75,12 @@ RunNeighborhoodToCell <- function(sys.small, } # Add metadata based on ident slot - demo <- Seurat::AddMetaData(demo,metadata = barcodes,col.name = 'ReceivingCell') # bug fix: add the Neighborhood - prefix - receiving_type.meta <- data.frame(Seurat::Idents(sys.small)[barcodes]) - rownames(receiving_type.meta) <- paste("Neighborhood",rownames(receiving_type.meta),sep = '—') + receiving_type.meta <- data.frame(ReceivingCell = barcodes, + ReceivingType = Seurat::Idents(sys.small)[barcodes], + row.names = paste("Neighborhood",barcodes,sep = '—')) - demo <- Seurat::AddMetaData(demo,metadata = receiving_type.meta,col.name = 'ReceivingType') + demo <- Seurat::AddMetaData(demo,metadata = receiving_type.meta,col.name = c("ReceivingCell","ReceivingType")) # Gather and assemble additional metadata if (!is.null(meta.data.to.map)){ From cfb75dc5bb7f197cd3da691a38e9717195847344 Mon Sep 17 00:00:00 2001 From: Junchen Yang Date: Tue, 16 Jul 2024 23:37:13 -0400 Subject: [PATCH 3/4] replace sys.small with filtered.obj when referencing a seurat obj internally --- R/RunCellToCell.R | 30 +++++++++++++++--------------- R/RunCellToCellSpatial.R | 36 ++++++++++++++++++------------------ R/RunCellToNeighborhood.R | 30 +++++++++++++++--------------- R/RunCellToSystem.R | 26 +++++++++++++------------- R/RunNICHES.R | 18 +++++++++--------- R/RunNeighborhoodToCell.R | 30 +++++++++++++++--------------- R/RunSystemToCell.R | 26 +++++++++++++------------- R/utils.R | 26 +++++++++++++------------- 8 files changed, 111 insertions(+), 111 deletions(-) diff --git a/R/RunCellToCell.R b/R/RunCellToCell.R index 91c6fe4..bfdf07e 100644 --- a/R/RunCellToCell.R +++ b/R/RunCellToCell.R @@ -5,8 +5,8 @@ #' The default assay of this object is called "CellToCell" to distinguish it from normal Seurat objects. #' Meta.data slots by default contain "SendingType" "ReceivingType" and "VectorType" information. #' -#' @param sys.small A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. -#' @param ground.truth Ground truth signaling mechanisms present in sys.small. +#' @param filtered.obj A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. +#' @param ground.truth Ground truth signaling mechanisms present in filtered.obj. #' @param assay The assay to run the SCC transformation on. Defaults to "RNA." #' @param meta.data.to.map A character vector of metadata names present in the original object which will be carried to the SCC objects #' @param output_format string. Choice of the output format. "seurat" will output a list of seurat objects, "raw" will output a list of lists with raw interaction matrix and compiled metadata @@ -16,7 +16,7 @@ #' @export -RunCellToCell <- function(sys.small, +RunCellToCell <- function(filtered.obj, ground.truth, assay, meta.data.to.map, @@ -25,13 +25,13 @@ RunCellToCell <- function(sys.small, ### CREATE MAPPING ### - # jc: Identify celltypes:names(table(Idents(sys.small))). Better to run check_celltypes, but harder to check - celltypes <- return_celltypes(sys.small) + # jc: Identify celltypes:names(table(Idents(filtered.obj))). Better to run check_celltypes, but harder to check + celltypes <- return_celltypes(filtered.obj) # Ligand dataset (listwise, for each celltype) lig.list <- list() for (i in 1:length(celltypes)){ - temp <- subset(sys.small,idents = celltypes[i]) + temp <- subset(filtered.obj,idents = celltypes[i]) subunit.list <- list() # Builds sending (ligand) data for any number of ligand subunits for (s in 1:ncol(ground.truth$source.subunits)){ #For each subunit column... subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(temp)) #initialize a mechanism x barcode matrix of all NAs @@ -47,7 +47,7 @@ RunCellToCell <- function(sys.small, # Receptor dataset (listwise, for each celltype) rec.list <- list() for (i in 1:length(celltypes)){ - temp <- subset(sys.small,idents = celltypes[i]) + temp <- subset(filtered.obj,idents = celltypes[i]) subunit.list <- list() # Builds receiving (receptor) data for any number of receptor subunits for (t in 1:ncol(ground.truth$target.subunits)){ subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(temp)) #initialize a mechanism x barcode matrix of all NAs @@ -72,7 +72,7 @@ RunCellToCell <- function(sys.small, for (i in 1:length(celltypes)){ # Define maximum number of comparisons for each pairing - num <- as.data.frame(table(Seurat::Idents(sys.small))) + num <- as.data.frame(table(Seurat::Idents(filtered.obj))) num$sender.freq <- ncol(lig.list[[i]]) rownames(num) <- num$Var1 num <- num[,-1] @@ -98,8 +98,8 @@ RunCellToCell <- function(sys.small, rownames(scc.data[[i]]) <- paste(rownames(lig.data[[i]]),rownames(rec.data[[i]]),sep = '—') colnames(scc.data[[i]]) <- paste(colnames(lig.data[[i]]),colnames(rec.data[[i]]),sep = '—') - sending.cell.idents[[i]] <- as.character(Seurat::Idents(sys.small)[colnames(lig.data[[i]])]) - receiving.cell.idents[[i]] <- as.character(Seurat::Idents(sys.small)[colnames(rec.data[[i]])]) + sending.cell.idents[[i]] <- as.character(Seurat::Idents(filtered.obj)[colnames(lig.data[[i]])]) + receiving.cell.idents[[i]] <- as.character(Seurat::Idents(filtered.obj)[colnames(rec.data[[i]])]) } @@ -110,7 +110,7 @@ RunCellToCell <- function(sys.small, demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'CellToCell') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot if(SeuratObject::Version(demo) >= "5.0.0"){ - demo <- NormalizeData(demo,assay = "CellToCell") # Seura Object need to be >= 5.0.1 + demo <- Seurat::NormalizeData(demo,assay = "CellToCell") # Seura Object need to be >= 5.0.1 demo@assays$CellToCell@layers$data <- demo@assays$CellToCell@layers$counts # Seura Object need to be>= 5.0.1 } @@ -133,9 +133,9 @@ RunCellToCell <- function(sys.small, # Identify sending and receiving barcodes sending.barcodes <- colnames(do.call(cbind,lig.data)) #This can be simplified if the above SCC construction is simplified receiving.barcodes <- colnames(do.call(cbind,rec.data)) #This can be simplified if the above SCC construction is simplified - # Pull and format sending and receiving metadata jc: possible bug, change object to sys.small - sending.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) - receiving.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) + # Pull and format sending and receiving metadata jc: possible bug, change object to filtered.obj + sending.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) + receiving.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) # Make joint metadata datArray <- abind::abind(sending.metadata,receiving.metadata,along=3) joint.metadata <- as.matrix(apply(datArray,1:2,function(x)paste(x[1],"-",x[2]))) @@ -154,7 +154,7 @@ RunCellToCell <- function(sys.small, Seurat::Idents(demo) <- demo$VectorType # How many vectors were captured by this sampling? - message(paste("\n",ncol(demo),'Cell-To-Cell edges computed, sampling',length(unique(demo$VectorType)),'distinct VectorTypes, out of',length(table(Seurat::Idents(sys.small)))^2,'total possible')) + message(paste("\n",ncol(demo),'Cell-To-Cell edges computed, sampling',length(unique(demo$VectorType)),'distinct VectorTypes, out of',length(table(Seurat::Idents(filtered.obj)))^2,'total possible')) if(output_format == "seurat") return(demo) else{ diff --git a/R/RunCellToCellSpatial.R b/R/RunCellToCellSpatial.R index 88d7844..f61601b 100644 --- a/R/RunCellToCellSpatial.R +++ b/R/RunCellToCellSpatial.R @@ -1,7 +1,7 @@ #' RunCellToCellSpatial #' -#' @param sys.small A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. -#' @param ground.truth Ground truth signaling mechanisms present in sys.small. +#' @param filtered.obj A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. +#' @param ground.truth Ground truth signaling mechanisms present in filtered.obj. #' @param assay The assay to run the SCC transformation on. Defaults to "RNA." #' @param meta.data.to.map A character vector of metadata names present in the original object which will be carried to the NICHES objects #' @param edgelist data.frame. Each row is an directional edge between two spatially connected cells @@ -11,7 +11,7 @@ #' @importFrom dplyr %>% #' @export -RunCellToCellSpatial <- function(sys.small, +RunCellToCellSpatial <- function(filtered.obj, ground.truth, assay, meta.data.to.map, @@ -22,30 +22,30 @@ RunCellToCellSpatial <- function(sys.small, # Make ligand matrix - #lig.data <- sys.small@assays[[assay]]@data[ligands,edgelist$from] + #lig.data <- filtered.obj@assays[[assay]]@data[ligands,edgelist$from] subunit.list <- list() # Builds sending (ligand) data for any number of ligand subunits for (s in 1:ncol(ground.truth$source.subunits)){ #For each subunit column... - subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(getSeuratAssay(sys.small,assay,"data")[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[s]]) <- colnames(getSeuratAssay(sys.small,assay,"data")[,edgelist$from]) + subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[s]]) <- colnames(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$from]) rownames(subunit.list[[s]]) <- rownames(ground.truth$source.subunits) non.na.indices <- !is.na(ground.truth$source.subunits[,s]) #Identify rows in the s-th column of the ground truth which are not NA - subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$source.subunits[non.na.indices,s],edgelist$from]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$source.subunits[non.na.indices,s],edgelist$from]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } lig.data <- Reduce('*',subunit.list) rm(subunit.list) # Make receptor matrix - #rec.data <- sys.small@assays[[assay]]@data[receptors,edgelist$to] + #rec.data <- filtered.obj@assays[[assay]]@data[receptors,edgelist$to] subunit.list <- list() # Builds receiving (receptor) data for any number of receptor subunits for (t in 1:ncol(ground.truth$target.subunits)){ - subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(getSeuratAssay(sys.small,assay,"data")[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[t]]) <- colnames(getSeuratAssay(sys.small,assay,"data")[,edgelist$to]) + subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[t]]) <- colnames(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$to]) rownames(subunit.list[[t]]) <- rownames(ground.truth$target.subunits) non.na.indices <- !is.na(ground.truth$target.subunits[,t]) #Identify rows in the t-th column of the ground truth which are not NA - subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$target.subunits[non.na.indices,t],edgelist$to]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$target.subunits[non.na.indices,t],edgelist$to]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } rec.data <- Reduce('*',subunit.list) rm(subunit.list) @@ -54,8 +54,8 @@ RunCellToCellSpatial <- function(sys.small, scc <- lig.data*rec.data rownames(scc) <- paste(rownames(lig.data),rownames(rec.data),sep = '—') colnames(scc) <- paste(colnames(lig.data),colnames(rec.data),sep = '—') - sending.cell.idents <- as.character(Seurat::Idents(sys.small)[colnames(lig.data)]) - receiving.cell.idents <- as.character(Seurat::Idents(sys.small)[colnames(rec.data)]) + sending.cell.idents <- as.character(Seurat::Idents(filtered.obj)[colnames(lig.data)]) + receiving.cell.idents <- as.character(Seurat::Idents(filtered.obj)[colnames(rec.data)]) dim(scc) # Use this matrix to create a Seurat object: @@ -63,7 +63,7 @@ RunCellToCellSpatial <- function(sys.small, # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot if(SeuratObject::Version(demo) >= "5.0.0"){ - demo <- NormalizeData(demo,assay = "CellToCellSpatial") # Seura Object need to be >= 5.0.1 + demo <- Seurat::NormalizeData(demo,assay = "CellToCellSpatial") # Seura Object need to be >= 5.0.1 demo@assays$CellToCellSpatial@layers$data <- demo@assays$CellToCellSpatial@layers$counts # Seura Object need to be >= 5.0.1 } @@ -87,9 +87,9 @@ RunCellToCellSpatial <- function(sys.small, sending.barcodes <- colnames(lig.data) receiving.barcodes <- colnames(rec.data) # Pull and format sending and receiving metadata - # jc: possible bug, change object to sys.small - sending.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) - receiving.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) + # jc: possible bug, change object to filtered.obj + sending.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) + receiving.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) # Make joint metadata datArray <- abind::abind(sending.metadata,receiving.metadata,along=3) joint.metadata <- as.matrix(apply(datArray,1:2,function(x)paste(x[1],"-",x[2]))) @@ -107,7 +107,7 @@ RunCellToCellSpatial <- function(sys.small, Seurat::Idents(demo) <- demo$VectorType # How many vectors were captured by this sampling? - message(paste("\n",length(unique(demo$VectorType)),'distinct VectorTypes were computed, out of',length(table(Seurat::Idents(sys.small)))^2,'total possible')) + message(paste("\n",length(unique(demo$VectorType)),'distinct VectorTypes were computed, out of',length(table(Seurat::Idents(filtered.obj)))^2,'total possible')) if(output_format == "seurat") return(demo) diff --git a/R/RunCellToNeighborhood.R b/R/RunCellToNeighborhood.R index b0930bb..fdb410f 100644 --- a/R/RunCellToNeighborhood.R +++ b/R/RunCellToNeighborhood.R @@ -1,7 +1,7 @@ #' RunCellToNeighborhood #' -#' @param sys.small A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. -#' @param ground.truth Ground truth signaling mechanisms present in sys.small. +#' @param filtered.obj A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. +#' @param ground.truth Ground truth signaling mechanisms present in filtered.obj. #' @param assay The assay to run the SCC transformation on. Defaults to "RNA." #' @param meta.data.to.map A character vector of metadata names present in the original object which will be carried to the NICHES objects #' @param blend Choice of linear operator to combine edges. Defaults to "mean", also accepts "sum" @@ -10,7 +10,7 @@ #' #' @export -RunCellToNeighborhood <- function(sys.small, +RunCellToNeighborhood <- function(filtered.obj, ground.truth, assay, meta.data.to.map, @@ -21,30 +21,30 @@ RunCellToNeighborhood <- function(sys.small, # Make ligand matrix - #lig.data <- sys.small@assays[[assay]]@data[ligands,edgelist$from] + #lig.data <- filtered.obj@assays[[assay]]@data[ligands,edgelist$from] subunit.list <- list() # Builds sending (ligand) data for any number of ligand subunits for (s in 1:ncol(ground.truth$source.subunits)){ #For each subunit column... - subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(getSeuratAssay(sys.small,assay,"data")[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[s]]) <- colnames(getSeuratAssay(sys.small,assay,"data")[,edgelist$from]) + subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[s]]) <- colnames(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$from]) rownames(subunit.list[[s]]) <- rownames(ground.truth$source.subunits) non.na.indices <- !is.na(ground.truth$source.subunits[,s]) #Identify rows in the s-th column of the ground truth which are not NA - subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$source.subunits[non.na.indices,s],edgelist$from]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$source.subunits[non.na.indices,s],edgelist$from]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } lig.data <- Reduce('*',subunit.list) rm(subunit.list) # Make receptor matrix - #rec.data <- sys.small@assays[[assay]]@data[receptors,edgelist$to] + #rec.data <- filtered.obj@assays[[assay]]@data[receptors,edgelist$to] subunit.list <- list() # Builds receiving (receptor) data for any number of receptor subunits for (t in 1:ncol(ground.truth$target.subunits)){ - subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(getSeuratAssay(sys.small,assay,"data")[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[t]]) <- colnames(getSeuratAssay(sys.small,assay,"data")[,edgelist$to]) + subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[t]]) <- colnames(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$to]) rownames(subunit.list[[t]]) <- rownames(ground.truth$target.subunits) non.na.indices <- !is.na(ground.truth$target.subunits[,t]) #Identify rows in the t-th column of the ground truth which are not NA - subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$target.subunits[non.na.indices,t],edgelist$to]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$target.subunits[non.na.indices,t],edgelist$to]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } rec.data <- Reduce('*',subunit.list) rm(subunit.list) @@ -69,7 +69,7 @@ RunCellToNeighborhood <- function(sys.small, demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'CellToNeighborhood') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot if(SeuratObject::Version(demo) >= "5.0.0"){ - demo <- NormalizeData(demo,assay = "CellToNeighborhood") # Seura Object need to be >= 5.0.1 + demo <- Seurat::NormalizeData(demo,assay = "CellToNeighborhood") # Seura Object need to be >= 5.0.1 demo@assays$CellToNeighborhood@layers$data <- demo@assays$CellToNeighborhood@layers$counts # Seura Object need to be >= 5.0.1 } @@ -77,7 +77,7 @@ RunCellToNeighborhood <- function(sys.small, # Add metadata based on ident slot # bug fix: add the Neighborhood - prefix sending_type.meta <- data.frame(SendingCell = barcodes, - SendingType = Seurat::Idents(sys.small)[barcodes], + SendingType = Seurat::Idents(filtered.obj)[barcodes], row.names = paste(barcodes,"Neighborhood",sep = '—')) demo <- Seurat::AddMetaData(demo,metadata = sending_type.meta,col.name = c("SendingCell","SendingType")) @@ -88,8 +88,8 @@ RunCellToNeighborhood <- function(sys.small, sending.barcodes <- barcodes # Only sending cell metadata applies for this function #receiving.barcodes <- colnames(rec.map) # Pull and format sending and receiving metadata - # jc: possible bug, change object to sys.small - sending.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) + # jc: possible bug, change object to filtered.obj + sending.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) #receiving.metadata <- as.matrix(object@meta.data[,meta.data.to.map][receiving.barcodes,]) # Make joint metadata #datArray <- abind(sending.metadata,receiving.metadata,along=3) diff --git a/R/RunCellToSystem.R b/R/RunCellToSystem.R index 5b49ac7..93a195d 100644 --- a/R/RunCellToSystem.R +++ b/R/RunCellToSystem.R @@ -9,8 +9,8 @@ #' Meta.data slots by default contain "SendingType" information, which is the celltypes for each point, #' and "SendingCell" which is the exact cell barcode present in the original Seurat object. #' -#' @param sys.small A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. -#' @param ground.truth Ground truth signaling mechanisms present in sys.small. +#' @param filtered.obj A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. +#' @param ground.truth Ground truth signaling mechanisms present in filtered.obj. #' @param assay The assay to run the CellToSystem transformation on. Defaults to "RNA." #' @param blend Choice of linear operator to combine edges. Defaults to "mean", also accepts "sum","mean.adj" #' @param meta.data.to.map A character vector of metadata names present in the original object which will be carried to the NICHES objects @@ -19,7 +19,7 @@ #' @export -RunCellToSystem <- function(sys.small, +RunCellToSystem <- function(filtered.obj, ground.truth, assay, blend = 'mean', @@ -33,11 +33,11 @@ RunCellToSystem <- function(sys.small, # Receptor data subunit.list <- list() # Builds receiving (receptor) data for any number of receptor subunits for (t in 1:ncol(ground.truth$target.subunits)){ - subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(sys.small)) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[t]]) <- colnames(sys.small) + subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(filtered.obj)) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[t]]) <- colnames(filtered.obj) rownames(subunit.list[[t]]) <- rownames(ground.truth$target.subunits) non.na.indices <- !is.na(ground.truth$target.subunits[,t]) #Identify rows in the s-th column of the ground truth which are not NA - subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$target.subunits[non.na.indices,t],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$target.subunits[non.na.indices,t],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } rec.map <- Reduce('*',subunit.list) rm(subunit.list) @@ -61,11 +61,11 @@ RunCellToSystem <- function(sys.small, # Ligand data subunit.list <- list() # Builds sending (ligand) data for any number of ligand subunits for (s in 1:ncol(ground.truth$source.subunits)){ #For each subunit column... - subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(sys.small)) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[s]]) <- colnames(sys.small) + subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(filtered.obj)) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[s]]) <- colnames(filtered.obj) rownames(subunit.list[[s]]) <- rownames(ground.truth$source.subunits) non.na.indices <- !is.na(ground.truth$source.subunits[,s]) #Identify rows in the s-th column of the ground truth which are not NA - subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$source.subunits[non.na.indices,s],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$source.subunits[non.na.indices,s],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } lig.map <- Reduce('*',subunit.list) rm(subunit.list) @@ -84,7 +84,7 @@ RunCellToSystem <- function(sys.small, demo <- Seurat::CreateSeuratObject(counts = as.matrix(sc.connectome),assay = 'CellToSystem') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot if(SeuratObject::Version(demo) >= "5.0.0"){ - demo <- NormalizeData(demo,assay = "CellToSystem") # Seura Object need to be >= 5.0.1 + demo <- Seurat::NormalizeData(demo,assay = "CellToSystem") # Seura Object need to be >= 5.0.1 demo@assays$CellToSystem@layers$data <- demo@assays$CellToSystem@layers$counts # Seura Object need to be >= 5.0.1 } @@ -95,7 +95,7 @@ RunCellToSystem <- function(sys.small, demo <- Seurat::AddMetaData(demo,metadata = meta.data.to.add,col.name = 'SendingCell') # bug fix: add the system - prefix - sending_type.meta <- data.frame(Seurat::Idents(sys.small)) + sending_type.meta <- data.frame(Seurat::Idents(filtered.obj)) rownames(sending_type.meta) <- paste(rownames(sending_type.meta),"System",sep = '—') demo <- Seurat::AddMetaData(demo,metadata = sending_type.meta,col.name = 'SendingType') @@ -106,8 +106,8 @@ RunCellToSystem <- function(sys.small, sending.barcodes <- colnames(lig.map) # Only sending cell metadata applies for this function #receiving.barcodes <- colnames(rec.map) # Pull and format sending and receiving metadata - # jc: possible bug, change object to sys.small - sending.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) + # jc: possible bug, change object to filtered.obj + sending.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,]) #receiving.metadata <- as.matrix(object@meta.data[,meta.data.to.map][receiving.barcodes,]) # Make joint metadata #datArray <- abind(sending.metadata,receiving.metadata,along=3) diff --git a/R/RunNICHES.R b/R/RunNICHES.R index 3a324d2..3f3fcef 100644 --- a/R/RunNICHES.R +++ b/R/RunNICHES.R @@ -161,12 +161,12 @@ RunNICHES.default <- function(object, # jc: move the shared preprocessing steps here to avoid redundancy and reduce the number of parameters to be passed to other functions # NOTE: relies on Idents(object) to be cell types to subset - sys.small <- prepSeurat(object,assay,min.cells.per.ident,min.cells.per.gene) - ground.truth <- lr_load(LR.database,custom_LR_database,species,rownames(sys.small@assays[[assay]])) + filtered.obj <- prepSeurat(object,assay,min.cells.per.ident,min.cells.per.gene) + ground.truth <- lr_load(LR.database,custom_LR_database,species,rownames(filtered.obj@assays[[assay]])) if (org_names_indicator["CellToCellSpatial"] == T | org_names_indicator["CellToNeighborhood"] == T | org_names_indicator["NeighborhoodToCell"] == T){ ## 1. Move the neighbor graph construction here ## 2. Enable a k-nearest-neighbor parameter as an alternative - edgelist <- compute_edgelist(sys.small,position.x,position.y,k,rad.set) + edgelist <- compute_edgelist(filtered.obj,position.x,position.y,k,rad.set) } # check the output format @@ -179,20 +179,20 @@ RunNICHES.default <- function(object, # NOTE: RunCellToCell relies on Idents(object) to be cell types to subset # Also each RunXXX function needs Idents(object) to build VectorType meta data - if (CellToCell == T){output[[length(output)+1]] <- RunCellToCell(sys.small=sys.small, + if (CellToCell == T){output[[length(output)+1]] <- RunCellToCell(filtered.obj=filtered.obj, ground.truth=ground.truth, assay = assay, meta.data.to.map = meta.data.to.map, output_format = output_format )} - if (CellToSystem == T){output[[length(output)+1]] <- RunCellToSystem(sys.small=sys.small, + if (CellToSystem == T){output[[length(output)+1]] <- RunCellToSystem(filtered.obj=filtered.obj, ground.truth=ground.truth, assay = assay, meta.data.to.map = meta.data.to.map, blend = blend, output_format = output_format )} - if (SystemToCell == T){output[[length(output)+1]] <- RunSystemToCell(sys.small=sys.small, + if (SystemToCell == T){output[[length(output)+1]] <- RunSystemToCell(filtered.obj=filtered.obj, ground.truth=ground.truth, assay = assay, meta.data.to.map = meta.data.to.map, @@ -201,14 +201,14 @@ RunNICHES.default <- function(object, )} - if (CellToCellSpatial == T){output[[length(output)+1]] <- RunCellToCellSpatial(sys.small=sys.small, + if (CellToCellSpatial == T){output[[length(output)+1]] <- RunCellToCellSpatial(filtered.obj=filtered.obj, ground.truth=ground.truth, assay = assay, meta.data.to.map = meta.data.to.map, edgelist = edgelist, output_format = output_format )} #Spatially-limited Cell-Cell vectors - if (CellToNeighborhood == T){output[[length(output)+1]] <- RunCellToNeighborhood(sys.small=sys.small, + if (CellToNeighborhood == T){output[[length(output)+1]] <- RunCellToNeighborhood(filtered.obj=filtered.obj, ground.truth=ground.truth, assay = assay, meta.data.to.map = meta.data.to.map, @@ -216,7 +216,7 @@ RunNICHES.default <- function(object, edgelist = edgelist, output_format = output_format )} #Spatially-limited Cell-Neighborhood vectors - if (NeighborhoodToCell == T){output[[length(output)+1]] <- RunNeighborhoodToCell(sys.small=sys.small, + if (NeighborhoodToCell == T){output[[length(output)+1]] <- RunNeighborhoodToCell(filtered.obj=filtered.obj, ground.truth=ground.truth, assay = assay, meta.data.to.map = meta.data.to.map, diff --git a/R/RunNeighborhoodToCell.R b/R/RunNeighborhoodToCell.R index 2e70f1b..39895f8 100644 --- a/R/RunNeighborhoodToCell.R +++ b/R/RunNeighborhoodToCell.R @@ -1,7 +1,7 @@ #' RunNeighborhoodToCell #' -#' @param sys.small A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. -#' @param ground.truth Ground truth signaling mechanisms present in sys.small. +#' @param filtered.obj A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. +#' @param ground.truth Ground truth signaling mechanisms present in filtered.obj. #' @param assay The assay to run the SCC transformation on. Defaults to "RNA." #' @param meta.data.to.map A character vector of metadata names present in the original object which will be carried to the NICHES objects #' @param blend Choice of linear operator to combine edges. Defaults to "mean", also accepts "sum" @@ -10,7 +10,7 @@ #' #' @export -RunNeighborhoodToCell <- function(sys.small, +RunNeighborhoodToCell <- function(filtered.obj, ground.truth, assay, meta.data.to.map, @@ -21,30 +21,30 @@ RunNeighborhoodToCell <- function(sys.small, # Make ligand matrix - #lig.data <- sys.small@assays[[assay]]@data[ligands,edgelist$from] + #lig.data <- filtered.obj@assays[[assay]]@data[ligands,edgelist$from] subunit.list <- list() # Builds sending (ligand) data for any number of ligand subunits for (s in 1:ncol(ground.truth$source.subunits)){ #For each subunit column... - subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(getSeuratAssay(sys.small,assay,"data")[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[s]]) <- colnames(getSeuratAssay(sys.small,assay,"data")[,edgelist$from]) + subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[s]]) <- colnames(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$from]) rownames(subunit.list[[s]]) <- rownames(ground.truth$source.subunits) non.na.indices <- !is.na(ground.truth$source.subunits[,s]) #Identify rows in the s-th column of the ground truth which are not NA - subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$source.subunits[non.na.indices,s],edgelist$from]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$source.subunits[non.na.indices,s],edgelist$from]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } lig.data <- Reduce('*',subunit.list) rm(subunit.list) # Make receptor matrix - #rec.data <- sys.small@assays[[assay]]@data[receptors,edgelist$to] + #rec.data <- filtered.obj@assays[[assay]]@data[receptors,edgelist$to] subunit.list <- list() # Builds receiving (receptor) data for any number of receptor subunits for (t in 1:ncol(ground.truth$target.subunits)){ - subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(getSeuratAssay(sys.small,assay,"data")[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[t]]) <- colnames(getSeuratAssay(sys.small,assay,"data")[,edgelist$to]) + subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[t]]) <- colnames(getSeuratAssay(filtered.obj,assay,"data")[,edgelist$to]) rownames(subunit.list[[t]]) <- rownames(ground.truth$target.subunits) non.na.indices <- !is.na(ground.truth$target.subunits[,t]) #Identify rows in the t-th column of the ground truth which are not NA - subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$target.subunits[non.na.indices,t],edgelist$to]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$target.subunits[non.na.indices,t],edgelist$to]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } rec.data <- Reduce('*',subunit.list) rm(subunit.list) @@ -69,7 +69,7 @@ RunNeighborhoodToCell <- function(sys.small, demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'NeighborhoodToCell') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot if(SeuratObject::Version(demo) >= "5.0.0"){ - demo <- NormalizeData(demo,assay = "NeighborhoodToCell") # Seura Object need to be >= 5.0.1 + demo <- Seurat::NormalizeData(demo,assay = "NeighborhoodToCell") # Seura Object need to be >= 5.0.1 demo@assays$NeighborhoodToCell@layers$data <- demo@assays$NeighborhoodToCell@layers$counts # Seura Object need to be >= 5.0.1 } @@ -77,7 +77,7 @@ RunNeighborhoodToCell <- function(sys.small, # Add metadata based on ident slot # bug fix: add the Neighborhood - prefix receiving_type.meta <- data.frame(ReceivingCell = barcodes, - ReceivingType = Seurat::Idents(sys.small)[barcodes], + ReceivingType = Seurat::Idents(filtered.obj)[barcodes], row.names = paste("Neighborhood",barcodes,sep = '—')) demo <- Seurat::AddMetaData(demo,metadata = receiving_type.meta,col.name = c("ReceivingCell","ReceivingType")) @@ -89,8 +89,8 @@ RunNeighborhoodToCell <- function(sys.small, receiving.barcodes <- barcodes # Only receiving cell metadata applies for this function # Pull and format sending and receiving metadata #sending.metadata <- as.matrix(object@meta.data[,meta.data.to.map][sending.barcodes,]) - # jc: possible bug, change object to sys.small - receiving.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) + # jc: possible bug, change object to filtered.obj + receiving.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) # Make joint metadata #datArray <- abind(sending.metadata,receiving.metadata,along=3) #joint.metadata <- as.matrix(apply(datArray,1:2,function(x)paste(x[1],"-",x[2]))) diff --git a/R/RunSystemToCell.R b/R/RunSystemToCell.R index bdf347b..475234d 100644 --- a/R/RunSystemToCell.R +++ b/R/RunSystemToCell.R @@ -9,8 +9,8 @@ #' Meta.data slots by default contain "ReceivingType" information, which is the celltypes for each point, #' and "ReceivingCell" which is the exact cell barcode present in the original Seurat object. #' -#' @param sys.small A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. -#' @param ground.truth Ground truth signaling mechanisms present in sys.small. +#' @param filtered.obj A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. +#' @param ground.truth Ground truth signaling mechanisms present in filtered.obj. #' @param assay The assay to run the SystemToCell transformation on. Defaults to "RNA." #' @param blend Choice of linear operator to combine edges. Defaults to "mean", also accepts "sum", "mean.adj" #' @param meta.data.to.map A character vector of metadata names present in the original object which will be carried to the NICHES objects @@ -19,7 +19,7 @@ #' @export -RunSystemToCell <- function(sys.small, +RunSystemToCell <- function(filtered.obj, ground.truth, assay, blend = 'mean', @@ -33,11 +33,11 @@ RunSystemToCell <- function(sys.small, # Ligand data subunit.list <- list() # Builds sending (ligand) data for any number of ligand subunits for (s in 1:ncol(ground.truth$source.subunits)){ #For each subunit column... - subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(sys.small)) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[s]]) <- colnames(sys.small) + subunit.list[[s]] <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(filtered.obj)) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[s]]) <- colnames(filtered.obj) rownames(subunit.list[[s]]) <- rownames(ground.truth$source.subunits) non.na.indices <- !is.na(ground.truth$source.subunits[,s]) #Identify rows in the s-th column of the ground truth which are not NA - subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$source.subunits[non.na.indices,s],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[s]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$source.subunits[non.na.indices,s],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } lig.map <- Reduce('*',subunit.list) rm(subunit.list) @@ -60,11 +60,11 @@ RunSystemToCell <- function(sys.small, # Receptor data subunit.list <- list() # Builds receiving (receptor) data for any number of receptor subunits for (t in 1:ncol(ground.truth$target.subunits)){ - subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(sys.small)) #initialize a mechanism x barcode matrix of all NAs - colnames(subunit.list[[t]]) <- colnames(sys.small) + subunit.list[[t]] <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(filtered.obj)) #initialize a mechanism x barcode matrix of all NAs + colnames(subunit.list[[t]]) <- colnames(filtered.obj) rownames(subunit.list[[t]]) <- rownames(ground.truth$target.subunits) non.na.indices <- !is.na(ground.truth$target.subunits[,t]) #Identify rows in the s-th column of the ground truth which are not NA - subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(sys.small,assay,"data")[ground.truth$target.subunits[non.na.indices,t],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices + subunit.list[[t]][non.na.indices,] <- as.matrix(getSeuratAssay(filtered.obj,assay,"data")[ground.truth$target.subunits[non.na.indices,t],]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices } rec.map <- Reduce('*',subunit.list) rm(subunit.list) @@ -82,7 +82,7 @@ RunSystemToCell <- function(sys.small, demo <- Seurat::CreateSeuratObject(counts = as.matrix(sc.connectome),assay = 'SystemToCell') # JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot if(SeuratObject::Version(demo) >= "5.0.0"){ - demo <- NormalizeData(demo,assay = "SystemToCell") # Seurat Object need to be >= 5.0.1 + demo <- Seurat::NormalizeData(demo,assay = "SystemToCell") # Seurat Object need to be >= 5.0.1 demo@assays$SystemToCell@layers$data <- demo@assays$SystemToCell@layers$counts # Seurat Object need to be >= 5.0.1 } @@ -93,7 +93,7 @@ RunSystemToCell <- function(sys.small, demo <- Seurat::AddMetaData(demo,metadata = meta.data.to.add,col.name = 'ReceivingCell') # bug fix: add the system - prefix - receiving_type.meta <- data.frame(Seurat::Idents(sys.small)) + receiving_type.meta <- data.frame(Seurat::Idents(filtered.obj)) rownames(receiving_type.meta) <- paste("System",rownames(receiving_type.meta),sep = '—') demo <- Seurat::AddMetaData(demo,metadata = receiving_type.meta,col.name = 'ReceivingType') @@ -105,8 +105,8 @@ RunSystemToCell <- function(sys.small, receiving.barcodes <- colnames(rec.map) # Pull and format sending and receiving metadata #sending.metadata <- as.matrix(object@meta.data[,meta.data.to.map][sending.barcodes,]) - # jc: possible bug, change object to sys.small - receiving.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) + # jc: possible bug, change object to filtered.obj + receiving.metadata <- as.matrix(filtered.obj@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,]) # Make joint metadata #datArray <- abind(sending.metadata,receiving.metadata,along=3) #joint.metadata <- as.matrix(apply(datArray,1:2,function(x)paste(x[1],"-",x[2]))) diff --git a/R/utils.R b/R/utils.R index 63d9d78..a7af241 100644 --- a/R/utils.R +++ b/R/utils.R @@ -26,14 +26,14 @@ prepSeurat <- function(object,assay,min.cells.per.ident,min.cells.per.gene){ Seurat::DefaultAssay(object) <- assay # Stash object - sys.small <- object + filtered.obj <- object # Limit object to cell populations larger than requested minimum if (!is.null(min.cells.per.ident)){ message(paste("\n",'Subsetting to populations with greater than',min.cells.per.ident,'cells')) - idents.include <- names(table(Seurat::Idents(sys.small)))[table(Seurat::Idents(sys.small)) > min.cells.per.ident] - sys.small <- subset(sys.small,idents = idents.include) + idents.include <- names(table(Seurat::Idents(filtered.obj)))[table(Seurat::Idents(filtered.obj)) > min.cells.per.ident] + filtered.obj <- subset(filtered.obj,idents = idents.include) } # Limit analysis to interaction involving genes expressed above minimum threshold number of cells in the system @@ -41,18 +41,18 @@ prepSeurat <- function(object,assay,min.cells.per.ident,min.cells.per.gene){ message(paste("\n",'Subsetting to genes expressed in greater than',min.cells.per.gene,'cells')) # jc: return an error if the count matrix in the given assay is empty - if(!length(getSeuratAssay(sys.small,assay,"counts"))) stop("Unable to subset: the count matrix in the given assay is empty.") + if(!length(getSeuratAssay(filtered.obj,assay,"counts"))) stop("Unable to subset: the count matrix in the given assay is empty.") - cells.per.gene <- data.frame(non.zero.cells = Matrix::rowSums(getSeuratAssay(sys.small,assay,"counts")>0)) + cells.per.gene <- data.frame(non.zero.cells = Matrix::rowSums(getSeuratAssay(filtered.obj,assay,"counts")>0)) GOI <- subset(cells.per.gene,non.zero.cells > min.cells.per.gene) - sys.small <- sys.small[rownames(GOI),] + filtered.obj <- filtered.obj[rownames(GOI),] } - num.cells <- ncol(sys.small) + num.cells <- ncol(filtered.obj) message(paste("\n",num.cells,'distinct cells from', - length(names(table(Seurat::Idents(sys.small)))),'celltypes to be analyzed')) + length(names(table(Seurat::Idents(filtered.obj)))),'celltypes to be analyzed')) - return(sys.small) + return(filtered.obj) } @@ -100,7 +100,7 @@ return_celltypes <- function(seurat_object){ #' Compute an edgelist based on the spatial coordinates #' -#' @param sys.small A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. +#' @param filtered.obj A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings. #' @param position.x string. Optional. Default: NULL. The name of the meta.data column specifying location on the spatial x-axis. Only required for spatial omics data. #' @param position.y string. Optional. Default: NULL. The name of the meta.data column specifying location on the spatial y-axis. Only required for spatial omics data. #' @param k integer. Optional. Default: 4. Number of neighbors in a knn graph. Used to compute a mutual nearest neighbor graph based on the spatial coordinates of the spatial transcriptomic datasets. @@ -108,7 +108,7 @@ return_celltypes <- function(seurat_object){ #' #' @export #' -compute_edgelist <- function(sys.small, +compute_edgelist <- function(filtered.obj, position.x, position.y, k=4, @@ -120,8 +120,8 @@ compute_edgelist <- function(sys.small, # Create adjacency matrix # Adapted from :: https://stackoverflow.com/questions/16075232/how-to-create-adjacency-matrix-from-grid-coordinates-in-r # Setup numbering and labeling - # jc: possible bug, change object to sys.small - df <- data.frame(x = sys.small[[position.x]], y = sys.small[[position.y]]) + # jc: possible bug, change object to filtered.obj + df <- data.frame(x = filtered.obj[[position.x]], y = filtered.obj[[position.y]]) df$barcode <- rownames(df) df$x <- as.character(df$x) df$y <- as.character(df$y) From 3dd08a6f29248469287af740eb7fcae67af55ce6 Mon Sep 17 00:00:00 2001 From: Junchen Yang Date: Tue, 16 Jul 2024 23:37:44 -0400 Subject: [PATCH 4/4] updated documentation --- DESCRIPTION | 2 +- man/RunCellToCell.Rd | 12 +++++++++--- man/RunCellToCellSpatial.Rd | 6 +++--- man/RunCellToNeighborhood.Rd | 6 +++--- man/RunCellToSystem.Rd | 6 +++--- man/RunNeighborhoodToCell.Rd | 6 +++--- man/RunSystemToCell.Rd | 6 +++--- man/compute_edgelist.Rd | 4 ++-- 8 files changed, 27 insertions(+), 21 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f5a0480..faa656c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -7,7 +7,7 @@ Description: Cell-cell Interactions at Single-Cell Resolution License: GPL-3 Encoding: UTF-8 LazyData: yes -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 VignetteBuilder: knitr Depends: R (>= 4.0) diff --git a/man/RunCellToCell.Rd b/man/RunCellToCell.Rd index 0f77f19..84e45b1 100644 --- a/man/RunCellToCell.Rd +++ b/man/RunCellToCell.Rd @@ -4,12 +4,18 @@ \alias{RunCellToCell} \title{RunCellToCell} \usage{ -RunCellToCell(sys.small, ground.truth, assay, meta.data.to.map, output_format) +RunCellToCell( + filtered.obj, + ground.truth, + assay, + meta.data.to.map, + output_format +) } \arguments{ -\item{sys.small}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} +\item{filtered.obj}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} -\item{ground.truth}{Ground truth signaling mechanisms present in sys.small.} +\item{ground.truth}{Ground truth signaling mechanisms present in filtered.obj.} \item{assay}{The assay to run the SCC transformation on. Defaults to "RNA."} diff --git a/man/RunCellToCellSpatial.Rd b/man/RunCellToCellSpatial.Rd index 75268d0..24b5a29 100644 --- a/man/RunCellToCellSpatial.Rd +++ b/man/RunCellToCellSpatial.Rd @@ -5,7 +5,7 @@ \title{RunCellToCellSpatial} \usage{ RunCellToCellSpatial( - sys.small, + filtered.obj, ground.truth, assay, meta.data.to.map, @@ -14,9 +14,9 @@ RunCellToCellSpatial( ) } \arguments{ -\item{sys.small}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} +\item{filtered.obj}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} -\item{ground.truth}{Ground truth signaling mechanisms present in sys.small.} +\item{ground.truth}{Ground truth signaling mechanisms present in filtered.obj.} \item{assay}{The assay to run the SCC transformation on. Defaults to "RNA."} diff --git a/man/RunCellToNeighborhood.Rd b/man/RunCellToNeighborhood.Rd index a95665a..480ff03 100644 --- a/man/RunCellToNeighborhood.Rd +++ b/man/RunCellToNeighborhood.Rd @@ -5,7 +5,7 @@ \title{RunCellToNeighborhood} \usage{ RunCellToNeighborhood( - sys.small, + filtered.obj, ground.truth, assay, meta.data.to.map, @@ -15,9 +15,9 @@ RunCellToNeighborhood( ) } \arguments{ -\item{sys.small}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} +\item{filtered.obj}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} -\item{ground.truth}{Ground truth signaling mechanisms present in sys.small.} +\item{ground.truth}{Ground truth signaling mechanisms present in filtered.obj.} \item{assay}{The assay to run the SCC transformation on. Defaults to "RNA."} diff --git a/man/RunCellToSystem.Rd b/man/RunCellToSystem.Rd index 4a5e6fa..5913dc1 100644 --- a/man/RunCellToSystem.Rd +++ b/man/RunCellToSystem.Rd @@ -5,7 +5,7 @@ \title{RunCellToSystem} \usage{ RunCellToSystem( - sys.small, + filtered.obj, ground.truth, assay, blend = "mean", @@ -14,9 +14,9 @@ RunCellToSystem( ) } \arguments{ -\item{sys.small}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} +\item{filtered.obj}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} -\item{ground.truth}{Ground truth signaling mechanisms present in sys.small.} +\item{ground.truth}{Ground truth signaling mechanisms present in filtered.obj.} \item{assay}{The assay to run the CellToSystem transformation on. Defaults to "RNA."} diff --git a/man/RunNeighborhoodToCell.Rd b/man/RunNeighborhoodToCell.Rd index aef0e6a..3876c49 100644 --- a/man/RunNeighborhoodToCell.Rd +++ b/man/RunNeighborhoodToCell.Rd @@ -5,7 +5,7 @@ \title{RunNeighborhoodToCell} \usage{ RunNeighborhoodToCell( - sys.small, + filtered.obj, ground.truth, assay, meta.data.to.map, @@ -15,9 +15,9 @@ RunNeighborhoodToCell( ) } \arguments{ -\item{sys.small}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} +\item{filtered.obj}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} -\item{ground.truth}{Ground truth signaling mechanisms present in sys.small.} +\item{ground.truth}{Ground truth signaling mechanisms present in filtered.obj.} \item{assay}{The assay to run the SCC transformation on. Defaults to "RNA."} diff --git a/man/RunSystemToCell.Rd b/man/RunSystemToCell.Rd index 97c73e8..4e602a9 100644 --- a/man/RunSystemToCell.Rd +++ b/man/RunSystemToCell.Rd @@ -5,7 +5,7 @@ \title{RunSystemToCell} \usage{ RunSystemToCell( - sys.small, + filtered.obj, ground.truth, assay, blend = "mean", @@ -14,9 +14,9 @@ RunSystemToCell( ) } \arguments{ -\item{sys.small}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} +\item{filtered.obj}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} -\item{ground.truth}{Ground truth signaling mechanisms present in sys.small.} +\item{ground.truth}{Ground truth signaling mechanisms present in filtered.obj.} \item{assay}{The assay to run the SystemToCell transformation on. Defaults to "RNA."} diff --git a/man/compute_edgelist.Rd b/man/compute_edgelist.Rd index f79e8d6..240a4bc 100644 --- a/man/compute_edgelist.Rd +++ b/man/compute_edgelist.Rd @@ -4,10 +4,10 @@ \alias{compute_edgelist} \title{Compute an edgelist based on the spatial coordinates} \usage{ -compute_edgelist(sys.small, position.x, position.y, k = 4, rad.set = NULL) +compute_edgelist(filtered.obj, position.x, position.y, k = 4, rad.set = NULL) } \arguments{ -\item{sys.small}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} +\item{filtered.obj}{A filtered Seurat object. The active identity will be used to define populations for connectomic sampling and crossings.} \item{position.x}{string. Optional. Default: NULL. The name of the meta.data column specifying location on the spatial x-axis. Only required for spatial omics data.}