diff --git a/libcodonusage/__init__.py b/libcodonusage/__init__.py index 8d68210b1570b3050c1a22f2523baf274e77a9a8..4fcad87316df480991f3e8fabe88abfeb3522f6b 100644 --- a/libcodonusage/__init__.py +++ b/libcodonusage/__init__.py @@ -1,6 +1,6 @@ __copyright__ = "Copyright (C) 2022 Blaise Li" __licence__ = "GNU GPLv3" -__version__ = "0.24" +__version__ = "0.25" from .libcodonusage import ( aa2colour, aa_usage, diff --git a/libcodonusage/libcodonusage.py b/libcodonusage/libcodonusage.py index a95f8807b0dae91e549dea22c4d36b132ff2647a..49d5c6b46ca620a30ac2b1c1910a384619614a6c 100644 --- a/libcodonusage/libcodonusage.py +++ b/libcodonusage/libcodonusage.py @@ -60,6 +60,13 @@ from sklearn.preprocessing import normalize from upsetplot import from_indicators, UpSet +fmt_metadata = { + "png": {"creationDate": None}, + "svg": { + "Creator": None, "Date": None, + "Title": "Distribution of standardized codon usages biases (by aa), by chromosome"} +} + def render_md(md_str): """ Render a markdown string *md_str* in a Jupyter notebook. @@ -669,7 +676,8 @@ methionine (M) and tryptophan (W). def codon_usage_pca( usage_data, - figs_dir=None, hue="chrom", exclude_cols=None): + figs_dir=None, hue="chrom", exclude_cols=None, + formats=None): """ Perform Principal Component Analysis on *usage_data*. @@ -691,6 +699,8 @@ def codon_usage_pca( in the first four principal components (0 vs. 1 and 2 vs. 3) as well as graphics representing the influence of each data column on the first four principal components. + *formats* should be a list of formats in which the figures should + be saved, such as "svg" or "png". If *exclude_cols* is not None, the columns with the names contained in the iterable *exclude_cols* will not be included in the PCA analysis. @@ -713,10 +723,11 @@ def codon_usage_pca( sns.scatterplot( data=transformed_data, x=2, y=3, hue=hue, marker=".", ax=axes[1]) - if figs_dir is not None: - plt.savefig( - figs_dir.joinpath("PCA_projections.png"), - metadata={'creationDate': None}) + if figs_dir is not None and formats is not None: + for ext in formats: + plt.savefig( + figs_dir.joinpath(f"PCA_projections.{ext}"), + metadata=fmt_metadata[ext]) display(fig) plt.close(fig) render_md( @@ -734,10 +745,11 @@ def codon_usage_pca( axis.set_ylabel(f"weight in component {component}") # axis.set_xticklabels(axis.get_xticklabels(), rotation=90) fig.subplots_adjust(hspace=.5) - if figs_dir is not None: - plt.savefig( - figs_dir.joinpath("PCA_components.png"), - metadata={'creationDate': None}) + if figs_dir is not None and formats is not None: + for ext in formats: + plt.savefig( + figs_dir.joinpath(f"PCA_components.{ext}"), + metadata=fmt_metadata[ext]) display(fig) plt.close(fig) return (pca, transformed_data) @@ -994,7 +1006,7 @@ def write_cluster_lists( vertical=True) path_to_fig = aa_dir.joinpath(star2stop( f"usage_biases_violin_plots_by_cluster_for_{aa}.png")) - plt.savefig(path_to_fig, metadata={'creationDate': None}) + plt.savefig(path_to_fig, metadata=fmt_metadata["png"]) plt.close() relpath_to_fig = str(path_to_fig.relative_to('.')) md_report += ( @@ -1202,7 +1214,7 @@ def plot_codon_usage_for_gene_list( aa_dir.mkdir(parents=True, exist_ok=True) path_to_fig = aa_dir.joinpath(star2stop( f"usage_biases_violin_plot_{aa}_{codon}.png")) - plt.savefig(path_to_fig, metadata={'creationDate': None}) + plt.savefig(path_to_fig, metadata=fmt_metadata["png"]) plt.close() # Save the list of the top genes # path_to_top = aa_dir.joinpath(star2stop(f"{aa}_{codon}_top.txt"))