[Python] Weird dots when I try to map my cluster to my tissue image

Discussão em 'Python' iniciado por Stack, Junho 19, 2025.

  1. Stack

    Stack Membro Participativo

    I just started with Visium. I found these public datasets from a paper, and I've been trying to navigate the pipeline to analyze them. However, when I map the clusters to the tissue, there are some weird gray dots that do not belong to any clusters. Could anyone let me know what the issue might be?

    import scanpy as sc
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import json

    # === 1. Load raw counts matrix ===
    adata = sc.read_10x_mtx(
    "/path/GSE158328_RAW/A1/raw_feature_bc_matrix/",
    var_names="gene_symbols",
    cache=True
    )
    print(f"Raw counts matrix shape: {adata.shape}")

    # === 2. Load tissue positions and filter by in_tissue == 1 ===
    spots = pd.read_csv(
    "/path/GSE158328_RAW/A1/spatial/tissue_positions_list.csv",
    header=None
    )
    spots.columns = ['barcode', 'in_tissue', 'array_row', 'array_col',
    'pxl_row_in_fullres', 'pxl_col_in_fullres']

    # Keep only spots in tissue
    spots = spots[spots['in_tissue'] == 1].copy()
    spots = spots.set_index('barcode')

    # Filter AnnData barcodes to match tissue spots
    adata = adata[adata.obs_names.isin(spots.index)].copy()
    print(f"After filtering, adata shape: {adata.shape}")

    # === 3. Assign raw spatial coordinates to adata.obsm['spatial'] ===
    coords = spots.loc[adata.obs_names, ['pxl_col_in_fullres', 'pxl_row_in_fullres']].values
    adata.obsm["spatial"] = coords

    print(f"Raw coordinate sample (first 5 spots):\n{coords[:5]}")
    print(f"X coordinate min/max: {coords[:,0].min()} / {coords[:,0].max()}")
    print(f"Y coordinate min/max: {coords[:,1].min()} / {coords[:,1].max()}")

    # === 4. Load scaling factors ===
    with open("/path/GSE158328_RAW/A1/spatial/scalefactors_json.json") as f:
    sf = json.load(f)
    print("Scaling factors:", sf)

    # === 5. Load hires image ===
    img_path = "/path/GSE158328_RAW/A1/spatial/tissue_hires_image.png"
    img = plt.imread(img_path)
    print(f"Hires image shape (H, W, C): {img.shape}")

    # === 6. Store image + scaling in adata.uns for later ===
    adata.uns["spatial"] = {
    "A1": {
    "images": {"hires": img},
    "scalefactors": sf,
    "metadata": {"source": "GSE158328"}
    }
    }

    # === 7. Plot raw spots on hires image (no scaling, no flipping) ===
    plt.figure(figsize=(8,8))
    plt.imshow(img)
    plt.scatter(coords[:,0], coords[:,1], s=5, color="red", alpha=0.6)
    plt.title("Raw spots on hires image (unscaled, unflipped)")
    plt.show()


    After reading the data in, I did some QC, normalization, and clustering. Then, I tried to map the cluster to my tissue image.

    sc.pl.spatial(
    adata,
    color='leiden',
    img_key='hires',
    spot_size=sf["spot_diameter_fullres"] * sf["tissue_hires_scalef"],
    frameon=False,
    legend_loc='right margin'
    )


    What are the gray dots here? Is that normal? And how should I remove them?

    [​IMG]

    Continue reading...

Compartilhe esta Página