<jupyter_start><jupyter_text>Calculate Cell Doublet Score ContentHere we assign a doublet score to each cell. The MethylScrblet method we used here is adapted from the [Scrublet package](https://github.com/AllonKleinLab/scrublet) (REF). High doublet score indicates the cell is likely merged from different cell types. PrerequisiteBefore this step, run the [Basic Clustering Walk-through](../basic/mch_mcg_100k_basic.ipynb) to get the cell-by-feature AnnData file. Input- MCDS Files (contains chrom100K dim)- Cell Metadata- Output AnnData file from [Basic Clustering Walk-through](../basic/mch_mcg_100k_basic.ipynb). Output- Cell doublet score  Import<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import anndata

from ALLCools.clustering import MethylScrublet
from ALLCools.plot import *
from ALLCools.mcds import MCDS<jupyter_output><empty_output><jupyter_text>Parameters<jupyter_code># change this to the paths to your MCDS files
mcds_path_list = [
    '../../data/Brain/3C-171206.mcds',
]

adata_path = '../step_by_step/100kb/adata.with_coords.h5ad'
cef_path = '../step_by_step/100kb/L1_enriched_features.mCH.txt'
var_dim = 'chrom100k'
obs_dim = 'cell'
cluster_col = 'L1'

load = True
mc_type = 'CHN'

# Clustering resolution
n_neighbors = 10
expected_doublet_rate=0.06

plot_type = 'static'<jupyter_output><empty_output><jupyter_text>Load AnnData with clustering results<jupyter_code>adata = anndata.read_h5ad(adata_path)

use_features = pd.read_csv(cef_path, index_col=0, header=None).index
use_features.name = var_dim<jupyter_output><empty_output><jupyter_text>Raw mC counts<jupyter_code>mcds = MCDS.open(mcds_path_list, obs_dim=obs_dim, use_obs=adata.obs_names)
adata = adata[mcds.get_index(obs_dim), :].copy()
mc = mcds['chrom100k_da'].sel({
    'mc_type': mc_type,
    'count_type': 'mc',
    use_features.name: use_features
})
cov = mcds['chrom100k_da'].sel({
    'mc_type': mc_type,
    'count_type': 'cov',
    use_features.name: use_features
})

if load and (mcds.get_index('cell').size <= 20000):
    mc.load()
    cov.load()<jupyter_output><empty_output><jupyter_text>Calculate Doublet Scores<jupyter_code>scrublet = MethylScrublet(sim_doublet_ratio=2.0,
                          n_neighbors=n_neighbors,
                          expected_doublet_rate=expected_doublet_rate,
                          stdev_doublet_rate=0.02,
                          metric='euclidean',
                          random_state=0,
                          n_jobs=-1)
score, judge = scrublet.fit(mc, cov, clusters=adata.obs[cluster_col])
adata.obs['doublet_score'] = score
adata.obs['is_doublet'] = judge
scrublet.plot()
fig, ax = plt.subplots(figsize=(4, 4), dpi=250)
_ = categorical_scatter(data=adata.obs, ax=ax, coord_base='umap', hue=cluster_col, show_legend=True)
fig, ax = plt.subplots(figsize=(4, 4), dpi=250)
_ = continuous_scatter(data=adata.obs, ax=ax, coord_base='umap', hue='doublet_score')<jupyter_output><empty_output><jupyter_text>Plots<jupyter_code>fig, ax = plt.subplots(figsize=(4, 4), dpi=250)
_ = categorical_scatter(data=adata.obs,
                        ax=ax,
                        coord_base='umap',
                        hue='CellTypeAnno',
                        text_anno='CellTypeAnno',
                        palette='tab20',
                        show_legend=True)
fig, ax = plt.subplots(figsize=(4, 4), dpi=250)
_ = continuous_scatter(data=adata.obs, ax=ax, coord_base='umap', hue='doublet_score')
fig, ax = plt.subplots(figsize=(4, 4), dpi=250)
_ = categorical_scatter(data=adata.obs, ax=ax, coord_base='umap', hue='is_doublet', show_legend=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12103825136612022, 'fraction_numerical': 0.02185792349726776, 'mean_word_length': 4.105997210599721, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '201380', 'license': 'MIT', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1308, 'n_words': 239, 'path': 'docs/allcools/cell_level/doublets/doublet_score_chrom100k.ipynb', 'repo_name': 'mukamel-lab/ALLCools', 'size': '3660'}