Tutorial to generate checkerbord cell simulation with an artbitray expression
[13]:
import numpy as np
from matplotlib import pyplot as plt
from scipy import ndimage as ndi
import skimage
from sklearn.utils.random import sample_without_replacement
from scipy.sparse import csr_array
[14]:
import sys
import simtissue
from simtissue.arbitrary_shape import checkerboard_mask, generate_ellipse, add_sphere_nuclei
from simtissue.plot import plot_contour
from simtissue.control_expression import create_fix_profile, simulate_arbritrary_expression
from simtissue.utils import get_dict_coord_map
1) Generate checkerboard
The first step is to generate a cells as checkerboard of the needed size
[15]:
mask_cyto = checkerboard_mask(shape = [1, 1000, 1100],
cube_square_size = 100)
plot_contour(mask_cyto = mask_cyto, figsize = (5,5))
list index out of range
no contour for cell 110.0
[15]:
(<Figure size 500x500 with 1 Axes>, <Axes: >)
2) Add nuclei in the checker board
The number and position of the cell without nuclei can be parametrize with argument list_nuclei in add_sphere_nuclei
[16]:
## leave 20% of the cells without cytoplasm
list_nuclei_unique = np.unique(mask_cyto)
index = sample_without_replacement(len(list_nuclei_unique), int(0.80 * len(list_nuclei_unique)))
list_nuclei = list_nuclei_unique[index]
mask_nuclei = add_sphere_nuclei(mask_cyto = mask_cyto,
cube_square_size = 100,
nuclei_radius = 25,
list_nuclei = list_nuclei)
plot_contour(mask_cyto = mask_cyto, mask_nuclei = mask_nuclei, figsize = (5,5))
list index out of range
no contour for cell 110.0
[16]:
(<Figure size 500x500 with 1 Axes>, <Axes: >)
3) Simulation of arbitrary cell expression
-we simulate a cell-expression matrix containing the expression profile we want to map in the checkerboard
[17]:
### generate_anndata
## RNA profile of each cell type
genes_list_to_simulate = ['A', 'B']
dict_profile = {'typeA': [50, 0],
'typeB': [0, 200]
}
## cell type of each cell index
cell_index_typeA = [i for i in range(1, 111) if i % 2 == 0]
cell_index_typeB = [i for i in range(1, 111) if i % 2 == 1]
cell_type_distribution={'typeA': cell_index_typeA,
'typeB': cell_index_typeB,}
image_name = "image0"
## get the anndata with simulate_arbritrary_expression()
anndata = simulate_arbritrary_expression(dict_profile,
cell_type_distribution,
mask_cyto = mask_cyto,
genes_list_to_simulate = ['A', 'B'],
image_name = "image0"
)
anndata
[17]:
AnnData object with n_obs × n_vars = 110 × 2
obs: 'image_name', 'genes', 'coordinate', 'cell_index'
var: 'features'
Plot of final simulation
[18]:
plot_contour(mask_cyto = mask_cyto,
mask_nuclei = mask_nuclei,
figsize = (15,15),
anndata = anndata,
dico_color = {"A" : "r", "B": "b"},
spot_size = 3)
list index out of range
no contour for cell 110.0
[18]:
(<Figure size 1500x1500 with 1 Axes>, <Axes: >)
Save and load simulation
[19]:
import pickle
from pathlib import Path
filename = "simulated_image.h5ad"
with open(filename, 'wb') as handle:
pickle.dump(anndata, handle, -1)
[20]:
file_pi2 = open(filename, 'rb')
anndata = pickle.load(file_pi2)
anndata
[20]:
AnnData object with n_obs × n_vars = 110 × 2
obs: 'image_name', 'genes', 'coordinate', 'cell_index'
var: 'features'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/home/tom/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3875, in atexit_operations
self._atexit_once()
File "/home/tom/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3854, in _atexit_once
self.reset(new_session=False)
File "/home/tom/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 1373, in reset
self.history_manager.reset(new_session)
File "/home/tom/anaconda3/lib/python3.8/site-packages/IPython/core/history.py", line 597, in reset
self.dir_hist[:] = [Path.cwd()]
File "/home/tom/anaconda3/lib/python3.8/pathlib.py", line 1095, in cwd
return cls(os.getcwd())
FileNotFoundError: [Errno 2] No such file or directory
[ ]: