diff --git a/docs/source/api/docstrings/pydeseq2.dds.DeseqDataSet.rst b/docs/source/api/docstrings/pydeseq2.dds.DeseqDataSet.rst index f167f7f4..c2df993c 100644 --- a/docs/source/api/docstrings/pydeseq2.dds.DeseqDataSet.rst +++ b/docs/source/api/docstrings/pydeseq2.dds.DeseqDataSet.rst @@ -10,6 +10,7 @@ .. autosummary:: ~DeseqDataSet.calculate_cooks + ~DeseqDataSet.cond ~DeseqDataSet.deseq2 ~DeseqDataSet.fit_LFC ~DeseqDataSet.fit_MAP_dispersions diff --git a/pydeseq2/dds.py b/pydeseq2/dds.py index 4c16ef89..525b6b00 100644 --- a/pydeseq2/dds.py +++ b/pydeseq2/dds.py @@ -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", diff --git a/pydeseq2/ds.py b/pydeseq2/ds.py index fb527ebf..5a379b43 100644 --- a/pydeseq2/ds.py +++ b/pydeseq2/ds.py @@ -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} + )