Merge pull request #350 from taoliu/fix_test_signal_py38_dev
[MACS.git] / test / test_Pileup.py
blob4983eb0b5f1de7e73fef5647b44551d319d9e342
1 #!/usr/bin/env python
2 # Time-stamp: <2019-09-20 15:06:44 taoliu>
4 """Module Description: Test functions for pileup functions.
6 This code is free software; you can redistribute it and/or modify it
7 under the terms of the BSD License (see the file LICENSE included with
8 the distribution).
9 """
11 import unittest
13 from math import log10
14 from MACS2.Pileup import *
15 from MACS2.IO.FixWidthTrack import FWTrack
17 # ------------------------------------
18 # Main function
19 # ------------------------------------
21 class Test_pileup(unittest.TestCase):
22 """Unittest for pileup_bdg() in Pileup.pyx.
24 """
25 def setUp(self):
26 self.maxDiff = None
27 self.chrom = b"chr1"
28 self.plus_pos = ( 0, 1, 3, 4, 5 )
29 self.minus_pos = ( 5, 6, 8, 9, 10 )
30 self.d = 5
31 self.scale_factor = 0.5
32 self.expect = [ ( 0, 1, 1.0 ),
33 ( 1, 3, 2.0 ),
34 ( 3, 4, 3.0 ),
35 ( 4, 6, 4.0 ),
36 ( 6, 8, 3.0 ),
37 ( 8, 9, 2.0 ),
38 ( 9, 10, 1.0 )
40 self.expect2 = [(0, 1, 13.0),
41 (1, 3, 14.0),
42 (3, 4, 16.0),
43 (4, 6, 18.0),
44 (6, 8, 16.0),
45 (8, 9, 14.0),
46 (9, 10, 13.0)]
48 self.d_s = [ 5, 10, 100 ]
49 self.scale_factor_s = [ 0.5, 1, 2 ]
51 def test_pileup(self):
52 # build FWTrackII
53 self.fwtrack2 = FWTrack()
54 for i in self.plus_pos:
55 self.fwtrack2.add_loc(self.chrom, i, 0)
56 for i in self.minus_pos:
57 self.fwtrack2.add_loc(self.chrom, i, 1)
58 self.fwtrack2.finalize()
60 self.pileup = unified_pileup_bdg(self.fwtrack2, self.d, self.scale_factor, halfextension=False)
61 self.result = []
62 chrs = self.pileup.get_chr_names()
63 for chrom in chrs:
64 (p,v) = self.pileup.get_data_by_chr(chrom)
65 pnext = iter(p).__next__
66 vnext = iter(v).__next__
67 pre = 0
68 for i in range(len(p)):
69 pos = pnext()
70 value = vnext()
71 self.result.append( (pre,pos,value) )
72 pre = pos
73 # check result
74 self.assertEqual(self.result, self.expect)
76 def test_pileup_w_multiple_d_bdg ( self ):
77 # build FWTrackII
78 self.fwtrack2 = FWTrack(fw=5)
79 for i in self.plus_pos:
80 self.fwtrack2.add_loc(self.chrom, i, 0)
81 for i in self.minus_pos:
82 self.fwtrack2.add_loc(self.chrom, i, 1)
83 self.fwtrack2.finalize()
84 # pileup test
85 self.pileup = unified_pileup_bdg(self.fwtrack2, self.d_s, self.scale_factor_s, baseline_value=13, halfextension=False)
86 self.result = []
87 chrs = self.pileup.get_chr_names()
88 for chrom in chrs:
89 (p,v) = self.pileup.get_data_by_chr(chrom)
90 pnext = iter(p).__next__
91 vnext = iter(v).__next__
92 pre = 0
93 for i in range(len(p)):
94 pos = pnext()
95 value = vnext()
96 self.result.append( (pre,pos,value) )
97 pre = pos
98 # check result
99 self.assertEqual(self.result, self.expect2)