sparrow.pl.sanity_plot_transcripts_matrix#
- sparrow.pl.sanity_plot_transcripts_matrix(sdata, img_layer=None, labels_layer=None, points_layer='transcripts', shapes_layer=None, channel=None, z_slice=None, plot_cell_number=False, n_sample=None, name_x='x', name_y='y', name_z='z', name_gene_column='gene', gene=None, crd=None, to_coordinate_system='global', figsize=None, output=None)#
Produce a sanity plot to visualize spatial transcriptomics data on top of an image.
This function plots a spatial image (e.g. microscopy or histology image) and overlays transcripts and optionally cell boundaries to visually inspect the spatial alignment of the data. This can be particularly useful to check data registration and alignment in spatial transcriptomics.
- Parameters:
sdata (
SpatialData
) – The SpatialData object containing the spatial image and transcripts data.img_layer (
Optional
[str
] (default:None
)) – The layer of the SpatialData object to be plotted. Defaults to the last layer if not provided.labels_layer (
Optional
[str
] (default:None
)) – Labels layer to be plotted.points_layer (
str
(default:'transcripts'
)) – The points layer in the SpatialData object representing transcripts.shapes_layer (
Optional
[str
] (default:None
)) – The layer in the SpatialData object representing cell boundaries. If None, no cell boundaries are plotted.channel (
Union
[int
,str
,None
] (default:None
)) – Channel to display from the img_layer. If none provided, or if provided channel could not be found, first channel is plot.z_slice (
Optional
[float
] (default:None
)) – The z_slice to visualize in case of 3D (c,z,y,x) image/polygons. For transcripts, if the z_slice is specified, the transcripts at index corresponding to the z_slice in the image layer will be plotted. If no z_slice is specified andimg_layer
orlabels_layer
is 3D, a max projection along the z-axis will be performed. If no z_slice is specified andshapes_layer
is 3D, all polygons in all z-stacks will be plotted. If no z-slice is specified andpoints_layer
is 3D, all transcripts in all z-stacks will be plotted.plot_cell_number (
bool
(default:False
)) – Whether to annotate cells with their numbers on the plot.n_sample (
Optional
[int
] (default:None
)) – The number of transcripts to sample for plotting. Useful for large datasets. Ignored ifgene
is specified.name_x (
str
(default:'x'
)) – Column name in the points_layer representing x-coordinates of transcripts.name_y (
str
(default:'y'
)) – Column name in the points_layer representing y-coordinates of transcripts.name_z (
str
(default:'z'
)) – Column name in the points_layer representing z-coordinates of transcripts.name_gene_column (
str
(default:'gene'
)) – Column name in the points_layer representing gene information.gene (
Optional
[str
] (default:None
)) – Specific gene to filter and plot. If None, all genes are plotted.crd (
Optional
[tuple
[int
,int
,int
,int
]] (default:None
)) – Coordinates to define a rectangular region for plotting as (xmin, xmax, ymin, ymax). If None, the entire image boundary is used.to_coordinate_system (
str
(default:'global'
)) – Coordinate system to plot.figsize (
Optional
[tuple
[int
,int
]] (default:None
)) – Size of the figure for plotting.output (
Union
[str
,Path
,None
] (default:None
)) – Filepath to save the generated plot. If not provided, the plot will be displayed using plt.show().
- Return type:
None
- Returns:
: Either displays the plot or saves it to the specified output location.
- Raises:
ValueError – If both
img_layer
andlabels_layer
are specified.ValueError – If
img_layer
orlabels_layer
is specified, and they are not found insdata.images
respectivelysdata.labels
.AttributeError – If
sdata
does not contain apoints_layer
.Warning – If provided coordinates (crd) and image boundary do not have overlap.
Warning – If provided shapes_layer is not present in the SpatialData object.
Notes
If a specific gene is provided, only transcripts corresponding to that gene are plotted.
If
plot_cell_number
is set to True, cells are annotated with their numbers.If the
output
parameter is provided, the plot is saved to the specified location, otherwise, it’s displayed.