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

Remove side effect of remove_codons.

parent c8981ee9
Branches
No related tags found
No related merge requests found
__copyright__ = "Copyright (C) 2022-2023 Blaise Li, Marie Anselmet"
__licence__ = "GNU GPLv3"
__version__ = "0.28.4"
__version__ = "0.28.5"
from .libcodonusage import (
aa2colour,
aa_usage,
......
......@@ -442,7 +442,7 @@ SUZUKI_DOI = "10.1016/j.febslet.2005.10.032"
SUZUKI_LINK = f"[Suzuki et al (2005)](https://doi.org/{SUZUKI_DOI})"
def remove_codons(codon_counts, codon_list):
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).
......@@ -451,6 +451,14 @@ def remove_codons(codon_counts, codon_list):
return codon_counts
def remove_codons(codon_counts, codon_list):
"""
Filter out codons in a table *codon_counts* based on codons
present in the list *codon_list* (like stop codons).
"""
return codon_counts.drop(columns=codon_list)
def sum_codon_counts(row, codons):
"""
Perform the row-wise sum of codon counts for the codons
......@@ -477,46 +485,6 @@ def max_codon_counts(row, codons):
return max(counts_codons)
def group_codons_by_class_old(
codon_counts, group_name, dict_classes,
mode="max", keep_only_groups=False):
"""
Group codons given specific classes in *codon_counts* table.
*group_name* contains the name of the grouping, and plays the role
of aa names in the original codon counts table.
*dict_classes* contains the different classes under this grouping
as keys and the associated list of codons as values.
*mode* defines the way grouping is computed.
If mode is "max", the maximum value of counts of codons belonging
to the same class is used for the grouped class.
Otherwise, the sum of counts values for all codons 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
other codons than the ones specified in dict_classes.
If set to False (default), the original codon_counts table
is returned with additional columns for the grouped_classes.
"""
list_classes_names = []
# pylint issues the following warning:
# "W0640: Cell variable value defined in loop (cell-var-from-loop)"
# Since the lambda function is used immediately,
# this should not be an actual issue
# (see https://stackoverflow.com/q/25314547/1878788 and answers)
for key, value in dict_classes.items():
if mode == "max":
codon_counts[group_name, key] = codon_counts.apply(
lambda row: max_codon_counts(row, value), axis=1)
else:
codon_counts[group_name, key] = codon_counts.apply(
lambda row: sum_codon_counts(row, value), axis=1)
list_classes_names.append(key)
if keep_only_groups:
return codon_counts.loc[:, ([group_name], list_classes_names)]
else:
return codon_counts
def group_codons_by_class(
codon_counts, group_name, dict_classes,
mode="max", keep_only_groups=False):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment