From 2c5909d81de8cbb69011ea79dfe99d461e925e7b Mon Sep 17 00:00:00 2001 From: Zhonghan Date: Mon, 2 Dec 2024 17:26:56 +0800 Subject: [PATCH] fixs: details are as follows: 1. if matrix input is gem, transfer gem to gef 2. single cell demo refer to StereoSeq_ssDNA_param.json --- .../StereoSeq_singlecell_dapi_param.json | 46 +++++++++++++ cellbin2/matrix/matrix.py | 17 +++++ cellbin2/modules/extract/image_extract.py | 66 ++++++++++--------- cellbin2/modules/extract/matrix_extract.py | 8 ++- 4 files changed, 105 insertions(+), 32 deletions(-) create mode 100644 cellbin2/config/StereoSeq_singlecell_dapi_param.json diff --git a/cellbin2/config/StereoSeq_singlecell_dapi_param.json b/cellbin2/config/StereoSeq_singlecell_dapi_param.json new file mode 100644 index 0000000..2d7ad1f --- /dev/null +++ b/cellbin2/config/StereoSeq_singlecell_dapi_param.json @@ -0,0 +1,46 @@ +{ + "image_process": { + "0": { + "file_path": "/media/Data/dzh/data/cellbin2/demo_data/Y00788P9/Y00788P9_aligned_image.tif", + "tech_type": "ssDNA", + "chip_detect": false, + "quality_control": false, + "registration": { + "fixed_image": 1, + "trackline": true, + "reuse": -1 + }, + "tissue_segmentation": false, + "cell_segmentation": true, + "correct_r": 10, + "channel_align": -1 + }, + "1": { + "file_path": "/media/Data/dzh/data/cellbin2/demo_data/Y00788P9/Y00788P9.gem.gz", + "tech_type": "Transcriptomics", + "chip_detect": false, + "quality_control": false, + "registration": { + "fixed_image": -1, + "trackline": true, + "reuse": -1 + }, + "tissue_segmentation": true, + "cell_segmentation": false, + "correct_r": 0, + "channel_align": -1 + } + }, + "molecular_classify": { + "0": { + "exp_matrix": 1, + "cell_mask": [0], + "extra_method": "" + } + }, + "analysis": { + "cluster": true, + "annotation": false, + "report": true + } +} \ No newline at end of file diff --git a/cellbin2/matrix/matrix.py b/cellbin2/matrix/matrix.py index 2cb6fd8..aa92909 100644 --- a/cellbin2/matrix/matrix.py +++ b/cellbin2/matrix/matrix.py @@ -212,12 +212,29 @@ def adjust_mask_shape(gef_path, mask_path): return new_path +def gem_to_gef(gem_path, gef_path): + from gefpy.bgef_writer_cy import generate_bgef + generate_bgef(input_file=gem_path, + bgef_file=gef_path, + stromics="Transcriptomics", + n_thread=8, + bin_sizes=[1], + ) + + def save_cell_bin_data(src_path: str, dst_path: str, cell_mask: str): """ 获取:单细胞数据(mask可来自配准图像 or 矩阵自身 ) """ src_path = str(src_path) dst_path = str(dst_path) cell_mask = str(cell_mask) from gefpy import cgef_writer_cy + if src_path.endswith(".gem.gz"): + gef_path = os.path.join(os.path.dirname(dst_path), os.path.basename(src_path).replace(".gem.gz", ".raw.gef")) + if os.path.exists(gef_path): + src_path = gef_path + else: + gem_to_gef(src_path, gef_path) + src_path = gef_path if src_path.endswith(".gef"): cell_mask = adjust_mask_shape(gef_path=src_path, mask_path=cell_mask) cgef_writer_cy.generate_cgef(dst_path, src_path, cell_mask, [256, 256]) diff --git a/cellbin2/modules/extract/image_extract.py b/cellbin2/modules/extract/image_extract.py index 889af8f..87842c1 100644 --- a/cellbin2/modules/extract/image_extract.py +++ b/cellbin2/modules/extract/image_extract.py @@ -154,41 +154,47 @@ def extract4transform( trans_chip_box_info.ScaleX, trans_chip_box_info.ScaleY = 1.0, 1.0 trans_chip_box_info.Rotation = 0.0 self._channel_image.Stitch.TransformChipBBox.update(trans_chip_box_info) - - if self._image_file.tissue_segmentation and not self._naming.transform_tissue_mask_raw.exists(): - tissue_mask = self._tissue_segmentation( - image_path=self._naming.transformed_image, - res_path=self._naming.transform_tissue_mask_raw - ) - if self._image_file.cell_segmentation and not self._naming.transform_cell_mask_raw.exists(): - cell_mask = self._cell_segmentation( - image_path=self._naming.transformed_image, - res_path=self._naming.transform_cell_mask_raw - ) + final_tissue_mask = None + final_cell_mask = None + tissue_mask = None + cell_mask = None if self._image_file.tissue_segmentation: - if 'tissue_mask' not in locals(): + if not self._naming.transform_tissue_mask_raw.exists(): + tissue_mask = self._tissue_segmentation( + image_path=self._naming.transformed_image, + res_path=self._naming.transform_tissue_mask_raw + ) + else: tissue_mask_p = self._naming.transform_tissue_mask_raw tissue_mask = cbimread(tissue_mask_p, only_np=True) - if self._image_file.cell_segmentation: - if 'cell_mask' not in locals(): - cell_mask_p = self._naming.transform_cell_mask_raw - cell_mask = cbimread(cell_mask_p, only_np=True) - input_data = MaskManagerInfo( - tissue_mask=tissue_mask, - cell_mask=cell_mask, - chip_box=self._channel_image.Stitch.TransformChipBBox.get(), - method=1, - stain_type=self._image_file.tech - ) - btcm = BestTissueCellMask.get_best_tissue_cell_mask(input_data=input_data) - final_tissue_mask = btcm.best_tissue_mask - final_cell_mask = btcm.best_cell_mask - cbimwrite( - output_path=self._naming.transform_cell_mask, - files=final_cell_mask + final_tissue_mask = tissue_mask + if self._image_file.cell_segmentation: + if not self._naming.transform_cell_mask_raw.exists(): + cell_mask = self._cell_segmentation( + image_path=self._naming.transformed_image, + res_path=self._naming.transform_cell_mask_raw ) else: - final_tissue_mask = tissue_mask + cell_mask_p = self._naming.transform_cell_mask_raw + cell_mask = cbimread(cell_mask_p, only_np=True) + final_cell_mask = cell_mask + if tissue_mask is not None and cell_mask is not None: + input_data = MaskManagerInfo( + tissue_mask=tissue_mask, + cell_mask=cell_mask, + chip_box=self._channel_image.Stitch.TransformChipBBox.get(), + method=1, + stain_type=self._image_file.tech + ) + btcm = BestTissueCellMask.get_best_tissue_cell_mask(input_data=input_data) + final_tissue_mask = btcm.best_tissue_mask + final_cell_mask = btcm.best_cell_mask + if final_cell_mask is not None: + cbimwrite( + output_path=self._naming.transform_cell_mask, + files=final_cell_mask + ) + if final_tissue_mask is not None: cbimwrite( output_path=self._naming.transform_tissue_mask, files=final_tissue_mask diff --git a/cellbin2/modules/extract/matrix_extract.py b/cellbin2/modules/extract/matrix_extract.py index 9c02f53..0a61a4b 100644 --- a/cellbin2/modules/extract/matrix_extract.py +++ b/cellbin2/modules/extract/matrix_extract.py @@ -41,20 +41,24 @@ def extract4matrix(self, m_naming: naming.DumpPipelineFileNaming): cell_mask_path = m_naming.final_nuclear_mask tissue_mask_path = m_naming.final_tissue_mask cell_correct_mask_path = m_naming.final_cell_mask + c_inp = None if Path(tissue_mask_path).exists(): save_tissue_bin_data( self._image_file.file_path, str(self._naming.tissue_bin_matrix), tissue_mask_path, ) + c_inp = self._naming.tissue_bin_matrix + if c_inp is None: + c_inp = self._image_file.file_path if Path(cell_mask_path).exists(): save_cell_bin_data( - self._naming.tissue_bin_matrix, + c_inp, str(self._naming.cell_bin_matrix), cell_mask_path) if Path(cell_correct_mask_path).exists(): save_cell_bin_data( - self._naming.tissue_bin_matrix, + c_inp, str(self._naming.cell_correct_bin_matrix), cell_correct_mask_path )