File: test_scripts.py

package info (click to toggle)
python-pybedtools 0.8.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,140 kB
  • sloc: python: 9,589; cpp: 899; makefile: 149; sh: 116
file content (174 lines) | stat: -rw-r--r-- 5,831 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import pybedtools
from textwrap import dedent
from .tfuncs import setup_module, teardown_module
from pybedtools.scripts import annotate, venn_mpl, venn_gchart
import pytest
import subprocess as sp
import os
import sys


def test_annotate_main():
    # exits after printing help when sys.argv is not as it should be.
    orig_stderr = sys.stderr
    sys.stderr = open('annotmp','w')
    with pytest.raises(SystemExit):
        annotate.main()
    sys.stderr = orig_stderr
    os.unlink('annotmp')

def test_annotate_closest():
    a = pybedtools.example_bedtool('m1.bed')
    b = pybedtools.example_bedtool('mm9.bed12')
    c = annotate.add_closest(a, b)
    assert len(a) == len(c), (len(a), len(c), str(c))
    assert a.field_count() == c.field_count() - 2
    # in this test-case, the final column should be exon;intron
    # since m1 completely contains both an exon and an intron.
    f = next(iter(c))
    # waiting for fix to bedtools:
    #assert f.fields[-1] == "exon;intron", f.fields[-1]


def test_annotate_xstream():
    a = pybedtools.example_bedtool('m1.bed')
    b = pybedtools.example_bedtool('mm9.bed12')
    c = annotate.add_xstream(a, b, dist=1000, updown="up")
    assert a.field_count() == c.field_count() - 1
    assert len(a) == len(c)
    d = annotate.add_xstream(c, b, dist=1000, updown="down")
    assert a.field_count() == d.field_count() - 2


def test_venn_mpl():
    """
    compares output image to expected
    """
    try:
        import matplotlib
    except ImportError:
        import sys
        sys.stderr.write('Need matplotlib installed to test venn_mpl')
        return

    here = os.path.dirname(__file__)
    expected_fn = os.path.join(here, 'mpl-expected.png')

    original = pybedtools.example_bedtool('rmsk.hg18.chr21.small.bed').sort().merge()
    a = pybedtools.BedTool(original[:300]).saveas()
    b = pybedtools.BedTool(original[:20]).saveas().cat(pybedtools.BedTool(original[400:500]).saveas())
    c = pybedtools.BedTool(original[15:30]).saveas().cat(pybedtools.BedTool(original[450:650]).saveas())

    outfn = 'mplout.png'
    venn_mpl.venn_mpl(a=a.fn, b=b.fn, c=c.fn, colors=['r','b','g'], outfn=outfn, labels=['a','b','c'])

    # On a different machine, the created image is not visibly different but is
    # numerically different.  Not sure what a reasonable tolerance is, but this
    # seems to work for now....
    o = matplotlib.image.imread(outfn)
    e = matplotlib.image.imread(expected_fn)

    TOLERANCE = 200
    SUM = abs((o - e).sum())
    assert SUM < TOLERANCE, SUM

    os.unlink(outfn)

def test_venn_gchart_data_is_correct():
    original = pybedtools.example_bedtool('rmsk.hg18.chr21.small.bed').sort().merge()
    a = pybedtools.BedTool(original[:300]).saveas()
    b = pybedtools.BedTool(original[:20]).saveas().cat(pybedtools.BedTool(original[400:500]).saveas())
    c = pybedtools.BedTool(original[15:30]).saveas().cat(pybedtools.BedTool(original[450:650]).saveas())

    colors='00FF00,FF0000,0000FF'
    labels = 'a,b,c'

    expected_data = {'chco': '00FF00,FF0000,0000FF',
                     'chd': 't:1.0,0.4,0.7167,0.0667,0.05,0.1833,0.0167',
                     'chs': '300x300',
                     'cht': 'v',
                     'chdl': 'a|b|c'}

    data = venn_gchart.venn_gchart(a=a.fn,
                            b=b.fn,
                            c=c.fn,
                            colors=colors.split(','),
                            labels=labels.split(','),
                            size='300x300')

    for key in expected_data.keys():
        e = expected_data[key]
        o = data[key]
        assert e == o


@pytest.mark.skip(
    'Small differences between fonts in PNG are sometimes produced and need to be accounted for'
)
def test_venn_gchart_png_is_saved_correctly():

    here = os.path.dirname(__file__)
    expected = open(os.path.join(here, 'gchart-expected.png')).read()

    original = pybedtools.example_bedtool('rmsk.hg18.chr21.small.bed').sort().merge()
    a = pybedtools.BedTool(original[:300]).saveas()
    b = pybedtools.BedTool(original[:20]).saveas().cat(pybedtools.BedTool(original[400:500]).saveas())
    c = pybedtools.BedTool(original[15:30]).saveas().cat(pybedtools.BedTool(original[450:650]).saveas())

    colors='00FF00,FF0000,0000FF'
    outfn = 'gchart_out.png'
    labels = 'a,b,c'

    data = venn_gchart.venn_gchart(a=a.fn,
                            b=b.fn,
                            c=c.fn,
                            colors=colors.split(','),
                            labels=labels.split(','),
                            size='300x300')

    venn_gchart.gchart(data, outfn)
    assert_equal(open(outfn).read(), expected)
    os.unlink(outfn)

def test_venn_mpl_main():
    orig_stderr = sys.stderr
    sys.stderr = open('mpltmp','w')
    with pytest.raises(SystemExit):
        venn_mpl.main()
    sys.stderr = orig_stderr
    os.unlink('mpltmp')

def test_venn_gchart_main():
    orig_stderr = sys.stderr
    sys.stderr = open('gcharttmp','w')
    with pytest.raises(SystemExit):
        venn_gchart.main()
    sys.stderr = orig_stderr
    os.unlink('gcharttmp')


def test_intron_exon_reads():
    gff = pybedtools.example_filename('gdc.gff')
    bam = pybedtools.example_filename('gdc.bam')
    cmds = [
        'intron_exon_reads.py',
        '--gff', gff, '--bam', bam, '--processes', '2']
    out =  sp.check_output(cmds, universal_newlines=True)
    assert out == dedent(
        """\
        exon_only	3
        intron_only	3
        intron_and_exon	1
        """)


    cmds = [
        'intron_exon_reads.py',
        '--gff', gff, '--bam', bam, '--processes', '2', '--stranded']
    out =  sp.check_output(cmds, universal_newlines=True)
    assert out == dedent(
        """\
        exon_only	0
        intron_only	0
        intron_and_exon	0
        """)