2 # Time-stamp: <2021-03-10 18:55:41 Tao Liu>
6 from MACS3
.IO
.BAM
import *
8 class Test_BAIFile ( unittest
.TestCase
):
11 self
.bamfile
= "test/tiny.bam"
12 self
.baifile
= "test/tiny.bam.bai"
13 self
.bai
= BAIFile( self
.baifile
)
15 def test_load ( self
):
16 expected
= {'ref_beg': 50724864, 'ref_end': 2709258240, 'n_mapped': 971, 'n_unmapped': 0}
17 meta
= self
.bai
.get_metadata_by_refseq( 0 )
18 self
.assertDictEqual( expected
, meta
)
20 def test_get_chunks_bin ( self
):
21 expected
= [(2178362419, 2178363422)]
22 chunks
= self
.bai
.get_chunks_by_bin( 0, 4728 )
23 self
.assertListEqual( chunks
, expected
)
25 def test_get_chunks_list_bins ( self
):
26 expected
= [(50741975, 50751615), (2178362419, 2178363422), (2178363422, 2709258240)]
27 chunks
= self
.bai
.get_chunks_by_list_of_bins( 0, [ 591, 4694, 4728] )
28 self
.assertListEqual( chunks
, expected
)
30 def test_get_chunks_region ( self
):
31 expected
= [(1130775424, 1130820164), (1130820164, 1130822629), (1130822629, 2178352280), (2178352280, 2178353973), (2178353973, 2178356807), (2178356807, 2178358139), (2178358139, 2178359954), (2178359954, 2178362419), (2178362419, 2178363422), (2178363422, 2709258240)]
32 chunks
= self
.bai
.get_chunks_by_region( 0, 557000, 851600 )
33 self
.assertListEqual( chunks
, expected
)
35 def test_get_chunks_list_regions ( self
):
36 expected
= [(1130770109, 1130772126), (1130772126, 1130775424), (1130775424, 1130820164), (2178363422, 2709258240)]
37 chunks
= self
.bai
.get_chunks_by_list_of_regions( 0, [ (500000, 600000), (800000, 850000) ] )
38 self
.assertListEqual( chunks
, expected
)
41 class Test_BAM_w_BAI ( unittest
.TestCase
):
44 self
.bamfile
= "test/tiny.bam"
45 self
.baifile
= "test/tiny.bam.bai"
46 self
.bam
= BAMaccessor( self
.bamfile
)
48 def test_get_reads_large_dedup ( self
):
52 a
= self
.bam
.get_reads_in_region( c
, s
, e
, maxDuplicate
= 1 )
53 self
.assertEqual( len(a
), 941 )
55 def test_get_reads_large ( self
):
59 a
= self
.bam
.get_reads_in_region( c
, s
, e
, maxDuplicate
= 100 )
60 self
.assertEqual( len(a
), 971 )
62 def test_get_reads_mod ( self
):
66 a
= self
.bam
.get_reads_in_region( c
, s
, e
, maxDuplicate
= 100 )
67 self
.assertEqual( len(a
), 520 )
69 def test_get_reads_few( self
):
73 expected
= """chr10 145930 145966 MAGNUM:8:93:1052:1153#0 36 +
74 chr10 148133 148169 MAGNUM:8:108:19082:3782#0 36 -
75 chr10 148930 148966 MAGNUM:8:34:18198:13544#0 36 +
76 chr10 152575 152611 ROCKFORD:1:114:13238:15292#0 36 +
77 chr10 152927 152963 ROCKFORD:1:21:4055:6592#0 36 -
78 chr10 153995 154031 MAGNUM:8:44:12716:12267#0 36 +
79 chr10 156153 156189 MAGNUM:8:74:16381:18650#0 36 -
80 chr10 159259 159295 ROCKFORD:1:112:5095:4754#0 36 +"""
81 a
= self
.bam
.get_reads_in_region( c
, s
, e
, maxDuplicate
= 100 )
82 result
= "\n".join( map( str, a
) )
83 self
.assertEqual( len(a
), 8 )
84 self
.assertEqual( result
, expected
)
86 def test_get_empty1 ( self
):
87 # region outside of bams
91 a
= self
.bam
.get_reads_in_region( c
, s
, e
)
92 self
.assertEqual( len(a
), 0 )
94 def test_get_empty2 ( self
):
95 # small region missed in bam
99 a
= self
.bam
.get_reads_in_region( c
, s
, e
)
100 self
.assertEqual( len(a
), 0 )