Skip to content
Snippets Groups Projects
Commit e6f55702 authored by Blaise Li's avatar Blaise Li
Browse files

More easy to choose fig formats.

parent 64894825
No related branches found
No related tags found
No related merge requests found
__copyright__ = "Copyright (C) 2022 Blaise Li"
__licence__ = "GNU GPLv3"
__version__ = "0.24"
__version__ = "0.25"
from .libcodonusage import (
aa2colour,
aa_usage,
......
......@@ -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:
if figs_dir is not None and formats is not None:
for ext in formats:
plt.savefig(
figs_dir.joinpath("PCA_projections.png"),
metadata={'creationDate': None})
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:
if figs_dir is not None and formats is not None:
for ext in formats:
plt.savefig(
figs_dir.joinpath("PCA_components.png"),
metadata={'creationDate': None})
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"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment