Skip to content

Commit

Permalink
Fix edgeR rpy2 tests (#692)
Browse files Browse the repository at this point in the history
* fix broken rpy2 edger tests

* updated edger tests
  • Loading branch information
emdann authored Jan 4, 2025
1 parent 298db0f commit 59d815a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 4 additions & 2 deletions pertpy/tools/_differential_gene_expression/_edger.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ def fit(self, **kwargs): # adata, design, mask, layer
logger.info("Calculating NormFactors")
dge = edger.calcNormFactors(dge)

with localconverter(get_conversion() + pandas2ri.converter):
design_r = ro.conversion.py2rpy(pd.DataFrame(self.design))
with localconverter(get_conversion() + numpy2ri.converter):
# dt = np.dtype([(name, 'float64') for name in self.design.columns])
# design_array = np.array(self.design.values, dtype=dt)
design_r = ro.conversion.py2rpy(self.design.values)

logger.info("Estimating Dispersions")
dge = edger.estimateDisp(dge, design=design_r)
Expand Down
24 changes: 21 additions & 3 deletions tests/tools/_differential_gene_expression/test_edger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pertpy.tools._differential_gene_expression import EdgeR
from pertpy.tools._differential_gene_expression import EdgeR, PyDESeq2
import numpy.testing as npt


def test_edger_simple(test_adata):
Expand All @@ -13,6 +14,17 @@ def test_edger_simple(test_adata):
res_df = method.test_contrasts(method.contrast("condition", "A", "B"))

assert len(res_df) == test_adata.n_vars
# Compare against snapshot
npt.assert_almost_equal(
res_df.p_value.values,
[8.0000e-05, 1.8000e-04, 5.3000e-04, 1.1800e-03, 3.3800e-02, 3.3820e-02, 7.7980e-02, 1.3715e-01, 2.5052e-01, 9.2485e-01],
decimal=4,
)
npt.assert_almost_equal(
res_df.log_fc.values,
[ 0.61208, -0.39374, 0.57944, 0.7343 , -0.58675, 0.42575, -0.23951, -0.20761, 0.17489, 0.0247],
decimal=4,
)


def test_edger_complex(test_adata):
Expand All @@ -28,6 +40,12 @@ def test_edger_complex(test_adata):
# Check that the index of the result matches the var_names of the AnnData object
assert set(test_adata.var_names) == set(res_df["variable"])

# Compare ranking of genes from a different method (without design matrix handling)
down_gene = res_df.set_index("variable").loc['gene3', 'log_fc']
up_gene = res_df.set_index("variable").loc['gene1', 'log_fc']
assert down_gene < up_gene

# TODO: there should be a test checking if, for a concrete example, the output p-values and effect sizes are what
# we expect (-> frozen snapshot, that way we also get a heads-up if something changes upstream)
method = PyDESeq2(adata=test_adata, design="~condition1+group")
method.fit()
deseq_res_df = method.test_contrasts(method.contrast("condition1", "A", "B"))
assert all(res_df.sort_values('log_fc')['variable'].values == deseq_res_df.sort_values('log_fc')['variable'].values)

0 comments on commit 59d815a

Please sign in to comment.