Skip to main content

Differential Abundance

So far, we have seen how to merge, normalize, integrate and annotate the cell types in our dataset. We can now proceed to the biological interpretation of our experiment. Proximity Network Assay (PNA) data contains a wealth of spatial data that we will discuss in the following tutorials. Here, we will focus on the differential abundance of markers across conditions.

After completing this tutorial, you should be able to:

  • Identify abundance changes across conditions within a cell type
  • Visualize these changes in a clear way

Setup​

from matplotlib.pyplot import rc_context
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
import pandas as pd
from pixelator import read_pna as read
from pixelator.common.statistics import clr_transformation
from pixelator.pna.analysis import differential_abundance
import seaborn as sns

sns.set_style("whitegrid")
DATA_DIR = Path("./pna-analysis")
from pixelator.pna.pixeldataset.download import DownloadableDatasets

DATA_DIR.mkdir(parents=True, exist_ok=True)

DownloadableDatasets.download_dataset(
"pna062-unstim-pbmcs",
output_path=DATA_DIR / "PNA062_unstim_PBMCs_1000cells_S02_S2.layout.pxl",
)
DownloadableDatasets.download_dataset(
"pna062-pha-pbmcs",
output_path=DATA_DIR / "PNA062_PHA_PBMCs_1000cells_S04_S4.layout.pxl",
)

Load data​

To illustrate the annotation workflow we continue with the cell type annotations from the previous tutorial. We will also recreate the merged and harmonized object. As described before, this merged and harmonized object contains two samples: a resting and PHA stimulated PBMC sample.

pg_data_combined_pxl_object =  read([
DATA_DIR/ "PNA062_unstim_PBMCs_1000cells_S02_S2.layout.pxl",
DATA_DIR/ "PNA062_PHA_PBMCs_1000cells_S04_S4.layout.pxl",
])
filtered_cells = list(pd.read_csv(DATA_DIR / "qc_filtered_cells.csv")["component"])
pg_data_combined_pxl_object = pg_data_combined_pxl_object.filter(components=filtered_cells)
# In this tutorial we will mostly work with the AnnData object
# so we will start by selecting that from the pixel data object
pg_data_combined = pg_data_combined_pxl_object.adata()

# Adding cell-type annotations saved in the last tutorial
pg_data_combined.obs = pg_data_combined.obs.join(pd.read_csv(DATA_DIR / "cell_type_annotations.csv").set_index("component"))

# Perform CLR normalization
pg_data_combined.layers["clr"] = clr_transformation(
pg_data_combined.to_df(),
axis=1, non_negative=True
)

Differential Abundance in CD8 T cells​

As PHA is a T cell mitogen, one might be interested in how PHA treatment affects protein abundance in T cells. Therefore, in this tutorial, we will subset the data to only include the CD8 T cells and then perform a differential abundance analysis of the PHA treated cells versus the resting cells.

differential_abundance compares a target condition with a reference on the matrix it is given. contrast_column names the observation column that holds the conditions, targets is the condition of interest, and reference is the baseline. The cells are already subset to CD8 T cells, so this comparison is PHA stimulated versus Resting within that cell type. CLR normalization is done above and stored in the clr layer; pass that layer so the test uses those values. difference is the mean CLR in the target minus the mean CLR in the reference. pvals_adj is the multiple-testing adjustment across the markers in this call.

# Subset data to only include CD8 T cells
cd8_t_cells = pg_data_combined[pg_data_combined.obs['cell_type'] == 'CD8 T',:]

# naming the conditions
cd8_t_cells.obs["condition"] = "Resting"
cd8_t_cells.obs.loc[
cd8_t_cells.obs["sample"].astype(str).str.contains("PHA", na=False),
"condition",
] = "PHA stimulated"
cd8_t_cells.obs["condition"] = pd.Categorical(
cd8_t_cells.obs["condition"],
categories=["Resting", "PHA stimulated"],
ordered=True,
)

# Mean CLR difference between PHA-stimulated and resting CD8 T cells
diff_exp_df = differential_abundance(
cd8_t_cells,
contrast_column="condition",
reference="Resting",
targets="PHA stimulated",
layer="clr",
)
diff_exp_df
markerpvalspvals_adjdifferencepct_nz_grouppct_nz_referencetargetreference
0CD379.988615e-591.578201e-560.8378150.9968251.000000PHA stimulatedResting
1CD203.697717e-535.842393e-510.5849361.0000000.880795PHA stimulatedResting
2CD2782.790213e-484.408536e-460.7394801.0000000.847682PHA stimulatedResting
3CD385.677483e-488.970422e-462.0588931.0000000.973510PHA stimulatedResting
4CD2746.224112e-469.834097e-440.3790401.0000000.960265PHA stimulatedResting
...........................
153CD413.368665e-355.322490e-33-0.4064990.9904760.993377PHA stimulatedResting
154CD483.726829e-425.888391e-40-0.8489041.0000001.000000PHA stimulatedResting
155CD2771.332853e-422.105907e-40-0.8076450.9968251.000000PHA stimulatedResting
156CD3052.120235e-433.349971e-41-0.9657470.9936511.000000PHA stimulatedResting
157CD366.047983e-609.555814e-58-0.6818830.9841271.000000PHA stimulatedResting

158 rows × 8 columns

Let’s visualize these results as a volcano plot and annotate the 10 markers with the highest mean difference and the 10 with the lowest. The x-axis is difference (mean CLR in PHA-stimulated cells minus mean CLR in resting cells).

diff_exp_df["-log10(adjusted p-value)"] = -np.log10(diff_exp_df["pvals_adj"])
diff_exp_df["Significant"] = diff_exp_df["pvals_adj"] < 0.01

markers_to_label = (
diff_exp_df.nlargest(10, "difference")["marker"].tolist()
+ diff_exp_df.nsmallest(10, "difference")["marker"].tolist()
)

# Create scatter plot
plt.figure(figsize=(10, 6))
sns.scatterplot(data=diff_exp_df, x='difference', y='-log10(adjusted p-value)', hue='Significant')

for _, row in diff_exp_df[diff_exp_df["marker"].isin(markers_to_label)].iterrows():
plt.annotate(
row["marker"],
(row["difference"], row["-log10(adjusted p-value)"]),
xytext=(5, 5),
textcoords="offset points",
)

Alternatively, in a heatmap we can visualize the average normalized values in the CD8 T cells of each sample for the 7 markers with the highest mean difference and the 7 with the lowest.

markers_to_plot = (
diff_exp_df.nlargest(7, "difference")["marker"].tolist()
+ diff_exp_df.nsmallest(7, "difference")["marker"].tolist()
)

sample_order = (
cd8_t_cells.obs[["sample", "condition"]]
.drop_duplicates()
.sort_values("condition")["sample"]
)
mean_values = pd.DataFrame()
for sample in sample_order:
sample_cells = cd8_t_cells[cd8_t_cells.obs["sample"] == sample]
mean_values[sample] = sample_cells[:, markers_to_plot].to_df("clr").mean()

plt.figure(figsize=(10, 8))
sns.heatmap(
mean_values.loc[markers_to_plot],
cmap="viridis",
yticklabels=True,
xticklabels=True,
linewidths=1,
linecolor="black",
)
plt.title("Average CLR values in CD8 T cells per sample")
plt.xticks(rotation=0)
plt.yticks(rotation=0)
plt.tight_layout()

In this example, it seems that many of the T cell relevant markers (such as CD71, CD38 and CD25) show a significant increase in abundance upon PHA stimulation. KLRG1 decreases, and so do CD162, CD48, and CD102, which stay high in both samples.

However, abundance is only one facet of the rich multidimensional data generated by Proximity Network experiments. We are now ready to transition into the spatial realm, exploring the arrangement and interactions of proteins on the cell surface. In the upcoming tutorials, we will leverage the graph nature and spatial metrics within Proximity Network Assay data to unravel the spatial biology of cells, surveying protein clustering and colocalization patterns and how these contribute to cell identity, signaling, and function. Abundance has illuminated cell types; spatiality will illuminate the molecular geography that defines them.