The import-segmentation
pipeline allows you to specify 2D nuclei and/or cell segmentation results to use for assigning transcripts to cells and recalculate all Xenium Onboard Analysis (XOA) outputs that depend on segmentation. Segmentation results can be generated by community-developed tools or a prior Xenium segmentation result.
import-segmentation
using the --transcript-assignment
argument (i.e., Baysor input).Since
relabel
, resegment
, and import-segmentation
pipelines require the segmentation mask files, they cannot be run sequentially after importing transcript-based segmentation results.The input files for import-segmentation
are:
- The Xenium output bundle generated by the Xenium Onboard Analysis pipeline (see Input overview).
- Segmentation file(s) from community-developed tools (see compatible formats for examples). Xenium Ranger supports importing 2D segmentation results.
The xeniumranger import-segmentation
command line arguments are listed on the Xenium Ranger command line arguments page. There are separate command line arguments for Baysor inputs due to differences in how it generates segmentation results.
Below are examples for importing segmentation results from community-developed tools. Make sure to edit the paths to input/output files in the examples below for your own datasets. The examples provide the recommended settings for --localcores
and --localmem
parameter values (see Resource limits for more details).
If you have QC'd the nucleus segmentation in Xenium Explorer and want to adjust the nuclear expansion distance without altering nuclei detection, use the following arguments. In Xenium Ranger v2.0 and later, import-segmentation
with --nuclei=cells.zarr.zip
is the preferred method to import existing nucleus segmentation results, instead of using the resegment
pipeline with --resegment-nuclei=false. This approach is also faster as it skips the nucleus segmentation step.
In this example, say the original dataset was analyzed with 15 µm nuclear expansion distance. Let's adjust it to 5 µm. Since 5 µm is the default value in Xenium Ranger v2.0 and later, --expansion-distance=5
does not need to be specified in this example:
xeniumranger import-segmentation --id=demo \
--xenium-bundle=/path/to/xenium/files \
--nuclei=/path/to/cells.zarr.zip \
--localcores=32 \
--localmem=128
Viewing the original output in Xenium Explorer with a post-Xenium H&E image, the cell and nuclei outlines look like this at 15 µm expansion distance (default in XOA v1.0 - 1.9) for segmentation of cells in the lumen of the human colon:

The same area of the resegment
analysis output with 5 µm expansion distance is shown below. The cell boundaries now better match the biology of the sample indicated in the H&E image:

To import XOA segmentation results and create a nucleus-only count matrix, set the expansion distance to 0 µm:
xeniumranger import-segmentation --id=demo \
--xenium-bundle=/path/to/xenium/files \
--nuclei=/path/to/cells.zarr.zip \
--expansion-distance=0 \
--localcores=32 \
--localmem=128
Generate polygon segmentation results in GeoJSON format (i.e., from QuPath) and then import. QuPath input file specifications are described here.
polygon.geojson
containing GeoJSON polygons- Note: we recommend including nucleus segmentation when running cell detection so both nucleus and cell segmentations are in one GeoJSON file (default export format for QuPath), with nucleus segmentations named
nucleusGeometry
. - Features where
object_type
is notcell
will be ignored (i.e.,annotations
).
Example to import nucleus and cell segmentation using a single GeoJSON that contains both:
xeniumranger import-segmentation --id=polygon-demo \
--xenium-bundle=/path/to/xenium/files \
--nuclei=polygon.geojson \
--cells=polygon.geojson \
--units=pixels \
--localcores=32 \
--localmem=128
You can view the output in Xenium Explorer. For example, here are the cell clusters with nuclei outlines for a mouse pup sample with cell segmentation results generated by QuPath:


Example to only import nucleus segmentation below. If unspecified, Xenium Ranger will use the default nuclear expansion distance (5 µm):
xeniumranger import-segmentation --id=polygon-demo-nuclei \
--xenium-bundle=/path/to/xenium/files \
--nuclei=polygon.geojson \
--units=pixels \
--localcores=32 \
--localmem=128
Generate 2D mask segmentation results (i.e., 2D from Cellpose) and then import. The nucleus and cell segmentation files can be in either TIFF or NPY format. For example:
nuclei.tif
ornuclei.npy
containing nucleus segmentation resultscells.tif
orcells.npy
containing cell segmentation results
Example to import nucleus and cell segmentation:
xeniumranger import-segmentation --id=mask-demo-nuclei \
--xenium-bundle=/path/to/xenium/files \
--nuclei=nuclei.tif \
--cells=cells.npy \
--localcores=32 \
--localmem=128
Example to only import nucleus segmentation below. If unspecified, Xenium Ranger will use the default nuclear expansion distance (5 µm):
xeniumranger import-segmentation --id=mask-demo-cells \
--xenium-bundle=/path/to/xenium/files \
--nuclei=nuclei.tif \
--localcores=32 \
--localmem=128
Transcript-based segmentation methods like Baysor and Proseg are alternatives to image-based segmentation. They convert in situ RNA data into single cell data when cell stain images are insufficient for capturing the morphology of cells in tissue. They use the gene identity and location of transcripts to assign them to cells and draw cell boundaries.
--transcript-assignment
) with Xenium Protein data, the protein quantification and analysis will be disabled because there are no cell masks for calculating mean fluorescence intensity.Generate segmentation results using ≥ Q20 transcripts (e.g., Baysor v0.6 - 0.7.1) and then import. Baysor input file specifications are described here.
segmentation.csv
containing segmentation and transcript assignment resultssegmentation_polygons_2d.json
containing GeoJSON 2D polygons
xeniumranger import-segmentation --id=baysor-demo \
--xenium-bundle=/path/to/xenium/files \
--transcript-assignment=segmentation.csv \
--viz-polygons=segmentation_polygons_2d.json \
--units=microns \
--localcores=32 \
--localmem=128
For this use case, some nuclei and cell import-segmentation metrics are not computed because a single segmentation_polygons_2d
GeoJSON file is imported (compared to the polygon input usage). Mapping between transcript-based method and XOA cell IDs is described on the algorithms page.
View the output in Xenium Explorer
Here is an example showing the cell clusters for a mouse brain sample with cell segmentation results generated by Baysor:

This option enables you to inform your Xenium analysis with cell segmentation based on post-Xenium imaging. You can use Xenium Explorer or community-developed tools to align a post-Xenium H&E or IF image and generate an image alignment transformation matrix for input to Xenium Ranger’s import-segmentation
pipeline.
The image alignment file format should be a 3x3 transformation matrix, where the last row is [0,0,1]. It captures the translation, scaling, rotation, and reflection applied to the imported image (see Xenium Explorer's alignment algorithm section). Some image alignment CSV files are available on the Xenium Human Colon Preview dataset page (for Non-diseased, pre-designed panel and Cancer, pre-designed + add-on panel samples).
In this example, the cells and nuclei are segmented on a post-Xenium H&E image using QuPath. The image is then aligned in Xenium Explorer, where the image alignment CSV transformation matrix is downloaded.
To visualize the QuPath cell/nucleus segmentation polygons on the H&E image in Xenium Explorer, we need to import the QuPath results into the Xenium output bundle.
The QuPath GeoJSON segmentation results and the Xenium Explorer matrix CSV are the inputs for Xenium Ranger import-segmentation
:
xeniumranger import-segmentation --id=image-transform-demo \
--xenium-bundle=/path/to/xenium/files \
--coordinate-transform=demo_imagealignment.csv \
--nuclei=qupath.geojson \
--cells=qupath.geojson \
--units=pixels \
--localcores=32 \
--localmem=128
In Xenium Ranger v1.7 and later, the segmentation results can be in either polygon or mask format.
After running this command, the dataset can be opened in Xenium Explorer to visualize segmentation and transcript results overlaid on the H&E image (select option to upload the image alignment matrix to open the already aligned H&E image).
After the pipeline completes, check the output outs/
directory (see Output overview). For example, look at these files:
- Analysis summary: check metrics and plots on the Cell Segmentation tab. There are new metrics (described here) for imported segmentation results (present in
analysis_summary.html
andmetrics_summary.csv
). - All secondary analysis files
- All cell, nuclei, and transcript files
Morphology images are unchanged. The updated segmentation results can be visualized in Xenium Explorer. Starting in Xenium Ranger v4.0, this pipeline also generates a CSV file (cell_id_map.csv.gz
) with the mapping of Xenium Ranger's cell IDs to the imported file's cell IDs for downstream analysis. Learn more about use cases for this CSV file here.