From e6f55702f4c6238c78e63914af77d634b1dd7886 Mon Sep 17 00:00:00 2001
From: Blaise Li <blaise.li__git@nsup.org>
Date: Fri, 29 Apr 2022 17:29:33 +0200
Subject: [PATCH] More easy to choose fig formats.

---
 libcodonusage/__init__.py      |  2 +-
 libcodonusage/libcodonusage.py | 34 +++++++++++++++++++++++-----------
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/libcodonusage/__init__.py b/libcodonusage/__init__.py
index 8d68210..4fcad87 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 a95f880..49d5c6b 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"))
-- 
GitLab