Spaces:
Sleeping
Sleeping
File size: 14,505 Bytes
2999286 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
#!/usr/bin/env python # coding: utf-8 # # Preprocessing the data of scRNA-seq with omicverse # # The count table, a numeric matrix of genes × cells, is the basic input data structure in the analysis of single-cell RNA-sequencing data. A common preprocessing step is to adjust the counts for variable sampling efficiency and to transform them so that the variance is similar across the dynamic range. # # Suitable methods to preprocess the scRNA-seq is important. Here, we introduce some preprocessing step to help researchers can perform downstream analysis easyier. # # User can compare our tutorial with [scanpy'tutorial](https://scanpy-tutorials.readthedocs.io/en/latest/pbmc3k.html) to learn how to use omicverse well # # Colab_Reproducibility:https://colab.research.google.com/drive/1DXLSls_ppgJmAaZTUvqazNC_E7EDCxUe?usp=sharing # In[1]: import scanpy as sc import omicverse as ov ov.plot_set() # The data consist of 3k PBMCs from a Healthy Donor and are freely available from 10x Genomics ([here](http://cf.10xgenomics.com/samples/cell-exp/1.1.0/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz) from this [webpage](https://support.10xgenomics.com/single-cell-gene-expression/datasets/1.1.0/pbmc3k)). On a unix system, you can uncomment and run the following to download and unpack the data. The last line creates a directory for writing processed data. # In[ ]: # !mkdir data get_ipython().system('wget http://cf.10xgenomics.com/samples/cell-exp/1.1.0/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz -O data/pbmc3k_filtered_gene_bc_matrices.tar.gz') get_ipython().system('cd data; tar -xzf pbmc3k_filtered_gene_bc_matrices.tar.gz') # !mkdir write # In[2]: adata = sc.read_10x_mtx( 'data/filtered_gene_bc_matrices/hg19/', # the directory with the `.mtx` file var_names='gene_symbols', # use gene symbols for the variable names (variables-axis index) cache=True) # write a cache file for faster subsequent reading adata # In[3]: adata.var_names_make_unique() adata.obs_names_make_unique() # ## Preprocessing # # ### Quantity control # # For single-cell data, we require quality control prior to analysis, including the removal of cells containing double cells, low-expressing cells, and low-expressing genes. In addition to this, we need to filter based on mitochondrial gene ratios, number of transcripts, number of genes expressed per cell, cellular Complexity, etc. For a detailed description of the different QCs please see the document: https://hbctraining.github.io/scRNA-seq/lessons/04_SC_quality_control.html # # <div class="admonition warning"> # <p class="admonition-title">Note</p> # <p> # if the version of `omicverse` larger than `1.6.4`, the `doublets_method` can be set between `scrublet` and `sccomposite`. # </p> # </div> # # COMPOSITE (COMpound POiSson multIplet deTEction model) is a computational tool for multiplet detection in both single-cell single-omics and multiomics settings. It has been implemented as an automated pipeline and is available as both a cloud-based application with a user-friendly interface and a Python package. # # Hu, H., Wang, X., Feng, S. et al. A unified model-based framework for doublet or multiplet detection in single-cell multiomics data. Nat Commun 15, 5562 (2024). https://doi.org/10.1038/s41467-024-49448-x # In[4]: get_ipython().run_cell_magic('time', '', "adata=ov.pp.qc(adata,\n tresh={'mito_perc': 0.2, 'nUMIs': 500, 'detected_genes': 250},\n doublets_method='sccomposite',\n batch_key=None)\nadata\n") # ### High variable Gene Detection # # Here we try to use Pearson's method to calculate highly variable genes. This is the method that is proposed to be superior to ordinary normalisation. See [Article](https://www.nature.com/articles/s41592-023-01814-1#MOESM3) in *Nature Method* for details. # # normalize|HVGs:We use | to control the preprocessing step, | before for the normalisation step, either `shiftlog` or `pearson`, and | after for the highly variable gene calculation step, either `pearson` or `seurat`. Our default is `shiftlog|pearson`. # # - if you use `mode`=`shiftlog|pearson` you need to set `target_sum=50*1e4`, more people like to se `target_sum=1e4`, we test the result think 50*1e4 will be better # - if you use `mode`=`pearson|pearson`, you don't need to set `target_sum` # # <div class="admonition warning"> # <p class="admonition-title">Note</p> # <p> # if the version of `omicverse` lower than `1.4.13`, the mode can only be set between `scanpy` and `pearson`. # </p> # </div> # # In[5]: get_ipython().run_cell_magic('time', '', "adata=ov.pp.preprocess(adata,mode='shiftlog|pearson',n_HVGs=2000,)\nadata\n") # Set the .raw attribute of the AnnData object to the normalized and logarithmized raw gene expression for later use in differential testing and visualizations of gene expression. This simply freezes the state of the AnnData object. # In[6]: get_ipython().run_cell_magic('time', '', 'adata.raw = adata\nadata = adata[:, adata.var.highly_variable_features]\nadata\n') # ## Principal component analysis # # In contrast to scanpy, we do not directly scale the variance of the original expression matrix, but store the results of the variance scaling in the layer, due to the fact that scale may cause changes in the data distribution, and we have not found scale to be meaningful in any scenario other than a principal component analysis # In[7]: get_ipython().run_cell_magic('time', '', 'ov.pp.scale(adata)\nadata\n') # If you want to perform pca in normlog layer, you can set `layer`=`normlog`, but we think scaled is necessary in PCA. # In[8]: get_ipython().run_cell_magic('time', '', "ov.pp.pca(adata,layer='scaled',n_pcs=50)\nadata\n") # In[9]: adata.obsm['X_pca']=adata.obsm['scaled|original|X_pca'] ov.pl.embedding(adata, basis='X_pca', color='CST3', frameon='small') # ## Embedding the neighborhood graph # # We suggest embedding the graph in two dimensions using UMAP (McInnes et al., 2018), see below. It is potentially more faithful to the global connectivity of the manifold than tSNE, i.e., it better preserves trajectories. In some ocassions, you might still observe disconnected clusters and similar connectivity violations. They can usually be remedied by running: # In[10]: get_ipython().run_cell_magic('time', '', "ov.pp.neighbors(adata, n_neighbors=15, n_pcs=50,\n use_rep='scaled|original|X_pca')\n") # You also can use `umap` to visualize the neighborhood graph # In[11]: get_ipython().run_cell_magic('time', '', 'ov.pp.umap(adata)\n') # In[12]: ov.pl.embedding(adata, basis='X_umap', color='CST3', frameon='small') # To visualize the PCA’s embeddings, we use the `pymde` package wrapper in omicverse. This is an alternative to UMAP that is GPU-accelerated. # In[13]: ov.pp.mde(adata,embedding_dim=2,n_neighbors=15, basis='X_mde', n_pcs=50, use_rep='scaled|original|X_pca',) # In[14]: ov.pl.embedding(adata, basis='X_mde', color='CST3', frameon='small') # ## Score cell cyle # # In OmicVerse, we store both G1M/S and G2M genes into the function (both human and mouse), so you can run cell cycle analysis without having to manually enter cycle genes! # In[19]: adata_raw=adata.raw.to_adata() ov.pp.score_genes_cell_cycle(adata_raw,species='human') # In[21]: ov.pl.embedding(adata_raw, basis='X_mde', color='phase', frameon='small') # ## Clustering the neighborhood graph # # As with Seurat and many other frameworks, we recommend the Leiden graph-clustering method (community detection based on optimizing modularity) by Traag *et al.* (2018). Note that Leiden clustering directly clusters the neighborhood graph of cells, which we already computed in the previous section. # In[22]: ov.pp.leiden(adata,resolution=1) # We redesigned the visualisation of embedding to distinguish it from scanpy's embedding by adding the parameter `fraemon='small'`, which causes the axes to be scaled with the colourbar # In[23]: ov.pl.embedding(adata, basis='X_mde', color=['leiden', 'CST3', 'NKG7'], frameon='small') # We also provide a boundary visualisation function `ov.utils.plot_ConvexHull` to visualise specific clusters. # # Arguments: # - color: if None will use the color of clusters # - alpha: default is 0.2 # In[24]: import matplotlib.pyplot as plt fig,ax=plt.subplots( figsize = (4,4)) ov.pl.embedding(adata, basis='X_mde', color=['leiden'], frameon='small', show=False, ax=ax) ov.pl.ConvexHull(adata, basis='X_mde', cluster_key='leiden', hull_cluster='0', ax=ax) # If you have too many labels, e.g. too many cell types, and you are concerned about cell overlap, then consider trying the `ov.utils.gen_mpl_labels` function, which improves text overlap. # In addition, we make use of the `patheffects` function, which makes our text have outlines # # - adjust_kwargs: it could be found in package `adjusttext` # - text_kwargs: it could be found in class `plt.texts` # In[25]: from matplotlib import patheffects import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(4,4)) ov.pl.embedding(adata, basis='X_mde', color=['leiden'], show=False, legend_loc=None, add_outline=False, frameon='small',legend_fontoutline=2,ax=ax ) ov.utils.gen_mpl_labels( adata, 'leiden', exclude=("None",), basis='X_mde', ax=ax, adjust_kwargs=dict(arrowprops=dict(arrowstyle='-', color='black')), text_kwargs=dict(fontsize= 12 ,weight='bold', path_effects=[patheffects.withStroke(linewidth=2, foreground='w')] ), ) # In[26]: marker_genes = ['IL7R', 'CD79A', 'MS4A1', 'CD8A', 'CD8B', 'LYZ', 'CD14', 'LGALS3', 'S100A8', 'GNLY', 'NKG7', 'KLRB1', 'FCGR3A', 'MS4A7', 'FCER1A', 'CST3', 'PPBP'] # In[27]: sc.pl.dotplot(adata, marker_genes, groupby='leiden', standard_scale='var'); # ## Finding marker genes # # Let us compute a ranking for the highly differential genes in each cluster. For this, by default, the .raw attribute of AnnData is used in case it has been initialized before. The simplest and fastest method to do so is the t-test. # In[28]: sc.tl.dendrogram(adata,'leiden',use_rep='scaled|original|X_pca') sc.tl.rank_genes_groups(adata, 'leiden', use_rep='scaled|original|X_pca', method='t-test',use_raw=False,key_added='leiden_ttest') sc.pl.rank_genes_groups_dotplot(adata,groupby='leiden', cmap='Spectral_r',key='leiden_ttest', standard_scale='var',n_genes=3) # cosg is also considered to be a better algorithm for finding marker genes. Here, omicverse provides the calculation of cosg # # Paper: [Accurate and fast cell marker gene identification with COSG](https://academic.oup.com/bib/advance-article-abstract/doi/10.1093/bib/bbab579/6511197?redirectedFrom=fulltext) # # Code: https://github.com/genecell/COSG # # In[29]: sc.tl.rank_genes_groups(adata, groupby='leiden', method='t-test',use_rep='scaled|original|X_pca',) ov.single.cosg(adata, key_added='leiden_cosg', groupby='leiden') sc.pl.rank_genes_groups_dotplot(adata,groupby='leiden', cmap='Spectral_r',key='leiden_cosg', standard_scale='var',n_genes=3) # ## Other plotting # # Next, let's try another chart, which we call the Stacked Volcano Chart. We need to prepare two dictionaries, a `data_dict` and a `color_dict`, both of which have the same key requirements. # # For `data_dict`. we require the contents within each key to be a DataFrame containing ['names','logfoldchanges','pvals_adj'], where names stands for gene names, logfoldchanges stands for differential expression multiplicity, pvals_adj stands for significance p-value # # In[51]: data_dict={} for i in adata.obs['leiden'].cat.categories: data_dict[i]=sc.get.rank_genes_groups_df(adata, group=i, key='leiden_ttest', pval_cutoff=None,log2fc_min=None) # In[65]: data_dict.keys() # In[64]: data_dict[i].head() # For `color_dict`, we require that the colour to be displayed for the current key is stored within each key.` # In[63]: type_color_dict=dict(zip(adata.obs['leiden'].cat.categories, adata.uns['leiden_colors'])) type_color_dict # There are a number of parameters available here for us to customise the settings. Note that when drawing stacking_vol with omicverse version less than 1.4.13, there is a bug that the vertical coordinate is constant at [-15,15], so we have added some code in this tutorial for visualisation. # # - data_dict: dict, in each key, there is a dataframe with columns of ['logfoldchanges','pvals_adj','names'] # - color_dict: dict, in each key, there is a color for each omic # - pval_threshold: float, pvalue threshold for significant genes # - log2fc_threshold: float, log2fc threshold for significant genes # - figsize: tuple, figure size # - sig_color: str, color for significant genes # - normal_color: str, color for non-significant genes # - plot_genes_num: int, number of genes to plot # - plot_genes_fontsize: int, fontsize for gene names # - plot_genes_weight: str, weight for gene names # In[62]: fig,axes=ov.utils.stacking_vol(data_dict,type_color_dict, pval_threshold=0.01, log2fc_threshold=2, figsize=(8,4), sig_color='#a51616', normal_color='#c7c7c7', plot_genes_num=2, plot_genes_fontsize=6, plot_genes_weight='bold', ) #The following code will be removed in future y_min,y_max=0,0 for i in data_dict.keys(): y_min=min(y_min,data_dict[i]['logfoldchanges'].min()) y_max=max(y_max,data_dict[i]['logfoldchanges'].max()) for i in adata.obs['leiden'].cat.categories: axes[i].set_ylim(y_min,y_max) plt.suptitle('Stacking_vol',fontsize=12) # In[ ]: |