diff --git a/R/spatial_enrichment.R b/R/spatial_enrichment.R index 9c0254ec7..4f5314302 100644 --- a/R/spatial_enrichment.R +++ b/R/spatial_enrichment.R @@ -136,7 +136,7 @@ do_rank_permutation <- function(sc_gene, n){ do_page_permutation<-function(gobject, sig_gene, ntimes){ - # check available gene + # check available gene available_ct<-c() for (i in colnames(sig_gene)){ gene_i=rownames(sig_gene)[which(sig_gene[,i]==1)] @@ -221,8 +221,8 @@ PAGEEnrich <- function(gobject, # expression values to be used values = match.arg(expression_values, c('normalized', 'scaled', 'custom')) expr_values = select_expression_values(gobject = gobject, values = values) - - # check available gene + + # check available gene available_ct<-c() for (i in colnames(sign_matrix)){ gene_i=rownames(sign_matrix)[which(sign_matrix[,i]==1)] @@ -359,7 +359,7 @@ rankEnrich <- function(gobject, multiplyRank = (filterRankFold*filterSig[,i])^(1/2) rpb = 0.01*(0.99^(multiplyRank-1)) - + vectorX = rep(NA, dim(filterRankFold)[2]) for (j in (1:dim(filterRankFold)[2])){ @@ -503,7 +503,7 @@ createSpatialEnrich = function(gobject, expression_values = c('normalized', 'scaled', 'custom'), reverse_log_scale = TRUE, logbase = 2, - p_value = TRUE, + p_value = FALSE, n_genes = 100, n_times = 1000, top_percentage = 5, @@ -527,7 +527,7 @@ createSpatialEnrich = function(gobject, # default name for page enrichment if(is.null(name)) name = 'PAGE' if (p_value==TRUE){ - # check available gene + # check available gene available_ct<-c() for (i in colnames(sign_matrix)){ gene_i=rownames(sign_matrix)[which(sign_matrix[,i]==1)] diff --git a/inst/examples/mini_starmap_test/mini_starmap_v0.3.5_200614.R b/inst/examples/mini_starmap_test/mini_starmap_v0.3.5_200614.R new file mode 100644 index 000000000..5b24f2a6c --- /dev/null +++ b/inst/examples/mini_starmap_test/mini_starmap_v0.3.5_200614.R @@ -0,0 +1,160 @@ + + +library(Giotto) + +# createGiottoInstructions(python_path = '/your/path') + +## Giotto 0.3.5 ## +## mini-test Visium Brain Giotto 0.3.5 ## + +## !! change this directory path !!: +temp_dir = '/path/to/your/temporary/directory/results' +temp_dir = '/Volumes/Ruben_Seagate/Dropbox/Projects/GC_lab/Ruben_Dries/190225_spatial_package/Results/Temp/starmap/' + +## 1. giotto object #### +expr_path = system.file("extdata", "starmap_expr.txt", package = 'Giotto') +loc_path = system.file("extdata", "starmap_cell_loc.txt", package = 'Giotto') + +# default +star_small <- createGiottoObject(raw_exprs = expr_path, spatial_locs = loc_path) + +## 2. processing steps #### +filterDistributions(star_small, detection = 'genes') +filterDistributions(star_small, detection = 'cells') +filterCombinations(star_small, + expression_thresholds = c(1), + gene_det_in_min_cells = c(50, 100, 200), + min_det_genes_per_cell = c(20, 28, 28)) + +star_small <- filterGiotto(gobject = star_small, + expression_threshold = 1, + gene_det_in_min_cells = 50, + min_det_genes_per_cell = 20, + expression_values = c('raw'), + verbose = T) +star_small <- normalizeGiotto(gobject = star_small, scalefactor = 6000, verbose = T) +star_small <- addStatistics(gobject = star_small) + + +## 3. dimension reduction #### +star_small <- runPCA(gobject = star_small, method = 'factominer') +screePlot(star_small, ncp = 30) +plotPCA(gobject = star_small) + +# 2D umap +star_small <- runUMAP(star_small, dimensions_to_use = 1:8) +plotUMAP(gobject = star_small) + +# 3D umap +star_small <- runUMAP(star_small, dimensions_to_use = 1:8, name = '3D_umap', n_components = 3) +plotUMAP_3D(gobject = star_small, dim_reduction_name = '3D_umap') + +# 2D tsne +star_small <- runtSNE(star_small, dimensions_to_use = 1:8) +plotTSNE(gobject = star_small) + +## 4. clustering #### +star_small <- createNearestNetwork(gobject = star_small, dimensions_to_use = 1:8, k = 25) +star_small <- doLeidenCluster(gobject = star_small, resolution = 0.5, n_iterations = 1000) + +# 2D umap +plotUMAP(gobject = star_small, cell_color = 'leiden_clus', show_NN_network = T, point_size = 2.5) + +# 3D umap +plotUMAP_3D(gobject = star_small, dim_reduction_name = '3D_umap', cell_color = 'leiden_clus') + + +## 5. co-visualize #### + +# 2D +spatDimPlot(gobject = star_small, cell_color = 'leiden_clus', + dim_point_size = 2, spat_point_size = 2.5) + +# 3D +spatDimPlot3D(gobject = star_small, cell_color = 'leiden_clus', dim_reduction_name = '3D_umap') + + + +## 6. differential expression #### +markers = findMarkers_one_vs_all(gobject = star_small, + method = 'gini', + expression_values = 'normalized', + cluster_column = 'leiden_clus') + +# violinplot +topgenes = markers[, head(.SD, 2), by = 'cluster']$genes +violinPlot(star_small, genes = topgenes, cluster_column = 'leiden_clus') + +# genes heatmap +plotHeatmap(star_small, genes = star_small@gene_ID, cluster_column = 'leiden_clus', + legend_nrows = 2, expression_values = 'scaled', + cluster_order = 'correlation', gene_order = 'correlation') + +# cluster heatmap +plotMetaDataHeatmap(star_small, expression_values = 'scaled', metadata_cols = c('leiden_clus')) + + + +## 7. gene expression +dimGenePlot3D(star_small, + dim_reduction_name = '3D_umap', + expression_values = 'scaled', + genes = "Pcp4", + genes_high_color = 'red', genes_mid_color = 'white', genes_low_color = 'darkblue') + +spatGenePlot3D(star_small, + expression_values = 'scaled', + genes = "Pcp4", + show_other_cells = F, + genes_high_color = 'red', genes_mid_color = 'white', genes_low_color = 'darkblue') + + + + +## 8. cross section +# install.packages('geometry') # necessary for 3D delaunay network +star_small <- createSpatialNetwork(gobject = star_small, delaunay_method = 'delaunayn_geometry') + +star_small = createCrossSection(star_small, + method="equation", + equation=c(0,1,0,600), + extend_ratio = 0.6) + +# show cross section +insertCrossSectionSpatPlot3D(star_small, cell_color = 'leiden_clus', + axis_scale = 'cube', + point_size = 2) + +insertCrossSectionGenePlot3D(star_small, expression_values = 'scaled', + axis_scale = "cube", + genes = "Slc17a7") + + +# for cell annotation +crossSectionPlot(star_small, + point_size = 2, point_shape = "border", + cell_color = "leiden_clus") + +crossSectionPlot3D(star_small, + point_size = 2, cell_color = "leiden_clus", + axis_scale = "cube") + + +# for gene expression +crossSectionGenePlot(star_small, + genes = "Slc17a7", + point_size = 2, + point_shape = "border", + cow_n_col = 1.5, + expression_values = 'scaled') + +crossSectionGenePlot3D(star_small, + point_size = 2, + genes = c("Slc17a7"), + expression_values = 'scaled') + + + + + + diff --git a/inst/examples/mini_visium_test/mini_visium_deg_v0.3.5_200612.R b/inst/examples/mini_visium_test/mini_visium_deg_v0.3.5_200612.R index d3efc9d1c..649e845f4 100644 --- a/inst/examples/mini_visium_test/mini_visium_deg_v0.3.5_200612.R +++ b/inst/examples/mini_visium_test/mini_visium_deg_v0.3.5_200612.R @@ -9,6 +9,7 @@ library(Giotto) ## !! change this directory path !!: temp_dir = '/path/to/your/temporary/directory/results' +temp_dir = '/Volumes/Ruben_Seagate/Dropbox/Projects/GC_lab/Ruben_Dries/190225_spatial_package/Results/Temp/visium/' ## 1. giotto object #### expr_path = system.file("extdata", "visium_DG_expr.txt", package = 'Giotto') @@ -126,14 +127,25 @@ brain_sc_markers = data.table::fread(sign_matrix_path) # file don't exist in dat sig_matrix = as.matrix(brain_sc_markers[,-1]); rownames(sig_matrix) = brain_sc_markers$Event ## enrichment tests -visium_brain = createSpatialEnrich(brain_small, sign_matrix = sig_matrix, enrich_method = 'PAGE') #default = 'PAGE' +brain_small = createSpatialEnrich(brain_small, sign_matrix = sig_matrix, enrich_method = 'PAGE') #default = 'PAGE' ## heatmap of enrichment versus annotation (e.g. clustering result) cell_types = colnames(sig_matrix) -plotMetaDataCellsHeatmap(gobject = visium_brain, +plotMetaDataCellsHeatmap(gobject = brain_small, metadata_cols = 'leiden_clus', value_cols = cell_types, - spat_enr_names = 'PAGE',x_text_size = 8, y_text_size = 8) + spat_enr_names = 'PAGE', + x_text_size = 8, y_text_size = 8) + + +enrichment_results = brain_small@spatial_enrichment$PAGE +enrich_cell_types = colnames(enrichment_results) +enrich_cell_types = enrich_cell_types[enrich_cell_types != 'cell_ID'] + +## spatplot +spatCellPlot(gobject = brain_small, spat_enr_names = 'PAGE', + cell_annotation_values = enrich_cell_types, + cow_n_col = 3,coord_fix_ratio = NULL, point_size = 1)