### Install BANKSY from Source for Development Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Clone the BANKSY repository and install it in editable mode for development purposes. ```bash git clone https://github.com/prabhakarlab/Banksy_py.git cd Banksy_py pip install -e . ``` -------------------------------- ### Install BANKSY from GitHub Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Install the BANKSY package directly from its GitHub repository. ```bash pip install git+https://github.com/prabhakarlab/Banksy_py.git ``` -------------------------------- ### Install Optional Dependencies from Source for BANKSY Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Install optional dependencies for BANKSY when installing from source. Use '.[notebooks]', '.[mclust]', or '.[all]'. ```bash pip install -e ".[notebooks]" ``` ```bash pip install -e ".[mclust]" ``` ```bash pip install -e ".[all]" ``` -------------------------------- ### Install All Optional Dependencies for BANKSY Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Use this command to install BANKSY with all optional dependencies, including mclust and Jupyter. ```bash pip install "pybanksy[all]" ``` -------------------------------- ### Install Harmony Integration for BANKSY Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Install the Harmony PyTorch package using conda, required for running the harmony integration example notebook. ```bash conda install bioconda::harmony-pytorch ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv1_analysis.ipynb Imports necessary libraries and configures environment settings for data analysis. Sets random seeds for reproducibility. ```python import os, re import numpy as np import pandas as pd from IPython.display import display import warnings warnings.filterwarnings("ignore") import scipy.sparse as sparse from scipy.io import mmread from scipy.stats import pearsonr, pointbiserialr import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from matplotlib.lines import Line2D import seaborn as sns import scanpy as sc sc.logging.print_header() sc.set_figure_params(facecolor="white", figsize=(8, 8)) sc.settings.verbosity = 1 # errors (0), warnings (1), info (2), hints (3) plt.rcParams["font.family"] = "Arial" sns.set_style("white") import random # Note that BANKSY itself is deterministic, here the seeds affect the umap clusters and leiden partition seed = 1234 np.random.seed(seed) random.seed(seed) ``` -------------------------------- ### Install pybanksy Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Install the pybanksy package using pip. Optional dependencies can be included for notebooks, mclust, or all functionalities. ```bash pip install pybanksy ``` -------------------------------- ### BANKSY Analysis Setup and Execution Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb This snippet outlines the initial setup and execution of the BANKSY analysis. It shows the number of genes analyzed, gene list, and the creation of the BANKSY matrix. It also details the decay types and dimensionality reduction steps. ```python results_df_modi ``` -------------------------------- ### Import Libraries and Setup Environment Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv2_analysis.ipynb Imports essential Python libraries for data manipulation, scientific computing, plotting, and spatial transcriptomics analysis. Sets up Scanpy's logging and figure parameters, and initializes random seeds for reproducibility. ```python import os, re import numpy as np import pandas as pd from IPython.display import display import warnings warnings.filterwarnings("ignore") import scipy.sparse as sparse from scipy.io import mmread from scipy.stats import pearsonr, pointbiserialr import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from matplotlib.lines import Line2D import seaborn as sns import scanpy as sc sc.logging.print_header() sc.set_figure_params(facecolor="white", figsize=(8, 8)) sc.settings.verbosity = 1 # errors (0), warnings (1), info (2), hints (3) plt.rcParams["font.family"] = "Arial" sns.set_style("white") import random # Note that BANKSY itself is deterministic, here the seeds affect the umap clusters and leiden partition seed = 1234 np.random.seed(seed) random.seed(seed) ``` -------------------------------- ### Install Jupyter Notebook Optional Dependency for BANKSY Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Use this command to install BANKSY with Jupyter notebook support. ```bash pip install "pybanksy[notebooks]" ``` -------------------------------- ### Quick Start: Initialize and Run BANKSY Clustering Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Load spatial transcriptomics data, initialize BANKSY with specified parameters, and run clustering with multiple lambda and resolution values. ```python import scanpy as sc from banksy.initialize_banksy import initialize_banksy from banksy.run_banksy import run_banksy_multiparam # Load your spatial transcriptomics data adata = sc.read_h5ad("your_data.h5ad") # Initialize BANKSY coord_keys = ('x', 'y', 'spatial') # Adjust based on your data banksy_dict = initialize_banksy( adata, coord_keys=coord_keys, num_neighbours=15, nbr_weight_decay='scaled_gaussian' ) # Run BANKSY clustering results_df = run_banksy_multiparam( adata, banksy_dict, lambda_list=[0.2], resolutions=[0.5, 1.0] ) ``` -------------------------------- ### Install mclust Optional Dependency for BANKSY Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Use this command to install BANKSY with mclust clustering support. Requires R and rpy2. ```bash pip install "pybanksy[mclust]" ``` -------------------------------- ### Run Leiden Partition for Cell Clustering Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv1_analysis.ipynb Clusters cells using the Leiden algorithm. Recommended for cases where the number of clusters is unknown. Ensure 'leidenalg' is installed if needed. ```python from banksy.cluster_methods import run_Leiden_partition results_df, max_num_labels = run_Leiden_partition( banksy_dict, resolutions, num_nn = 50, num_iterations = -1, partition_seed = seed, match_labels = True, ) ``` -------------------------------- ### Scanpy Version Information Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B006_ascending.ipynb Displays the installed versions of key libraries used in the analysis, including scanpy, anndata, and others. ```text scanpy==1.9.5 anndata==0.10.2 umap==0.5.4 numpy==1.24.4 scipy==1.11.3 pandas==2.1.1 scikit-learn==1.3.1 statsmodels==0.14.0 igraph==0.10.6 pynndescent==0.5.10 ``` -------------------------------- ### Import Libraries Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_concatenate_multisample.ipynb Imports common libraries used for data analysis, including pandas, numpy, scanpy, and scipy. Ensure these libraries are installed. ```python import os,csv,re import pandas as pd import numpy as np import math from scipy.sparse import issparse import random import warnings warnings.filterwarnings("ignore") import matplotlib.colors as clr import matplotlib.pyplot as plt import scanpy as sc # opencv for reading in images only # import cv2 import time ``` -------------------------------- ### PCA and UMAP with Scaled Gaussian Decay Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_concatenate_multisample.ipynb This snippet demonstrates the setup and execution of PCA and UMAP with scaled Gaussian decay. It includes nearest-neighbor graph construction, shared nearest-neighbor analysis, and graph partitioning. The output shows the modularity and unique labels found. ```python Output: Decay type: scaled_gaussian Neighbourhood Contribution (Lambda Parameter): 0.2 reduced_pc_20 reduced_pc_20_umap PCA dims to analyse: [20] ==================================================================================================== Setting up partitioner for (nbr decay = scaled_gaussian), Neighbourhood contribution = 0.2, PCA dimensions = 20) ==================================================================================================== Nearest-neighbour weighted graph (dtype: float64, shape: (14243, 14243)) has 712150 nonzero entries. ---- Ran find_nn in 1.83 s ---- Nearest-neighbour connectivity graph (dtype: int16, shape: (14243, 14243)) has 712150 nonzero entries. (after computing shared NN) Allowing nearest neighbours only reduced the number of shared NN from 12633261 to 710946. Shared nearest-neighbour (connections only) graph (dtype: int16, shape: (14243, 14243)) has 683775 nonzero entries. Shared nearest-neighbour (number of shared neighbours as weights) graph (dtype: int16, shape: (14243, 14243)) has 683775 nonzero entries. sNN graph data: [24 19 10 ... 7 6 6] ---- Ran shared_nn in 0.45 s ---- -- Multiplying sNN connectivity by weights -- shared NN with distance-based weights graph (dtype: float64, shape: (14243, 14243)) has 683775 nonzero entries. shared NN weighted graph data: [0.19646475 0.19658107 0.19720675 ... 0.12681301 0.13242565 0.1369962 ] Converting graph (dtype: float64, shape: (14243, 14243)) has 683775 nonzero entries. ---- Ran csr_to_igraph in 0.10 s ---- Resolution: 0.6 ------------------------------ ---- Partitioned BANKSY graph ---- modularity: 0.75 7 unique labels: [0 1 2 3 4 5 6] ---- Ran partition in 3.40 s ---- No annotated labels ``` -------------------------------- ### Load Manual Annotations Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb Loads and integrates manual cell type annotations into the AnnData object. This step is crucial for validating or guiding the clustering results. ```python '''Load manual annotations''' adata = adata[adata.obs["cluster_name"].notnull()] annotations = pd.read_csv(os.path.join(file_path, "Starmap_BY3_1k_meta_annotated_18oct22.csv")) manual_labels = "smoothed_manual" # Ground truth annotations in pd.DataFrame annotation_key = 'manual_annotations' # Key to access annotations in adata.obs[annotation_keys] print(annotations.loc[:,manual_labels ]) adata.obs[annotation_key] = annotations.loc[:,manual_labels ].values adata.obs[annotation_key] = adata.obs[annotation_key].astype('category') print(adata.obs[annotation_key]) # Add spatial coordinates to '.obsm' attribute adata.obsm[coord_keys[2]] = pd.concat([adata.obs[coord_keys[0]], adata.obs[coord_keys[1]]], axis=1).to_numpy() ``` -------------------------------- ### Initialize BANKSY and Scanpy Environment Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B012_tissue_segmentation.ipynb Sets up the Python environment by importing necessary libraries and configuring scanpy settings for data analysis. Includes random seed for reproducibility. ```python # This is the simplified python script that demonstrates how to directly run slide-seq v1 dataset with BANKSY import os, time, random, gc import anndata from anndata import AnnData import numpy as np import pandas as pd import warnings from banksy_utils.color_lists import spagcn_color warnings.filterwarnings("ignore") import scanpy as sc sc.logging.print_header() sc.set_figure_params(facecolor="white", figsize=(8, 8)) sc.settings.verbosity = 1 # errors (0), warnings (1), info (2), hints (3) seed = 0 np.random.seed(seed) random.seed(seed) start = time.perf_counter_ns() ``` -------------------------------- ### Initialize BANKSY Parameters Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_harmony_multisample.ipynb Sets up initial parameters for BANKSY, including coordinate keys, neighbor weight decay, and geometric parameters like k_geom. ```python ## banksy parameters ## from banksy.initialize_banksy import initialize_banksy coord_keys = ('x_pixel', 'y_pixel', 'coord_xy') nbr_weight_decay = 'scaled_gaussian' k_geom = 18 x_coord, y_coord, xy_coord = coord_keys[0], coord_keys[1], coord_keys[2] ``` -------------------------------- ### Get Python Version Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_concatenate_multisample.ipynb Retrieves the current Python version. Useful for environment checks. ```python import platform platform.python_version() ``` -------------------------------- ### Label Expansion and Matching Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_concatenate_multisample.ipynb This snippet details the post-partitioning steps of expanding labels to cover a full range and then matching them. It shows the resulting label IDs after these operations. ```python After sorting Dataframe Shape of dataframe: (2, 7) Maximum number of labels = 7 Indices of sorted list: [0 1] Expanding labels with ids: [0 1 2 3 4 5 6] so that ids range from 0 to 6 Label ids zerod: [0 1 2 3 4 5 6]. 0 to be inserted between each id: [0 0 0 0 0 0 0] 0 extra rows to be randomly inserted: [0 0 0 0 0 0] New ids: [0 1 2 3 4 5 6] ---- Ran expand_labels in 0.00 s ---- ---- Ran match_labels in 0.00 s ---- Matched Labels ``` -------------------------------- ### Initialize Banksy and Set Random Seeds Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb Imports necessary libraries and sets up random seeds for reproducibility. This code should be run at the beginning of the analysis. ```python import warnings warnings.filterwarnings("ignore") import os, time import pandas as pd import numpy as np import random import scipy.sparse as sparse from scipy.sparse import csr_matrix, issparse from banksy.initialize_banksy import initialize_banksy from banksy.run_banksy import run_banksy_multiparam from banksy_utils.color_lists import spagcn_color start = time.perf_counter_ns() random_seed = 1234 cluster_algorithm = 'leiden' np.random.seed(random_seed) random.seed(random_seed) ``` -------------------------------- ### Display AnnData Object Summary Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv2_analysis.ipynb Use the `display_adata` function to get a summary of an AnnData object, including its dimensions, sparsity, and attributes for observations and variables. ```python display_adata(adata) ``` -------------------------------- ### BANKSY Initialization Parameters Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B012_tissue_segmentation.ipynb Sets up default parameters for BANKSY, including geometric parameters, weighting decay, clustering resolutions, PCA dimensions, and lambda values. ```python from banksy.main import median_dist_to_nearest_neighbour from banksy.initialize_banksy import initialize_banksy from banksy.embed_banksy import generate_banksy_matrix from banksy.main import concatenate_all k_geom = 15 # only for fixed type max_m = 1 # azumithal transform up to kth order nbr_weight_decay = "scaled_gaussian" # can also be "reciprocal", "uniform" or "ranked" resolutions = list(np.arange(0.08, 0.1, 0.005))#[0.08] # clustering resolution for leiden algorithm max_labels = None # Number of clusters for tissue segmentation pca_dims = [20] # Dimensionality in which PCA reduces to lambda_list = [0.8] ``` -------------------------------- ### Initializing BANKSY Analysis Parameters Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb Initialize the BANKSY dictionary with various parameters for spatial graph construction and analysis. This includes setting the number of neighbors, decay method, and plotting options. ```python resolutions = [.9] # clustering resolution for Leiden clustering pca_dims = [20] # number of dimensions to keep after PCA lambda_list = [.8] # lambda k_geom = 8 #spatial neighbours max_m = 1 # use AGF nbr_weight_decay = "scaled_gaussian" # can also be "reciprocal", "uniform" or "ranked" ``` ```python banksy_dict_modi = initialize_banksy( adata, coord_keys, k_geom, nbr_weight_decay=nbr_weight_decay, max_m=max_m, plt_edge_hist=True, plt_nbr_weights=True, plt_agf_angles=False, plt_theta=False, ) ``` -------------------------------- ### Import BANKSY Libraries and Set Parameters Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B006_ascending.ipynb Imports necessary functions from the BANKSY library and defines default parameters for domain segmentation. These parameters include k_geom, max_m, nbr_weight_decay, resolutions, max_labels, pca_dims, and lambda_list. ```python from banksy.main import median_dist_to_nearest_neighbour from banksy.initialize_banksy import initialize_banksy from banksy.embed_banksy import generate_banksy_matrix from banksy.main import concatenate_all k_geom = 15 # only for fixed type max_m = 1 # azumithal transform up to kth order br_weight_decay = "scaled_gaussian" # can also be "reciprocal", "uniform" or "ranked" resolutions = None # clustering resolution for leiden algorithm max_labels = 8 # Number of clusters for tissue segmentation pca_dims = [20] # Dimensionality in which PCA reduces to lambda_list = [0.8] ``` -------------------------------- ### Generate BANKSY Embedding Matrix Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_harmony_multisample.ipynb Generates the BANKSY embedding matrix using specified lambda parameters and maximum order 'm'. Includes setup for PCA dimensions and clustering resolutions. ```python from banksy.main import concatenate_all from banksy.embed_banksy import generate_banksy_matrix resolutions = [0.40] # clustering resolution for UMAP pca_dims = [20] # Dimensionality in which PCA reduces to lambda_list = [0.2] # list of lambda parameters m = 1 from banksy_utils.umap_pca import pca_umap from banksy.cluster_methods import run_Leiden_partition from banksy.plot_banksy import plot_results c_map = 'tab20' # specify color map ### run banksy ### # Include spatial coordinates information raw_y, raw_x = adata.obs[y_coord], adata.obs[x_coord] adata.obsm[xy_coord] = np.vstack((adata.obs[x_coord].values, adata.obs[y_coord].values)).T banksy_dict = initialize_banksy(adata, coord_keys, k_geom, nbr_weight_decay=nbr_weight_decay, max_m=m, plt_edge_hist= True, plt_nbr_weights= True, plt_agf_angles=False, plt_theta=False ) banksy_dict, banksy_matrix = generate_banksy_matrix(adata, banksy_dict, lambda_list, max_m=m) ``` -------------------------------- ### BANKSY Analysis Results DataFrame Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb This is an example of the results dataframe generated by BANKSY after an initial analysis. It includes parameters used, clustering metrics like ARI, and references to the data object. ```text Result: decay lambda_param num_pcs scaled_gaussian_pc20_nc0.80_r0.90 scaled_gaussian 0.8 20 resolution num_labels scaled_gaussian_pc20_nc0.80_r0.90 0.9 7 labels scaled_gaussian_pc20_nc0.80_r0.90 Label object:\nNumber of labels: 7, number of ... adata scaled_gaussian_pc20_nc0.80_r0.90 [[[View of AnnData object with n_obs × n_vars ... ari scaled_gaussian_pc20_nc0.80_r0.90 0.755481 ``` -------------------------------- ### Identify Mitochondrial Genes in AnnData Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv1_analysis.ipynb Adds a boolean column 'mt' to the AnnData object's variable attributes, indicating whether a gene name starts with 'MT-'. This is used for filtering or QC. ```python adata.var["mt"] = adata.var_names.str.startswith("MT-") ``` -------------------------------- ### Initialize Banksy with Visualization Options Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv2_analysis.ipynb Initializes the banksy object with specified parameters and enables several visualization options for edge histograms, neighbor weights, and theta plots. Use this to explore the spatial characteristics of your data. ```python from banksy.initialize_banksy import initialize_banksy banksy_dict = initialize_banksy( adata, coord_keys, k_geom, nbr_weight_decay=nbr_weight_decay, max_m=max_m, plt_edge_hist=True, plt_nbr_weights=True, plt_agf_angles=False, # takes long time to plot plt_theta=True, ) ``` -------------------------------- ### Run Leiden Partition and Plot Results Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B006_ascending.ipynb This snippet demonstrates how to perform Leiden clustering and visualize the results. It requires pre-defined data structures like `banksy_dict`, `resolutions`, `coord_keys`, and `file_path`. Ensure all necessary imports are available. ```python from banksy.cluster_methods import run_Leiden_partition banksy_df, max_num_labels = run_Leiden_partition( banksy_dict, resolutions, num_nn = 50, num_iterations = -1, partition_seed = 1234, match_labels = True, max_labels = max_labels, ) from banksy.plot_banksy import plot_results c_map = 'tab20' # specify color map weights_graph = banksy_dict['scaled_gaussian']['weights'][1] plot_results( banksy_df, weights_graph, c_map, match_labels = True, coord_keys = coord_keys, max_num_labels = max_num_labels, save_path = os.path.join(file_path, 'BANKSY-Results'), save_fig = False, # Save Spatial Plot Only save_fullfig = True, # Save Full Plot dataset_name = f"CODEX-{unique_regions}", save_labels=True ) banksy_df.to_csv(os.path.join(file_path, f"CODEX-{unique_regions}_BANKSY.csv")) ``` -------------------------------- ### Label Expansion and Sorting Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B006_ascending.ipynb This section details the process of expanding and sorting labels after partitioning. It identifies the resolution and handles cases with no annotated labels, preparing the data for further analysis. ```python Resolution found: 0.2 for labels: 8 Resolution: 0.2 ------------------------------ ---- Partitioned BANKSY graph ---- modularity: 0.75 7 unique labels: [0 1 2 3 4 5 6] ---- Ran partition in 4.32 s ---- No annotated labels After sorting Dataframe Shape of dataframe: (1, 7) Maximum number of labels = 7 Indices of sorted list: [0] Expanding labels with ids: [0 1 2 3 4 5 6] so that ids range from 0 to 6 Label ids zerod: [0 1 2 3 4 5 6]. 0 to be inserted between each id: [0 0 0 0 0 0 0] 0 extra rows to be randomly inserted: [0 0 0 0 0 0] New ids: [0 1 2 3 4 5 6] ---- Ran expand_labels in 0.00 s ---- ``` -------------------------------- ### Initialize Weighted Neighborhood Graphs Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B012_tissue_segmentation.ipynb Initializes the weighted neighborhood graphs for BANKSY using median distance to nearest neighbors and various plotting options. ```python nbrs = median_dist_to_nearest_neighbour(adata, key=coord_keys[2]) banksy_dict = initialize_banksy(adata, coord_keys, k_geom, nbr_weight_decay=nbr_weight_decay, max_m=max_m, plt_edge_hist=False, plt_nbr_weights=True, plt_agf_angles=False, plt_theta=False ) ``` -------------------------------- ### Initialize BANKSY Source: https://github.com/prabhakarlab/banksy_py/blob/main/README.md Initializes BANKSY to generate the spatial graph. This is a core step in the BANKSY pipeline. ```python banksy.initialise_banksy(adata, banksy_dict) ``` -------------------------------- ### Preview Preprocessed Data Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv1_analysis.ipynb Display the first few rows and columns of the preprocessed data to understand its structure and content. This is useful for a quick data sanity check after preprocessing. ```python Result: xcoord ycoord n_genes_by_counts \ barcodes TACTAAAGAATTA 2224.000000 1230.397849 35 TCTCTTAGTTGGC 1914.742424 2221.954545 33 GCTTTTCGTTCCC 1682.151515 3886.575758 57 TACGGGCGAAAAG 2416.261194 4216.902985 14 GTACCCTTCCGGG 3913.880342 1899.717949 283 ... ... ... ... CTACCGTGCGGCG 5176.223776 3567.398601 30 CCGATATGCGGCG 2222.324324 3906.189189 48 TTGGTATCGCCGC 3187.536082 2031.268041 55 TTCTTATCGCCGC 3686.817460 3833.047619 75 TTGTCGTATAGCG 4084.235294 1917.862745 106 log1p_n_genes_by_counts total_counts log1p_total_counts \ barcodes TACTAAAGAATTA 3.583519 39.0 3.688879 TCTCTTAGTTGGC 3.526361 38.0 3.663562 GCTTTTCGTTCCC 4.060443 63.0 4.158883 TACGGGCGAAAAG 2.708050 16.0 2.833213 GTACCCTTCCGGG 5.648974 345.0 5.846439 ... ... ... ... CTACCGTGCGGCG 3.433987 33.0 3.526361 CCGATATGCGGCG 3.891820 49.0 3.912023 TTGGTATCGCCGC 4.025352 58.0 4.077538 TTCTTATCGCCGC 4.330733 85.0 4.454347 TTGTCGTATAGCG 4.672829 113.0 4.736198 pct_counts_in_top_50_genes pct_counts_in_top_100_genes \ barcodes TACTAAAGAATTA 100.000000 100.000000 TCTCTTAGTTGGC 100.000000 100.000000 GCTTTTCGTTCCC 88.888889 100.000000 TACGGGCGAAAAG 100.000000 100.000000 GTACCCTTCCGGG 32.463768 46.956522 ... ... ... CTACCGTGCGGCG 100.000000 100.000000 CCGATATGCGGCG 100.000000 100.000000 TTGGTATCGCCGC 91.379310 100.000000 TTCTTATCGCCGC 70.588235 100.000000 TTGTCGTATAGCG 50.442478 94.690265 pct_counts_in_top_200_genes pct_counts_in_top_500_genes \ barcodes TACTAAAGAATTA 100.000000 100.0 TCTCTTAGTTGGC 100.000000 100.0 GCTTTTCGTTCCC 100.000000 100.0 TACGGGCGAAAAG 100.000000 100.0 GTACCCTTCCGGG 75.942029 100.0 ... ... ... CTACCGTGCGGCG 100.000000 100.0 CCGATATGCGGCG 100.000000 100.0 TTGGTATCGCCGC 100.000000 100.0 TTCTTATCGCCGC 100.000000 100.0 TTGTCGTATAGCG 100.000000 100.0 total_counts_mt log1p_total_counts_mt pct_counts_mt barcodes TACTAAAGAATTA 0.0 0.0 0.0 TCTCTTAGTTGGC 0.0 0.0 0.0 GCTTTTCGTTCCC 0.0 0.0 0.0 TACGGGCGAAAAG 0.0 0.0 0.0 GTACCCTTCCGGG 0.0 0.0 0.0 ... ... ... ... CTACCGTGCGGCG 0.0 0.0 0.0 CCGATATGCGGCG 0.0 0.0 0.0 TTGGTATCGCCGC 0.0 0.0 0.0 TTCTTATCGCCGC 0.0 0.0 0.0 TTGTCGTATAGCG 0.0 0.0 0.0 [25551 rows x 13 columns] ``` -------------------------------- ### Harmony Partitioning with Scaled Gaussian Decay Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_harmony_multisample.ipynb Sets up and runs graph partitioning using a scaled Gaussian decay type and a neighborhood contribution of 0.2 with 20 PCA dimensions. This snippet demonstrates the process of building weighted and connectivity graphs and performing community detection. ```python Output: Decay type: scaled_gaussian Neighbourhood Contribution (Lambda Parameter): 0.2 reduced_pc_20 reduced_pc_20_umap PCA dims to analyse: [20] ==================================================================================================== Setting up partitioner for (nbr decay = scaled_gaussian), Neighbourhood contribution = 0.2, PCA dimensions = 20) ==================================================================================================== Nearest-neighbour weighted graph (dtype: float64, shape: (11468, 11468)) has 573400 nonzero entries. ---- Ran find_nn in 1.49 s ---- Nearest-neighbour connectivity graph (dtype: int16, shape: (11468, 11468)) has 573400 nonzero entries. (after computing shared NN) Allowing nearest neighbours only reduced the number of shared NN from 9133448 to 572722. Shared nearest-neighbour (connections only) graph (dtype: int16, shape: (11468, 11468)) has 554159 nonzero entries. Shared nearest-neighbour (number of shared neighbours as weights) graph (dtype: int16, shape: (11468, 11468)) has 554159 nonzero entries. sNN graph data: [32 21 26 ... 12 14 14] ---- Ran shared_nn in 0.33 s ---- -- Multiplying sNN connectivity by weights -- shared NN with distance-based weights graph (dtype: float64, shape: (11468, 11468)) has 554159 nonzero entries. shared NN weighted graph data: [0.0864526 0.08653113 0.08680058 ... 0.24618953 0.25736866 0.27164641] Converting graph (dtype: float64, shape: (11468, 11468)) has 554159 nonzero entries. ---- Ran csr_to_igraph in 0.08 s ---- Resolution: 0.4 ------------------------------ ---- Partitioned BANKSY graph ---- modularity: 0.76 7 unique labels: [0 1 2 3 4 5 6] ---- Ran partition in 2.91 s ---- No annotated labels ``` -------------------------------- ### Import Similarity Metrics Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B006_ascending.ipynb Imports necessary metrics from scikit-learn for calculating similarity between cluster labels. It also accesses the 'Community' annotation from the adata object. ```python from sklearn.metrics import adjusted_rand_score as ari, adjusted_mutual_info_score as ami from sklearn.metrics import matthews_corrcoef as mcc # See the visualize the communities that we want to detect adata.obs['Community'] ``` -------------------------------- ### Perform Nonspatial Clustering and Visualization Source: https://github.com/prabhakarlab/banksy_py/blob/main/CODEX_B006_ascending.ipynb This snippet demonstrates how to set up and run nonspatial clustering using Leiden partitioning and then visualize the results. It requires pre-defined variables like 'adata', 'resolutions', 'max_labels', 'weights_graph', 'coord_keys', and 'unique_regions'. The visualization can be configured to save different plot types. ```python # Add nonspatial clustering nonspatial_dict = {"nonspatial" : {0.0: {"adata": concatenate_all([adata.X], 0, adata=adata), } } } pca_umap(nonspatial_dict, pca_dims = pca_dims, add_umap = True ) from banksy.cluster_methods import run_Leiden_partition nonspatial_df, max_num_labels = run_Leiden_partition( nonspatial_dict, resolutions, num_nn = 50, num_iterations = -1, partition_seed = 1234, match_labels = True, max_labels = max_labels, ) from banksy.plot_banksy import plot_results c_map = 'tab20' # specify color map plot_results( nonspatial_df, weights_graph, c_map, match_labels = True, coord_keys = coord_keys, max_num_labels = max_num_labels, save_path = os.path.join(file_path, 'BANKSY-Results'), save_fig = False, # Save Spatial Plot Only save_fullfig = True, # Save Full Plot dataset_name = f"CODEX-{unique_regions}", save_labels=True ) ``` -------------------------------- ### Define Sample List and Cluster Targets Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_concatenate_multisample.ipynb Initializes a list of sample identifiers and a corresponding list for the target number of clusters for each sample. Used for iterating through datasets. ```python from scanpy import read_10x_h5 samples = [ "151673","151674","151675","151676", ] num_clusters_list = [7,7,7,7] # target cluster number ``` -------------------------------- ### Preview Preprocessed Data Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv1_analysis.ipynb Displays a preview of the preprocessed data, showing various statistical metrics per row. This is useful for a quick overview of the data's structure and quality after preprocessing. ```python Result: mt n_cells_by_counts mean_counts log1p_mean_counts \ Row 0610005C13Rik False 3 0.000117 0.000117 0610007P14Rik False 553 0.024696 0.024396 0610009B22Rik False 238 0.009784 0.009737 0610009E02Rik False 11 0.000431 0.000430 0610009L18Rik False 67 0.002779 0.002775 ... ... ... ... ... mt-Ts2 False 539 0.021526 0.021297 mt-Tt False 173 0.006927 0.006903 mt-Tv False 1515 0.063872 0.061915 n-R5-8s1 False 320 0.012641 0.012562 n-R5s33 False 0 0.000000 0.000000 pct_dropout_by_counts total_counts log1p_total_counts Row 0610005C13Rik 99.988259 3.0 1.386294 0610007P14Rik 97.835701 631.0 6.448889 0610009B22Rik 99.068530 250.0 5.525453 0610009E02Rik 99.956949 11.0 2.484907 0610009L18Rik 99.737779 71.0 4.276666 ... ... ... ... mt-Ts2 97.890494 550.0 6.311735 mt-Tt 99.322923 177.0 5.181784 mt-Tv 94.070682 1632.0 7.398174 n-R5-8s1 98.747603 323.0 5.780744 n-R5s33 100.000000 0.0 0.000000 [18671 rows x 7 columns] ``` -------------------------------- ### Import Libraries and Warnings Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_harmony_multisample.ipynb Imports necessary libraries for data analysis, including pandas, numpy, scanpy, and anndata. Suppresses warnings to keep output clean. ```python import os,csv,re import pandas as pd import numpy as np import math from scipy.sparse import issparse import random import warnings warnings.filterwarnings("ignore") import matplotlib.pyplot as plt import scanpy as sc import anndata as ad import time ``` -------------------------------- ### BANKSY Refinement Output Summary Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb This output provides a summary of the cluster refinement process, including the number of nodes swapped, the ratio of swaps, and the total entropy before and after refinement. It also indicates where the refined plot was saved and the time taken for refinement. ```text Output: Refine only once Number of nodes swapped 26 | ratio: 0.022 Total Entropy: 0.14 Refined once | Total Entropy: 0.14 scaled_gaussian_pc20_nc0.80_r0.90 saved refined plot at W:\\Banksy\\YF_ver\\Banksy_py_CODEX_final\\data\\starmap\\tmp_png\\leiden\\seed1234 Time taken for refinement = 0.01 min BANKSY runtime = 0.277 mins ``` -------------------------------- ### Display Preprocessed adata with QC Metrics Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv2_analysis.ipynb Displays the AnnData object after preprocessing and adding quality control metrics. Useful for inspecting the results of QC calculations. ```python Result: mt n_cells_by_counts mean_counts log1p_mean_counts \ Row \ 0610005C13Rik False 1 0.000025 0.000025 \ 0610007P14Rik False 679 0.018888 0.018712 \ 0610009B22Rik False 433 0.011672 0.011604 \ 0610009E02Rik False 33 0.000836 0.000835 \ 0610009L18Rik False 173 0.004608 0.004597 \ ... ... ... ... ... \ mt-Tv False 160 0.004051 0.004043 \ n-R5-8s1 False 23 0.000582 0.000582 \ n-R5s173 False 1 0.000025 0.000025 \ n-R5s48 False 1 0.000025 0.000025 \ n-R5s76 False 1 0.000025 0.000025 \ pct_dropout_by_counts total_counts log1p_total_counts Row 0610005C13Rik 99.997468 1.0 0.693147 0610007P14Rik 98.280839 746.0 6.616065 0610009B22Rik 98.903686 461.0 6.135565 0610009E02Rik 99.916447 33.0 3.526361 0610009L18Rik 99.561981 182.0 5.209486 ... ... ... ... mt-Tv 99.594896 160.0 5.081404 n-R5-8s1 99.941766 23.0 3.178054 n-R5s173 99.997468 1.0 0.693147 n-R5s48 99.997468 1.0 0.693147 n-R5s76 99.997468 1.0 0.693147 [23096 rows x 7 columns] ``` -------------------------------- ### Plot Marker Gene Sets Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv1_analysis.ipynb Visualizes marker gene sets using a metagene dataframe, cell coordinates, and RCTD weights. Ensure that the coordinate and weight files are correctly loaded. ```python from banksy_utils.plot_utils import plot_markergene_sets import pandas as pd import os from scipy.io import mmread # load weights and locations (need to load locations because filtered seperately) rctd_coord_filename = "slideseqv1_mc_coords_filtered.csv" rctd_weights_filename = "slideseqv1_mc_RCTDweights.txt" rctd_coord = pd.read_csv(os.path.join(file_path, rctd_coord_filename), index_col=0) rctd_weights = mmread(os.path.join(file_path, rctd_weights_filename)).todense() plot_markergene_sets(metagene_df, rctd_coord, rctd_weights, coord_keys, save_fig=False) ``` -------------------------------- ### BANKSY Initialization and Matrix Generation Source: https://github.com/prabhakarlab/banksy_py/blob/main/DLPFC_concatenate_multisample.ipynb Initializes the BANKSY object with spatial coordinates and generates the BANKSY matrix. This snippet sets up parameters like k_geom, lambda, and m, and includes spatial coordinates in the AnnData object. It also prepares a non-spatial matrix for comparison. ```python ## banksy parameters ## from banksy.initialize_banksy import initialize_banksy banksy_dict = {} coord_keys = ('x_pixel', 'y_pixel', 'coord_xy') nbr_weight_decay = 'scaled_gaussian' k_geom = 6 x_coord, y_coord, xy_coord = coord_keys[0], coord_keys[1], coord_keys[2] from banksy.main import concatenate_all from banksy.embed_banksy import generate_banksy_matrix resolutions = [0.60] # clustering resolution for UMAP pca_dims = [20] # Dimensionality in which PCA reduces to lambda_list = [0.2] # list of lambda parameters m = 0 from banksy_utils.umap_pca import pca_umap from banksy.cluster_methods import run_Leiden_partition results_df = {} max_num_labels = {} from banksy.plot_banksy import plot_results c_map = 'tab20' # specify color map ### run banksy ### for sample in samples: # Include spatial coordinates information raw_y, raw_x = adata_list[sample].obs[y_coord], adata_list[sample].obs[x_coord] adata_list[sample].obsm[xy_coord] = np.vstack((adata_list[sample].obs[x_coord].values, adata_list[sample].obs[y_coord].values)).T banksy_dict[sample] = initialize_banksy(adata_list[sample], coord_keys, k_geom, nbr_weight_decay=nbr_weight_decay, max_m=m, plt_edge_hist= False, plt_nbr_weights= True, plt_agf_angles=False, plt_theta=False ) banksy_dict[sample], banksy_matrix = generate_banksy_matrix(adata_list[sample], banksy_dict[sample], lambda_list, max_m=m) banksy_dict[sample]["nonspatial"] = { # Here we simply append the nonspatial matrix (adata.X) to obtain the nonspatial clustering results 0.0: {"adata": concatenate_all([adata_list[sample].X], 0, adata=adata_list[sample]), } } print(banksy_dict[sample]['nonspatial'][0.0]['adata']) ``` -------------------------------- ### Initialize BANKSY Py Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb Initialize the BANKSY Py object with specified parameters and coordinate keys. This function generates spatial graphs and plots related histograms. ```python banksy_dict = initialize_banksy( adata, coord_keys, k_geom, nbr_weight_decay=nbr_weight_decay, max_m=max_m, plt_edge_hist=True, plt_nbr_weights=True, plt_agf_angles=False, plt_theta=False, ) ``` -------------------------------- ### Run BANKSY with Multiple Parameters Source: https://github.com/prabhakarlab/banksy_py/blob/main/starmap_analysis.ipynb Execute the BANKSY analysis pipeline with a comprehensive set of parameters. This function handles data preprocessing, matrix creation, dimensionality reduction, and clustering. ```python results_df = run_banksy_multiparam( adata, banksy_dict, lambda_list, resolutions, color_list = spagcn_color, max_m = max_m, filepath = output_folder, key = coord_keys, pca_dims = pca_dims, annotation_key = annotation_key, max_labels = num_clusters, cluster_algorithm = cluster_algorithm, match_labels = False, savefig = False, add_nonspatial = False, variance_balance = False, ) ``` -------------------------------- ### Display Preprocessed Data with QC Metrics Source: https://github.com/prabhakarlab/banksy_py/blob/main/slideseqv2_analysis.ipynb Displays the preprocessed AnnData object, which includes added quality control metrics. This output is useful for initial inspection of the data quality and cell filtering criteria. ```python import scanpy as sc # Assuming 'adata' is your AnnData object after QC metric calculation # Example: adata.obs['n_genes_by_counts'] = (adata.X.sum(axis=1) > 0).sum(axis=1).A1 # Example: adata.obs['total_counts'] = adata.X.sum(axis=1).A1 # Example: adata.var['mt'] = adata.var_names.str.startswith('MT-') # adata.obs['pct_counts_mt'] = np.sum(adata.X[:, adata.var['mt']], axis=1).A1 / np.sum(adata.X, axis=1).A1 # Display the adata with added qc_metrics print(adata) ```