Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
vcabeli committed Sep 5, 2023
1 parent dc24d17 commit 849e4d8
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/data/single_factor/r_test_res_greater.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"","baseMean","log2FoldChange","lfcSE","stat","pvalue","padj"
"gene1",8.54131729397935,0.632812315254828,0.289103638926866,0.459393440178817,0.322975829040018,0.999999999999993
"gene2",21.2812387436367,0.538551973774899,0.149962947101772,0.257076661401806,0.398559796861775,0.999999999999993
"gene3",5.01012348853472,-0.632831858568867,0.295221453480919,0,0.999937784387348,0.999999999999993
"gene4",100.51796142035,-0.412101731455795,0.118627877254952,0,0.999999999999993,0.999999999999993
"gene5",27.1424502740787,0.582066403700137,0.154730436026561,0.530383070115883,0.297923180833832,0.999999999999993
"gene6",5.4130427476525,0.00145730049165758,0.310309910140032,0,0.945928503558213,0.999999999999993
"gene7",28.2940230404605,0.134335684024564,0.149986853732723,0,0.992615118120242,0.999999999999993
"gene8",40.3583444203556,-0.270655780074569,0.136401659050961,0,0.999999991972956,0.999999999999993
"gene9",37.1661826339853,-0.212714657425421,0.133243837041424,0,0.999999955767077,0.999999999999993
"gene10",11.5893249023836,0.386011089345353,0.244586104807745,0,0.679409495317641,0.999999999999993
11 changes: 11 additions & 0 deletions tests/data/single_factor/r_test_res_greaterAbs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"","baseMean","log2FoldChange","lfcSE","stat","pvalue","padj"
"gene1",8.54131729397935,0.632812315254828,0.289103638926866,0.459393440178817,0.645951658080035,1
"gene2",21.2812387436367,0.538551973774899,0.149962947101772,0.257076661401806,0.79711959372355,1
"gene3",5.01012348853472,-0.632831858568867,0.295221453480919,-0.449939721529933,0.652753905192737,1
"gene4",100.51796142035,-0.412101731455795,0.118627877254952,0,1,1
"gene5",27.1424502740787,0.582066403700137,0.154730436026561,0.530383070115883,0.595846361667663,1
"gene6",5.4130427476525,0.00145730049165758,0.310309910140032,0,1,1
"gene7",28.2940230404605,0.134335684024564,0.149986853732723,0,1,1
"gene8",40.3583444203556,-0.270655780074569,0.136401659050961,0,1,1
"gene9",37.1661826339853,-0.212714657425421,0.133243837041424,0,1,1
"gene10",11.5893249023836,0.386011089345353,0.244586104807745,0,1,1
11 changes: 11 additions & 0 deletions tests/data/single_factor/r_test_res_less.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"","baseMean","log2FoldChange","lfcSE","stat","pvalue","padj"
"gene1",8.54131729397935,0.632812315254828,0.289103638926866,0,0.999955423372308,0.999999999998657
"gene2",21.2812387436367,0.538551973774899,0.149962947101772,0,0.999999999997826,0.999999999998657
"gene3",5.01012348853472,-0.632831858568867,0.295221453480919,-0.449939721529933,0.326376952596368,0.999999999998657
"gene4",100.51796142035,-0.412101731455795,0.118627877254952,0,0.77064053105774,0.999999999998657
"gene5",27.1424502740787,0.582066403700137,0.154730436026561,0,0.999999999998657,0.999999999998657
"gene6",5.4130427476525,0.00145730049165758,0.310309910140032,0,0.946951619107181,0.999999999998657
"gene7",28.2940230404605,0.134335684024564,0.149986853732723,0,0.999988277730404,0.999999999998657
"gene8",40.3583444203556,-0.270655780074569,0.136401659050961,0,0.953656288862981,0.999999999998657
"gene9",37.1661826339853,-0.212714657425421,0.133243837041424,0,0.984461574838508,0.999999999998657
"gene10",11.5893249023836,0.386011089345353,0.244586104807745,0,0.999854110482588,0.999999999998657
11 changes: 11 additions & 0 deletions tests/data/single_factor/r_test_res_lessAbs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"","baseMean","log2FoldChange","lfcSE","stat","pvalue","padj"
"gene1",8.54131729397935,0.632812315254828,0.289103638926866,0,0.677024170959982,0.702076819166168
"gene2",21.2812387436367,0.538551973774899,0.149962947101772,0,0.601440203138225,0.702076819166168
"gene3",5.01012348853472,-0.632831858568867,0.295221453480919,0,0.673623047403632,0.702076819166168
"gene4",100.51796142035,-0.412101731455795,0.118627877254952,0.740957948318473,0.22935946894226,0.458718937884521
"gene5",27.1424502740787,0.582066403700137,0.154730436026561,0,0.702076819166168,0.702076819166168
"gene6",5.4130427476525,0.00145730049165758,0.310309910140032,1.60659612605787,0.0540714964417869,0.135178741104467
"gene7",28.2940230404605,0.134335684024564,0.149986853732723,2.43797577504394,0.00738488187975816,0.0738488187975816
"gene8",40.3583444203556,-0.270655780074569,0.136401659050961,1.68138878603923,0.0463437111370189,0.135178741104467
"gene9",37.1661826339853,-0.212714657425421,0.133243837041424,2.15608728293576,0.0155384251614917,0.0776921258074587
"gene10",11.5893249023836,0.386011089345353,0.244586104807745,0.46604818676943,0.320590504682359,0.534317507803932
62 changes: 62 additions & 0 deletions tests/test_pydeseq2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import anndata as ad
import numpy as np
import pandas as pd
import pytest

import tests
from pydeseq2.dds import DeseqDataSet
Expand Down Expand Up @@ -56,6 +57,67 @@ def test_deseq(tol=0.02):
assert (abs(r_res.padj - res_df.padj) / r_res.padj).max() < tol


@pytest.mark.parametrize("alt_hypothesis", ["lessAbs", "greaterAbs", "greater"])
def test_alt_hypothesis(alt_hypothesis, tol=0.02):
"""Test that the outputs of the DESeq2 function match those of the original R
package, up to a tolerance in relative error, with the alternative hypothesis test.
"""

test_path = str(Path(os.path.realpath(tests.__file__)).parent.resolve())

counts_df = load_example_data(
modality="raw_counts",
dataset="synthetic",
debug=False,
)

metadata = load_example_data(
modality="metadata",
dataset="synthetic",
debug=False,
)

r_res = pd.read_csv(
os.path.join(test_path, f"data/single_factor/r_test_res_{alt_hypothesis}.csv"),
index_col=0,
)

dds = DeseqDataSet(
counts=counts_df,
metadata=metadata,
design_factors="condition",
lfc_null=-0.5 if alt_hypothesis == "less" else 0.5,
alt_hypothesis=alt_hypothesis,
)
dds.deseq2()

res = DeseqStats(dds)
res.summary()
res_df = res.results_df

# check that the same p-values are NaN
assert (res_df.pvalue.isna() == r_res.pvalue.isna()).all()
assert (res_df.padj.isna() == r_res.padj.isna()).all()

# Check that the same LFC and Wald statistics are found (up to tol)
assert (
abs(r_res.log2FoldChange - res_df.log2FoldChange) / abs(r_res.log2FoldChange)
).max() < tol
if alt_hypothesis == "lessAbs":
res_df.stat = res_df.stat.abs()
assert (abs(r_res.stat - res_df.stat) / abs(r_res.stat)).max() < tol
# Check for the same pvalue and padj where stat != 0
assert (
abs(r_res.pvalue[r_res.stat != 0] - res_df.pvalue[res_df.stat != 0])
/ r_res.pvalue[r_res.stat != 0]
).max() < tol
# Not passing
# assert (
# abs(r_res.padj[r_res.stat != 0] - res_df.padj[res_df.stat != 0])
# / r_res.padj[r_res.stat != 0]
# ).max() < tol


def test_deseq_no_refit_cooks(tol=0.02):
"""Test that the outputs of the DESeq2 function *without cooks refit*
match those of the original R package, up to a tolerance in relative error.
Expand Down

0 comments on commit 849e4d8

Please sign in to comment.