SPACEc: Tissue Extractor
This script takes a 3D tiff or qptiff fluorescent image containing multiple tissues, crops each tissue out of the larger image, and saves a tiff subimage for each tissue. It expects images of dimension (cycles, Ypixels, Xpixels). The script is based on image segmentation principles and code presented on the scikit-image site at this link (https://scikit-image.org/docs/dev/user_guide/tutorial_segmentation.html).
The steps of the script are:
The image is downsized by downscale_factor in each dimension.
A Gaussian filter with std. dev. equal to sigma is applied to the image to remove outliers.
Watershed segmentation is applied to segment the image into individual tissues.
(Optional) User can manually clean up tissue piece region assignments
A bounding box is generated around each region.
Each bounding box is used to crop the original full tiff stack.
The resulting cropped tiff stack of each region is saved as its own tiff.
# import spacec first
import spacec as sp
# import standard packages
import os
import warnings
import numpy as np
import scanpy as sc
import matplotlib
warnings.filterwarnings('ignore')
sc.settings.set_figure_params(dpi=80, facecolor='white')
# set the default color map to viridis, the below paramters can be chanaged
matplotlib.rcParams["image.cmap"] = 'viridis'
# Specify the path to the data
root_path = "/home/user/path/SPACEc/" # replace with your path
data_path = root_path + 'example_data/raw/' # where the data is stored
# where you want to store the output
output_dir = root_path + 'example_data/output/'
os.makedirs(output_dir, exist_ok=True)
# specify the image file for extraction
file = 'tonsil_tma.tif' # the file name of the image, can be qptiff or tiff
file_path = data_path + file # the path to the image
Downscale CODEX images
# downscale your image for easy segmentation computation
resized_im = sp.hf.downscale_tissue(file_path = file_path, # where qptiff or tiff is stored
downscale_factor = 64, # the downscale factor for the image
padding = 50, # the padding to add to the image
output_dir = output_dir # where to store the downscale image
)
Segment individual tissue pieces
tissueframe = sp.tl.label_tissue(resized_im, # the downsampled image
lower_cutoff = 0.2, #the lower cutoff of the image intensity for the tissue
upper_cutoff = 0.21 #the upper cutoff of the image intensity for the tissue
)
tissueframe.head()
| tissue | y | x | region1 | |
|---|---|---|---|---|
| 0 | 1 | 0 | 17 | 1 |
| 1 | 1 | 0 | 18 | 1 |
| 2 | 1 | 0 | 19 | 1 |
| 3 | 1 | 0 | 20 | 1 |
| 4 | 1 | 0 | 21 | 1 |
Rename tissue number (optional)
Extract individual labeled tissue
sp.tl.save_labelled_tissue(filepath=file_path,
tissueframe=tissueframe, # the data frame with pixel assignment to each tissue piece or region
output_dir = output_dir, # where to store the image
downscale_factor = 64, # the downscale factor for the image
region = 'region1',
padding = 50 # the padding to add to the image
)