### Install Scanorama Source: https://github.com/brianhie/scanorama/blob/master/README.md Standard installation methods for the Scanorama package. ```bash pip install scanorama ``` ```bash git clone https://github.com/brianhie/scanorama.git cd scanorama/ python setup.py install --user ``` ```bash conda install -c conda-forge python-annoy ``` -------------------------------- ### Sample Tab-Delimited Data Format Source: https://github.com/brianhie/scanorama/blob/master/README.md Example structure for input files where columns are cells and rows are genes. ```text gene cell_a cell_b gene_1 10 10 gene_2 20 20 ``` -------------------------------- ### Run Toy Dataset Integration Source: https://github.com/brianhie/scanorama/blob/master/README.md Execute integration on the 293T and Jurkat toy datasets. ```bash python bin/293t_jurkat.py ``` -------------------------------- ### Run Large-Scale Dataset Integration Source: https://github.com/brianhie/scanorama/blob/master/README.md Commands for integrating and batch-correcting multiple datasets defined in a configuration file. ```bash python bin/integration_panorama.py conf/panorama.txt ``` ```bash python bin/panorama.py conf/panorama.txt ``` -------------------------------- ### Download and Unpack Dataset Source: https://github.com/brianhie/scanorama/blob/master/README.md Commands to retrieve and extract the study's dataset files. ```bash wget http://cb.csail.mit.edu/cb/scanorama/data.tar.gz tar xvf data.tar.gz ``` -------------------------------- ### Scanorama Integrate with Sketching Source: https://context7.com/brianhie/scanorama/llms.txt Accelerates integration for very large datasets by using geometric sketching to downsample data while preserving geometric structure. This reduces computation time and memory usage. ```python import scanorama import numpy as np from scipy.sparse import csr_matrix # Large datasets (100k+ cells each) large_dataset1 = csr_matrix(np.random.rand(50000, 5000)) large_dataset2 = csr_matrix(np.random.rand(60000, 5000)) genes = ['gene_' + str(i) for i in range(5000)] datasets = [large_dataset1, large_dataset2] genes_list = [genes, genes] # Use sketch-based acceleration for large datasets integrated, common_genes = scanorama.integrate( datasets, genes_list, sketch=True, # Enable sketching sketch_method='geosketch', # 'geosketch' or 'uniform' sketch_max=10000, # Max cells per sketch dimred=100, verbose=2 ) # Result maintains full cell count but computed much faster print(f"Integrated shapes: {[arr.shape for arr in integrated]}") ``` -------------------------------- ### Configure Scanorama Algorithm Parameters Source: https://context7.com/brianhie/scanorama/llms.txt Adjust Scanorama's parameters for optimal integration based on dataset characteristics. Lower alpha for fewer shared cell types, increase sigma for noisy data, and reduce batch_size for large datasets. ```python import scanorama # Default parameter values ALPHA = 0.10 # Alignment score minimum cutoff APPROX = True # Use approximate nearest neighbors BATCH_SIZE = 5000 # Batch size for large datasets DIMRED = 100 # Dimensionality of embedding KNN = 20 # K-nearest neighbors for matching SIGMA = 15 # Gaussian kernel smoothing parameter # For datasets with few shared cell types, lower alpha threshold integrated, genes = scanorama.integrate( datasets, genes_list, alpha=0.05 # Lower threshold accepts weaker alignments ) ``` ```python # For noisy data, increase sigma for smoother corrections corrected, genes = scanorama.correct( datasets, genes_list, sigma=25 # Higher sigma = smoother batch correction ) ``` ```python # For very large datasets, reduce memory with smaller batch_size corrected, genes = scanorama.correct( datasets, genes_list, batch_size=2000 # Process in smaller chunks ) ``` ```python # If experiencing "Illegal instruction" errors with annoy, disable approximation integrated, genes = scanorama.integrate( datasets, genes_list, approx=False # Use exact sklearn nearest neighbors ) ``` ```python # Limit to highly variable genes for faster processing integrated, genes = scanorama.integrate( datasets, genes_list, hvg=2000 # Use only top 2000 highly variable genes ) ``` -------------------------------- ### Process Raw Data Source: https://github.com/brianhie/scanorama/blob/master/README.md Command to convert raw input files into consistent .npz format using the provided configuration. ```bash python bin/process.py conf/panorama.txt ``` -------------------------------- ### Run Pytest Unit Tests Source: https://github.com/brianhie/scanorama/blob/master/README.md Execute unit tests for Scanorama using pytest. Ensure you are in the top-level directory of the project. ```bash python -m pytest tests ``` -------------------------------- ### scanorama.integrate_scanpy Source: https://context7.com/brianhie/scanorama/llms.txt Integrates AnnData objects from Scanpy. ```APIDOC ## scanorama.integrate_scanpy ### Description Integrates AnnData objects from Scanpy, adding the integrated embeddings directly to each object's obsm['X_scanorama'] field. ### Parameters - **adatas** (list of AnnData) - Required - List of AnnData objects to integrate. ``` -------------------------------- ### scanorama.correct Source: https://context7.com/brianhie/scanorama/llms.txt Performs both integration and batch correction on expression matrices. ```APIDOC ## scanorama.correct ### Description Performs both integration and batch correction on expression matrices. Returns corrected gene expression values in addition to (optionally) the integrated embeddings. ### Parameters - **datasets** (list of scipy.sparse.csr_matrix) - Required - List of cells-by-genes matrices. - **genes_list** (list of list of str) - Required - List of gene names corresponding to each dataset. - **sigma** (float) - Optional - Correction smoothing parameter. - **alpha** (float) - Optional - Alignment score cutoff. - **knn** (int) - Optional - K-nearest neighbors. - **batch_size** (int) - Optional - Batch size for memory efficiency. - **return_dense** (bool) - Optional - Return dense matrices if True. - **return_dimred** (bool) - Optional - Return low-dimensional embeddings if True. - **dimred** (int) - Optional - Dimensionality of integrated embedding. - **hvg** (int) - Optional - Number of highly variable genes to use. ### Response - **corrected** (list of scipy.sparse.csr_matrix) - Corrected gene expression values. - **integrated** (list of numpy.ndarray) - (Optional) Low-dimensional embeddings. - **genes** (list of str) - Common genes found. ``` -------------------------------- ### scanorama.integrate Source: https://context7.com/brianhie/scanorama/llms.txt Integrates multiple scRNA-seq datasets into a unified low-dimensional embedding space. ```APIDOC ## scanorama.integrate ### Description Integrates multiple scRNA-seq datasets into a unified low-dimensional embedding space using mutual nearest neighbors matching. Returns integrated coordinates suitable for downstream clustering and visualization. ### Parameters - **datasets** (list of scipy.sparse.csr_matrix) - Required - List of cells-by-genes matrices. - **genes_list** (list of list of str) - Required - List of gene names corresponding to each dataset. - **dimred** (int) - Optional - Dimensionality of integrated embedding. - **knn** (int) - Optional - Number of nearest neighbors for matching. - **sigma** (float) - Optional - Gaussian kernel smoothing parameter. - **alpha** (float) - Optional - Alignment score minimum cutoff. - **approx** (bool) - Optional - Use approximate nearest neighbors. - **verbose** (int) - Optional - Logging output level. ### Response - **integrated** (list of numpy.ndarray) - Low-dimensional embedding for each dataset. - **genes** (list of str) - Common genes found across datasets. ``` -------------------------------- ### Scanpy Integration with Scanorama Source: https://github.com/brianhie/scanorama/blob/master/README.md These wrappers simplify Scanorama usage with Scanpy's AnnData objects. `integrate_scanpy` adds low-dimensional embeddings to `adata.obsm['X_scanorama']`, while `correct_scanpy` replaces `adata.X` with the corrected matrix. ```python # List of datasets: adatas = [ list of scanpy.AnnData ] import scanorama # Integration. scanorama.integrate_scanpy(adatas) # Batch correction. corrected = scanorama.correct_scanpy(adatas) # Integration and batch correction. corrected = scanorama.correct_scanpy(adatas, return_dimred=True) ``` -------------------------------- ### Visualize Integrated Datasets with Scanorama Source: https://context7.com/brianhie/scanorama/llms.txt Generates t-SNE visualizations for integrated datasets. Supports coloring by cluster labels and overlaying gene expression. Requires pre-computed integrated embeddings. ```python from scanorama.scanorama import visualize import numpy as np # After integration # integrated = [array1, array2, ...] # From scanorama.integrate() # Create labels for visualization n_cells_per_dataset = [1000, 800, 1200] labels = [] for i, n in enumerate(n_cells_per_dataset): labels.extend([i] * n) labels = np.array(labels) data_names = ['Dataset_A', 'Dataset_B', 'Dataset_C'] # Generate visualization (assumes integrated datasets exist) # embedding = visualize( # integrated, # labels, # namespace='my_analysis', # Output filename prefix # data_names=data_names, # n_iter=500, # t-SNE iterations # perplexity=30, # t-SNE perplexity # size=1, # Point size # ) ``` -------------------------------- ### Integrate Scanpy AnnData Objects Source: https://context7.com/brianhie/scanorama/llms.txt Use `scanorama.integrate_scanpy` to integrate AnnData objects directly. The integrated embeddings are added to `adata.obsm['X_scanorama']`, facilitating seamless integration into Scanpy workflows. ```python import scanorama import scanpy as sc from anndata import AnnData import numpy as np import pandas as pd # Create sample AnnData objects (typically loaded from files) n_cells_1, n_cells_2 = 500, 600 n_genes = 2000 gene_names = [f'gene_{i}' for i in range(n_genes)] # Dataset 1 adata1 = AnnData(np.random.rand(n_cells_1, n_genes)) adata1.var_names = gene_names adata1.obs['batch'] = 'batch1' adata1.obs['cell_type'] = np.random.choice(['T_cell', 'B_cell', 'Monocyte'], n_cells_1) # Dataset 2 adata2 = AnnData(np.random.rand(n_cells_2, n_genes)) adata2.var_names = gene_names adata2.obs['batch'] = 'batch2' adata2.obs['cell_type'] = np.random.choice(['T_cell', 'B_cell', 'Monocyte'], n_cells_2) adatas = [adata1, adata2] # The actual integration call would follow here, e.g.: # integrated_adatas, common_genes = scanorama.integrate_scanpy(adatas) # For demonstration, the code stops after preparing adatas. ``` -------------------------------- ### Scanorama Python Integration and Correction Source: https://github.com/brianhie/scanorama/blob/master/README.md Use these functions for integrating and batch-correcting scRNA-seq datasets. Ensure datasets are provided as a list of scipy.sparse.csr_matrix or numpy.ndarray, and genes_list as a list of lists of strings. ```python # List of datasets (matrices of cells-by-genes): datasets = [ list of scipy.sparse.csr_matrix or numpy.ndarray ] # List of gene lists: genes_list = [ list of list of string ] import scanorama # Integration. integrated, genes = scanorama.integrate(datasets, genes_list) # Batch correction. corrected, genes = scanorama.correct(datasets, genes_list) # Integration and batch correction. integrated, corrected, genes = scanorama.correct(datasets, genes_list, return_dimred=True) ``` -------------------------------- ### Scanorama R Interface via Reticulate Source: https://github.com/brianhie/scanorama/blob/master/README.md Call Scanorama functions from R using the reticulate package. Set `return_dense=TRUE` when using `correct()` to handle potential issues with sparse matrix returns, though this increases memory usage. ```r # List of datasets (matrices of cells-by-genes): datasets <- list( list of matrix ) # List of gene lists: genes_list <- list( list of list of string ) library(reticulate) scanorama <- import('scanorama') # Integration. integrated.data <- scanorama$integrate(datasets, genes_list) # Batch correction. corrected.data <- scanorama$correct(datasets, genes_list, return_dense=TRUE) # Integration and batch correction. integrated.corrected.data <- scanorama$correct(datasets, genes_list, return_dimred=TRUE, return_dense=TRUE) ``` -------------------------------- ### Integrate Multiple scRNA-seq Datasets Source: https://context7.com/brianhie/scanorama/llms.txt Use `scanorama.integrate` to align cells across multiple datasets into a unified low-dimensional embedding space. This function is suitable for downstream clustering and visualization. Ensure datasets are prepared as cells-by-genes matrices. ```python import scanorama import numpy as np from scipy.sparse import csr_matrix # Prepare datasets as cells-by-genes matrices # Dataset 1: 1000 cells x 5000 genes dataset1 = csr_matrix(np.random.rand(1000, 5000)) genes1 = ['gene_' + str(i) for i in range(5000)] # Dataset 2: 800 cells x 4500 genes (with overlapping genes) dataset2 = csr_matrix(np.random.rand(800, 4500)) genes2 = ['gene_' + str(i) for i in range(4500)] # Dataset 3: 1200 cells x 5200 genes dataset3 = csr_matrix(np.random.rand(1200, 5200)) genes3 = ['gene_' + str(i) for i in range(5200)] datasets = [dataset1, dataset2, dataset3] genes_list = [genes1, genes2, genes3] # Perform integration integrated, genes = scanorama.integrate( datasets, genes_list, dimred=100, # Dimensionality of integrated embedding knn=20, # Number of nearest neighbors for matching sigma=15, # Gaussian kernel smoothing parameter alpha=0.10, # Alignment score minimum cutoff approx=True, # Use approximate nearest neighbors (faster) verbose=2 # Print logging output ) # Result: integrated is a list of numpy arrays with shape (n_cells, dimred) # Each array corresponds to the low-dimensional embedding for each dataset print(f"Integrated embedding shapes: {[arr.shape for arr in integrated]}") print(f"Common genes found: {len(genes)}") ``` -------------------------------- ### Batch Correct Datasets with Scanpy Source: https://context7.com/brianhie/scanorama/llms.txt Performs batch correction on AnnData objects, returning new AnnData objects with corrected expression matrices. Metadata is preserved. Optionally include integrated embeddings by setting `return_dimred=True`. ```python import scanorama from anndata import AnnData import numpy as np import pandas as pd # Create sample AnnData objects n_genes = 1500 gene_names = [f'gene_{i}' for i in range(n_genes)] adata1 = AnnData(np.random.rand(400, n_genes)) adata1.var_names = gene_names adata1.obs['sample'] = 'sample_A' adata1.obs['condition'] = 'treated' adata1.var['gene_id'] = gene_names adata2 = AnnData(np.random.rand(350, n_genes)) adata2.var_names = gene_names adata2.obs['sample'] = 'sample_B' adata2.obs['condition'] = 'control' adata2.var['gene_id'] = gene_names adatas = [adata1, adata2] # Batch correction - returns new AnnData objects corrected_adatas = scanorama.correct_scanpy( adatas, dimred=100, knn=20, sigma=15, alpha=0.10 ) # Corrected expression values are in .X print(f"Original adata1 shape: {adata1.X.shape}") print(f"Corrected adata1 shape: {corrected_adatas[0].X.shape}") # Metadata is preserved print(f"Preserved obs columns: {list(corrected_adatas[0].obs.columns)}") print(f"Preserved var columns: {list(corrected_adatas[0].var.columns)}") # With integrated embeddings included corrected_with_dimred = scanorama.correct_scanpy( adatas, return_dimred=True # Include X_scanorama embeddings ) print(f"X_scanorama available: {'X_scanorama' in corrected_with_dimred[0].obsm}") ``` -------------------------------- ### Integrate Datasets with Scanpy Source: https://context7.com/brianhie/scanorama/llms.txt Modifies AnnData objects in place to integrate datasets. The integrated embeddings are stored in `obsm['X_scanorama']`. Use these embeddings for downstream analysis like UMAP and Leiden clustering. ```python import scanorama import scanpy as sc # Assuming adatas is a list of AnnData objects scanorama.integrate_scanpy( adatas, dimred=100, knn=20, sigma=15, alpha=0.10, approx=True ) # Integrated embeddings are now in obsm['X_scanorama'] print(f"Embedding shape for adata1: {adata1.obsm['X_scanorama'].shape}") print(f"Embedding shape for adata2: {adata2.obsm['X_scanorama'].shape}") # Use integrated embeddings for downstream analysis combined = sc.concat(adatas) sc.pp.neighbors(combined, use_rep='X_scanorama') sc.tl.umap(combined) sc.tl.leiden(combined) ``` -------------------------------- ### Integrate Data in R using reticulate Source: https://context7.com/brianhie/scanorama/llms.txt Import and use Scanorama's integration function from R via the reticulate package. Ensure data is prepared as R matrices and specify gene lists. ```r library(reticulate) # Import scanorama scanorama <- import('scanorama') # Prepare data as R matrices (cells x genes) dataset1 <- matrix(rnorm(1000 * 500), nrow = 1000, ncol = 500) dataset2 <- matrix(rnorm(800 * 500), nrow = 800, ncol = 500) genes1 <- paste0("gene_", 1:500) genes2 <- paste0("gene_", 1:500) datasets <- list(dataset1, dataset2) genes_list <- list(genes1, genes2) # Integration integrated_data <- scanorama$integrate(datasets, genes_list) integrated <- integrated_data[[1]] # List of integrated matrices genes <- integrated_data[[2]] # Common genes ``` ```r # Batch correction (use return_dense=TRUE for R compatibility) corrected_data <- scanorama$correct( datasets, genes_list, return_dense = TRUE # Required for R to handle output ) corrected <- corrected_data[[1]] genes <- corrected_data[[2]] ``` ```r # Integration and batch correction together full_result <- scanorama$correct( datasets, genes_list, return_dimred = TRUE, return_dense = TRUE ) integrated <- full_result[[1]] corrected <- full_result[[2]] genes <- full_result[[3]] cat("Number of common genes:", length(genes), "\n") cat("Integrated matrix 1 dimensions:", dim(integrated[[1]]), "\n") ``` -------------------------------- ### Merge Datasets with Scanorama Source: https://context7.com/brianhie/scanorama/llms.txt Merges multiple datasets, optionally using only genes present in all datasets (intersection) or the union of all genes. Ensures all merged datasets have the same genes in the same order. ```python from scanorama.scanorama import merge_datasets import numpy as np from scipy.sparse import csr_matrix # Datasets with different gene sets dataset1 = csr_matrix(np.random.rand(100, 500)) dataset2 = csr_matrix(np.random.rand(150, 600)) genes1 = ['gene_' + str(i) for i in range(500)] genes2 = ['gene_' + str(i) for i in range(100, 700)] # Partial overlap datasets = [dataset1, dataset2] genes_list = [genes1, genes2] # Merge using gene intersection (recommended) merged_datasets, common_genes = merge_datasets( datasets, genes_list, verbose=True, union=False # Use intersection (default) ) print(f"Common genes: {len(common_genes)}") print(f"Merged shapes: {[ds.shape for ds in merged_datasets]}") ``` -------------------------------- ### Correct scRNA-seq Expression Matrices Source: https://context7.com/brianhie/scanorama/llms.txt Use `scanorama.correct` for both integration and batch correction. It returns corrected gene expression values and optionally integrated embeddings. The `return_dense` parameter controls whether sparse or dense matrices are returned. ```python import scanorama import numpy as np from scipy.sparse import csr_matrix # Prepare datasets (cells-by-genes matrices) dataset1 = csr_matrix(np.random.rand(500, 3000)) dataset2 = csr_matrix(np.random.rand(600, 3000)) genes1 = ['gene_' + str(i) for i in range(3000)] genes2 = ['gene_' + str(i) for i in range(3000)] datasets = [dataset1, dataset2] genes_list = [genes1, genes2] # Batch correction only corrected, genes = scanorama.correct( datasets, genes_list, sigma=15, # Correction smoothing parameter alpha=0.10, # Alignment score cutoff knn=20, # K-nearest neighbors batch_size=5000, # Batch size for memory efficiency return_dense=False # Return sparse matrices (default) ) # Result: corrected is a list of scipy.sparse.csr_matrix with corrected values print(f"Corrected matrix shapes: {[m.shape for m in corrected]}") # Integration AND batch correction together integrated, corrected, genes = scanorama.correct( datasets, genes_list, return_dimred=True, # Also return low-dimensional embeddings dimred=100, hvg=2000 # Use top 2000 highly variable genes ) print(f"Integrated shapes: {[arr.shape for arr in integrated]}") print(f"Corrected shapes: {[m.shape for m in corrected]}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.