Skip to main content

Data Handling

Legacy MPX tutorial

MPX support has been removed from pixelator in versions after v0.30.0. To run the code in this tutorial, pin your installation to that release:

pip install pixelgen-pixelator==0.30.0

This tutorial describes how to start working with the MPX output data format. The primary data output file of pixelator is the PXL file. Here, you will learn how to read and interact with it. We will go through the different components of a single sample PXL file and also how to aggregate results from multiple samples. You can read more about the PXL file format here.

After completing this tutorial, you will be able to:

  • Load PXL files in Python or R and access the multi-modal data contents including protein counts, metadata and spatial scores.
  • Understand and work with spatial polarity (protein clustering patterns).
  • Interpret and work with colocalization scores (protein co-clustering).
  • Directly access the spatial graph structure through the edge list.
  • Aggregate multiple PXL files into an integrated data object with sample identities.
  • Save aggregated data objects for later reuse in analyses.

Setup​

To start with, we need to load some packages and functions that we will need.

from pixelator.mpx import read
DATA_DIR = Path("<path to the directory to save datasets to>")

Loading data​

We begin by locating the output from the pixelator pipeline. The input for downstream analysis is the PXL file contained in the analysis directory.

baseurl = "https://pixelgen-technologies-datasets.s3.eu-north-1.amazonaws.com/mpx-datasets/pixelator/next/technote-v1-vs-v2-immunology-II"

!curl -L -O -C - --create-dirs --output-dir {DATA_DIR} "{baseurl}/Sample05_V2_PBMC_r1.layout.dataset.pxl"
from pathlib import Path

pg_data = read(DATA_DIR / "Sample05_V2_PBMC_r1.layout.dataset.pxl")

Component meta data​

In addition to the counts data, the object contains meta data of each MPX graph component, each corresponding to a cell. This table contains information that can be useful in quality control, with information such as how many protein molecules were detected (edges), the sequencing depth (reads), and the graph connectivity of each component (mean_upia_degree).

pg_data.adata.obs
pixelsa_pixelsb_pixelsantibodiesmoleculesreadsmean_reads_per_moleculemedian_reads_per_moleculemean_b_pixels_per_a_pixelmedian_b_pixels_per_a_pixelmean_a_pixels_per_b_pixelmedian_a_pixels_per_b_pixela_pixel_b_pixel_ratiomean_molecules_per_a_pixelmedian_molecules_per_a_pixelis_potential_doubletn_edges_to_split_doubletleidentau_typetau
component
002f13bdbf78b4992964193110337721000659683.1413333.03.1403422.05.8702813.01.86931310.8751946.0False0.012normal0.932284
00387aa4175efe8d4936343615007829584973573.2908673.02.6169972.05.9946673.02.2906678.6100124.0False0.00normal0.942541
005dd44dcce1635a51453326181977348451123563.2244513.02.8379432.05.1891153.01.82847710.4765486.0False0.02normal0.982450
00ccd2f98f49bd73282619059217718128562033.1003423.03.0062992.06.2182414.02.0684049.5160105.0False0.01normal0.968978
0105b317c16664a6229814818177713043404893.1042703.02.9743422.05.3916773.01.8127298.8068875.0False0.012normal0.923715
...............................................................
fe9dfa1d7899a40646273175145278383881230153.2045173.02.8919692.06.3236914.02.18663912.0907097.0False0.012normal0.929266
feeaa3dfeb603c36235315528017713130399503.0426503.02.6849232.05.2022473.01.9375788.4600525.0False0.012normal0.931461
ff05dde6ba92a2404746308616607722972714653.1109613.02.8972782.05.3861453.01.8590367.4439404.0False0.00normal0.964793
ff32ebf3d43d0ad25026332317037825411776593.0561173.02.7381882.05.3429243.01.9512627.6470065.0False0.02normal0.981417
ffe179c90dda64a250453510153578385041256333.2628563.02.8279202.06.4664503.02.28664510.9698016.0False0.01normal0.974452

1101 rows × 20 columns

The antibody count table is also present in the data object.

pg_data.adata.to_df()
markerB2MCD102CD11aCD11bCD11cCD123CD14CD150CD152CD154...CD86CD9CD94HLA-ABCHLA-DRTCRVB5ACTBmIgG1mIgG2amIgG2b
component
002f13bdbf78b499173330553315513361612...12200723146528138
00387aa4175efe8d512877402194159429252535...1443221233828194141917
005dd44dcce1635a197011335827052717418272428...113590728255246514232
00ccd2f98f49bd735071391164721215752915...9300205938184015148
0105b317c16664a615871368161522027515...4200655131025747
..................................................................
fe9dfa1d7899a4064967371948842840612433...116301859541774132221
feeaa3dfeb603c361124847281631334810...51504571623310125
ff05dde6ba92a24025589423711346436372351...295401148733677314746
ff32ebf3d43d0ad218297327111026711361141515...11243063520014417215
ffe179c90dda64a261268924756377271582332...16270218922194715349

1101 rows × 84 columns

Spatial data​

In addition to count data corresponding to protein abundance, MPX also generates spatial data which be used to visualize individual cells, and to find spatial structures of proteins across cells. The PXL file comes with MPX polarity scores, describing the degree of spatial clustering of each protein on each cell, and MPX colocalization scores, which describe the degree of spatial colocalization of pairs of proteins on each cell.

MPX Polarity scores​

The polarity scores are stored in the polarization attribute of the PXL object.

pg_data.polarization
markermorans_imorans_zmorans_p_valuemorans_p_adjustedcomponent
0ACTB-0.008266-2.0317480.0210900.1524256b437c2d776b156c
1B2M-0.005748-1.3079240.0954500.3416476b437c2d776b156c
2CD102-0.001478-0.1625680.4354290.4877916b437c2d776b156c
3CD11a0.0086081.5670350.0585530.2755016b437c2d776b156c
4CD11b0.0040120.9869470.1618340.4057876b437c2d776b156c
.....................
82410CD9-0.006795-0.3650570.3575340.471918401f95f79fab5327
82411HLA-ABC-0.003767-0.2424670.4042090.482039401f95f79fab5327
82412HLA-DR-0.018672-1.1106810.1333530.384813401f95f79fab5327
82413TCRVB5-0.008693-0.6079330.2716160.452067401f95f79fab5327
82414mIgG2a-0.010073-0.5002960.3084330.460591401f95f79fab5327

82415 rows × 6 columns

MPX colocalization scores​

The colocalization scores are stored in the colocalization attribute of the PXL object.

pg_data.colocalization
marker_1marker_2pearsonpearson_meanpearson_stdevpearson_zpearson_p_valuepearson_p_value_adjustedjaccardjaccard_meanjaccard_stdevjaccard_zjaccard_p_valuejaccard_p_value_adjustedcomponent
0ACTBB2M-0.004560-0.0157680.0211770.5292212.983260e-013.279794e-010.2387760.2559710.006499-2.6458970.0040740.0082906b437c2d776b156c
1ACTBCD102-0.139127-0.0012990.022592-6.1006715.281217e-102.546859e-090.2412120.2567220.006304-2.4602070.0069430.0133056b437c2d776b156c
2B2MCD102-0.207535-0.0389870.024024-7.0157751.143388e-127.468879e-120.2710560.2969460.008064-3.2104760.0006630.0016336b437c2d776b156c
3ACTBCD11a-0.080339-0.0098590.026331-2.6766883.717688e-036.546656e-030.2454920.2547570.007883-1.1753370.1199300.1581496b437c2d776b156c
4B2MCD11a0.033593-0.0448870.0229713.4164193.172522e-046.780218e-040.3267440.2968440.0072724.1116680.0000200.0000676b437c2d776b156c
................................................
3054474CD9mIgG2a-0.0036380.0053540.023431-0.3837883.505677e-013.751922e-010.2055820.2054150.0092610.0179880.4928240.494683fdd311340a4cf64f
3054475HLA-ABCmIgG2a0.041583-0.0046470.0226632.0399192.067921e-023.121286e-020.1902020.1905170.008624-0.0365040.4854400.489189fdd311340a4cf64f
3054476HLA-DRmIgG2a0.016440-0.0011830.0222560.7918092.142360e-012.476247e-010.1635690.1929630.008628-3.4067310.0003290.000869fdd311340a4cf64f
3054477TCRVB5mIgG2a0.0085470.0037690.0280880.1701134.324607e-014.454262e-010.2118430.2081880.0117690.3106030.3780510.404345fdd311340a4cf64f
3054478mIgG1mIgG2a-0.0918920.0017740.027947-3.3515334.018274e-048.438472e-040.1558800.1878080.011331-2.8176810.0024190.005211fdd311340a4cf64f

3054479 rows × 15 columns

Edgelist​

The edgelist is a list of uniquely identified antibodies per each cell, and simultaneously constitutes the bipartite graph which makes up an MPX experiment. Each row contains an edge (a uniquely identified antibody) which is identified by a UMI, UPIA, and a UPIB. The UPIA is the unique identifier for the specific A pixel, while the UPIB is the unique identifier for the B pixel. Both A pixels and B pixels may contain multiple edges, resulting in a spatial graph which can be used to infer spatial relationships between antibody molecules, to calculate spatial statistics, and to visualize the cell.

pg_data.edgelist
upiaupibumimarkersequencecountunique_molecules_countcomponent
0CGGGTTAGTTCATAATTAGCCCATTATTGCAGGTCGTTAGACAGATAGTGCGTCAGTGGGCD19ACCAACTT1516b437c2d776b156c
1CGTGATCAGACAACACGATGTGAGGGCAGGGAACGACGGGTTTGCGTGGCGAAGCGTCCTCD19ACCAACTT1518914534a50b9a420
2CTTTTAGTGTTTTCTATATTTCTTACGTTTGTTTTCTGCGAAATTACTGCTGCGAGGGGGCD19ACCAACTT151b033c579272106dc
3TTGGTGTACTTCATTCTGTCGGGTCAGCGGGGAGCTTGCGGTTTGTACATTAGTGGACCACD19ACCAACTT141d45e83b2ccb78637
4GTGAAAAATTATCGTGTTGTGTGCTATTACAAGGTGTATCTTGGAATTCGGTGCAAGCATCD19ACCAACTT131597225108b6c1cd9
...........................
25842387TGTGCGCGTTTGGATTAGTTGGTCTTTGTACGAGGTCGGGGCGAGCATGGGACCATGGTTB2MTAGCGCAG212ab91b04ef86fe1b
25842388TACGTGGGGCGGGGTGCTCTTAGAGGATTTTGTATTTTTTACTGCTCTGTTACCATCGCGB2MTAGCGCAG21f90f28a810d96e1b
25842389GATAATTATGCTGCCCACTAGGGAGGTATCGCCATGTGAGTACGCTGGTACACAAATCAGB2MTAGCGCAG21e8ecdfba70823d72
25842390GAGGATCATGGTAGGAAGGTTGCGTTATCCGGTTTCATTCTAGGCGGTTGCGAAAGGTTTB2MTAGCGCAG21cab06520f2dc08c6
25842391GGGGTAGTCTTTAGTATTCGCACTTTTTTTGCTATTCTTGAAATGTATCTCTAATAGACGB2MTAGCGCAG214f3ab4049ece50f0

25842392 rows × 8 columns

Aggregating data​

For cases when we want to process and analyze samples in parallel, it is convenient to merge them into a single object. Here, we read multiple files and aggregate them into a single object. When doing so, the cell ID and sample ID are merged to form the new cell ID in the merged object (i.e. “S1_RCVCMP0000830”).

In the code chunk below, we will create a merged object from four PXL files. These PXL files represent a resting and PHA stimulated PBMC sample, both in duplicate. We will create a merged object and update the object’s metadata to indicate the condition and replicate number. The resulting object will be explored further in the next tutorial on Quality Control.

baseurl = "https://pixelgen-technologies-datasets.s3.eu-north-1.amazonaws.com/mpx-datasets/pixelator/next/technote-v1-vs-v2-immunology-II"

!curl -L -O -C - --create-dirs --output-dir {DATA_DIR} "{baseurl}/Sample05_V2_PBMC_r1.layout.dataset.pxl"
!curl -L -O -C - --create-dirs --output-dir {DATA_DIR} "{baseurl}/Sample06_V2_PBMC_r2.layout.dataset.pxl"
!curl -L -O -C - --create-dirs --output-dir {DATA_DIR} "{baseurl}/Sample07_V2_PHA_PBMC_r1.layout.dataset.pxl"
!curl -L -O -C - --create-dirs --output-dir {DATA_DIR} "{baseurl}/Sample08_V2_PHA_PBMC_r2.layout.dataset.pxl"
from pixelator import simple_aggregate_mpx

paths = [
DATA_DIR / "Sample05_V2_PBMC_r1.layout.dataset.pxl",
DATA_DIR / "Sample06_V2_PBMC_r2.layout.dataset.pxl",
DATA_DIR / "Sample07_V2_PHA_PBMC_r1.layout.dataset.pxl",
DATA_DIR / "Sample08_V2_PHA_PBMC_r2.layout.dataset.pxl",
]

datasets = list([read(path) for path in paths])
pg_data_combined = simple_aggregate_mpx(
["resting_r1", "resting_r2", "stimulated_r1", "stimulated_r2"], datasets
)
pg_data_combined
Pixel dataset contains:
AnnData with 4273 obs and 84 vars
Edge list with 104324377 edges
Polarization scores with 320210 elements
Colocalization scores with 11871509 elements
Contains precomputed layouts
Metadata:
samples: {'resting_r1': {'version': '0.19.0', 'sample': 'Sample05_V2_PBMC_r1', 'analysis': {'params': {'polarization': {'polarization': {'transformation': 'log1p', 'permutations': 50, 'min_marker_count': 5, 'random_seed': None}}, 'colocalization': {'colocalization': {'transformation_type': 'rate-diff', 'neighbourhood_size': 1, 'n_permutations': 50, 'min_region_count': 5, 'min_marker_count': 5}}}}}, 'resting_r2': {'version': '0.19.0', 'sample': 'Sample06_V2_PBMC_r2', 'analysis': {'params': {'polarization': {'polarization': {'transformation': 'log1p', 'permutations': 50, 'min_marker_count': 5, 'random_seed': None}}, 'colocalization': {'colocalization': {'transformation_type': 'rate-diff', 'neighbourhood_size': 1, 'n_permutations': 50, 'min_region_count': 5, 'min_marker_count': 5}}}}}, 'stimulated_r1': {'version': '0.19.0', 'sample': 'Sample07_V2_PHA_PBMC_r1', 'analysis': {'params': {'polarization': {'polarization': {'transformation': 'log1p', 'permutations': 50, 'min_marker_count': 5, 'random_seed': None}}, 'colocalization': {'colocalization': {'transformation_type': 'rate-diff', 'neighbourhood_size': 1, 'n_permutations': 50, 'min_region_count': 5, 'min_marker_count': 5}}}}}, 'stimulated_r2': {'version': '0.19.0', 'sample': 'Sample08_V2_PHA_PBMC_r2', 'analysis': {'params': {'polarization': {'polarization': {'transformation': 'log1p', 'permutations': 50, 'min_marker_count': 5, 'random_seed': None}}, 'colocalization': {'colocalization': {'transformation_type': 'rate-diff', 'neighbourhood_size': 1, 'n_permutations': 50, 'min_region_count': 5, 'min_marker_count': 5}}}}}}

Save data​

After combining the samples, we can save the merged data object to use later.

pg_data_combined.save(DATA_DIR / "combined_data.pxl", force_overwrite=True)

We have now seen how to load MPX data, inspect its key properties and prepare the data for integrated analysis across experimental samples. In the next tutorial we will apply these skills and demonstrate how to quality control MPX data.