|
a |
|
b/tests/deseq2/test_DESeqDataSet.py |
|
|
1 |
import unittest |
|
|
2 |
|
|
|
3 |
import numpy as np |
|
|
4 |
from pandas.api.types import CategoricalDtype |
|
|
5 |
|
|
|
6 |
from inmoose.deseq2 import DESeqDataSet |
|
|
7 |
|
|
|
8 |
|
|
|
9 |
class Test(unittest.TestCase): |
|
|
10 |
def test_counts(self): |
|
|
11 |
"""test that normalized counts are properly computed""" |
|
|
12 |
dds = DESeqDataSet( |
|
|
13 |
np.arange(24).reshape(4, 6), clinicalData=["A", "A", "B", "B"], design="~1" |
|
|
14 |
) |
|
|
15 |
with self.assertRaisesRegex( |
|
|
16 |
ValueError, |
|
|
17 |
expected_regex="first calculate size factors, add normalizationFactors, or set normalized=False", |
|
|
18 |
): |
|
|
19 |
dds.counts(normalized=True) |
|
|
20 |
dds = dds.estimateSizeFactors() |
|
|
21 |
ref = np.array( |
|
|
22 |
[ |
|
|
23 |
[0.000000, 3.201086, 6.402172, 9.603258, 12.80434, 16.00543], |
|
|
24 |
[6.402172, 7.469200, 8.536229, 9.603258, 10.67029, 11.73731], |
|
|
25 |
[7.682606, 8.322823, 8.963040, 9.603258, 10.24347, 10.88369], |
|
|
26 |
[8.231364, 8.688662, 9.145960, 9.603258, 10.06056, 10.51785], |
|
|
27 |
] |
|
|
28 |
) |
|
|
29 |
res = dds.counts(normalized=True) |
|
|
30 |
self.assertTrue(np.allclose(res, ref)) |
|
|
31 |
|
|
|
32 |
def test_design(self): |
|
|
33 |
"""test that categorical variable in the design are properly accounted for""" |
|
|
34 |
dds = DESeqDataSet(np.arange(24).reshape(4, 6)) |
|
|
35 |
dds.obs["x"] = ["A", "A", "B", "B"] |
|
|
36 |
dds.obs["y"] = [1, 2, 1, 2] |
|
|
37 |
dds.design = "x + y" |
|
|
38 |
self.assertTrue("C(x)" in dds.obs) |
|
|
39 |
self.assertTrue("C(y)" not in dds.obs) |
|
|
40 |
self.assertFalse(isinstance(dds.obs["x"].dtype, CategoricalDtype)) |
|
|
41 |
self.assertTrue(isinstance(dds.obs["C(x)"].dtype, CategoricalDtype)) |
|
|
42 |
self.assertFalse(isinstance(dds.obs["y"].dtype, CategoricalDtype)) |
|
|
43 |
|
|
|
44 |
dds = DESeqDataSet(np.arange(24).reshape(4, 6)) |
|
|
45 |
dds.obs["x"] = ["A", "A", "B", "B"] |
|
|
46 |
dds.obs["y"] = [1, 2, 1, 2] |
|
|
47 |
dds.design = "C(x) + C(y)" |
|
|
48 |
self.assertTrue("C(x)" in dds.obs) |
|
|
49 |
self.assertTrue("C(y)" in dds.obs) |
|
|
50 |
self.assertFalse(isinstance(dds.obs["x"].dtype, CategoricalDtype)) |
|
|
51 |
self.assertTrue(isinstance(dds.obs["C(x)"].dtype, CategoricalDtype)) |
|
|
52 |
self.assertFalse(isinstance(dds.obs["y"].dtype, CategoricalDtype)) |
|
|
53 |
self.assertTrue(isinstance(dds.obs["C(y)"].dtype, CategoricalDtype)) |