|
1 | 1 | import unittest |
2 | | -import cmapPy.pandasGEXpress.setup_GCToo_logger as setup_logger |
3 | 2 | import logging |
4 | 3 | import pandas as pd |
5 | | -import sys |
| 4 | +import cmapPy.pandasGEXpress.setup_GCToo_logger as setup_logger |
6 | 5 | import cmapPy.pandasGEXpress.GCToo as GCToo |
7 | | -import cmapPy.pandasGEXpress.diff_gctoo as differential |
| 6 | +import cmapPy.pandasGEXpress.diff_gctoo as diff_gctoo |
8 | 7 |
|
9 | 8 | logger = logging.getLogger(setup_logger.LOGGER_NAME) |
10 | 9 |
|
11 | | -test_mat = pd.DataFrame({'A':[4,2,3], 'B': [2,8,6], 'C': [6,5,9], 'D': [5,2,1], 'E':[8,8,6], 'F': [7,6,6]}) |
12 | | -test_col_meta = pd.DataFrame({'pert_type': ['trt_cp', 'trt_cp', 'trt_cp', 'trt_cp','ctl_vehicle','ctl_vehicle'], |
13 | | - 'pert_iname': ['bort', 'bort', 'DMSO', 'DMSO', 'bort', 'bort']}, |
14 | | - index=['A', 'B', 'C', 'D', 'E', 'F']) |
15 | | -test_gctoo = GCToo.GCToo(data_df=test_mat, col_metadata_df=test_col_meta, row_metadata_df=pd.DataFrame(index=range(0,3))) |
| 10 | +test_mat = pd.DataFrame({'A':[4,2,3], 'B': [2,8,6], 'C': [6,5,9], |
| 11 | + 'D': [5,2,1], 'E':[8,8,6], 'F': [7,6,6]}) |
| 12 | +test_col_meta = pd.DataFrame( |
| 13 | + {'pert_type': ['trt_cp', 'trt_cp', 'trt_cp', |
| 14 | + 'trt_cp', 'ctl_vehicle', 'ctl_vehicle'], |
| 15 | + 'pert_iname': ['bort', 'bort', 'DMSO', 'DMSO', 'bort', 'bort']}, |
| 16 | + index=['A', 'B', 'C', 'D', 'E', 'F']) |
| 17 | +test_gctoo = GCToo.GCToo(data_df=test_mat, |
| 18 | + col_metadata_df=test_col_meta) |
| 19 | + |
16 | 20 |
|
17 | 21 | class TestDifferential(unittest.TestCase): |
18 | | - def test_differential(self): |
19 | | - pc_zscores = differential.calc_differential(test_gctoo, True) |
20 | | - vc_zscores1 = differential.calc_differential(test_gctoo, False) |
21 | | - vc_zscores2 = differential.calc_differential(test_gctoo, False, 'pert_iname', 'DMSO') |
| 22 | + def test_diff_gctoo_pc(self): |
| 23 | + pc_zscores = diff_gctoo.diff_gctoo(test_gctoo, plate_control=True, lower_diff_thresh=-2) |
22 | 24 | self.assertTrue(pc_zscores.data_df.shape == (3, 6)) |
| 25 | + |
| 26 | + pd.util.testing.assert_frame_equal(pc_zscores.data_df, pd.DataFrame( |
| 27 | + {'A': [-0.6745, -0.9443, -1.349], |
| 28 | + 'C': [0.2248, -0.1349, 1.349], |
| 29 | + 'B': [-1.5738, 0.6745, 0.0], 'E': [1.1242, 0.6745, 0.0], |
| 30 | + 'D': [-0.2248, -0.9443, -2], # last val should be -2 bc of thresholding |
| 31 | + 'F': [0.6745, 0.1349, 0.0]})) |
| 32 | + |
| 33 | + # test diff_method assertion |
| 34 | + with self.assertRaises(AssertionError) as e: |
| 35 | + diff_gctoo.diff_gctoo(test_gctoo, plate_control=True, diff_method="robust_zs") |
| 36 | + self.assertIn("diff_method: robust_zs", str(e.exception)) |
| 37 | + |
| 38 | + # test median norm |
| 39 | + pc_median_normed_df = diff_gctoo.diff_gctoo(test_gctoo, diff_method="median_norm") |
| 40 | + self.assertEqual(pc_median_normed_df.data_df.iloc[0, 0], -1.5) |
| 41 | + self.assertEqual(pc_median_normed_df.data_df.loc[2, "B"], 0) |
| 42 | + |
| 43 | + def test_diff_gctoo_vc(self): |
| 44 | + vc_zscores1 = diff_gctoo.diff_gctoo(test_gctoo, plate_control=False) |
| 45 | + vc_zscores2 = diff_gctoo.diff_gctoo(test_gctoo, plate_control=False, |
| 46 | + group_field='pert_iname', |
| 47 | + group_val='DMSO') |
23 | 48 | self.assertTrue(vc_zscores1.data_df.shape == (3, 6)) |
24 | 49 | self.assertTrue(vc_zscores2.data_df.shape == (3, 6)) |
25 | | - pd.util.testing.assert_frame_equal(pc_zscores.data_df, pd.DataFrame({'A': [-0.6745, -0.9443, -1.349], |
26 | | - 'C': [0.2248, -0.1349, 1.349], |
27 | | - 'B': [-1.5738, 0.6745, 0.0], 'E': [1.1242, 0.6745, 0.0], |
28 | | - 'D': [-0.2248, -0.9443, -2.2483], |
29 | | - 'F': [0.6745, 0.1349, 0.0]}, index=[0,1,2])) |
30 | | - |
31 | | - pd.util.testing.assert_frame_equal(vc_zscores1.data_df, pd.DataFrame({'A': [-4.7214, -3.3725, -10.0], |
32 | | - 'C': [-2.0235, -1.349, 10.0], |
33 | | - 'B': [-7.4194, 0.6745, 0.0], |
34 | | - 'E': [0.6745, 0.6745, 0.0], |
35 | | - 'D': [-3.3725, -3.3725, -10.0], |
36 | | - 'F': [-0.6745, -0.6745, 0.0]}, |
37 | | - index=[0,1,2])) |
38 | | - |
39 | | - pd.util.testing.assert_frame_equal(vc_zscores2.data_df, pd.DataFrame({'A': [-2.0235, -0.6745, -0.3372], |
40 | | - 'C': [0.6745, 0.6745, 0.6745], |
41 | | - 'B': [-4.7214, 2.0235, 0.1686], |
42 | | - 'E': [3.3725, 2.0235, 0.1686], |
43 | | - 'D': [-0.6745, -0.6745, -0.6745], |
44 | | - 'F': [2.0235, 1.1242, 0.1686]}, |
45 | | - index=[0,1,2])) |
| 50 | + |
| 51 | + pd.util.testing.assert_frame_equal(vc_zscores1.data_df, pd.DataFrame( |
| 52 | + {'A': [-4.7214, -3.3725, -10.0], # check for thresholding |
| 53 | + 'C': [-2.0235, -1.349, 10.0], |
| 54 | + 'B': [-7.4194, 0.6745, 0.0], |
| 55 | + 'E': [0.6745, 0.6745, 0.0], |
| 56 | + 'D': [-3.3725, -3.3725, -10.0], |
| 57 | + 'F': [-0.6745, -0.6745, 0.0]})) |
| 58 | + |
| 59 | + pd.util.testing.assert_frame_equal(vc_zscores2.data_df, pd.DataFrame( |
| 60 | + {'A': [-2.0235, -0.6745, -0.3372], |
| 61 | + 'C': [0.6745, 0.6745, 0.6745], |
| 62 | + 'B': [-4.7214, 2.0235, 0.1686], |
| 63 | + 'E': [3.3725, 2.0235, 0.1686], |
| 64 | + 'D': [-0.6745, -0.6745, -0.6745], |
| 65 | + 'F': [2.0235, 1.1242, 0.1686]})) |
| 66 | + |
| 67 | + # test group_val assertion |
| 68 | + with self.assertRaises(AssertionError) as e: |
| 69 | + diff_gctoo.diff_gctoo(test_gctoo, plate_control=False, group_val="dmso") |
| 70 | + self.assertIn("dmso not present", str(e.exception)) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + setup_logger.setup(verbose=True) |
| 75 | + unittest.main() |
| 76 | + |
0 commit comments