Misc
Settings
Disabling auto-save
By default, if your SpatialData
object is stored on-disk, it will also store the new elements on disk.
You can disable this behavior as follow:
Parallelization backends
Some methods (for instance sopa.segmentation.cellpose
) may need a parallelization backend to run fast enough.
You can set it as below:
sopa.settings.parallelization_backend = "dask" # using dask
sopa.settings.parallelization_backend = None # no backend (i.e., sequential)
Warning
The dask
backend is still experimental. You can add a comment to this issue to help us improve it.
You can also pass some kwargs to the dask Client:
Otherwise, if you don't use the API, you can also set the SOPA_PARALLELIZATION_BACKEND
env variable, e.g.:
Xenium Explorer
sopa.io.explorer.write(path, sdata, image_key=None, shapes_key=None, points_key=None, gene_column=None, pixel_size=0.2125, layer=None, polygon_max_vertices=13, lazy=True, ram_threshold_gb=4, mode=None, save_h5ad=False, run_name=None)
Transform a SpatialData object into inputs for the Xenium Explorer.
After running this function, double-click on the experiment.xenium
file to open it.
Software download
Make sure you have the latest version of the Xenium Explorer
Note
This function will create up to 7 files, depending on the SpatialData
object and the arguments:
-
experiment.xenium
contains some experiment metadata. Double-click on this file to open the Xenium Explorer. This file can also be created withwrite_metadata
. -
morphology.ome.tif
is the primary image. This file can also be created withwrite_image
. Add more images withalign
. -
analysis.zarr.zip
contains the cells categories (or clusters), i.e.adata.obs
. This file can also be created withwrite_cell_categories
. -
cell_feature_matrix.zarr.zip
contains the cell-by-gene counts. This file can also be created withwrite_gene_counts
. -
cells.zarr.zip
contains the cells polygon boundaries. This file can also be created withwrite_polygons
. -
transcripts.zarr.zip
contains transcripts locations. This file can also be created withwrite_transcripts
. -
adata.h5ad
is theAnnData
object from theSpatialData
. This is not used by the Explorer, but only saved for convenience.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the directory where files will be saved. |
required |
sdata
|
SpatialData
|
SpatialData object. |
required |
image_key
|
str | None
|
Name of the image of interest (key of |
None
|
shapes_key
|
str | None
|
Name of the cell shapes (key of |
None
|
points_key
|
str | None
|
Name of the transcripts (key of |
None
|
gene_column
|
str | None
|
Column name of the points dataframe containing the gene names. |
None
|
pixel_size
|
float
|
Number of microns in a pixel. Invalid value can lead to inconsistent scales in the Explorer. |
0.2125
|
layer
|
str | None
|
Layer of the AnnData table where the gene counts are saved. If |
None
|
polygon_max_vertices
|
int
|
Maximum number of vertices for the cell polygons. |
13
|
lazy
|
bool
|
If |
True
|
ram_threshold_gb
|
int | None
|
Threshold (in gygabytes) from which image can be loaded in memory. If |
4
|
mode
|
str
|
string that indicated which files should be created. "-ib" means everything except images and boundaries, while "+tocm" means only transcripts/observations/counts/metadata (each letter corresponds to one explorer file). By default, keeps everything. |
None
|
save_h5ad
|
bool
|
Whether to save the adata as h5ad in the explorer directory (for convenience only, since h5ad is faster to open than the original .zarr table) |
False
|
run_name
|
str | None
|
Name of the run displayed in the Xenium Explorer. If |
None
|
Source code in sopa/io/explorer/converter.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
sopa.io.explorer.align(sdata, image, transformation_matrix_path, key_added=None, image_key=None, overwrite=False)
Add an image to the SpatialData
object after alignment with the Xenium Explorer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
image
|
DataArray
|
A |
required |
transformation_matrix_path
|
str
|
Path to the |
required |
key_added
|
str | None
|
Optional name to add to the new image. If |
None
|
image_key
|
str
|
Optional name of the image on which it has been aligned. Required if multiple images in the |
None
|
overwrite
|
bool
|
Whether to overwrite the image, if already existing. |
False
|
Source code in sopa/io/explorer/images.py
sopa.io.explorer.add_explorer_selection(sdata, path, key_added='explorer_selection', shapes_key=None, image_key=None, pixel_size=0.2125)
After saving a selection on the Xenium Explorer, it will add all polygons inside sdata.shapes[shapes_key]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
path
|
str
|
The path to the |
required |
key_added
|
str
|
The name to provide to the selection as shapes |
'explorer_selection'
|
shapes_key
|
str | None
|
Deprecated. Use |
None
|
image_key
|
str | None
|
The original image name |
None
|
pixel_size
|
float
|
Number of microns in a pixel. It must be the same value as the one used in |
0.2125
|
Source code in sopa/io/explorer/utils.py
sopa.io.explorer.int_cell_id(explorer_cell_id)
Transforms an alphabetical cell id from the Xenium Explorer to an integer ID
E.g., int_cell_id('aaaachba-1') = 10000
Source code in sopa/io/explorer/utils.py
sopa.io.explorer.str_cell_id(cell_id)
Transforms an integer cell ID into an Xenium Explorer alphabetical cell id
E.g., str_cell_id(10000) = 'aaaachba-1'
Source code in sopa/io/explorer/utils.py
sopa.io.explorer.write_image(path, image, lazy=True, tile_width=TILE_SIZE, n_subscales=5, pixel_size=0.2125, ram_threshold_gb=4, is_dir=True)
Convert an image into a morphology.ome.tif
file that can be read by the Xenium Explorer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the Xenium Explorer directory where the image will be written |
required |
image
|
DataTree | DataArray | ndarray
|
Image of shape |
required |
lazy
|
bool
|
If |
True
|
tile_width
|
int
|
Xenium tile width (do not update). |
TILE_SIZE
|
n_subscales
|
int
|
Number of sub-scales in the pyramidal image. |
5
|
pixel_size
|
float
|
Xenium pixel size (do not update). |
0.2125
|
ram_threshold_gb
|
int | None
|
If an image (of any level of the pyramid) is below this threshold, it will be loaded in-memory. |
4
|
is_dir
|
bool
|
If |
True
|
Source code in sopa/io/explorer/images.py
sopa.io.explorer.save_column_csv(path, adata, key)
Save one column of the AnnData object as a CSV that can be open interactively in the explorer, under the "cell" panel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path where to write the CSV that will be open in the Xenium Explorer |
required |
adata
|
AnnData
|
An |
required |
key
|
str
|
Key of |
required |
Source code in sopa/io/explorer/table.py
Report
sopa.io.write_report(path, sdata)
Create a HTML report (or web report) after running Sopa.
Note
This report is automatically generated based on a custom python-to-html engine
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the |
required |
sdata
|
SpatialData
|
A |
required |