Merge pull request #350 from taoliu/fix_test_signal_py38_dev
[MACS.git] / test / test_BedGraph.py
blob0021de166e2faa209a6f5b6eee041778c299ceb2
1 #!/usr/bin/env python
2 # Time-stamp: <2019-09-25 14:16:56 taoliu>
4 import unittest
6 from MACS2.IO.BedGraph import *
8 class Test_bedGraphTrackI_add_loc(unittest.TestCase):
10 def setUp(self):
11 self.test_regions1 = [(b"chrY",0,10593155,0.0),
12 (b"chrY",10593155,10597655,0.0066254580149)]
14 def test_add_loc1(self):
15 # make sure the shuffled sequence does not lose any elements
16 bdg = bedGraphTrackI()
17 for a in self.test_regions1:
18 bdg.add_loc(a[0],a[1],a[2],a[3])
20 #self.assertTrue( abs(result - expect) < 1e-5*result)
22 #self.assertEqual(result, expect)
24 #self.assertEqual(result, expect)
26 class Test_bedGraphTrackI_overlie(unittest.TestCase):
28 def setUp(self):
29 self.test_regions1 = [(b"chrY",0,70,0.00),
30 (b"chrY",70,80,7.00),
31 (b"chrY",80,150,0.00)]
32 self.test_regions2 = [(b"chrY",0,85,0.00),
33 (b"chrY",85,90,75.00),
34 (b"chrY",90,155,0.00)]
35 self.test_regions3 = [(b"chrY",0,75,20.0),
36 (b"chrY",75,90,35.0),
37 (b"chrY",90,150,10.0)]
38 self.test_overlie_max = [(b"chrY", 0, 75, 20.0), # max
39 (b"chrY", 75, 85, 35.0),
40 (b"chrY", 85, 90, 75.0),
41 (b"chrY", 90,150, 10.0),
42 (b"chrY",150,155, 0.0)]
43 self.test_overlie_mean = [(b"chrY", 0, 70, 6.66667), # mean
44 (b"chrY", 70, 75, 9),
45 (b"chrY", 75, 80, 14),
46 (b"chrY", 80, 85, 11.66667),
47 (b"chrY", 85, 90, 36.66667),
48 (b"chrY", 90,150, 3.33333),
49 (b"chrY",150,155, 0.0)]
50 self.test_overlie_fisher = [(b"chrY", 0, 70, (0,0,20), 92.10340371976183, 1.1074313239555578e-17, 16.9557 ), # fisher
51 (b"chrY", 70, 75, (7,0,20), 124.33959502167846, 1.9957116587802055e-24, 23.6999 ),
52 (b"chrY", 75, 80, (7,0,35), 193.41714781149986, 4.773982707347631e-39, 38.3211 ),
53 (b"chrY", 80, 85, (0,0,35), 161.1809565095832, 3.329003070922764e-32, 31.4777),
54 (b"chrY", 85, 90, (0,75,35), 506.56872045869005, 3.233076792862357e-106, 105.4904),
55 (b"chrY", 90,150, (0,0,10), 46.051701859880914, 2.8912075645386016e-08, 7.5389),
56 (b"chrY",150,155, (0,0,0), 0, 1.0, 0.0)]
58 self.bdg1 = bedGraphTrackI()
59 self.bdg2 = bedGraphTrackI()
60 self.bdg3 = bedGraphTrackI()
61 for a in self.test_regions1:
62 self.bdg1.safe_add_loc(a[0],a[1],a[2],a[3])
64 for a in self.test_regions2:
65 self.bdg2.safe_add_loc(a[0],a[1],a[2],a[3])
67 for a in self.test_regions3:
68 self.bdg3.safe_add_loc(a[0],a[1],a[2],a[3])
70 def assertEqual_float ( self, a, b, roundn = 4 ):
71 self.assertEqual( round( a, roundn ), round( b, roundn ) )
73 def test_overlie_max(self):
74 bdgb = self.bdg1.overlie([self.bdg2,self.bdg3], func="max")
76 chrom = b"chrY"
77 (p,v) = bdgb.get_data_by_chr(chrom)
78 pre = 0
79 for i in range(len(p)):
80 pos = p[i]
81 value = v[i]
82 self.assertEqual_float( self.test_overlie_max[i][1], pre )
83 self.assertEqual_float( self.test_overlie_max[i][2], pos )
84 self.assertEqual_float( self.test_overlie_max[i][3], value )
85 pre = pos
87 def test_overlie_mean(self):
88 bdgb = self.bdg1.overlie([self.bdg2,self.bdg3], func="mean")
90 chrom = b"chrY"
91 (p,v) = bdgb.get_data_by_chr(chrom)
92 pre = 0
93 for i in range(len(p)):
94 pos = p[i]
95 value = v[i]
96 self.assertEqual_float( self.test_overlie_mean[i][1], pre )
97 self.assertEqual_float( self.test_overlie_mean[i][2], pos )
98 self.assertEqual_float( self.test_overlie_mean[i][3], value )
99 pre = pos
101 def test_overlie_fisher(self):
102 bdgb = self.bdg1.overlie([self.bdg2,self.bdg3], func="fisher")
104 chrom = b"chrY"
105 (p,v) = bdgb.get_data_by_chr(chrom)
106 pre = 0
107 for i in range(len(p)):
108 pos = p[i]
109 value = v[i]
110 self.assertEqual_float( self.test_overlie_fisher[i][1], pre )
111 self.assertEqual_float( self.test_overlie_fisher[i][2], pos )
112 self.assertEqual_float( self.test_overlie_fisher[i][6], value )
113 pre = pos