diff --git a/libcodonusage/__init__.py b/libcodonusage/__init__.py index 93cf75e2c6d182fd906c785221e537aba3da4472..7ea81e160a1c1a2e6eeeaf43aea9cdc034f7e73e 100644 --- a/libcodonusage/__init__.py +++ b/libcodonusage/__init__.py @@ -1,6 +1,6 @@ __copyright__ = "Copyright (C) 2022 Blaise Li" __licence__ = "GNU GPLv3" -__version__ = "0.19" +__version__ = "0.20" from .libcodonusage import ( aa2colour, aa_usage, diff --git a/libcodonusage/libcodonusage.py b/libcodonusage/libcodonusage.py index 8f1e0a68fb36e54b7efbde2f5c36703d664fcb57..fbf886ede2f425e22054723235ba0cf187eeef92 100644 --- a/libcodonusage/libcodonusage.py +++ b/libcodonusage/libcodonusage.py @@ -19,7 +19,7 @@ import json from operator import itemgetter from pathlib import Path # python3 -m pip install cytoolz -from cytoolz import groupby, unique +from cytoolz import concat, groupby, unique # To render mardown in a Jupyter notebook on gitlab from IPython.core.display import display, HTML # python3 -m pip install markdown @@ -575,7 +575,7 @@ methionine (M) and tryptophan (W). render_md("The following columns contain only NaNs:") display(all_nan_cols) render_md("This likely resulted from a division by zero.") - render_md("These columns will be excluded") + render_md("These columns will be excluded.") return ( standardized_usage_biases.drop(columns=all_nan_cols).fillna(0), all_nan_cols) @@ -1118,15 +1118,24 @@ def violin_usage(usage_table, variable, ylabel, hue="aa", axis=None): long_form = to_long_form(usage_table, ylabel) if axis is None: _, axis = plt.subplots(figsize=(18, 6)) + do_legend = True + else: + do_legend = False if hue == "aa": palette = aa2colour else: palette = None - sns.violinplot(x=variable, y=ylabel, + if variable == "aa": + order = [aa for aa in columns_by_aa.keys()] + elif variable == "codon": + order = [codon for (_, codon) in concat(columns_by_aa.values())] + else: + raise ValueError(f"variable can only be 'aa' or 'codon'.\n") + sns.violinplot(x=variable, y=ylabel, order=order, hue=hue, palette=palette, dodge=False, data=long_form, ax=axis, orient="v", scale="count") - plt.legend(bbox_to_anchor=(1.01, 1), - borderaxespad=0) + if do_legend: + plt.legend(bbox_to_anchor=(1.01, 1), borderaxespad=0) axis.set_xticklabels(axis.get_xticklabels(), rotation=90) return axis @@ -1144,8 +1153,14 @@ def violin_usage_vertical(usage_table, variable, ylabel, hue="aa", axis=None): palette = aa2colour else: palette = None + if variable == "aa": + order = [aa for aa in columns_by_aa.keys()] + elif variable == "codon": + order = [codon for (_, codon) in concat(columns_by_aa.values())] + else: + raise ValueError(f"variable can only be 'aa' or 'codon'.\n") sns.violinplot( - y=variable, x=ylabel, + y=variable, x=ylabel, order=order, hue=hue, palette=palette, dodge=False, data=long_form, ax=axis, orient="h", scale="count") return axis