From ffd1c50cc7134f021c7a1729e00478c9231981e2 Mon Sep 17 00:00:00 2001 From: DoronHav Date: Mon, 20 May 2024 12:09:31 -0400 Subject: [PATCH 1/2] Fix args to highly_variable_genes function in ENVI.py --- scenvi/ENVI.py | 20 ++++++++++++-------- scenvi/utils.py | 14 ++++++++++---- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/scenvi/ENVI.py b/scenvi/ENVI.py index 1e8ed97..e5f6c77 100644 --- a/scenvi/ENVI.py +++ b/scenvi/ENVI.py @@ -77,18 +77,22 @@ def __init__( stable_eps=1e-6, ): - self.spatial_data = spatial_data + self.spatial_data = spatial_data[:, np.intersect1d(spatial_data.var_names, sc_data.var_names)] self.sc_data = sc_data - if "highly_variable" not in sc_data.var.columns: - - sc_data.layers["log"] = np.log(sc_data.X + 1) - sc.pp.highly_variable_genes( - sc_data, layer="log", n_top_genes=min(num_HVG, sc_data.shape[-1]) - ) + if "highly_variable" not in self.sc_data.var.columns: + if 'log' in self.sc_data.layers.keys(): + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g, layer="log") + elif('log1p' in self.sc_data.layers.keys()): + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g, layer="log1p") + elif(self.sc_data.X.min() < 0): + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g) + else: + sc_data.layers["log"] = np.log(self.sc_data.X + 1) + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g, layer="log") sc_genes_keep = np.union1d( - sc_data.var_names[sc_data.var.highly_variable], self.spatial_data.var_names + self.sc_data.var_names[self.sc_data.var.highly_variable], self.spatial_data.var_names ) if len(sc_genes) > 0: sc_genes_keep = np.union1d(sc_genes_keep, sc_genes) diff --git a/scenvi/utils.py b/scenvi/utils.py index c77c764..0d1fd93 100644 --- a/scenvi/utils.py +++ b/scenvi/utils.py @@ -9,7 +9,7 @@ from flax import struct from flax.training import train_state from jax import random - +import scipy.sparse class FeedForward(nn.Module): """ @@ -254,7 +254,6 @@ def niche_cell_type( ) return cell_type_niche - def compute_covet( spatial_data, k=8, g=64, genes=[], spatial_key="spatial", batch_key=-1 ): @@ -277,8 +276,15 @@ def compute_covet( CovGenes = spatial_data.var_names else: if "highly_variable" not in spatial_data.var.columns: - spatial_data.layers["log"] = np.log(spatial_data.X + 1) - sc.pp.highly_variable_genes(spatial_data, n_top_genes=g, layer="log") + if 'log' in spatial_data.layers.keys(): + sc.pp.highly_variable_genes(spatial_data, n_top_genes=g, layer="log") + elif('log1p' in spatial_data.layers.keys()): + sc.pp.highly_variable_genes(spatial_data, n_top_genes=g, layer="log1p") + elif(spatial_data.X.min() < 0): + sc.pp.highly_variable_genes(spatial_data, n_top_genes=g) + else: + spatial_data.layers["log"] = np.log(spatial_data.X + 1) + sc.pp.highly_variable_genes(spatial_data, n_top_genes=g, layer="log") CovGenes = np.asarray(spatial_data.var_names[spatial_data.var.highly_variable]) if len(genes) > 0: From 39234852ba241ae5adcb24046ca99adf1e7f6d41 Mon Sep 17 00:00:00 2001 From: DoronHav Date: Mon, 20 May 2024 12:16:18 -0400 Subject: [PATCH 2/2] Fix args to highly_variable_genes function in ENVI.py --- pyproject.toml | 2 +- scenvi/ENVI.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4eedd51..a1e9034 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "scenvi" -version = "0.3.3" +version = "0.3.4" description = "Integration of scRNA-seq and spatial transcriptomics data" authors = ["Doron Haviv"] license = "MIT" diff --git a/scenvi/ENVI.py b/scenvi/ENVI.py index e5f6c77..1ada9cb 100644 --- a/scenvi/ENVI.py +++ b/scenvi/ENVI.py @@ -82,14 +82,14 @@ def __init__( if "highly_variable" not in self.sc_data.var.columns: if 'log' in self.sc_data.layers.keys(): - sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g, layer="log") + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=num_HVG, layer="log") elif('log1p' in self.sc_data.layers.keys()): - sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g, layer="log1p") + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=num_HVG, layer="log1p") elif(self.sc_data.X.min() < 0): - sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g) + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=num_HVG) else: sc_data.layers["log"] = np.log(self.sc_data.X + 1) - sc.pp.highly_variable_genes(self.sc_data, n_top_genes=g, layer="log") + sc.pp.highly_variable_genes(self.sc_data, n_top_genes=num_HVG, layer="log") sc_genes_keep = np.union1d( self.sc_data.var_names[self.sc_data.var.highly_variable], self.spatial_data.var_names