File: test_bedgraph.py

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (124 lines) | stat: -rw-r--r-- 5,631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
"""Unit tests for Mage format writer.
"""
from __future__ import division
from cogent.util.unit_test import TestCase, main
from cogent.util.table import Table
from cogent.format.bedgraph import get_header

__author__ = "Gavin Huttley"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__credits__ = ["Gavin Huttley"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Gavin Huttley"
__email__ = "gavin.huttley@anu.edu.au"
__status__ = "Production"

from cogent.util.unit_test import TestCase, main

class FormatBedgraph(TestCase):
    def test_only_required_columns(self):
        """generate bedgraph from minimal data"""
        table = Table(header=['chrom', 'start', 'end', 'value'],
                    rows=[['1', 100, i, 0] for i in range(101,111)] + \
                         [['1', 150, i, 10] for i in range(151,161)])
        
        bgraph = table.tostring(format='bedgraph', name='test track',
                    description='test of bedgraph', color=(255,0,0))
        self.assertTrue(bgraph,
            '\n'.join(['track type=bedGraph name="test track" '\
            +'description="test of bedgraph" color=255,0,0',
            '1\t100\t110\t0', '1\t150\t160\t10']))
    
    def test_merged_overlapping_spans(self):
        """bedgraph merged overlapping spans, one chrom"""
        rows = [['1', i, i+1, 0] for i in range(100, 121)] +\
                [['1', i, i+1, 10] for i in range(150, 161)]
        table = Table(header=['chrom', 'start', 'end', 'value'], rows=rows)
        
        bgraph = table.tostring(format='bedgraph', name='test track',
                    description='test of bedgraph', color=(255,0,0))
        self.assertTrue(bgraph,
            '\n'.join(['track type=bedGraph name="test track" '\
            +'description="test of bedgraph" color=255,0,0',
            '1\t100\t120\t0', '1\t150\t160\t10']))
    
    def test_merged_overlapping_spans_multichrom(self):
        """bedgraph merged overlapping spans, two crhoms"""
        rows = [['1', i, i+1, 0] for i in range(100, 121)] +\
                [['1', i, i+1, 10] for i in range(150, 161)]
        rows += [['2', i, i+1, 0] for i in range(100, 121)]
        table = Table(header=['chrom', 'start', 'end', 'value'], rows=rows)
        bgraph = table.tostring(format='bedgraph', name='test track',
                    description='test of bedgraph', color=(255,0,0))
        
        self.assertTrue(bgraph,
            '\n'.join(['track type=bedGraph name="test track" '\
            +'description="test of bedgraph" color=255,0,0',
            '1\t100\t120\t1', '1\t150\t160\t10', '2\t105\t120\t1',]))
    
    def test_invalid_args_fail(self):
        """incorrect bedgraph args causes RuntimeError"""
        rows = [['1', i, i+1, 0] for i in range(100, 121)] +\
                [['1', i, i+1, 10] for i in range(150, 161)]
        table = Table(header=['chrom', 'start', 'end', 'value'], rows=rows)
        
        self.assertRaises(RuntimeError, table.tostring,
            format='bedgraph', name='test track',
            description='test of bedgraph', color=(255,0,0), abc=None)
    
    def test_invalid_table_fails(self):
        """assertion error if table has > 4 columns"""
        rows = [['1', i, i+1, 0, 1] for i in range(100, 121)] +\
                [['1', i, i+1, 10, 1] for i in range(150, 161)]
        table = Table(header=['chrom', 'start', 'end', 'value', 'blah'],
                    rows=rows)
        
        self.assertRaises(AssertionError, table.tostring,
            format='bedgraph', name='test track',
            description='test of bedgraph', color=(255,0,0), abc=None)
    
    def test_boolean_correctly_formatted(self):
        """boolean setting correctly formatted"""
        rows = [['1', i, i+1, 0] for i in range(100, 121)] +\
                [['1', i, i+1, 10] for i in range(150, 161)]
        table = Table(header=['chrom', 'start', 'end', 'value'], rows=rows)
        
        bgraph = table.tostring(format='bedgraph', name='test track',
            description='test of bedgraph', color=(255,0,0), autoScale=True)
        
        self.assertTrue(bgraph,
            '\n'.join(['track type=bedGraph name="test track" '\
            +'description="test of bedgraph" color=255,0,0 autoScale=on',
            '1\t100\t110\t1', '1\t150\t160\t10']))
    
    def test_int_correctly_formatted(self):
        """int should be correctly formatted"""
        rows = [['1', i, i+1, 0] for i in range(100, 121)] +\
                [['1', i, i+1, 10] for i in range(150, 161)]
        table = Table(header=['chrom', 'start', 'end', 'value'], rows=rows)
        
        bgraph = table.tostring(format='bedgraph', name='test track',
            description='test of bedgraph', color=(255,0,0), smoothingWindow=10)
        
        self.assertTrue(bgraph,
            '\n'.join(['track type=bedGraph name="test track" '\
            +'description="test of bedgraph" color=255,0,0 smoothingWindow=10',
            '1\t100\t110\t1', '1\t150\t160\t10']))
        
    
    def test_raises_on_incorrect_format_val(self):
        """raise AssertionError when provide incorrect format value"""
        rows = [['1', i, i+1, 0] for i in range(100, 121)] +\
                [['1', i, i+1, 10] for i in range(150, 161)]
        table = Table(header=['chrom', 'start', 'end', 'value'], rows=rows)
        
        self.assertRaises(AssertionError, table.tostring,
            format='bedgraph', name='test track',
            description='test of bedgraph', color=(255,0,0),
            windowingFunction='sqrt')
    

if __name__ == '__main__':
    main()