This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_plot_rank_abundance_graph.py
executable file
·179 lines (142 loc) · 6.52 KB
/
test_plot_rank_abundance_graph.py
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
175
176
177
178
179
#!/usr/bin/env python
# File created on 17 Aug 2010
from __future__ import division
__author__ = "Jens Reeder"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["Jens Reeder", "Greg Caporaso"]
__license__ = "GPL"
__version__ = "1.9.1"
__maintainer__ = "Justin Kuczynski"
__email__ = "justinak@gmail.com"
from os import close
from os.path import exists, abspath
from shutil import rmtree
from numpy import array
from matplotlib.axes import Subplot
from tempfile import mkstemp, mkdtemp
from unittest import TestCase, main
from skbio.util import remove_files
from qiime.plot_rank_abundance_graph import make_sorted_frequencies,\
plot_rank_abundance_graph, plot_rank_abundance_graphs
from qiime.util import create_dir
from biom.parse import parse_biom_table
class PlotRankAbundance(TestCase):
def setUp(self):
self.tmp_dir = '/tmp/'
self.files_to_remove = []
self._dirs_to_remove = []
def tearDown(self):
remove_files(self.files_to_remove)
if self._dirs_to_remove:
for i in self._dirs_to_remove:
rmtree(i)
def test_make_sorted_frequencies(self):
"""make_sorted_frequencies transforms and sorts correctly"""
# works on empty
counts = array([])
self.assertItemsEqual(make_sorted_frequencies(counts), [])
# works on zeros
counts = array([0, 0, 0, 0, 0, 0])
self.assertItemsEqual(make_sorted_frequencies(counts), [])
# works on flat data
counts = array([3, 3, 3, 3, 3])
expected_freqs = [0.2, 0.2, 0.2, 0.2, 0.2]
observed_freqs = make_sorted_frequencies(counts)
self.assertItemsEqual(observed_freqs, expected_freqs)
# works on real data
counts = array([1, 2, 0, 1, 0, 2, 4])
expected_freqs = [0.4, 0.2, 0.2, 0.1, 0.1]
observed_freqs = make_sorted_frequencies(counts)
self.assertItemsEqual(observed_freqs, expected_freqs)
def test_make_sorted_frequencies_abolute(self):
"""make_sorted_frequencies returns correct absolute values"""
# works on empty
counts = array([])
self.assertItemsEqual(make_sorted_frequencies(counts, True), [])
# works on zeros
counts = array([0, 0, 0, 0, 0, 0])
self.assertItemsEqual(make_sorted_frequencies(counts, True), [])
# works on flat data
counts = array([3, 3, 3, 3, 3])
expected_freqs = [3, 3, 3, 3, 3]
observed_freqs = make_sorted_frequencies(counts, True)
self.assertItemsEqual(observed_freqs, expected_freqs)
# works o real data
counts = array([1, 2, 0, 1, 0, 2, 4])
expected_freqs = [4, 2, 2, 1, 1]
observed_freqs = make_sorted_frequencies(counts, True)
self.assertItemsEqual(observed_freqs, expected_freqs)
def test_plot_rank_abundance_graph(self):
"""plot_rank_abudance_graph plots something"""
counts = array([20, 15, 12, 8, 4, 2, 1, 3, 1, 2])
observed = plot_rank_abundance_graph(counts)
# can we test something more clever here?
# basically just tests that something is drawn, but not what
self.assertEqual(type(observed), Subplot)
def test_plot_rank_abundance_graphs_filetype(self):
"""plot_rank_abundance_graphs works with all filetypes"""
self.otu_table = parse_biom_table(otu_table_sparse)
self.dir = mkdtemp(dir=self.tmp_dir,
prefix="test_plot_rank_abundance",
suffix="/")
self._dirs_to_remove.append(self.dir)
# test all supported filetypes
for file_type in ['pdf', 'svg', 'png', 'eps']:
tmp_file = abspath(self.dir + "rank_abundance_cols_0." + file_type)
plot_rank_abundance_graphs(
tmp_file,
'S3',
self.otu_table,
file_type=file_type)
self.files_to_remove.append(tmp_file)
self.assertTrue(exists(tmp_file))
def test_plot_rank_abundance_graphs(self):
"""plot_rank_abundance_graphs works with any number of samples (Table)"""
self.otu_table = parse_biom_table(otu_table_sparse)
self.dir = mkdtemp(dir=self.tmp_dir,
prefix="test_plot_rank_abundance",
suffix="/")
self._dirs_to_remove.append(self.dir)
fd, tmp_fname = mkstemp(dir=self.dir)
close(fd)
# test empty sample name
self.assertRaises(
ValueError, plot_rank_abundance_graphs, tmp_fname, '',
self.otu_table)
# test invalid sample name
self.assertRaises(ValueError, plot_rank_abundance_graphs, tmp_fname,
'Invalid_sample_name',
self.otu_table)
# test with two samples
file_type = "pdf"
tmp_file = abspath(self.dir + "rank_abundance_cols_0_2." + file_type)
plot_rank_abundance_graphs(tmp_file, 'S3,S5', self.otu_table,
file_type=file_type)
self.assertTrue(exists(tmp_file))
self.files_to_remove.append(tmp_file)
# test with all samples
tmp_file = abspath(self.dir + "rank_abundance_cols_0_1_2." + file_type)
plot_rank_abundance_graphs(tmp_file, '*', self.otu_table,
file_type=file_type)
self.files_to_remove.append(tmp_file)
self.assertTrue(exists(tmp_file))
otu_table_sparse = ('{"rows": [{"id": "0", "metadata": '
'{"taxonomy": ["Root", "Bacteria"]}}, {"id": "3", '
'"metadata": {"taxonomy": ["Root", "Bacteria", "Acidobacteria"]}}, '
'{"id": "4", "metadata": '
'{"taxonomy": ["Root", "Bacteria", "Bacteroidetes"]}}, '
'{"id": "2", "metadata": {"taxonomy": '
'["Root", "Bacteria", "Acidobacteria", "Acidobacteria", "Gp5"]}}, '
'{"id": "6", "metadata": {"taxonomy": ["Root", "Archaea"]}}], '
'"format": "Biological Observation Matrix v0.9", "data": '
'[[0, 0, 1.0], [0, 2, 1.0], [1, 0, 2.0], [1, 2, 1.0], [2, 0, 1.0], '
'[2, 2, 9.0], [3, 0, 1.0], [3, 2, 1.0], [4, 0, 1.0], [4, 1, 25.0], '
'[4, 2, 42.0]], "columns": [{"id": "S3", "metadata": null}, '
'{"id": "S4", "metadata": null}, {"id": "S5", "metadata": null}], '
'"generated_by": "QIIME 1.4.0-dev, svn revision 2571", "matrix_type": '
'"sparse", "shape": [5, 3], '
'"format_url": "http://www.qiime.org/svn_documentation/documentation/'
'biom_format.html", "date": "2011-12-21T19:33:37.780300", '
'"type": "OTU table", "id": null, "matrix_element_type": "float"}')
if __name__ == "__main__":
main()