Skip to content

Commit

Permalink
Merge pull request #30 from msraredon/dev_master
Browse files Browse the repository at this point in the history
Updated NICHES to be compatible with Seurat V5
  • Loading branch information
jcyang34 authored Mar 3, 2024
2 parents 96ad10f + 858c90d commit 4650e48
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 41 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Description: Cell-cell Interactions at Single-Cell Resolution
License: GPL-3
Encoding: UTF-8
LazyData: yes
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
VignetteBuilder: knitr
Depends:
R (>= 4.0)
Expand Down
16 changes: 12 additions & 4 deletions R/RunCellToCell.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RunCellToCell <- function(sys.small,
colnames(subunit.list[[s]]) <- colnames(temp)
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(temp@assays[[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(temp,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.list[[i]] <- Reduce('*',subunit.list)
rm(subunit.list)
Expand All @@ -54,7 +54,7 @@ RunCellToCell <- function(sys.small,
colnames(subunit.list[[t]]) <- colnames(temp)
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(temp@assays[[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(temp,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.list[[i]] <- Reduce('*',subunit.list)
rm(subunit.list)
Expand Down Expand Up @@ -108,6 +108,13 @@ 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){
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

}


# Gather and assemble metadata based on "ident" slot
sending.cell.idents.2 <- do.call(c,sending.cell.idents)
Expand Down Expand Up @@ -139,7 +146,8 @@ RunCellToCell <- function(sys.small,
# Compile
meta.data.to.add.also <- cbind(sending.metadata,receiving.metadata,joint.metadata)
rownames(meta.data.to.add.also) <- paste(sending.barcodes,receiving.barcodes,sep='')
# Add additional metadata
# Add additional metadata
### COMMENT 2024-02-03 MSBR: Is this compatible with Seurat v5?###
demo <- Seurat::AddMetaData(demo,metadata = as.data.frame(meta.data.to.add.also))
}
# Define initial identity
Expand All @@ -152,7 +160,7 @@ RunCellToCell <- function(sys.small,
else{
output_list <- vector(mode = "list",length=2)
names(output_list) <- c("CellToCellMatrix","metadata")
output_list[["CellToCellMatrix"]] <- demo[["CellToCell"]]@counts
output_list[["CellToCellMatrix"]] <- getSeuratAssay(demo,"CellToCell","counts")
output_list[["metadata"]] <- demo@meta.data
return(output_list)
}
Expand Down
23 changes: 15 additions & 8 deletions R/RunCellToCellSpatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ RunCellToCellSpatial <- function(sys.small,

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@assays[[assay]]@data[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs
colnames(subunit.list[[s]]) <- colnames(sys.small@assays[[assay]]@data[,edgelist$from])
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])
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(sys.small@assays[[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(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
}
lig.data <- Reduce('*',subunit.list)
rm(subunit.list)
Expand All @@ -41,11 +41,11 @@ RunCellToCellSpatial <- function(sys.small,

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@assays[[assay]]@data[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs
colnames(subunit.list[[t]]) <- colnames(sys.small@assays[[assay]]@data[,edgelist$to])
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])
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(sys.small@assays[[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(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
}
rec.data <- Reduce('*',subunit.list)
rm(subunit.list)
Expand All @@ -61,6 +61,13 @@ RunCellToCellSpatial <- function(sys.small,
# Use this matrix to create a Seurat object:
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){
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

}

# Add key metadata

meta.data.to.add <- data.frame(SendingType = sending.cell.idents,
Expand All @@ -73,7 +80,7 @@ RunCellToCellSpatial <- function(sys.small,

#Add metadata to the Seurat object
demo <- Seurat::AddMetaData(demo,metadata = meta.data.to.add)

# Gather and assemble additional metadata
if (!is.null(meta.data.to.map)){
# Identify sending and receiving barcodes
Expand Down Expand Up @@ -107,7 +114,7 @@ RunCellToCellSpatial <- function(sys.small,
else{
output_list <- vector(mode = "list",length=2)
names(output_list) <- c("CellToCellSpatialMatrix","metadata")
output_list[["CellToCellSpatialMatrix"]] <- demo[["CellToCellSpatial"]]@counts
output_list[["CellToCellSpatialMatrix"]] <- getSeuratAssay(demo,"CellToCellSpatial","counts")
output_list[["metadata"]] <- demo@meta.data
return(output_list)
}
Expand Down
22 changes: 14 additions & 8 deletions R/RunCellToNeighborhood.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ RunCellToNeighborhood <- function(sys.small,

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@assays[[assay]]@data[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs
colnames(subunit.list[[s]]) <- colnames(sys.small@assays[[assay]]@data[,edgelist$from])
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])
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(sys.small@assays[[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(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
}
lig.data <- Reduce('*',subunit.list)
rm(subunit.list)
Expand All @@ -40,11 +40,11 @@ RunCellToNeighborhood <- function(sys.small,

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@assays[[assay]]@data[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs
colnames(subunit.list[[t]]) <- colnames(sys.small@assays[[assay]]@data[,edgelist$to])
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])
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(sys.small@assays[[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(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
}
rec.data <- Reduce('*',subunit.list)
rm(subunit.list)
Expand All @@ -67,7 +67,13 @@ 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){
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

}

# Add metadata based on ident slot
demo <- Seurat::AddMetaData(demo,metadata = barcodes,col.name = 'SendingCell')
# bug fix: add the Neighborhood - prefix
Expand Down Expand Up @@ -107,7 +113,7 @@ RunCellToNeighborhood <- function(sys.small,
else{
output_list <- vector(mode = "list",length=2)
names(output_list) <- c("CellToNeighborhoodMatrix","metadata")
output_list[["CellToNeighborhoodMatrix"]] <- demo[["CellToNeighborhood"]]@counts
output_list[["CellToNeighborhoodMatrix"]] <- getSeuratAssay(demo,"CellToNeighborhood","counts")
output_list[["metadata"]] <- demo@meta.data
return(output_list)
}
Expand Down
12 changes: 9 additions & 3 deletions R/RunCellToSystem.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RunCellToSystem <- function(sys.small,
colnames(subunit.list[[t]]) <- colnames(sys.small)
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(sys.small@assays[[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(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
}
rec.map <- Reduce('*',subunit.list)
rm(subunit.list)
Expand Down Expand Up @@ -65,7 +65,7 @@ RunCellToSystem <- function(sys.small,
colnames(subunit.list[[s]]) <- colnames(sys.small)
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(sys.small@assays[[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(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
}
lig.map <- Reduce('*',subunit.list)
rm(subunit.list)
Expand All @@ -82,6 +82,12 @@ 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){
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

}

# Add metadata to the Seurat object
meta.data.to.add <- data.frame(as.character(colnames(lig.map)))
Expand Down Expand Up @@ -126,7 +132,7 @@ RunCellToSystem <- function(sys.small,
else{
output_list <- vector(mode = "list",length=2)
names(output_list) <- c("CellToSystemMatrix","metadata")
output_list[["CellToSystemMatrix"]] <- demo[["CellToSystem"]]@counts
output_list[["CellToSystemMatrix"]] <- getSeuratAssay(demo,"CellToSystem","counts")
output_list[["metadata"]] <- demo@meta.data
return(output_list)
}
Expand Down
Loading

0 comments on commit 4650e48

Please sign in to comment.