Skip to content

Commit 331650d

Browse files
committed
Merge branch 'release/v2.0.8' into main
2 parents 47481bd + 9e5d249 commit 331650d

117 files changed

Lines changed: 1027 additions & 392 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: scigenex
22
Type: Package
33
Title: The scigenex package (Single-Cell Informative GENe Explorer)
4-
Version: 2.0.7
4+
Version: 2.0.8
55
Date: 2020-07-22
66
Author: J. Bavais, Sebastien Nin, Lionel Spinelli and Denis Puthier
77
Maintainer: J. Bavais <julie.bavais@yahoo.fr>

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MAKEFILE=Makefile
2-
VERSION=2.0.7
2+
VERSION=2.0.8
33

44
.PHONY: help
55

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ importFrom(Matrix,colSums)
116116
importFrom(Matrix,rowSums)
117117
importFrom(Matrix,t)
118118
importFrom(Seurat,AddModuleScore)
119+
importFrom(Seurat,Assays)
119120
importFrom(Seurat,DimPlot)
120121
importFrom(Seurat,FeaturePlot)
121122
importFrom(Seurat,NoAxes)

R/ClusterSet_class.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ setMethod("[", signature(x = "ClusterSet"),
306306
}
307307
} else {
308308
if (missing(i)) {
309-
n_data <- x@data[, j]
309+
n_data <- x@data[, j, drop = FALSE]
310310
n_gene_clusters <- x@gene_clusters
311311
n_top_genes <- x@top_genes
312312
n_gene_clusters_metadata <- x@gene_clusters_metadata
@@ -909,7 +909,7 @@ setGeneric("subsample_by_ident",
909909
#' @param object a ClusterSet object.
910910
#' @param ident A named vector. Names are cell/column names, values are classes/identity.
911911
#' Typically the result of the Seurat::Ident() function.
912-
#' @param nbcell The number of cell to select.
912+
#' @param nbcell The number of cell to select per population (cell identity).
913913
#' @param seed A seed for subsampling.
914914
#' @export
915915
#' @examples
@@ -927,7 +927,7 @@ setMethod("subsample_by_ident",
927927
signature("ClusterSet"),
928928
function(object,
929929
ident=NULL,
930-
nbcell=TRUE,
930+
nbcell=1e6,
931931
seed=123) {
932932

933933

R/convert.R

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,120 @@
11
#' @title Transform a Seurat objects / FindAllMarkers result into a ClusterSet.
22
#' @description Transform a Seurat objects into a ClusterSet.
33
#' @param object A Seurat object.
4-
#' @param markers A Seurat::FindAllMarkers() result or a named vector (clusters with gene_names as named).
4+
#' @param markers A Seurat::FindAllMarkers() result or a named vector (clusters with gene_names as named).
5+
#' Named vectors can contains several position with the same gene name (overlapping clusters).
56
#' @param layer One of 'data', 'counts' or 'scale.data'. The slot to extract from the seurat object to perform clustering analysis.
67
#' SCT is the recommended method from Seurat package when working with spatial transcriptomics data.
78
#' @param assay The type of assay (e.g. "RNA", "Spatial", "Sketch", "SCT"...).
9+
#' @param p_val_adj If markers is the output from Seurat::FindAllMarkers(), the adjusted p-value threshold.
810
#' @importFrom SeuratObject LayerData
911
#' @importFrom Matrix Matrix
12+
#' @importFrom Seurat Assays
1013
#' @examples
1114
#' ## From a scRNA-seq/Seurat object
1215
#' library(SeuratObject)
1316
#' library(Seurat)
1417
#' data("pbmc_small", package="SeuratObject")
15-
#' cs <- cluster_set_from_seurat(pbmc_small, Seurat::FindAllMarkers(pbmc_small))
18+
#' markers <- Seurat::FindAllMarkers(pbmc_small, only.pos = TRUE)
19+
#' cs <- cluster_set_from_seurat(pbmc_small, markers)
20+
#' cs <- top_genes(cs)
1621
#' plot_heatmap(cs)
1722
#' plot_heatmap(cs)
18-
#' plot_heatmap(cs[1,])
23+
#' plot_heatmap(cs[2, ])
1924
#' plot_heatmap(cs, cell_clusters = Seurat::Idents(pbmc_small))
2025
#' plot_heatmap(cs[1,Idents(pbmc_small) == "0"],
21-
#' cell_clusters = Seurat::Idents(pbmc_small), label_size = 6)
26+
#' cell_clusters = Seurat::Idents(pbmc_small), label_size = 6)
2227
#' plot_profiles(cs, ident = Seurat::Idents(pbmc_small))
2328
#' @export
2429
cluster_set_from_seurat <- function(object=NULL,
2530
markers=NULL,
2631
layer=c('data', 'counts', 'scale.data'),
27-
assay='RNA'){
32+
assay='RNA',
33+
p_val_adj=0.001){
2834

35+
print_msg("Converting a Seurat object from cluster_set...", msg_type = "DEBUG")
36+
37+
if(is.null(object) | !inherits(object, "Seurat"))
38+
print_msg("The 'object' argument should be a Seurat object.", msg_type="STOP")
39+
40+
if(is.null(markers))
41+
print_msg("Please provide a set of markers/clusters...", msg_type="STOP")
42+
43+
if(is.factor(markers))
44+
markers <- as.character(markers)
45+
46+
print_msg(paste0("This seurat object contains the following assay : ",
47+
Seurat::Assays(object), ".") , msg_type = "INFO")
48+
49+
if(!assay %in% Seurat::Assays(object)){
50+
print_msg("The selected assay is not available. See 'assay' argument...", msg_type="STOP")
51+
}
52+
2953
layer <- match.arg(layer)
3054

3155
object <- SeuratObject::LayerData(object, assay=assay, layer=layer)
3256

3357
if(inherits(markers, "data.frame")){
3458

35-
gn <- markers$gene
36-
clusters <- markers$cluster
59+
print_msg("Selecting markers based on p_val_adj...", msg_type = "DEBUG")
60+
markers <- markers[markers$p_val_adj <= p_val_adj, , drop=FALSE]
61+
object <- object[markers$gene, , drop = FALSE]
62+
print_msg("Disambiguating gene duplicates using '~' separator", msg_type = "DEBUG")
63+
gn <- make.unique(markers$gene, sep = "~")
64+
65+
clusters <- as.character(markers$cluster)
3766
names(clusters) <- gn
38-
object <- object[markers$gene, ]
39-
67+
4068
}else if(is.vector(markers)){
4169
if(is.null(names(markers)))
4270
print_msg("The 'markers' argument should be a named vector.",
4371
msg_type="STOP")
72+
object <- object[names(markers), , drop = FALSE]
4473
clusters <- markers
74+
names(clusters) <- make.unique(names(clusters), sep = "~")
75+
gn <- names(clusters)
76+
4577
}else{
4678
print_msg("The 'markers' argument should be a data.frame or named vector.",
4779
msg_type="STOP")
4880
}
81+
82+
if(length(grep("~", gn))){
83+
print_msg("There are duplicated gene names, handling them...", msg_type = "DEBUG")
84+
gn_non_dup <- gn[-grep("~[0-9]+$", gn)]
85+
86+
tmp_mat <- object[gn_non_dup, ]
87+
88+
gn_dup <- gn[grep("~[0-9]+$", gn)]
89+
max_dup <- max(as.numeric(sub(".*~([0-9]+)$", "\\1", gn_dup)))
90+
91+
print_msg(paste0("The maximum number of duplicates for one gene is : ", max_dup, "."), msg_type = "DEBUG")
92+
93+
for(i in 1:max_dup){
94+
print_msg(paste0("Looping though duplicate : ", i, "."), msg_type = "DEBUG")
95+
gn_dup_i <- gn_dup[grep(paste0("~", i, "$"), gn_dup)]
96+
97+
if(length(gn_dup_i) > 0){
98+
print_msg(paste0("Adding ", length(gn_dup_i), " genes to the matrix."), msg_type = "DEBUG")
99+
to_select <- gsub(gn_dup_i, pattern = "~[0-9]+$", replacement = "")
100+
print_msg("Subsetting...", msg_type = "DEBUG")
101+
sub_mat <- object[to_select, , drop = FALSE]
102+
rownames(sub_mat) <- gn_dup_i
103+
print_msg("Binding...", msg_type = "DEBUG")
104+
tmp_mat <- rbind(tmp_mat, sub_mat)
105+
}
106+
107+
}
108+
109+
object <- tmp_mat
110+
}
49111

50112
gn <- split(names(clusters), clusters)
51113

52114
obj_out <- new(Class = "ClusterSet")
53115
obj_out@gene_clusters <- gn
54-
obj_out@data <- Matrix::Matrix(object, sparse = TRUE)
116+
117+
obj_out@data <- object
55118
obj_out@gene_clusters_metadata <- list("cluster_id" = setNames(as.character(unique(clusters)),
56119
as.character(unique(clusters))),
57120
"number" = length(table(clusters)),
@@ -72,6 +135,8 @@ cluster_set_from_seurat <- function(object=NULL,
72135
"all_gene_expression_matrix" = vector(),
73136
"all_neighbor_distances" = vector())
74137

138+
obj_out <- compute_centers(obj_out)
139+
75140
return(obj_out)
76141
}
77142

@@ -100,8 +165,7 @@ cluster_set_from_matrix <- function(object=NULL,
100165
print_msg("The 'object' argument should be a data.frame or matrix.",
101166
msg_type="STOP")
102167
}
103-
104-
168+
105169
if(!inherits(markers, "list")){
106170
print_msg("The 'marker' argument should be a list.",
107171
msg_type="STOP")
@@ -114,7 +178,7 @@ cluster_set_from_matrix <- function(object=NULL,
114178
print_msg("No marker were found in the matrix.")
115179
return(new(Class = "ClusterSet"))
116180
}
117-
181+
118182
print_msg("Subsetting object.", msg_type = "INFO")
119183
object <- object[marker_found, ]
120184

R/gm_report.R

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#' @param SpatialFeaturePlot_params Some parameters for Seurat::SpatialFeaturePlot() function.
3535
#' @param SpatialDimPlot_params Some parameters for Seurat::SpatialDimPlot() function.
3636
#' @param plot_ggheatmap_params Some parameters for plot_ggheatmap() function.
37+
#' @param subsample_by_ident_params The number of cell to take per cell type when subsampling the data for plotting interactive heatmap.
38+
#' Reduce this number if you have a large dataset (e.g. > 10000 cells/spots). Otherwise the dataset will be too large to be handled by the web browser.
3739
#' @param plot_heatmap_params Some parameters for plot_heatmap() function.
3840
#' @param cnetplot_params Some parameters for enrichplot:::cnetplot.enrichResult() function.
3941
#' @param rm_tmpdir Whether to delete temporary directory.
@@ -58,7 +60,7 @@
5860
#' set_verbosity(3)
5961
#' load_example_dataset('7870305/files/lymph_node_tiny_clusters_2')
6062
#' load_example_dataset('7870305/files/lymph_node_tiny_2')
61-
#' gm_report(lymph_node_tiny_clusters_2[1:4,],
63+
#' gm_report(lymph_node_tiny_clusters_2[1:2,],
6264
#' lymph_node_tiny_2,
6365
#' smp_species="Homo sapiens",
6466
#' smp_region="total",
@@ -70,6 +72,19 @@
7072
#' is_spatial_exp=TRUE,
7173
#' SpatialFeaturePlot_params=list(pt.size.factor = 3000),
7274
#' SpatialDimPlot_params=list(pt.size.factor = 3000)) # Object was created with an older seurat version
75+
#' set_verbosity(3)
76+
#' markers <- Seurat::FindAllMarkers(lymph_node_tiny_2, only.pos = TRUE)
77+
#' cs <- cluster_set_from_seurat(lymph_node_tiny_2, markers, p_val_adj=0.001, assay="Spatial")
78+
#' gm_report(cs[1:2,],
79+
#' lymph_node_tiny_2,
80+
#' smp_species="Homo sapiens",
81+
#' smp_region="total",
82+
#' smp_organ="lymph node",
83+
#' smp_stage="adult",
84+
#' annotation_src="CC",
85+
#' is_spatial_exp=TRUE,
86+
#' bioc_org_db="org.Hs.eg.db",
87+
#' api_key=NULL) # Object was created with an older seurat version
7388
#' @importFrom fs path_home
7489
#' @importFrom bookdown render_book
7590
#' @importFrom xaringanExtra use_panelset
@@ -129,7 +144,10 @@ gm_report <- function(cluster_set = NULL,
129144
SpatialFeaturePlot_params=list(pt.size.factor = 1.7),
130145
SpatialDimPlot_params=list(pt.size.factor = 1.7),
131146
plot_ggheatmap_params=list(use_top_genes=FALSE,
132-
hide_gene_name=TRUE),
147+
hide_gene_name=TRUE,
148+
xlab = "Cells/Spots",
149+
ylab="Genes"),
150+
subsample_by_ident_params=list(nbcell=200),
133151
plot_heatmap_params=list(link="complete",
134152
use_top_genes=FALSE,
135153
interactive=TRUE,
@@ -149,6 +167,8 @@ gm_report <- function(cluster_set = NULL,
149167
"exp_genes",
150168
"exp_heatmap",
151169
"exp_dimplot",
170+
"exp_metrics",
171+
"exp_pop",
152172
"exp_spatial_dist",
153173
"exp_spatial_dimplot",
154174
"exp_mean_1",
@@ -230,12 +250,12 @@ gm_report <- function(cluster_set = NULL,
230250
}
231251

232252
add_module_score_used_params$object <- seurat_object
233-
add_module_score_used_params$features <- cluster_set@gene_clusters
253+
add_module_score_used_params$features <- lapply(cluster_set@gene_clusters, gsub, pattern = "~[0-9]+$", replacement = "_")
234254
add_module_score_used_params$name <- "MOD_"
235255
seurat_object <- do.call(Seurat::AddModuleScore, add_module_score_used_params)
236256

237257
tmp_dir <- tempdir(check = FALSE)
238-
tmp_dir <- paste0(tmp_dir, format(Sys.time(), "%a-%b-%e-%H-%M-%S-%Y"))
258+
tmp_dir <- paste0(tmp_dir, gsub(" ", "", format(Sys.time(), "%a-%b-%H-%M-%S-%Y")))
239259

240260
dir.create(tmp_dir, showWarnings = FALSE, recursive = TRUE)
241261

@@ -252,7 +272,6 @@ gm_report <- function(cluster_set = NULL,
252272
print_msg(paste0("The temporary dir contains file: ", tpfile))
253273
}
254274

255-
256275
print_msg("Computing top genes.", msg_type = "DEBUG")
257276
cluster_set <- top_genes(cluster_set)
258277

@@ -275,7 +294,6 @@ gm_report <- function(cluster_set = NULL,
275294
}
276295

277296

278-
279297
module_rmd <- file.path(tmp_dir, "module.Rmd")
280298

281299
print_msg("Preparing parameters for the report.")

R/plot_profiles.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ plot_profiles <- function(data = NULL,
172172
axis.text.y = ggplot2::element_text(size = size_text_y),
173173
strip.text = ggplot2::element_blank()
174174
) +
175-
ggplot2::scale_fill_manual(values=color_cluster, name=legend_name)
175+
ggplot2::scale_fill_manual(values=color_cluster, name=legend_name) +
176+
ggplot2::xlab("Cell Indentity")
176177

177178
}
178179

@@ -297,14 +298,14 @@ plot_multi_profiles <- function(data = NULL,
297298

298299
m <- reshape2::melt(centers_summarized_by_cell_type)
299300

300-
colnames(m) <- c("Cluster", "Cell_type", "Intensity")
301-
m$Cell_type <- factor(m$Cell_type, levels=levels(ident), ordered = TRUE)
301+
colnames(m) <- c("Cluster", "Cell_Indentity", "Intensity")
302+
m$Cell_Indentity <- factor(m$Cell_Indentity, levels=levels(ident), ordered = TRUE)
302303
m$Cluster <- factor(m$Cluster, levels=unique(m$Cluster), ordered = TRUE)
303304

304305
Cluster <- NULL
305306
ggplot2::ggplot(data= m,
306307
ggplot2::aes(
307-
x = .data[["Cell_type"]],
308+
x = .data[["Cell_Indentity"]],
308309
y = .data[["Intensity"]],
309310
group = Cluster,
310311
color = .data[["Cluster"]]

0 commit comments

Comments
 (0)