### Environment Setup YAML Configuration Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/dropdowns/env_setup.md Example of a YAML configuration file for environment setup. ```yaml dependencies: - python=3.9 - pip - virtualenv packages: - numpy - pandas - scikit-learn settings: data_path: "/data" model_path: "/models" log_level: "INFO" ``` -------------------------------- ### Setup and Track Lamindb Instance Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/conditions/differential_gene_expression_dataset.ipynb Initializes lamindb settings and starts tracking notebook execution. Ensure the instance slug matches your project. ```python import lamindb as ln assert ln.setup.settings.instance.slug == "theislab/sc-best-practices" ln.track() ``` -------------------------------- ### Install pre-commit Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Install the pre-commit tool for automatically checking code and markdown before commits. ```bash pip install pre-commit ``` -------------------------------- ### Default Environment Setup Text Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/dropdowns/env_setup.md Provides the default text content for environment setup instructions. ```markdown This section outlines the steps to set up your environment. It includes installing necessary dependencies, configuring your project, and preparing for development. **1. Install Dependencies:** ```bash pip install -r requirements.txt ``` **2. Configure Project:** Ensure your configuration files are correctly set up. Refer to the `config.yaml` for details. **3. Prepare for Development:** Run the setup script to finalize the environment preparation. ```bash python setup.py ``` **Note:** Always ensure you are using the correct Python version as specified in the project documentation. ``` -------------------------------- ### View AnnData Setup for scvi-tools Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/integration.ipynb Displays the AnnData setup information for a scvi-tools model. This confirms the version of scvi-tools used and the setup status. ```python model_scanvi.view_anndata_setup() ``` -------------------------------- ### Install and Load FigR Package Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/gene_regulatory_networks_atac.ipynb Installs the FigR package and its dependency BuenColors from GitHub if not already installed. This is a prerequisite for running the notebook. ```r # the installation of this package is required for the proper execution of this notebook. if(!suppressMessages(require("FigR"))){ suppressMessages(devtools::install_github("caleblareau/BuenColors")) # the package BuenColors is also a devtools dependency to install FigR. suppressMessages(devtools::install_github("buenrostrolab/FigR")) } ``` -------------------------------- ### View AnnData Setup for scVI Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/integration.ipynb Display a detailed description of the AnnData object's setup for scVI, including parameters and keys used. ```python model_scvi.view_anndata_setup() ``` -------------------------------- ### Install cisTopic Package Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/gene_regulatory_networks_atac.ipynb Installs the cisTopic package from GitHub using devtools. A kernel restart might be required after installation. ```r if(!suppressMessages(require("cisTopic"))) suppressMessages(devtools::install_github("aertslab/cisTopic")) ``` -------------------------------- ### Display Setup Arguments for SCANVI Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/integration.ipynb Shows the arguments used during the `SCANVI.setup_anndata` process. This is helpful for debugging and verifying the AnnData preparation steps. ```python print(model_scanvi.summary()) ``` -------------------------------- ### Python Environment Setup Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Sets up the Python environment by filtering warnings. This is useful for cleaning up output during data analysis. ```python import os import warnings warnings.filterwarnings("ignore") os.environ["PYTHONWARNINGS"] = "ignore" ``` -------------------------------- ### Install SpatialData using pip Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Installs the SpatialData library using pip, a package installer for Python. This is one of the methods to get SpatialData ready for use. ```bash pip install spatialdata ``` -------------------------------- ### Example Project Folder Layout Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Illustrates the directory structure of the single-cell best practices book, showing how chapters and their associated files are organized within section folders. ```bash ├── conditions │ ├── compositional_keytakeaways.txt │ ├── compositional.bib │ ├── compositional.ipynb │ ├── compositional.yml │ ├── differential_gene_expression_keytakeaways.txt │ ├── differential_gene_expression.bib │ ├── differential_gene_expression.ipynb │ ├── differential_gene_expression.yml │ ├── gsea_pathway_keytakeaways.txt │ └── ... ├── ... ├── _toc.yml ├── _config.yml ├── acknowledgements.md ├── CHANGELOG.md ├── glossary.md ├── outlook.md ├── preamble.md ├── _static │ ├── book.css │ ├── book.js │ ├── favicon.ico │ ├── images │ │ ├── conditions │ │ │ ├── compositional.jpg │ │ │ └── differential_gene_expression.jpg │ │ └── ... ``` -------------------------------- ### Example Chapter Folder Structure Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Shows the essential files required for each chapter, including the notebook, bibliography, Conda environment file, and key takeaways summary. ```bash ├── SECTION-NAME │ ├── CHAPTER-NAME.ipynb │ ├── CHAPTER-NAME.bib │ ├── CHAPTER-NAME.yml │ ├── CHAPTER-NAME_keytakeaways.txt │ ├── ... ``` -------------------------------- ### Install pre-commit in repository Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Activate pre-commit hooks in the root of the repository to enforce code quality standards. ```bash pre-commit install ``` -------------------------------- ### Setup and Tracking Imports Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/trajectory/rna_velocity_dataset.ipynb Imports necessary libraries and asserts the laminDB instance. Tracks the current run. ```python from pathlib import Path import lamindb as ln import scvelo as scv assert ln.setup.settings.instance.slug == "theislab/sc-best-practices" ln.track("aWcFeMqzSw0X") ``` -------------------------------- ### Install AnnData using pip Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/fundamental_data_structures_and_frameworks.ipynb Install the AnnData library using pip. This is a common method for Python package management. ```bash pip install anndata ``` -------------------------------- ### Install lamindb Package Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/_static/default_text_lamindb_setup.md Install the lamindb Python package using pip. This is the first step to using lamindb. ```bash pip install lamindb ``` -------------------------------- ### Import Libraries and Setup Scanpy Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/spatial/domains.ipynb Imports necessary libraries (scanpy and squidpy) and sets up global configurations for scanpy, including verbosity and figure parameters. This is a standard setup for most spatial omics analyses using these libraries. ```python import scanpy as sc import squidpy as sq sc.settings.verbosity = 3 sc.settings.set_figure_params(dpi=80, facecolor="white") ``` -------------------------------- ### Create Working Directory and Download Data Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/raw_data_processing.md Sets up a working directory, downloads an example dataset and a CB permit list, and decompresses them. The `&&` operator chains commands, and the `|` operator pipes output between commands. ```bash # Create a working dir and go to the working directory ## The && operator helps execute two commands using a single line of code. mkdir af_xmpl_run && cd af_xmpl_run # Fetch the example dataset and CB permit list and decompress them ## The pipe operator (|) passes the output of the wget command to the tar command. ## The dash operator (-) after `tar xzf` captures the output of the first command. ## - example dataset wget -qO- https://umd.box.com/shared/static/lx2xownlrhz3us8496tyu9c4dgade814.gz | tar xzf - --strip-components=1 -C . ## The fetched folder containing the fastq files is called toy_read_fastq. fastq_dir="toy_read_fastq" ## The fetched folder containing the human ref files is called toy_human_ref. ref_dir="toy_human_ref" # Fetch CB permit list ## the right chevron (>) redirects the STDOUT to a file. wget -qO- https://github.com/f0t1h/3M-february-2018/raw/master/3M-february-2018.txt.gz | gunzip - > 3M-february-2018.txt ``` -------------------------------- ### Install lamindb with extensions Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Install the lamindb Python package with bionty, jupyter, and zarr extensions for data management and analysis. ```bash pip install lamindb[bionty,jupyter,zarr] ``` -------------------------------- ### Create and Activate Conda Environment Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/raw_data_processing.md Installs the Simpleaf package and its dependencies into a new conda environment named 'af'. ```bash conda create -n af -y -c bioconda simpleaf conda activate af ``` -------------------------------- ### Install Scanpy using pip Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/fundamental_data_structures_and_frameworks.ipynb Use this command to install the scanpy package via pip. ```bash pip install scanpy ``` -------------------------------- ### Setup AnnData for Cell2location Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/spatial/deconvolution.ipynb Prepares the AnnData object for the Cell2location model by specifying the batch key. This is a prerequisite for model initialization. ```python c2l.models.Cell2location.setup_anndata( adata=adata_st, batch_key="patient", ) ``` -------------------------------- ### Install and Load BSgenome Package Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/gene_regulatory_networks_atac.ipynb Ensures the BSgenome.Hsapiens.UCSC.hg38 package is installed and loaded for human genome annotations. Requires BiocManager for installation. ```r if(!suppressMessages(require('BSgenome.Hsapiens.UCSC.hg38'))){ if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("BSgenome.Hsapiens.UCSC.hg38") } ``` ```r suppressMessages(library(BSgenome.Hsapiens.UCSC.hg38)) ``` -------------------------------- ### Connect to Lamindb Instance and Load Artifact Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/template/template.ipynb Connects to a specified lamindb instance and loads a dataset artifact. Ensure lamindb is installed and configured. ```python import lamindb as ln assert ln.setup.settings.instance.slug == "theislab/sc-best-practices" ln.track() af = ln.Artifact.connect("theislab/sc-best-practices").get( key="introduction/interoperability_mdata.h5mu", is_latest=True ) adata = af.load() ``` -------------------------------- ### Install rapids-singlecell with CUDA 12 Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/rapids_singlecell.ipynb Install the rapids-singlecell package with CUDA 12 support and the full RAPIDS stack. The `--extra-index-url` is necessary as RAPIDS wheels are hosted on the NVIDIA PyPI index. ```bash pip install 'rapids-singlecell-cu12[rapids]' --extra-index-url=https://pypi.nvidia.com ``` -------------------------------- ### Install Muon Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Install the Muon package using pip. This command is used to add Muon to your Python environment for multimodal data analysis. ```bash pip install muon ``` -------------------------------- ### Setup R dependencies Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/conditions/gsea_pathway.ipynb Activates R integration with Python using rpy2 and loads the SingleCellExperiment package in R. ```python # Setting up R dependencies import anndata2ri %load_ext rpy2.ipython anndata2ri.activate() ``` ```R suppressPackageStartupMessages({ library(SingleCellExperiment) }) ``` -------------------------------- ### Install AnnData using Conda Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/fundamental_data_structures_and_frameworks.ipynb Install the AnnData library using Conda. This is useful for managing environments and dependencies, especially in scientific computing. ```bash conda install -c conda-forge anndata ``` -------------------------------- ### Install rapids-singlecell with CUDA 13 Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/rapids_singlecell.ipynb Install the rapids-singlecell package with CUDA 13 support and the full RAPIDS stack. The `--extra-index-url` is necessary as RAPIDS wheels are hosted on the NVIDIA PyPI index. ```bash pip install 'rapids-singlecell-cu13[rapids]' --extra-index-url=https://pypi.nvidia.com ``` -------------------------------- ### Setup AnnData for scVI Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/integration.ipynb Prepare the AnnData object for scVI by specifying the counts layer and batch key. This stores necessary information for the scVI model. ```python scvi.model.SCVI.setup_anndata(adata_scvi, layer="counts", batch_key=batch_key) ``` -------------------------------- ### Initialize lamindb and Track Run Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/clustering.ipynb Initializes the lamindb instance and starts tracking the current analysis run. This is essential for reproducibility and logging. ```python import lamindb as ln import scanpy as sc assert ln.setup.settings.instance.slug == "theislab/sc-best-practices" ln.track("rJhR7SskiROg") ``` -------------------------------- ### Importing Libraries and Setting Up Warnings Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/air_repertoire/clonotype.ipynb Imports necessary libraries for data analysis and sets up warning filters to suppress non-critical messages during execution. This is a common setup for data science workflows. ```python import warnings warnings.filterwarnings( "ignore", ".*IProgress not found*", ) warnings.simplefilter(action="ignore", category=FutureWarning) import numpy as np import pandas as pd import scanpy as sc import scirpy as ir from palmotif import compute_motif, svg_logo warnings.simplefilter(action="ignore", category=pd.errors.DtypeWarning) ``` -------------------------------- ### Setup AnnData for scGen Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/conditions/perturbation_modeling.ipynb Configures the AnnData object for scGen, specifying the batch key and labels key. This is a mandatory step before using scGen. ```python pt.tl.SCGEN.setup_anndata(adata_t, batch_key="condition", labels_key="cell_type") ``` -------------------------------- ### Import Libraries and Track Run Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/conditions/differential_gene_expression.ipynb Imports essential libraries for data analysis and tracking the notebook run with lamindb. Ensure these libraries are installed in your environment. ```python import decoupler as dc import lamindb as ln import numpy as np import pandas as pd import pertpy as pt import scanpy as sc ln.track() ``` -------------------------------- ### Setup AnnData for totalVI Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/paired_integration.ipynb Configures the AnnData object for totalVI, specifying the protein expression key, RNA counts layer, and batch key for batch effect correction. ```python scvi.model.TOTALVI.setup_anndata( adata, protein_expression_obsm_key="protein_expression", layer="counts", batch_key="batch", ) ``` -------------------------------- ### Get lamindb Version Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/cellular_structure/integration_dataset.ipynb Retrieves the currently installed version of the lamindb library. ```python ln.__version__ ``` -------------------------------- ### Setup AnnData for totalVI Model Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/advanced_integration.ipynb Configure the AnnData object for the totalVI model, specifying the layer for counts, the batch key, and the key for protein expression in the observations' metadata. ```python TOTALVI.setup_anndata( adata, layer="counts", batch_key="batch", protein_expression_obsm_key="protein_counts", ) ``` -------------------------------- ### Getting Image Channel Names Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb This function returns the names or identifiers for each channel within an image. For example, it can indicate which staining or wavelength each channel corresponds to. ```python sd.models.get_channel_names(sdata["raw_image"]) ``` -------------------------------- ### Import Libraries and Setup Scanpy Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/preprocessing_visualization/dimensionality_reduction.ipynb Imports necessary Python libraries (lamindb, scanpy) and configures Scanpy settings for plotting and verbosity. It also tracks the current run with lamindb. ```python import lamindb as ln import scanpy as sc # Suppress verbose logging from Scanpy sc.settings.verbosity = 0 # Set figure parameters for clean, minimal plots sc.settings.set_figure_params(dpi=80, facecolor="white", frameon=False) assert ln.setup.settings.instance.slug == "theislab/sc-best-practices" ln.track() ``` -------------------------------- ### Setup AnnData for cell2location Regression Model Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/spatial/deconvolution.ipynb Prepares the AnnData object for training the cell2location regression model by specifying batch keys, labels, covariates, and the layer containing counts. ```python c2l.models.RegressionModel.setup_anndata( adata=adata_sc, batch_key="donor_id", labels_key="cell_type_original", categorical_covariate_keys=["assay"], layer="counts", ) ``` -------------------------------- ### Install SpatialData using Conda Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Installs the SpatialData library using Conda, a cross-platform package and environment manager. This is an alternative installation method. ```bash conda install -c conda-forge spatialdata ``` -------------------------------- ### Initialize Environment and Load Data Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/annotation.ipynb Sets up the environment by connecting to a lamindb instance and tracking a notebook run. Loads a preprocessed AnnData object. ```python import shutil import sys from pathlib import Path import celltypist import lamindb as ln import matplotlib.pyplot as plt import numpy as np import pandas as pd import pandas.core.indexes.base as pandas_indexes_base import scanpy as sc import scarches as sca import seaborn as sns from celltypist import models from scipy.sparse import csr_matrix ln.connect("theislab/sc-best-practices") ln.track("BnpvfJLWjHuE") ``` ```python sc.set_figure_params(figsize=(5, 5)) ``` ```python af = ln.Artifact.get(key="cellular_structure/s4d8_clustered.h5ad", is_latest=True) adata = af.load(is_run_input=False) ``` -------------------------------- ### Initialize LaminDB and Load Data Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/dropdowns/lamin_setup.md Sets up the LaminDB instance and loads data from a specified path. Ensure the path points to your data directory. ```python import lamindb as la # Initialize LaminDB la.setup.init( "your_project_name", "your_instance_name", storage="/path/to/your/storage", db="postgresql://user:password@host:port/database", ) # Load data from a directory la.from_dir("/path/to/your/data") ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/surface_protein/batch_correction.ipynb Imports necessary libraries like warnings, muon, scanpy, and lamindb. It also sets global options for these libraries, such as disabling warnings, configuring Muon, and setting Scanpy's figure parameters. Finally, it initializes lamindb tracking. ```python import warnings import muon as mu import scanpy as sc warnings.filterwarnings("ignore") mu.set_options(pull_on_update=False) sc.settings.verbosity = 0 sc.set_figure_params( dpi=80, facecolor="white", frameon=False, ) import lamindb as ln ln.track() ``` -------------------------------- ### Install Scanpy using Conda Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/fundamental_data_structures_and_frameworks.ipynb Use this command to install the scanpy package via Conda. ```bash conda install -c conda-forge scanpy ``` -------------------------------- ### Build the Book Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Execute this command to build the complete book. This command does not run any notebooks. ```bash make ``` -------------------------------- ### Import Libraries and Initialize LAMINDB Tracking Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/surface_protein/normalization.ipynb Imports necessary libraries for data manipulation and analysis, and initializes LAMINDB for tracking experiments and artifacts. This setup is standard for data science workflows involving LAMINDB. ```python import warnings import matplotlib.pyplot as plt import muon as mu import pandas as pd import scanpy as sc import seaborn as sns warnings.filterwarnings("ignore") mu.set_options(pull_on_update=False) sc.settings.verbosity = 0 sc.set_figure_params(dpi=80, facecolor="white", frameon=False) import lamindb as ln ln.track() ``` -------------------------------- ### Install towncrier for changelog management Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Install the towncrier package, used for managing changelog entries for pull requests. ```bash pip install towncrier ``` -------------------------------- ### Initialize Network Visualization Libraries Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/gene_regulatory_networks_atac.ipynb Load necessary R libraries for creating network visualizations, including networkD3, r2d3, and imager. ```r library(networkD3) library(r2d3) library(imager) ``` -------------------------------- ### Load FigR Library Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/gene_regulatory_networks_atac.ipynb Loads the installed FigR package into the current R session. Ensure FigR is installed before running this. ```r suppressMessages(library(FigR)) ``` -------------------------------- ### Instantiate VanillaGreedySolver Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/trajectories/lineage_tracing.ipynb Creates an instance of the VanillaGreedySolver, a heuristic-based algorithm suitable for initial lineage reconstruction. ```python greedy_solver = cas.solver.VanillaGreedySolver() ``` -------------------------------- ### Install Squidpy using pip Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Installs the Squidpy package using pip. This is a common method for adding Python libraries to your environment. ```bash pip install squidpy ``` -------------------------------- ### Install MuData Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Install the MuData library using pip or conda. This package is used for storing and manipulating multimodal biological data. ```bash pip install mudata ``` ```bash conda install -c conda-forge mudata ``` -------------------------------- ### Setup and Train scVI Model for Batch Correction Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/conditions/compositional.ipynb Prepares the AnnData object for scVI by setting up the AnnData object with the specified layer and batch key, then trains the scVI model. The number of training epochs is dynamically determined based on the number of observations. ```python import scvi adata_scvi = adata[:, adata.var["highly_variable"]].copy() scvi.model.SCVI.setup_anndata(adata_scvi, layer="counts", batch_key="batch") model_scvi = scvi.model.SCVI(adata_scvi) max_epochs_scvi = int(np.min([round((20000 / adata.n_obs) * 400), 400])) model_scvi.train(max_epochs=max_epochs_scvi) adata.obsm["X_scVI"] = model_scvi.get_latent_representation() ``` -------------------------------- ### Import Libraries and Setup Environment Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/surface_protein/quality_control.ipynb Imports essential Python libraries for CITE-seq data analysis, including scanpy, muon, and lamindb. It also configures warnings, muon options, scanpy settings, and initializes lamindb tracking. ```python import warnings import muon as mu import numpy as np import pandas as pd import scanpy as sc import seaborn as sns from scipy.stats import median_abs_deviation warnings.filterwarnings("ignore") mu.set_options(pull_on_update=False) sc.settings.verbosity = 0 sc.set_figure_params( dpi=80, facecolor="white", frameon=False, ) import lamindb as ln ln.track() ``` -------------------------------- ### Define Data Directory and File Path Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/trajectory/rna_velocity_dataset.ipynb Sets up the directory for data storage and defines the path for the dataset file. ```python DATA_DIR = Path("../../data/") DATA_DIR.mkdir(parents=True, exist_ok=True) FILE_PATH = DATA_DIR / "pancreas.h5ad" ``` -------------------------------- ### Setup AnnData for MultiVAE Model Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/advanced_integration.ipynb Registers categorical covariates for a MultiVAE model. Specifies the end index for RNA data, which is crucial for model setup. ```python mtg.model.MultiVAE.setup_anndata( adata, categorical_covariate_keys=["Modality", "Samplename"], rna_indices_end=4000, ) ``` -------------------------------- ### Install Squidpy using Conda Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Installs the Squidpy package using Conda from the conda-forge channel. This is recommended for managing complex dependencies in scientific Python projects. ```bash conda install -c conda-forge squidpy ``` -------------------------------- ### Define Data Paths Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/air_repertoire/ir_profiling.ipynb Sets up variables for local data storage paths. Ensure the 'data/' directory exists before running. ```python path_data = "data/" path_bcr_input = f"{path_data}/BCR_00_read_aligned.csv" path_tcr_input = f"{path_data}/TCR_00_read_aligned.tsv" ``` -------------------------------- ### List All Conda Environments Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/_static/default_text_env_setup.md Verify the successful creation and activation of your Conda environment by listing all available environments on your system. ```bash conda env list ``` -------------------------------- ### Configure Simpleaf Environment Variables Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/raw_data_processing.md Sets the ALEVIN_FRY_HOME environment variable to specify where Simpleaf should store configuration and data. It then runs `simpleaf set-paths` to locate and configure necessary tools. ```bash # simpleaf needs the environment variable ALEVIN_FRY_HOME to store configuration and data. # For example, the paths to the underlying programs it uses and the CB permit list mkdir alevin_fry_home && export ALEVIN_FRY_HOME='alevin_fry_home' # the simpleaf set-paths command finds the path to the required tools and writes a configuration JSON file in the ALEVIN_FRY_HOME folder. simpleaf set-paths ``` -------------------------------- ### Get AnnData Filename Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/fundamental_data_structures_and_frameworks.ipynb Retrieve the filename associated with a backed AnnData object. ```python adata.filename ``` -------------------------------- ### Load a Notebook (Transform) Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/_static/default_text_lamindb_setup.md Download a specific notebook (Transform) from a LaminDB instance to your current working directory using its URL. Older versions can be accessed by adapting the URL. ```bash lamin load ``` -------------------------------- ### Connect to lamindb instance Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Connect to the 'theislab/sc-best-practices' lamindb instance to manage and access datasets. ```bash lamin connect theislab/sc-best-practices ``` -------------------------------- ### Preliminary Preprocessing and Clustering Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/preprocessing_visualization/normalization.ipynb Performs initial preprocessing steps including normalization, log transformation, PCA, neighbor computation, and Leiden clustering. This sets up the data for subsequent group-based normalization. ```python adata_pp = adata.copy() sc.pp.normalize_total(adata_pp) sc.pp.log1p(adata_pp) sc.pp.pca(adata_pp, n_comps=15) sc.pp.neighbors(adata_pp) sc.tl.leiden( adata_pp, key_added="groups", flavor="igraph", n_iterations=2, directed=False ) ``` -------------------------------- ### Initialize LamineDB Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/introduction/interoperability_dataset.ipynb Imports the lamindb library and asserts the correct instance slug is set. This is a prerequisite for tracking data. ```python import lamindb as ln ``` ```python assert ln.setup.settings.instance.slug == "theislab/sc-best-practices" ``` -------------------------------- ### Get Graph Node and Edge Counts Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/advanced_integration.ipynb Retrieves the total number of nodes and edges in the constructed multimodal graph. ```python graph.number_of_nodes(), graph.number_of_edges() ``` -------------------------------- ### Initialize PyDESeq2 with a Simple Design Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/conditions/differential_gene_expression.ipynb Construct a PyDESeq2 object with a basic design matrix for initial analysis. This is useful when you want to model the effect of a single factor, like a treatment label, across the entire dataset. ```python pds2 = pt.tl.PyDESeq2(adata=adata_pb, design="~ label") ``` -------------------------------- ### Track Annotation Run Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/cellular_structure/annotation_reference_embedding.ipynb Starts tracking a new run for annotation. This function should be called at the beginning of a tracking session. ```python ln.track("fYSEh5kJ5gI8") ``` -------------------------------- ### Initialize scArches Model for Query Data Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/annotation.ipynb Loads the reference model and prepares it to accept new query data. This step involves specifying the reference model path and freezing dropout for consistency. ```python scarches_model = sca.models.SCVI.load_query_data( adata=adata_to_map_augmented, reference_model=str(model_dir), freeze_dropout=True, ) ``` -------------------------------- ### Load NicheNet Resources Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/mechanisms/cell_cell_communication.ipynb Loads the NicheNet ligand-target matrix and ligand-receptor network from URLs. Ensure the 'nichenetr' package is installed. ```R suppressPackageStartupMessages({ if(!require(nichenetr)) remotes::install_github("saeyslab/nichenetr", upgrade = "never") }) # Increase timeout threshold options(timeout=600) # Load PK ligand_target_matrix <- readRDS(url("https://zenodo.org/record/7074291/files/ligand_target_matrix_nsga2r_final.rds")) lr_network <- readRDS(url("https://zenodo.org/record/7074291/files/lr_network_human_21122021.rds")) ``` -------------------------------- ### Run helper script for quizzes Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Run the helper script from 'jupyter-book/src/lib.py' to enable quiz and flashcard creation in a notebook. ```python %run ../src/lib.py ``` -------------------------------- ### Load cisTopic Library Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/gene_regulatory_networks_atac.ipynb Loads the cisTopic library into the current R session. Use this after installation to access cisTopic functions. ```r suppressMessages(library(cisTopic)) ``` -------------------------------- ### Setup R Dependencies with reticulate Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/mechanisms/cell_cell_communication.ipynb Activates the anndata2ri converter and loads R extensions for use within a Python environment. ```python # Setting up R dependencies import anndata2ri anndata2ri.activate() %load_ext rpy2.ipython ``` -------------------------------- ### Set R Library Path Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/quality_control.ipynb Ensures R uses the correct path to the installed environment libraries before running doublet detection. ```R %%R .libPaths() ``` -------------------------------- ### Create a changelog fragment Source: https://github.com/theislab/single-cell-best-practices/blob/main/CONTRIBUTING.md Create a changelog fragment file for a pull request, specifying the change description, PR number, and author. ```bash towncrier create -c 'update blah blah ([#34](https://github.com/theislab/single-cell-best-practices/pull/34)) @seohyonkim' 34.changed.md ``` -------------------------------- ### Set R Library Paths Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/chromatin_accessibility/muon_to_seurat.ipynb Specifies the library paths for R to find installed packages. Ensure these paths are correct for your environment. ```bash .libPaths(c("/home/icb/laura.martens/miniconda3/envs/best_practices/lib/R/library", "/home/icb/laura.martens/miniconda3/envs/signac/lib/R/library")) ``` -------------------------------- ### Initialize and Train totalVI Model Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/advanced_integration.ipynb Initialize the totalVI model with specified architecture parameters and train it using the prepared AnnData object. The training process optimizes the model for multimodal data integration. ```python arches_params = { "use_layer_norm": "both", "use_batch_norm": "none", "n_layers_decoder": 2, "n_layers_encoder": 2, } vae = TOTALVI(adata, **arches_params) vae.train() ``` -------------------------------- ### Connect to LaminDB Instance and Load DataFrame Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/_static/default_text_lamindb_setup.md Connect to a specified LaminDB instance and retrieve the first 100 stored datasets as a pandas DataFrame. Ensure you have authenticated with LaminDB. ```python import lamindb as ln ln.Artifact.connect("theislab/sc-best-practices").df() ``` -------------------------------- ### Get Latent Representation Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/paired_integration.ipynb Obtains the latent representation from the trained totalVI model and stores it in the AnnData object's 'X_totalVI' attribute. ```python mdata.obsm["X_totalVI"] = vae.get_latent_representation() ``` -------------------------------- ### Initialize totalVI Model Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/paired_integration.ipynb Initializes the totalVI model with the prepared AnnData object. This step may involve computing empirical prior initialization for protein background. ```python vae = scvi.model.TOTALVI(adata) ``` -------------------------------- ### Construct CoNGA Preprocessing Command Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/air_repertoire/multimodal_integration.ipynb Constructs the command-line string to execute the CoNGA setup script for processing 10x data. This script takes the filtered TCR CSV and outputs a clones file, specifying the organism. ```python cmd_conga_pp = ( "python conga/scripts/setup_10x_for_conga.py " f"--filtered_contig_annotations_csvfile {path_reduced_tcr} " f"--output_clones_file {path_conga_clones} " "--organism human" ) ``` -------------------------------- ### Load AnnData Object using lamindb Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/preprocessing_visualization/quality_control.ipynb Loads an AnnData object from a specified artifact using lamindb. Ensure lamindb is installed and configured. ```python import lamindb as ln af = ln.Artifact.connect("theislab/sc-best-practices").get( key="preprocessing_visualization/quality_control_adata.h5ad", is_latest=True ) ada = af.load() ada ``` -------------------------------- ### Prepare and Run scCODA Model Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/conditions/compositional.ipynb Prepares the scCODA data for analysis by specifying the formula and reference cell type, then runs the NUTS sampler for inference. Ensure the 'coda' modality is correctly prepared. ```python sccoda_data = sccoda_model.prepare( sccoda_data, modality_key="coda", formula="condition", reference_cell_type="Endocrine", ) sccoda_model.run_nuts(sccoda_data, modality_key="coda", rng_key=1234) ``` -------------------------------- ### Instantiate CassiopeiaTree Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/trajectories/lineage_tracing.ipynb Initializes a CassiopeiaTree object with the character matrix and priors. This is a required first step before performing tree inference. ```python tree = cas.data.CassiopeiaTree(character_matrix=character_matrix, priors=priors) ``` -------------------------------- ### Define Cell Types to Check Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/cellular_structure/annotation.ipynb Creates a list of specific cell types for which marker gene expression will be checked. This is a setup step for further analysis. ```python cell_types_to_check = [ "CD14+ Mono", "cDC2", "NK", "B1 B", "CD4+ T activated", "T naive", "MK/E prog", ] ``` -------------------------------- ### Quantify Reads with Simpleaf Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/raw_data_processing.md Performs quantification using simpleaf quant, mapping FASTQ files to a pre-built index. Options include chemistry type, threads, read files, index path, unspliced permit list, resolution, and output directory. ```bash simpleaf quant \ -c 10xv3 -t 8 \ -1 $reads1 -2 $reads2 \ -i simpleaf_index/index \ -u -r cr-like \ -m simpleaf_index/index/t2g_3col.tsv \ -o simpleaf_quant ``` -------------------------------- ### Load SpatialData Dataset with Lamindb Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/introduction/advanced_data_structures_and_frameworks.ipynb Loads a SpatialData dataset from a lamindb artifact. This example assumes the artifact 'introduction/advanced_data_structures_and_frameworks_spatialdata.zarr' is available in the lamindb storage. ```python sdata = ln.Artifact.get( key="introduction/advanced_data_structures_and_frameworks_spatialdata.zarr" ).load() sdata ``` -------------------------------- ### Import Libraries and Set Up Scanpy Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/spatial/deconvolution.ipynb Imports necessary libraries for cell2location, scanpy, and squidpy. Sets verbosity and figure parameters for scanpy. ```python import cell2location as c2l import matplotlib import scanpy as sc import squidpy as sq sc.settings.verbosity = 3 sc.settings.set_figure_params(dpi=80, facecolor="white") ``` -------------------------------- ### Define Number of Genes and Regions Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/multimodal_integration/paired_integration.ipynb Defines the number of genes and regions for subsequent model setup. Ensure 'rna' and 'atac' AnnData objects are available. ```python n_genes = len(rna.var_names) n_regions = len(atac.var_names) ``` -------------------------------- ### Import and Configure CellPhoneDB Method in Liana Source: https://github.com/theislab/single-cell-best-practices/blob/main/jupyter-book/mechanisms/cell_cell_communication.ipynb Demonstrates how to import the cellphonedb method using Liana, specifying source and target cell types, and applying filters and ordering based on p-values and expression means. Useful for identifying and prioritizing significant cell-cell interactions. ```python liana.cellphonedb( source_labels=["CD4 T cells", "B cells", "FCGR3A+ Monocytes"], target_labels=["CD8 T cells", "CD14+ Monocytes", "NK cells"], filter_fun=lambda x: x["cellphone_pvals"] <= 0.01, orderby="lr_means", orderby_ascending=False, top_n=20, figure_size=(9, 5), size_range=(1, 6), ) ``` -------------------------------- ### Track run and load instance settings Source: https://github.com/theislab/single-cell-best-practices/blob/main/scripts/cellular_structure/anndata_clustering.ipynb Asserts the current lamindb instance settings and tracks a new run with a specific ID. This is useful for reproducibility and logging. ```python assert ln.setup.settings.instance.slug == "theislab/sc-best-practices" ln.track("639B7kNp8Fcb") ```