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

Change default output of group_codons_by_class.

The codons belonging to the classes are not present in the output.
parent de4b5e81
No related branches found
No related tags found
No related merge requests found
__copyright__ = "Copyright (C) 2022-2023 Blaise Li, Marie Anselmet" __copyright__ = "Copyright (C) 2022-2023 Blaise Li, Marie Anselmet"
__licence__ = "GNU GPLv3" __licence__ = "GNU GPLv3"
__version__ = "0.28.5" __version__ = "0.29.0"
from .libcodonusage import ( from .libcodonusage import (
aa2colour, aa2colour,
aa_usage, aa_usage,
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Functions used in Jupyter notebooks.""" """Functions used in Jupyter notebooks."""
from itertools import combinations from itertools import chain, combinations
import json import json
from operator import attrgetter, itemgetter from operator import attrgetter, itemgetter
from pathlib import Path from pathlib import Path
...@@ -442,19 +442,13 @@ SUZUKI_DOI = "10.1016/j.febslet.2005.10.032" ...@@ -442,19 +442,13 @@ SUZUKI_DOI = "10.1016/j.febslet.2005.10.032"
SUZUKI_LINK = f"[Suzuki et al (2005)](https://doi.org/{SUZUKI_DOI})" SUZUKI_LINK = f"[Suzuki et al (2005)](https://doi.org/{SUZUKI_DOI})"
def remove_codons_old(codon_counts, codon_list):
"""
Filter out codons in a table *codon_counts* based on codons
present in the list *codon_list* (like stop codons).
"""
codon_counts.drop(columns=codon_list, inplace=True)
return codon_counts
def remove_codons(codon_counts, codon_list): def remove_codons(codon_counts, codon_list):
""" """
Filter out codons in a table *codon_counts* based on codons Filter out codons in a table *codon_counts* based on codons
present in the list *codon_list* (like stop codons). present in the list *codon_list* (like stop codons).
When the columns of *codon_counts* are a MultiIndex with a first
level corresponding to the amino-acid and the second to the codon,
the codons should be specified as (aa, codon) tuples.
""" """
return codon_counts.drop(columns=codon_list) return codon_counts.drop(columns=codon_list)
...@@ -486,14 +480,14 @@ def max_codon_counts(row, codons): ...@@ -486,14 +480,14 @@ def max_codon_counts(row, codons):
def group_codons_by_class( def group_codons_by_class(
codon_counts, group_name, dict_classes, codon_counts, group_name, codon_classes,
mode="max", keep_only_groups=False): mode="max", keep_only_groups=False, replace_groups=True):
""" """
Group codons given specific classes in *codon_counts* table. Group codons given specific classes in *codon_counts* table.
*group_name* contains the name of the grouping, and plays the role *group_name* contains the name of the grouping, and plays the role
of aa names in the original codon counts table. of aa names in the original codon counts table.
*dict_classes* contains the different classes under this grouping *codon_classes* contains the different classes under this grouping
as keys and the associated list of codons as values. as keys and the associated list of codons as values.
*mode* defines the way grouping is computed. It should be the name of *mode* defines the way grouping is computed. It should be the name of
a method of the `GroupBy` object, like "sum" or "max". a method of the `GroupBy` object, like "sum" or "max".
...@@ -502,14 +496,18 @@ def group_codons_by_class( ...@@ -502,14 +496,18 @@ def group_codons_by_class(
If *mode* is "sum", the sum of counts values for all codons If *mode* is "sum", the sum of counts values for all codons
belonging to the same class is used for the grouped class. belonging to the same class is used for the grouped class.
*keep_only_groups* is a boolean set to True if you want to filter out *keep_only_groups* is a boolean set to True if you want to filter out
other codons than the ones specified in dict_classes. other codons than the ones specified in codon_classes.
If set to False (default), the original codon_counts table If set to False (default), the original codon_counts table
is returned with additional columns for the grouped_classes. is returned with additional columns for the grouped_classes.
If the boolean *replace_groups* is set to True (default), the full
count table will be returned, except the columns used to compute the
grouped values. This option is ignored if *keep_only_groups* has been
set to True.
""" """
col_renames = { col_renames = {
(aa, codon): (class_name, group_name, codon) (aa, codon): (class_name, group_name, codon)
for class_name in dict_classes for class_name in codon_classes
for (aa, codon) in dict_classes[class_name]} for (aa, codon) in codon_classes[class_name]}
codon_counts_pre_group = codon_counts[col_renames.keys()] codon_counts_pre_group = codon_counts[col_renames.keys()]
# We add a level to the column names, # We add a level to the column names,
# "codon", becoming "sub_codon", will disappear # "codon", becoming "sub_codon", will disappear
...@@ -524,6 +522,10 @@ def group_codons_by_class( ...@@ -524,6 +522,10 @@ def group_codons_by_class(
mode)() mode)()
if keep_only_groups: if keep_only_groups:
return codon_counts_grouped return codon_counts_grouped
if replace_groups:
return remove_codons(
pd.concat([codon_counts, codon_counts_grouped], axis=1),
chain.from_iterable(codon_classes.values()))
return pd.concat([codon_counts, codon_counts_grouped], axis=1) return pd.concat([codon_counts, codon_counts_grouped], axis=1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment