diff --git a/spateo/svg/utils.py b/spateo/svg/utils.py index aad86858..9c90eb94 100644 --- a/spateo/svg/utils.py +++ b/spateo/svg/utils.py @@ -139,7 +139,9 @@ def add_pos_ratio_to_adata(adata: AnnData, layer: str = None, var_name: str = "r else np.sum(adata.layers[layer] > 0, axis=0) ) else: - adata.var[var_name] = np.sum(adata.X.toarray() > 0, axis=0) if issparse(adata.X) else np.sum(adata.X > 0, axis=0) + adata.var[var_name] = ( + np.sum(adata.X.toarray() > 0, axis=0) if issparse(adata.X) else np.sum(adata.X > 0, axis=0) + ) adata.var[var_name] = adata.var[var_name] / adata.n_obs diff --git a/spateo/tools/CCI_effects_modeling/MuSIC.py b/spateo/tools/CCI_effects_modeling/MuSIC.py index 4014f13b..fb74e169 100644 --- a/spateo/tools/CCI_effects_modeling/MuSIC.py +++ b/spateo/tools/CCI_effects_modeling/MuSIC.py @@ -2,6 +2,7 @@ Modeling cell-cell communication using a regression model that is considerate of the spatial heterogeneity of (and thus the context-dependency of the relationships of) the response variable. """ + import argparse import itertools import json @@ -1699,13 +1700,11 @@ def define_sig_inputs(self, adata: Optional[anndata.AnnData] = None, recompute: threshold = ( 0.67 if len(receptor_cols) == 2 - else 0.5 - if len(receptor_cols) == 3 - else 0.4 - if len(receptor_cols) == 4 - else 0.33 - if len(receptor_cols) >= 5 - else 1 + else ( + 0.5 + if len(receptor_cols) == 3 + else 0.4 if len(receptor_cols) == 4 else 0.33 if len(receptor_cols) >= 5 else 1 + ) ) # If overlap is greater than threshold, combine columns if len(receptor_cols) > 1 and overlap > threshold: @@ -1746,13 +1745,11 @@ def define_sig_inputs(self, adata: Optional[anndata.AnnData] = None, recompute: threshold = ( 0.67 if len(combined_cols) == 2 - else 0.5 - if len(combined_cols) == 3 - else 0.4 - if len(combined_cols) == 4 - else 0.33 - if len(combined_cols) >= 5 - else 1 + else ( + 0.5 + if len(combined_cols) == 3 + else 0.4 if len(combined_cols) == 4 else 0.33 if len(combined_cols) >= 5 else 1 + ) ) combined_receptor_df = receptor_df[(receptor_df[combined_cols] != 0).any(axis=1)] # Calculate overlap for combined ligands @@ -2473,7 +2470,11 @@ def map_new_cells(self): if hasattr(self, "targets_expr"): targets = self.targets_expr.columns y_arr = pd.DataFrame( - self.adata[:, targets].X.toarray() if scipy.sparse.issparse(self.adata.X) else self.adata[:, targets].X, + ( + self.adata[:, targets].X.toarray() + if scipy.sparse.issparse(self.adata.X) + else self.adata[:, targets].X + ), index=self.sample_names, columns=targets, ) diff --git a/spateo/tools/CCI_effects_modeling/MuSIC_downstream.py b/spateo/tools/CCI_effects_modeling/MuSIC_downstream.py index 95604f0c..c816a4d4 100644 --- a/spateo/tools/CCI_effects_modeling/MuSIC_downstream.py +++ b/spateo/tools/CCI_effects_modeling/MuSIC_downstream.py @@ -9,6 +9,7 @@ - following spatially-aware regression (or a sequence of spatially-aware regressions), overlay the directionality of the predicted influence of the ligand on downstream expression. """ + import argparse import collections import gc @@ -981,9 +982,9 @@ def plot_multiple_interaction_effects_3D( if len(strong_active_effects) >= 3: adata.obs.loc[idx, "interaction_categories"] = "Multiple interactions" elif len(strong_active_effects) == 2: - adata.obs.loc[ - idx, "interaction_categories" - ] = f"{strong_active_effects[0]} and {strong_active_effects[1]}" + adata.obs.loc[idx, "interaction_categories"] = ( + f"{strong_active_effects[0]} and {strong_active_effects[1]}" + ) elif len(active_effects) == 1: adata.obs.loc[idx, "interaction_categories"] = active_effects[0] else: @@ -1324,24 +1325,24 @@ def visualize_overlap_between_interacting_components_3D( overlap = target_expressing.intersection(interaction_expressing) adata.obs[f"{interaction}_{target}"] = "Other" - adata.obs.loc[ - target_expressing, f"{interaction}_{target}" - ] = f"{target} only (no {interaction} in neighborhood and/or receptor)" + adata.obs.loc[target_expressing, f"{interaction}_{target}"] = ( + f"{target} only (no {interaction} in neighborhood and/or receptor)" + ) if self.mod_type == "lr": ligand, receptor = interaction.split(":") - adata.obs.loc[ - interaction_expressing, f"{interaction}_{target}" - ] = f"{ligand.title()} in Neighborhood and {receptor}, no {target}" - adata.obs.loc[ - overlap, f"{interaction}_{target}" - ] = f"{ligand.title()} in Neighborhood, {receptor} and {target}" + adata.obs.loc[interaction_expressing, f"{interaction}_{target}"] = ( + f"{ligand.title()} in Neighborhood and {receptor}, no {target}" + ) + adata.obs.loc[overlap, f"{interaction}_{target}"] = ( + f"{ligand.title()} in Neighborhood, {receptor} and {target}" + ) elif self.mod_type == "ligand": - adata.obs.loc[ - interaction_expressing, f"{interaction}_{target}" - ] = f"{interaction.title()} in Neighborhood and Receptor, no {target}" - adata.obs.loc[ - overlap, f"{interaction}_{target}" - ] = f"{interaction.title()} in Neighborhood, Receptor and {target}" + adata.obs.loc[interaction_expressing, f"{interaction}_{target}"] = ( + f"{interaction.title()} in Neighborhood and Receptor, no {target}" + ) + adata.obs.loc[overlap, f"{interaction}_{target}"] = ( + f"{interaction.title()} in Neighborhood, Receptor and {target}" + ) color_mapping = dict(zip(adata.obs[f"{interaction}_{target}"].value_counts().index, godsnot_102)) color_mapping["Other"] = "#D3D3D3" @@ -3536,9 +3537,9 @@ def visualize_neighborhood( adata.obs.loc[ target_expressing_selected, f"{interaction}_{target}_{select_examples_criterion}_example_points" ] = target_expression - adata.obs.loc[ - neighbors_selected, f"{interaction}_{target}_{select_examples_criterion}_example_points" - ] = ligand_expression + adata.obs.loc[neighbors_selected, f"{interaction}_{target}_{select_examples_criterion}_example_points"] = ( + ligand_expression + ) if display_plot: # plotly to create 3D scatter plot: @@ -5568,13 +5569,13 @@ def get_effect_potential( if store_summed_potential: if self.mod_type == "niche": if receiver_cell_type is None: - self.adata.obs[ - f"norm_sum_sent_effect_potential_{sender_cell_type}_for_{target}" - ] = normalized_effect_potential_sum_sender + self.adata.obs[f"norm_sum_sent_effect_potential_{sender_cell_type}_for_{target}"] = ( + normalized_effect_potential_sum_sender + ) - self.adata.obs[ - f"norm_sum_received_effect_potential_from_{sender_cell_type}_for_{target}" - ] = normalized_effect_potential_sum_receiver + self.adata.obs[f"norm_sum_received_effect_potential_from_{sender_cell_type}_for_{target}"] = ( + normalized_effect_potential_sum_receiver + ) else: self.adata.obs[ f"norm_sum_sent_{sender_cell_type}_effect_potential_to_{receiver_cell_type}_for_{target}" @@ -5589,26 +5590,26 @@ def get_effect_potential( ligand = replace_col_with_collagens(ligand) ligand = replace_hla_with_hlas(ligand) - self.adata.obs[ - f"norm_sum_sent_effect_potential_{ligand}_for_{target}" - ] = normalized_effect_potential_sum_sender + self.adata.obs[f"norm_sum_sent_effect_potential_{ligand}_for_{target}"] = ( + normalized_effect_potential_sum_sender + ) - self.adata.obs[ - f"norm_sum_received_effect_potential_from_{ligand}_for_{target}" - ] = normalized_effect_potential_sum_receiver + self.adata.obs[f"norm_sum_received_effect_potential_from_{ligand}_for_{target}"] = ( + normalized_effect_potential_sum_receiver + ) elif self.mod_type == "lr": if "/" in ligand: ligand = replace_col_with_collagens(ligand) ligand = replace_hla_with_hlas(ligand) - self.adata.obs[ - f"norm_sum_sent_effect_potential_{ligand}_for_{target}_via_{receptor}" - ] = normalized_effect_potential_sum_sender + self.adata.obs[f"norm_sum_sent_effect_potential_{ligand}_for_{target}_via_{receptor}"] = ( + normalized_effect_potential_sum_sender + ) - self.adata.obs[ - f"norm_sum_received_effect_potential_from_{ligand}_for_{target}_via_{receptor}" - ] = normalized_effect_potential_sum_receiver + self.adata.obs[f"norm_sum_received_effect_potential_from_{ligand}_for_{target}_via_{receptor}"] = ( + normalized_effect_potential_sum_receiver + ) self.adata.obs["effect_sign"] = effect_sign.reshape(-1, 1) @@ -6846,7 +6847,11 @@ def CCI_deg_detection_setup( targets = [t for t in targets if t in self.adata.var_names] targets = list(set(targets)) targets_expr = pd.DataFrame( - self.adata[:, targets].X.toarray() if scipy.sparse.issparse(self.adata.X) else self.adata[:, targets].X, + ( + self.adata[:, targets].X.toarray() + if scipy.sparse.issparse(self.adata.X) + else self.adata[:, targets].X + ), index=self.adata.obs_names, columns=targets, ) @@ -6879,9 +6884,11 @@ def CCI_deg_detection_setup( ct_signaling = ct_signaling.var.index[sig_expr_percentage > self.target_expr_threshold] sig_expr = pd.DataFrame( - self.adata[:, ct_signaling].X.toarray() - if scipy.sparse.issparse(self.adata.X) - else self.adata[:, ct_signaling].X, + ( + self.adata[:, ct_signaling].X.toarray() + if scipy.sparse.issparse(self.adata.X) + else self.adata[:, ct_signaling].X + ), index=self.sample_names, columns=ct_signaling, ) diff --git a/spateo/tools/cci_two_cluster.py b/spateo/tools/cci_two_cluster.py index 41d738d0..ce85fe1e 100644 --- a/spateo/tools/cci_two_cluster.py +++ b/spateo/tools/cci_two_cluster.py @@ -374,7 +374,9 @@ def find_cci_two_group( per_ligand_data = adata[per_sender_id, lr_network["from"]] per_receptor_data = adata[per_receiver_id, lr_network["to"]] per_lr_data = ( - per_ligand_data.X.toarray() * per_receptor_data.X.toarray() if x_sparse else per_ligand_data.X * per_receptor_data.X + per_ligand_data.X.toarray() * per_receptor_data.X.toarray() + if x_sparse + else per_ligand_data.X * per_receptor_data.X ) per_lr_co_exp_ratio = np.apply_along_axis(lambda x: np.sum(x > 0) / x.size, 0, per_lr_data) if np.isnan(per_lr_co_exp_ratio).all(): diff --git a/spateo/tools/cell_communication.py b/spateo/tools/cell_communication.py index e7d79045..49ad056a 100644 --- a/spateo/tools/cell_communication.py +++ b/spateo/tools/cell_communication.py @@ -380,7 +380,9 @@ def predict_ligand_activities( response_expressed_genes = list(set(expressed_genes_receiver) & set(ligand_target_matrix.index)) response_expressed_genes_df = pd.DataFrame(response_expressed_genes) response_expressed_genes_df = response_expressed_genes_df.rename(columns={0: "gene"}) - response_expressed_genes_df["avg_expr"] = np.mean(adata[receiver_cells, response_expressed_genes].X.toarray(), axis=0) + response_expressed_genes_df["avg_expr"] = np.mean( + adata[receiver_cells, response_expressed_genes].X.toarray(), axis=0 + ) lt_matrix = ligand_target_matrix[potential_ligands.tolist()].loc[response_expressed_genes_df["gene"].tolist()] de = [] for ligand in lt_matrix: