From 62b0710835f7e1ad283055b6f7cc09a2551bacc0 Mon Sep 17 00:00:00 2001 From: Qing <44231502+byemaxx@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:49:55 -0400 Subject: [PATCH] - Fix: Fixed the bug of when plot the heatmap of correlation, the rotation of x-axis and y-axis raise error. --- Docs/ChangeLog.md | 6 ++ metax/gui/main_gui.py | 10 +-- metax/peptide_annotator/pep_to_taxafunc.py | 5 +- metax/taxafunc_ploter/basic_plot.py | 83 +++++++++++++++++----- metax/utils/version.py | 2 +- pyproject.toml | 2 +- 6 files changed, 81 insertions(+), 27 deletions(-) diff --git a/Docs/ChangeLog.md b/Docs/ChangeLog.md index a519131..a276296 100644 --- a/Docs/ChangeLog.md +++ b/Docs/ChangeLog.md @@ -1,3 +1,9 @@ +# Version: 1.111.7 +## Date: 2024-09-06 +### Changes: +- Fix: Fixed the bug of when plot the heatmap of correlation, the rotation of x-axis and y-axis raise error. + + # Version: 1.111.6 ## Date: 2024-08-21 ### Changes: diff --git a/metax/gui/main_gui.py b/metax/gui/main_gui.py index 5bfde74..c35f3e5 100644 --- a/metax/gui/main_gui.py +++ b/metax/gui/main_gui.py @@ -4227,10 +4227,10 @@ def get_title_by_table_name(self, table_name): if cluster: df = self.delete_zero_columns(df) self.show_message('Correlation is running, please wait...') - BasicPlot(self.tfa).plot_corr_sns(df=df, title_name=title_name, cluster= cluster, + BasicPlot(self.tfa, **self.heatmap_params_dict).plot_corr_sns(df=df, title_name=title_name, cluster= cluster, width=width, height=height, font_size=font_size, show_all_labels=show_all_labels, theme=theme, cmap=cmap, - rename_sample = rename_sample, **self.heatmap_params_dict) + rename_sample = rename_sample) elif method == 'alpha_div': self.show_message('Alpha diversity is running, please wait...') @@ -5038,14 +5038,14 @@ def plot_co_expr(self, plot_type = 'network'): self.checkBox_corr_hetatmap_show_all_labels_y.isChecked(), ) cmap = self.comboBox_corr_hetatmap_cmap.currentText() - BasicPlot(self.tfa).plot_items_corr_heatmap(df=df, + BasicPlot(self.tfa, **self.heatmap_params_dict).plot_items_corr_heatmap(df=df, title_name=f'Expression Correlation Heatmap({df_type})', cluster=True, cmap=cmap, width=width, height=height, font_size=font_size, - show_all_labels=show_all_labels, - **self.heatmap_params_dict) + show_all_labels=show_all_labels + ) except Exception as e: error_message = traceback.format_exc() diff --git a/metax/peptide_annotator/pep_to_taxafunc.py b/metax/peptide_annotator/pep_to_taxafunc.py index a79b09a..1766a63 100644 --- a/metax/peptide_annotator/pep_to_taxafunc.py +++ b/metax/peptide_annotator/pep_to_taxafunc.py @@ -219,15 +219,16 @@ def proteins_to_taxa_func(self,protein_list: list ) -> dict: pep_null = 'MGYG000000137_01815;MGYG000001639_01406;MGYG000000236_03945' pep_no_species_level = "MGYG000000385;MGYG000002077;MGYG000003829" pep_mag_level = 'MGYG000003142_02726;MGYG000003142_02725' +pep_t ='GCF_943193095.1-2283;concoct.103-megahit_28365_1' ### test data if __name__ == '__main__': import time t1 = time.time() - db_path = 'C:/Users/Qing/Desktop/MetaX_Suite/metaX_dev_files/MetaX-human-gut_20231211.db' + db_path = 'metax_db.db' - pep2taxafunc = Pep2TaxaFunc(db_path = db_path, threshold = 1, genome_mode = True) + pep2taxafunc = Pep2TaxaFunc(db_path = db_path, threshold = 1, genome_mode = True, protein_genome_separator= "_") # for i in [pep_no_species_level, pep_null, pep2, pep7, pep8, pep9, pep10, pep11]: diff --git a/metax/taxafunc_ploter/basic_plot.py b/metax/taxafunc_ploter/basic_plot.py index 4f597d4..7077990 100644 --- a/metax/taxafunc_ploter/basic_plot.py +++ b/metax/taxafunc_ploter/basic_plot.py @@ -10,16 +10,23 @@ from .get_distinct_colors import GetDistinctColors class BasicPlot: - def __init__(self, tfobj): + def __init__(self, tfobj, + linkage_method:str = 'average', distance_metric:str = 'correlation', + x_labels_rotation:int = 90, y_labels_rotation:int = 0): self.tfa = tfobj self.get_distinct_colors = GetDistinctColors().get_distinct_colors self.assign_colors = GetDistinctColors().assign_colors + # for heatmap + self.linkage_method = linkage_method + self.distance_metric = distance_metric + self.x_labels_rotation = x_labels_rotation + self.y_labels_rotation = y_labels_rotation + # reset the style plt.style.use('default') sns.set_theme() - def plot_taxa_stats_pie(self, theme:str = 'Auto', res_type = 'pic', font_size = 12): df = self.tfa.BasicStats.get_stats_peptide_num_in_taxa() @@ -361,8 +368,6 @@ def plot_corr_sns( theme: str = None, cmap: str = "Auto", rename_sample: bool = False, - linkage_method: str = "average", - distance_metric: str = "euclidean", ): dft= df.copy() if rename_sample: @@ -387,17 +392,29 @@ def plot_corr_sns( sns_params = {"linewidths":.01, "cmap":cmap, "cbar_kws":{ "shrink": 0.5}, 'col_cluster':True if cluster else False, 'row_cluster':True if cluster else False, - 'method':linkage_method, - 'metric':distance_metric, + 'method':self.linkage_method, + 'metric':self.distance_metric, "linecolor":(0/255, 0/255, 0/255, 0.01), "dendrogram_ratio":(.1, .2),"col_colors":color_list, "figsize":(width, height), "xticklabels":True if show_all_labels[0] else "auto", "yticklabels":True if show_all_labels[1] else 'auto'} fig = sns.clustermap(corr, **sns_params) - - fig.ax_col_dendrogram.set_title(f'Correlation of {title_name}', fontsize=font_size+2, fontweight='bold') ax = fig.ax_heatmap - ax.set_xticklabels(ax.get_xticklabels(), fontsize=font_size, rotation=90) - ax.set_yticklabels(ax.get_yticklabels(), fontsize=font_size, rotation=0) + + fig.ax_heatmap.set_xticklabels( + fig.ax_heatmap.get_xmajorticklabels(), + fontsize=font_size, + rotation=self.x_labels_rotation, + ha = self.get_x_labels_ha() + ) + fig.ax_heatmap.set_yticklabels( + fig.ax_heatmap.get_ymajorticklabels(), + fontsize=font_size, + rotation=self.y_labels_rotation, + ha = 'left', + va = self.get_y_labels_va() + ) + + fig.ax_col_dendrogram.set_title(f'Correlation of {title_name}', fontsize=font_size+2, fontweight='bold') cbar = fig.ax_heatmap.collections[0].colorbar cbar.set_label('Intensity', rotation=90, labelpad=1) @@ -594,8 +611,6 @@ def plot_items_corr_heatmap( height=8, font_size=10, show_all_labels=(False, False), - linkage_method: str = "average", - distance_metric: str = "euclidean", ): corr = df.copy() # mask = np.triu(np.ones_like(corr, dtype=bool)) @@ -609,17 +624,33 @@ def plot_items_corr_heatmap( sns_params = {"linewidths":.01, "cmap":cmap, "cbar_kws":{ "shrink": 0.5}, 'col_cluster':True if cluster else False, 'row_cluster':True if cluster else False, - 'method':linkage_method, - 'metric':distance_metric, + 'method':self.linkage_method, + 'metric':self.distance_metric, "linecolor":(0/255, 0/255, 0/255, 0.01), "dendrogram_ratio":(.1, .2), "figsize":(width, height), "xticklabels":True if show_all_labels[0] else "auto", "yticklabels":True if show_all_labels[1] else 'auto'} fig = sns.clustermap(corr, **sns_params) - - fig.ax_col_dendrogram.set_title(f'Correlation of {title_name}', fontsize=font_size+2, fontweight='bold') ax = fig.ax_heatmap - ax.set_xticklabels(ax.get_xticklabels(), fontsize=font_size, rotation=90) - ax.set_yticklabels(ax.get_yticklabels(), fontsize=font_size, rotation=0) + + + fig.ax_col_dendrogram.set_title(f'Correlation of {title_name}', fontsize=font_size+2, fontweight='bold') + fig.ax_heatmap.set_xticklabels( + fig.ax_heatmap.get_xmajorticklabels(), + fontsize=font_size, + rotation=self.x_labels_rotation, + ha = self.get_x_labels_ha() + ) + fig.ax_heatmap.set_yticklabels( + fig.ax_heatmap.get_ymajorticklabels(), + fontsize=font_size, + rotation=self.y_labels_rotation, + ha = 'left', + va = self.get_y_labels_va() + ) + + # hiend the x and y labels + fig.ax_heatmap.set_xlabel('') + fig.ax_heatmap.set_ylabel('') cbar = fig.ax_heatmap.collections[0].colorbar cbar.set_label("Correlation", rotation=90, labelpad=1) @@ -634,3 +665,19 @@ def plot_items_corr_heatmap( except Exception as e: plt.close('all') raise e + + + def get_x_labels_ha(self): + x_rotation = self.x_labels_rotation + if x_rotation > 0: + return 'right' + elif x_rotation < 0: + return 'left' + else: + return 'center' + def get_y_labels_va(self): + y_rotation = self.y_labels_rotation + if y_rotation >= 0: + return 'baseline' + else: + return 'top' diff --git a/metax/utils/version.py b/metax/utils/version.py index 7225fd1..eab6ab7 100644 --- a/metax/utils/version.py +++ b/metax/utils/version.py @@ -1,2 +1,2 @@ -__version__ = '1.111.6' +__version__ = '1.111.7' API_version = '2' \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index ea0a2f9..74307c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "MetaXTools" -version = "1.111.6" +version = "1.111.7" description = "MetaXTools is a novel tool for linking peptide sequences with taxonomic and functional information in Metaproteomics." readme = "README_PyPi.md" license = { text = "NorthOmics" }