High Resolution Matrix Export#

In some cases, we need to export part of selected high resolution area in expression matrix while we read them in low resolution.

Please download our example data.

[1]:
%matplotlib inline

import warnings
warnings.filterwarnings('ignore')
import stereo as st

# read GEF file in CGEF
# data_path = '../data/FP200000443TL_E2.adjusted.cellbin.gef'
# data = st.io.read_gef(data_path, bin_type='cell_bins')

# read GEF file in BGEF
data_path = '../data/SS200000135TL_D1.tissue.gef'
data = st.io.read_gef(data_path, bin_type='bins', bin_size=20)

data.tl.cal_qc()
[2024-01-16 15:44:44][Stereo][92142][MainThread][140530959435584][reader][1070][INFO]: read_gef begin ...
path:../data/SS200000135TL_D1.tissue.gef bin:20
               generateBinInfo - 26.210000 cpu sec
[2024-01-16 15:45:02][Stereo][92142][MainThread][140530959435584][reader][1241][INFO]: the matrix has 223053 cells, and 24302 genes.
[2024-01-16 15:45:02][Stereo][92142][MainThread][140530959435584][reader][1242][INFO]: read_gef end.
[2024-01-16 15:45:02][Stereo][92142][MainThread][140530959435584][st_pipeline][41][INFO]: start to run cal_qc...
[2024-01-16 15:45:03][Stereo][92142][MainThread][140530959435584][st_pipeline][44][INFO]: cal_qc end, consume time 0.5740s.

We have two selection mode, one is polygon selection and another one is box selection.

An example of polygon selection:

polygon_selection_1.gif

An example of box selection:

box_selection_1.gif

Area Selection#

Setting ploy_select to True to enter the polygon selection mode while setting to False to box selection mode.

In polygon selection mode, you need to click twice to start and end the selection, afterwards, for each selected area, you have to click it and click the add button so that all the selected areas are added into the queue to be exported.

In box selection mode, you only need to click the export button once after completing all selection, all the selected areas will be added into the queue.

[2]:
# plot and show rendering
# polygon selection when poly_select is True
# box selection when poly_select is False

ins = data.plt.interact_spatial_scatter(width=500, height=500, poly_select=True)
ins.show()
[2]:

Exporting as a GEF file.#

[4]:
ins.export_high_res_area(
    data_path,
    './SS200000135TL_D1.tissue.high_res.gef',
    # drop=True
)
processing selected 2 area
minx:0 miny:0 maxx:26458 maxy:26458
create bgef file: ./SS200000135TL_D1.tissue.high_res.gef
bin 1 matrix: min_x=0 len_x=26459 min_y=0 len_y=26459 matrix_len=700078681
createRegionGef - elapsed time: 14373.89696 ms
[4]:
'./SS200000135TL_D1.tissue.high_res.gef'

Exporting a sub-image#

We can also exporting the sub-image conrresponding to the selected areas.

[5]:
ins.export_roi_image(
    origin_file_path='../data/SS200000135TL_D1_regist.tif',
    output_path='./SS200000135TL_D1_regist_selected.tif',
    # drop=True
)
processing selected 2 area

Note

In polygon selection mode, both export_high_res_area and export_roi_image, you can also set the parameter drop to True to exclude all selected areas.

In box selection mode, whether to drop selected areas is determined by the options beside the export button.

Getting a sub-object#

In polygon selection mode, after clicking a selected area, you can also use the export button to get a new sub-object of StereoExpData conrresponding to the clicked area, note that only one selected area can be exported as sub-object.

In box selection mode, after clicking the export button, all selected areas will be exported as a sub-object.

You can get the sub-object by property ins.selected_exp_data.

[6]:
selected_exp_data = ins.selected_exp_data
selected_exp_data
[6]:
StereoExpData object with n_cells X n_genes = 15831 X 19127
bin_type: bins
bin_size: 20
offset_x = 0
offset_y = 0
cells: ['cell_name', 'total_counts', 'n_genes_by_counts', 'pct_counts_mt']
genes: ['gene_name', 'n_cells', 'n_counts', 'mean_umi']
result: []

If you want to get a sub-object containing all selected areas in polygon selection mode, you can run the code as below after adding all selected area to queue by clicking add button, similar to export_high_res_area and export_roi_image, the parameter drop determines whether to drop the selected areas.

[11]:
selected_exp_data_all = ins.get_selected_areas(drop=False)
selected_exp_data_all
processing selected 2 area
[11]:
StereoExpData object with n_cells X n_genes = 30888 X 20168
bin_type: bins
bin_size: 20
offset_x = 0
offset_y = 0
cells: ['cell_name', 'total_counts', 'n_genes_by_counts', 'pct_counts_mt']
genes: ['gene_name', 'n_cells', 'n_counts', 'mean_umi']
result: []

Reading exported GEF file#

[9]:
# read exported BGEF file
path_high_res = './SS200000135TL_D1.tissue.high_res.gef'
data_high_res = st.io.read_gef(path_high_res, bin_type='bins', bin_size=100)

# read exported CGEF file
# path_high_res = './data/FP200000443TL_E2.adjusted.cellbin.high_res.gef'
# data_high_res = st.io.read_gef(path_high_res, bin_type='cell_bins')

data_high_res.tl.cal_qc()
[2024-01-16 15:49:58][Stereo][92142][MainThread][140530959435584][reader][1070][INFO]: read_gef begin ...
[2024-01-16 15:50:00][Stereo][92142][MainThread][140530959435584][reader][1241][INFO]: the matrix has 1348 cells, and 20168 genes.
[2024-01-16 15:50:00][Stereo][92142][MainThread][140530959435584][reader][1242][INFO]: read_gef end.
[2024-01-16 15:50:00][Stereo][92142][MainThread][140530959435584][st_pipeline][41][INFO]: start to run cal_qc...
[2024-01-16 15:50:00][Stereo][92142][MainThread][140530959435584][st_pipeline][44][INFO]: cal_qc end, consume time 0.0549s.
path:./SS200000135TL_D1.tissue.high_res.gef bin:100
               generateBinInfo - 2.450000 cpu sec

Check the exported area.

[10]:
ins_high_res = data_high_res.plt.interact_spatial_scatter(width=500, height=500, poly_select=True)
ins_high_res.show()
[10]: