This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_core_diversity_analyses.py
executable file
·241 lines (212 loc) · 9.36 KB
/
test_core_diversity_analyses.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
# File created on 09 Feb 2013
from __future__ import division
__author__ = "Greg Caporaso"
__copyright__ = "Copyright 2011, The QIIME project"
__credits__ = ["Greg Caporaso"]
__license__ = "GPL"
__version__ = "1.9.1-dev"
__maintainer__ = "Greg Caporaso"
__email__ = "gregcaporaso@gmail.com"
import sys
from StringIO import StringIO
from shutil import rmtree
from os.path import exists
from tempfile import mkdtemp
from skbio.util import remove_files
from unittest import TestCase, main
from qiime.util import (get_qiime_temp_dir,
load_qiime_config)
from qiime.parse import (parse_qiime_parameters)
from qiime.test import (initiate_timeout,
disable_timeout,
get_test_data_fps)
from qiime.workflow.util import (no_status_updates)
from qiime.workflow.core_diversity_analyses import run_core_diversity_analyses
class CoreDiversityAnalysesTests(TestCase):
def setUp(self):
self.files_to_remove = []
self.dirs_to_remove = []
# Create example output directory
tmp_dir = get_qiime_temp_dir()
self.test_out = mkdtemp(dir=tmp_dir,
prefix='core_qiime_analyses_test_',
suffix='')
self.dirs_to_remove.append(self.test_out)
# Get input data
self.test_data = get_test_data_fps()
self.qiime_config = load_qiime_config()
self.qiime_config['jobs_to_start'] = 2
self.qiime_config['seconds_to_sleep'] = 1
# suppress stderr during tests (one of the systems calls in the
# workflow prints a warning, and we can't suppress that warning with
# warnings.filterwarnings) here because it comes from within the code
# executed through the system call. Found this trick here:
# http://stackoverflow.com/questions/9949633/suppressing-print-as-stdout-python
self.saved_stderr = sys.stderr
sys.stderr = StringIO()
# Define number of seconds a test can run for before timing out
# and failing
initiate_timeout(600)
def tearDown(self):
disable_timeout()
# reset sys.stderr
sys.stderr = self.saved_stderr
remove_files(self.files_to_remove)
# remove directories last, so we don't get errors
# trying to remove files which may be in the directories
for d in self.dirs_to_remove:
if exists(d):
rmtree(d)
def test_run_core_diversity_analyses_error_on_invalid_category(self):
"""run_core_diversity_analyses: error raised on invalid categories
"""
# too few values in 'month' category
self.assertRaises(ValueError,
run_core_diversity_analyses,
self.test_data['biom'][0],
self.test_data['map'][0],
20,
output_dir=self.test_out,
params=parse_qiime_parameters({}),
qiime_config=self.qiime_config,
categories=['SampleType', 'month'],
tree_fp=self.test_data['tree'][0],
parallel=False,
status_update_callback=no_status_updates)
# invalid category name
self.assertRaises(ValueError,
run_core_diversity_analyses,
self.test_data['biom'][0],
self.test_data['map'][0],
20,
output_dir=self.test_out,
params=parse_qiime_parameters({}),
qiime_config=self.qiime_config,
categories=['not-a-real-category'],
tree_fp=self.test_data['tree'][0],
parallel=False,
status_update_callback=no_status_updates)
def test_run_core_diversity_analyses(self):
"""run_core_diversity_analyses functions with categories
"""
run_core_diversity_analyses(
self.test_data['biom'][0],
self.test_data['map'][0],
20,
output_dir=self.test_out,
params=parse_qiime_parameters({}),
qiime_config=self.qiime_config,
categories=['SampleType', 'days_since_epoch'],
tree_fp=self.test_data['tree'][0],
parallel=False,
status_update_callback=no_status_updates)
# Basic sanity test that output directories and files exist
fps = [
'%s/bdiv_even20' % self.test_out,
'%s/arare_max20' % self.test_out,
'%s/taxa_plots' % self.test_out,
'%s/bdiv_even20/unweighted_unifrac_dm.txt' % self.test_out,
'%s/bdiv_even20/weighted_unifrac_pc.txt' % self.test_out,
'%s/arare_max20/compare_chao1/days_since_epoch_stats.txt' % self.test_out,
'%s/arare_max20/compare_PD_whole_tree/SampleType_boxplots.pdf' % self.test_out,
'%s/index.html' % self.test_out,
'%s/table_mc%d.biom.gz' % (self.test_out, 20)
]
for fp in fps:
self.assertTrue(exists(fp))
def test_run_core_diversity_analyses_parallel(self):
"""run_core_diversity_analyses functions with categories in parallel
"""
run_core_diversity_analyses(
self.test_data['biom'][0],
self.test_data['map'][0],
20,
output_dir=self.test_out,
params=parse_qiime_parameters({}),
arare_num_steps=3,
qiime_config=self.qiime_config,
categories=['SampleType', 'days_since_epoch'],
tree_fp=self.test_data['tree'][0],
parallel=True,
status_update_callback=no_status_updates)
# Basic sanity test that output directories and files exist
fps = [
'%s/bdiv_even20' % self.test_out,
'%s/arare_max20' % self.test_out,
'%s/taxa_plots' % self.test_out,
'%s/bdiv_even20/unweighted_unifrac_dm.txt' % self.test_out,
'%s/bdiv_even20/weighted_unifrac_pc.txt' % self.test_out,
'%s/arare_max20/compare_chao1/days_since_epoch_stats.txt' % self.test_out,
'%s/arare_max20/compare_PD_whole_tree/SampleType_boxplots.pdf' % self.test_out,
'%s/index.html' % self.test_out,
'%s/table_mc%d.biom.gz' % (self.test_out, 20)
]
for fp in fps:
self.assertTrue(exists(fp))
def test_run_core_diversity_analyses_no_categories(self):
"""run_core_diversity_analyses functions without categories
"""
# this takes a long time, so use a longer sigalrm
run_core_diversity_analyses(
self.test_data['biom'][0],
self.test_data['map'][0],
20,
output_dir=self.test_out,
params=parse_qiime_parameters({}),
qiime_config=self.qiime_config,
categories=None,
tree_fp=self.test_data['tree'][0],
parallel=False,
status_update_callback=no_status_updates)
# Basic sanity test that output directories and files exist
fps = [
'%s/bdiv_even20' % self.test_out,
'%s/arare_max20' % self.test_out,
'%s/taxa_plots' % self.test_out,
'%s/bdiv_even20/unweighted_unifrac_dm.txt' % self.test_out,
'%s/bdiv_even20/weighted_unifrac_pc.txt' % self.test_out,
'%s/index.html' % self.test_out,
'%s/table_mc%d.biom.gz' % (self.test_out, 20)
]
for fp in fps:
self.assertTrue(exists(fp))
# categorical output files don't exist
self.assertFalse(exists(
'%s/arare_max20/compare_chao1/days_since_epoch_stats.txt' % self.test_out))
self.assertFalse(exists(
'%s/arare_max20/compare_PD_whole_tree/SampleType_boxplots.pdf' % self.test_out))
def test_run_core_diversity_analyses_no_tree(self):
"""run_core_diversity_analyses functions without tree
"""
# this takes a long time, so use a longer sigalrm
run_core_diversity_analyses(
self.test_data['biom'][0],
self.test_data['map'][0],
20,
output_dir=self.test_out,
params=parse_qiime_parameters(
['beta_diversity:metrics bray_curtis',
'alpha_diversity:metrics observed_species,chao1']),
qiime_config=self.qiime_config,
categories=['SampleType'],
tree_fp=None,
parallel=False,
status_update_callback=no_status_updates)
# Basic sanity test that output directories and files exist
fps = [
'%s/bdiv_even20' % self.test_out,
'%s/arare_max20' % self.test_out,
'%s/taxa_plots' % self.test_out,
'%s/bdiv_even20/bray_curtis_dm.txt' % self.test_out,
'%s/arare_max20/compare_observed_species/SampleType_boxplots.pdf' % self.test_out,
'%s/index.html' % self.test_out,
'%s/table_mc%d.biom.gz' % (self.test_out, 20)
]
for fp in fps:
self.assertTrue(exists(fp))
# phylogenetic diversity output files do not exist
self.assertFalse(exists(
'%s/bdiv_even20/unweighted_unifrac_dm.txt' % self.test_out))
if __name__ == "__main__":
main()