Skip to content

Commit

Permalink
【Fix】 fix lasso area
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhenbin24 committed Nov 28, 2023
1 parent ca87632 commit 818a328
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -81,4 +81,5 @@ lxml>=4.8.0
fastcluster>=1.2.6
pycirclize>=0.5.0
plotly>=5.15.0
cusingler
cusingler
cell-bin==1.2.8.1
43 changes: 24 additions & 19 deletions stereo/plots/interact_plot/interactive_scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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):
Expand Down
41 changes: 26 additions & 15 deletions stereo/plots/interact_plot/poly_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand All @@ -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):
Expand All @@ -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

Expand Down

0 comments on commit 818a328

Please sign in to comment.