<jupyter_start><jupyter_text>Preclustering and Cluster Enriched Features PurposeThe purpose of this step is to perform a simple pre-clustering using the highly variable features to get a pre-clusters labeling. We then select top enriched features for each cluster (CEF) for further analysis. Input- HVF adata file. Output- HVF adata file with pre-clusters and CEF annotated.  Import<jupyter_code>import seaborn as sns
import anndata
import scanpy as sc
from ALLCools.clustering import cluster_enriched_features, significant_pc_test, log_scale
sns.set_context(context='notebook', font_scale=1.3)<jupyter_output><empty_output><jupyter_text>Parameters<jupyter_code>adata_path = 'mCH.HVF.h5ad'

# Cluster Enriched Features analysis
top_n=200
alpha=0.05
stat_plot=True

# you may provide a pre calculated cluster version. 
# If None, will perform basic clustering using parameters below.
cluster_col = None  

# These parameters only used when cluster_col is None
k=25
resolution=1
cluster_plot=True<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>adata = anndata.read_h5ad(adata_path)<jupyter_output><empty_output><jupyter_text>Pre-ClusteringIf cluster label is not provided, will perform basic clustering here<jupyter_code>if cluster_col is None:
    # IMPORTANT
    # put the unscaled matrix in adata.raw
    adata.raw = adata
    log_scale(adata)
    
    sc.tl.pca(adata, n_comps=100)
    significant_pc_test(adata, p_cutoff=0.1, update=True)
    
    sc.pp.neighbors(adata, n_neighbors=k)
    sc.tl.leiden(adata, resolution=resolution)
    
    if cluster_plot:
        sc.tl.umap(adata)
        sc.pl.umap(adata, color='leiden')
    
    # return to unscaled X, CEF need to use the unscaled matrix
    adata = adata.raw.to_adata()
    
    cluster_col = 'leiden'<jupyter_output>32 components passed P cutoff of 0.1.
Changing adata.obsm['X_pca'] from shape (16985, 100) to (16985, 32)<jupyter_text>Cluster Enriched Features (CEF)<jupyter_code>cluster_enriched_features(adata,
                          cluster_col=cluster_col,
                          top_n=top_n,
                          alpha=alpha,
                          stat_plot=True)<jupyter_output>Found 31 clusters to compute feature enrichment score
Computing enrichment score
Computing enrichment score FDR-corrected P values
Selected 3102 unique features<jupyter_text>Save AnnData<jupyter_code># save adata
adata.write_h5ad(adata_path)
adata<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07363710333604556, 'fraction_numerical': 0.01790073230268511, 'mean_word_length': 4.334056399132321, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '1093', 'license': 'MIT', 'n_tokens_mistral': 858, 'n_tokens_neox': 791, 'n_words': 209, 'path': 'docs/allcools/cell_level/step_by_step/100kb/04a-PreclusteringAndClusterEnrichedFeatures-mCH.ipynb', 'repo_name': 'mukamel-lab/ALLCools', 'size': '2458'}