From 818a3288fed62da71768f2e2531fc681cbd05fb2 Mon Sep 17 00:00:00 2001 From: Zhenbin24 <809120943@qq.com> Date: Tue, 28 Nov 2023 15:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90Fix=E3=80=91=20fix=20lasso=20area?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 5 ++- .../interact_plot/interactive_scatter.py | 43 +++++++++++-------- stereo/plots/interact_plot/poly_selection.py | 41 +++++++++++------- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/requirements.txt b/requirements.txt index 542bd7d3..1d492dd7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ pandas==1.5.3 scipy==1.10.1 seaborn~=0.11.1 h5py>=3.7.0 -gefpy>=0.7.6 +gefpy>=0.7.10 # setuptools>=41.0.0,<60.0.0 setuptools>=61.0.0 #todo python3.8/site-packages/umap/distances.py:1053: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. @@ -81,4 +81,5 @@ lxml>=4.8.0 fastcluster>=1.2.6 pycirclize>=0.5.0 plotly>=5.15.0 -cusingler \ No newline at end of file +cusingler +cell-bin==1.2.8.1 \ No newline at end of file diff --git a/stereo/plots/interact_plot/interactive_scatter.py b/stereo/plots/interact_plot/interactive_scatter.py index 32611daf..4fa33219 100644 --- a/stereo/plots/interact_plot/interactive_scatter.py +++ b/stereo/plots/interact_plot/interactive_scatter.py @@ -17,7 +17,6 @@ from stereo.log_manager import logger from stereo.stereo_config import stereo_conf -from stereo.tools.boundary import ConcaveHull from stereo.tools.tools import make_dirs link = link_selections.instance() @@ -34,7 +33,8 @@ class InteractiveScatter: def __init__( self, data, - width: Optional[int] = 500, height: Optional[int] = 500, + width: Optional[int] = 500, + height: Optional[int] = 500, bgcolor='#2F2F4F', ): self.data = data @@ -70,14 +70,13 @@ def __init__( ) self.download.on_click(self._download_callback) self.figure = self.interact_scatter() - self.list_poly_selection_exp_coors = [] def generate_selected_expr_matrix(self, selected_pos, drop=False): if selected_pos is not None: selected_index = self.scatter_df.index.drop(selected_pos) if drop else selected_pos data_temp = copy.deepcopy(self.data) - self.selected_exp_data = data_temp.sub_by_index( - cell_index=selected_index) + self.selected_exp_data = data_temp.sub_by_index(cell_index=selected_index) + self.selected_exp_data = self.selected_exp_data.tl.filter_genes(mean_umi_gt=0) else: self.selected_exp_data = None @@ -96,37 +95,43 @@ def get_selected_boundary_coors(self) -> list: Returns: """ - if not self.selected_exp_data: + if not self.selected_exp_data or self.selected_exp_data.shape == self.data.shape: raise Exception('Please select the data area in the picture first!') selected_pos = hv.Dataset(self.scatter_df).select(link.selection_expr).data.index self.generate_selected_expr_matrix(selected_pos, self.drop_checkbox.value) - exp_matrix_data = self.selected_exp_data.position.tolist() - init = ConcaveHull(exp_matrix_data, 3) - concave_hull = init.calculate().tolist() - concave_hull = [int(i) for k in concave_hull for i in k] - self.list_poly_selection_exp_coors.append(concave_hull) - return self.list_poly_selection_exp_coors - - def export_high_res_area(self, origin_file_path: str, output_path: str, cgef: bool = False) -> str: + list_poly_selection_exp_coors = list() + data_set = set() + for label in self.selected_exp_data.position.tolist(): + x_y = ','.join([str(label[0]), str(label[1])]) + if x_y in data_set: + continue + data_set.add(x_y) + list_poly_selection_exp_coors.append(label) + return list_poly_selection_exp_coors + + def export_high_res_area(self, origin_file_path: str, output_path: str) -> str: """ export selected area in high resolution Args: origin_file_path: origin file path which you read output_path: location the high res file storaged - cgef: bool, default False, set True if read in cellbin Returns: output_path """ coors = self.get_selected_boundary_coors() + print('coors length: %s' % len(coors)) + if not coors: + raise Exception('Please select the data area in the picture first!') + make_dirs(output_path) from gefpy.cgef_adjust_cy import CgefAdjust cg = CgefAdjust() - make_dirs(output_path) - if cgef: - cg.create_Region_Cgef(origin_file_path, output_path, coors) + if self.data.bin_type == 'cell_bins': + cg.generate_cgef_by_coordinate(origin_file_path, output_path, coors) else: - cg.create_Region_Bgef(origin_file_path, output_path, coors) + cg.generate_bgef_by_coordinate(origin_file_path, output_path, coors, self.data.bin_size) + return output_path def interact_scatter(self): diff --git a/stereo/plots/interact_plot/poly_selection.py b/stereo/plots/interact_plot/poly_selection.py index a7949108..b2855106 100644 --- a/stereo/plots/interact_plot/poly_selection.py +++ b/stereo/plots/interact_plot/poly_selection.py @@ -11,7 +11,6 @@ from holoviews.util.transform import dim from stereo.stereo_config import stereo_conf -from stereo.tools.boundary import ConcaveHull from stereo.tools.tools import make_dirs pn.extension() @@ -59,7 +58,6 @@ def __init__( width=150 ) self.list_poly_selection_coors = [] - self.list_poly_selection_exp_coors = [] self.download.on_click(self._download_callback) self.selected_exp_data = None self.figure = self.show() @@ -96,7 +94,12 @@ def get_selected_boundary_coors(self) -> list: Returns: """ + if not self.selected_exp_data or self.selected_exp_data.shape == self.data.shape: + raise Exception('Please select the data area in the picture first!') + print("processing selected {} area".format(len(self.list_poly_selection_coors))) + list_poly_selection_exp_coors = [] + data_set = set() for each_polygon in self.list_poly_selection_coors: if len(each_polygon): selected_point = each_polygon[0] @@ -110,33 +113,40 @@ def get_selected_boundary_coors(self) -> list: selected_pos = hv.Dataset(self.scatter_df).select(selection_expr).data.index data_temp = copy.deepcopy(self.data) self.selected_exp_data = data_temp.sub_by_index(cell_index=selected_pos) + self.selected_exp_data = self.selected_exp_data.tl.filter_genes(mean_umi_gt=0) exp_matrix_data = self.selected_exp_data.position.tolist() - init = ConcaveHull(exp_matrix_data, 3) - concave_hull = init.calculate().tolist() - concave_hull = [int(i) for k in concave_hull for i in k] - self.list_poly_selection_exp_coors.append(concave_hull) - return self.list_poly_selection_exp_coors - - def export_high_res_area(self, origin_file_path: str, output_path: str, cgef: bool = False) -> str: + if self.selected_exp_data.shape[0] * self.selected_exp_data.shape[1] == 0: + return [] + for label in exp_matrix_data: + x_y = ','.join([str(label[0]), str(label[1])]) + if x_y in data_set: + continue + data_set.add(x_y) + list_poly_selection_exp_coors.append(label) + return list_poly_selection_exp_coors + + def export_high_res_area(self, origin_file_path: str, output_path: str) -> str: """ export selected area in high resolution Args: origin_file_path: origin file path which you read output_path: location the high res file storaged - cgef: bool, default False, set True if read in cellbin Returns: output_path """ coors = self.get_selected_boundary_coors() + print('coors length: %s' % len(coors)) + if not coors: + raise Exception('Please select the data area in the picture first!') + make_dirs(output_path) from gefpy.cgef_adjust_cy import CgefAdjust cg = CgefAdjust() - make_dirs(output_path) - - if cgef: - cg.create_Region_Cgef(origin_file_path, output_path, coors) + if self.data.bin_type == 'cell_bins': + cg.generate_cgef_by_coordinate(origin_file_path, output_path, coors) else: - cg.create_Region_Bgef(origin_file_path, output_path, coors) + cg.generate_bgef_by_coordinate(origin_file_path, output_path, coors, self.data.bin_size) + return output_path def generate_selected_expr_matrix(self, selected_pos, drop=False): @@ -145,6 +155,7 @@ def generate_selected_expr_matrix(self, selected_pos, drop=False): selected_index = self.scatter_df.index.drop(selected_pos) if drop else selected_pos data_temp = copy.deepcopy(self.data) self.selected_exp_data = data_temp.sub_by_index(cell_index=selected_index) + self.selected_exp_data = self.selected_exp_data.tl.filter_genes(mean_umi_gt=0) else: self.selected_exp_data = None