Skip to contents

[Experimental]

The function only exports the essential data required to create a functional .pxl file. See details below for a description of what data is exported.

Usage

WriteMPX_pxl_file(
  object,
  file,
  assay = NULL,
  export_layouts = FALSE,
  overwrite = FALSE
)

Arguments

object

A Seurat object with a CellGraphAssay5 assay object created with pixelatorR.

file

A character string specifying the path to the .pxl file to be created.

assay

A character string specifying the name of the CellGraphAssay5. If set to NULL the default assay will be used.

export_layouts

A logical value specifying whether to export the layouts from the Seurat object. If set to TRUE, each component must have a layout. See LoadCellGraphs and ComputeLayout for details about how to load graphs and compute layouts.

overwrite

A logical value specifying whether to overwrite the file if it already exists.

Value

Nothing. The function writes the .pxl file to the specified location.

Exported data

  • Count data and metadata : The raw count matrix and component metadata from the Seurat object are exported into a .h5ad file that can be read with the anndata Python library.

  • Polarization scores : The polarization scores are exported to a .parquet file.

  • Colocalization scores : The colocalization scores are exported to a .parquet file.

  • Edgelist data : The edgelist data is first collected from the original .pxl file(s), filtered to include the components currently available in the Seurat object, and then the component IDs are updated. The resulting merged edgelist data is exported to a .parquet file.

  • Sample meta data : The sample meta data is extracted from the original .pxl file(s), then merged and exported to a .json file.

The structure of the .pxl file is detailed below:

|-- adata.h5ad
|-- polarization.parquet
|-- colocalization.parquet
|-- metadata.json
|-- edgelist.parquet

The merged files are converted into a zip archive and saved to the target .pxl file.

NOTE: Factors are currently not supported. These will be converted to string arrays.

Examples

# Use Assay5 as the default assay version
options(Seurat.object.assay.version = "v5")

# Create Seurat object
pxl_file <- system.file("extdata/five_cells",
                        "five_cells.pxl",
                        package = "pixelatorR")
se <- ReadMPX_Seurat(pxl_file)
#>  Created a 'Seurat' object with 5 cells and 80 targeted surface proteins

se_merged <- merge(se, list(se, se, se))
#> Warning: Some cell names are duplicated across objects provided. Renaming to enforce unique cell names.
pxl_file <- fs::path_temp("small.pxl")

# Export data to a new .pxl file
WriteMPX_pxl_file(se_merged, pxl_file)
#> ── Creating .pxl file ──────────────────────────────────────────────────────────
#> → Writing spatial metrics to .parquet files...
#> → Writing polarization scores to polarization.parquet
#> → Writing colocalization scores to colocalization.parquet
#>  Exported spatial metrics
#> → Collecting edge lists from 4 files
#> → Extracting sample 1 edge list
#> → Extracting sample 2 edge list
#> → Extracting sample 3 edge list
#> → Extracting sample 4 edge list
#> →  Merging edge lists
#> → Loaded sample 1 edge list
#> → Loaded sample 2 edge list
#> → Loaded sample 3 edge list
#> → Loaded sample 4 edge list
#> → Merging edge lists
#> → Exporting merged edge list
#>  Exported merged edge list
#>  Exported merged meta data
#>  Exported anndata file
#>  Saving .pxl file to /var/folders/gw/bdcqhnvs0m9gs_mq8n51jtbc0000gn/T/RtmpJCnvFY/small.pxl
#>  Finished!

# Read the new .pxl file
se_merged <- ReadMPX_Seurat(pxl_file)
#>  Created a 'Seurat' object with 20 cells and 80 targeted surface proteins

# Reset global option
options(Seurat.object.assay.version = "v3")