TranspaceR/Tutorial_TranspaceR.md at main · BOSTLAB/TranspaceR · GitHub
Skip to content

Latest commit

 

History

History
181 lines (151 loc) · 7.8 KB

Tutorial_TranspaceR.md

File metadata and controls

181 lines (151 loc) · 7.8 KB

Overview

This document presents the steps to analyze a subset (portion located between 2000 and 4000 on both axis) of the dataset Xenium Human Lymph node from 10XGenomics using the TranspaceR pipeline. The subset can be found here : Expression_file and Metadata. The analysis includes data curation, variable genes selection, clustering and visualization of results.

Directory Structure

/Spatial_atlas

          └── /Datasets
                   └── /Xenium
                          └── /Lymph_node
                                └── Xenium_Lymph_node_ExprMat.csv
                                └── Xenium_Lymph_node_Metadata.csv
          └── /Picture_pipeline
                   └── /Xenium
                          └── /Lymph_node
                                └── Saved Plots
                                └── /Variogram_plots
                                          └── Variogram plots

Loading

path = '~/Spatial_atlas/Datasets/Xenium/Lymph_node/'
Output_path = "~/Spatial_atlas/Picture_pipeline/Xenium/Lymph_node/"
Method = "Xenium"
Tissue = "Lymph_node"

Expression_file = read.delim(paste(path,"Xenium_Lymph_node_ExprMat.csv",sep =""),sep=",")
Meta_data = read.delim(paste(path,"Xenium_Lymph_node_Metadata.csv",sep =""),sep=",")

# In Meta_data, cell area, x and y coordinates MUST be named respectively 'Area, 'cell_centroid_x' and 'cell_centroid_y'
# colnames(Meta_data)[c(3,4)] <- c("cell_centroid_x", "cell_centroid_y")
# colnames(Meta_data)[10] = 'Area'


dim(Expression_file)
dim(Meta_data)
[1] 39047    539
[1] 39047     10

The dataset is loaded, if existing the columns of Expression_file containing unfit data must be removed. We should obtain a matrix that exclusively contains RNA counts for each gene (539) across individual cells (39047). The expression matrix and the metadata file should have the same cells.

Step 1: Quality control

Meta_data = Compute_radius(Meta_data,Method)
QC1_results =  QC_RNA_size_threshold(Expression_file,Meta_data,Method,Tissue,Output_path)
QC2_results =  QC_Gene_threshold(Expression_file,Meta_data,Method,Tissue,Output_path)
print('Otsu's threshold is', QC2_results$Otsu_threshold)
[1] Otsu's threshold is 53

These two plots are saved in the output path. The 2D density plot from QC1, which illustrates the relationship between the number of transcripts and the radius of cells, the appropriate thresholds. From QC2, Otsu's threshold for the number of transcripts per gene is automatically computed and utilized within the `curate_data` function as below.

Data curation

Data_curated = Curate_data(Expression_file,Meta_data,QC2_results,min_lib_size=10,max_lib_size=250,min_cell_radius=2,max_cell_radius=8)
Expression_file = Data_curated$Expression_file
Meta_data = Data_curated$Meta_data
dim(Expression_file)
dim(Meta_data)
[1] 38804    376
[1] 38804    11

After curation, about 99% of initial cells and 69% of initial transcripts are kept for further analysis.

Step 2 : Gene selection

Computation of scoring methods based on the total variance, zero proportion and spatial variance. The plots are saved in the provided Output_path. The selected genes from each scoring method are highlighted in red.

Total variance

Variance_computation = Excess_variance_ratio_NB(Expression_file,Output_path,Method,Tissue)
Variance_genes= Variance_computation$Selected_genes

Zero proportion genes

Zero_score = Excess_zero_score_NB(Expression_file,Output_path,Method,Tissue,P_value_threshold=0.01,Delta_threshold = 0.01)
Zero_genes = Zero_score_computation$Selected_genes

Spatially variable genes

Geary's C

Geary_computation = Geary_C_score(Expression_file,Meta_data,Output_path,Method,Tissue,pvalue = 0.01)
Geary_genes = Geary_computation$Selected_genes

The plot depicting the correlation between the Spatial variance score and Total variance score can be saved in the output path. It shows a correlation of 0.302 between the two methods.

Save_geary_variance_plot(Variance_computation,Geary_computation,Output_path,Method,Tissue)

Shared_genes = Select_genes(Selected_objects =list(Variance_genes,Zero_genes,Geary_genes),
                              Selected_names = c('Variance_genes','Zero_genes','Geary_genes'))

The selection of all the genes selected by at least one method is illustrated by this Upset Plot (saved automatically).

Step 3 : Clustering and annotation results

Clustering

Clustering_output = Clusters_maker(Expression_file, Shared_genes, K=30, metric_used="L2", nThreads = 20, resolution = 1)
Clustering = Clustering_output$Clustering  
PCA_data = Clustering_output$PCA_data
Mean_expression = Clustering_output$Mean_expression
Data_correction = Clustering_output$Data_correction
Log2FC_table = Clustering_output$Log2FC_table

The ouptuts of the function Clusters_maker() include the list of cluster affiliation, the PCA results, the log2FC table and the mean expression of genes within each cluster file.

Save_heatmap_markers(Expression_file, object = Clustering,Data_correction, Output_path,Method,Tissue,name_object = 'Clustering',n_top_genes = 5)
library(spatstat)
Save_tissue_visualization(Meta_data,object = Clustering, Output_path,Method,Tissue,name_object = 'Clustering',scaling_factor=1.5)

Make sure the 'name_object' parameter is never 'KNN' as it is the name used in the comparison plot output.

Scimilarity Annotation

  Annotation_cells_renamed = Load_scimilarity_results(path,celltype_threshold=0.01)
  Save_annotation_plot(Annotation_cells_renamed,Output_path,Method,Tissue)

Save_heatmap_markers(Expression_file, object = Annotation_cells_renamed, Output_path,Method,Tissue,name_object = 'Annotation')

Save_boxplot(Data_correction,object = Annotation_cells_renamed,gene = 'MS4A1', Output_path,Method,Tissue,name_object = 'Annotation')

Make sure the 'name_object' parameter is never 'Scimilarity' as it is the name_used in the comparison plot output.

Step 4 : Comparison KNN Clustering and Annotation

Save_comparison_plots(Clustering,Annotation_cells_renamed,Umap_result,Output_path,Method,Tissue,scaling_factor=1.5)
Scimilarity Atlas KNN Atlas

Save_dendogram(Clustering,Mean_expression,Annotation_cells_renamed,Output_path,Method,Tissue)

A tutorial to analyze multiple samples is available here

Notes:

  • Some dependencies can be missed when loading the package and should be installed manually.
  • The analysis can be expanded with additional steps or insights as needed.