Skip to content

Commit

Permalink
refactor: move .cond() to DeseqDataSet so that it can be used at Dese…
Browse files Browse the repository at this point in the history
…qStats initialization
  • Loading branch information
BorisMuzellec committed Nov 13, 2024
1 parent a404a3b commit 7da5320
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/source/api/docstrings/pydeseq2.dds.DeseqDataSet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.. autosummary::

~DeseqDataSet.calculate_cooks
~DeseqDataSet.cond
~DeseqDataSet.deseq2
~DeseqDataSet.fit_LFC
~DeseqDataSet.fit_MAP_dispersions
Expand Down
29 changes: 29 additions & 0 deletions pydeseq2/dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,35 @@ def deseq2(self, fit_type: Optional[Literal["parametric", "mean"]] = None) -> No
# Compute gene mask for cooks outliers
self.cooks_outlier()

def cond(self, **kwargs):
"""
Get a contrast vector representing a specific condition.
Parameters
----------
**kwargs
Column/value pairs.
Returns
-------
ndarray
A contrast vector that aligns to the columns of the design matrix.
"""
cond_dict = kwargs
if not set(cond_dict.keys()).issubset(self.variables):
raise ValueError(
"""You specified a variable that is not part of the model. Available
variables: """
+ ",".join(self.variables)
)
for var in self.variables:
if var in cond_dict:
self._check_category(var, cond_dict[var])
else:
cond_dict[var] = self._get_default_value(var)
df = pd.DataFrame([kwargs])
return self.obsm["design_matrix"].model_spec.get_model_matrix(df).iloc[0]

def fit_size_factors(
self,
fit_type: Literal["ratio", "poscounts", "iterative"] = "ratio",
Expand Down
33 changes: 3 additions & 30 deletions pydeseq2/ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,33 +614,6 @@ def _contrast(self, column: str, baseline: str, group_to_compare: str) -> np.nda
np.ndarray
The contrast vector.
"""
return self.cond(**{column: baseline}) - self.cond(**{column: group_to_compare})

def cond(self, **kwargs):
"""
Get a contrast vector representing a specific condition.
Parameters
----------
**kwargs
Column/value pairs.
Returns
-------
ndarray
A contrast vector that aligns to the columns of the design matrix.
"""
cond_dict = kwargs
if not set(cond_dict.keys()).issubset(self.dds.variables):
raise ValueError(
"""You specified a variable that is not part of the model. Available
variables: """
+ ",".join(self.dds.variables)
)
for var in self.dds.variables:
if var in cond_dict:
self.dds._check_category(var, cond_dict[var])
else:
cond_dict[var] = self.dds._get_default_value(var)
df = pd.DataFrame([kwargs])
return self.dds.obsm["design_matrix"].model_spec.get_model_matrix(df).iloc[0]
return self.dds.cond(**{column: baseline}) - self.dds.cond(
**{column: group_to_compare}
)

0 comments on commit 7da5320

Please sign in to comment.