This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_make_otu_network.py
333 lines (281 loc) · 17.6 KB
/
test_make_otu_network.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
#file test_make_otu_network.py
__author__ = "Julia Goodrich"
__copyright__ = "Copyright 2011, The QIIME Project" #consider project name
__credits__ = ["Julia Goodrich", "Jose Carlos Clemente Litran"] #remember to add yourself
__license__ = "GPL"
__version__ = "1.5.0"
__maintainer__ = "Jose Clemente"
__email__ = "jose.clemente@gmail.com"
__status__ = "Release"
from os import remove
from os.path import exists
import os
import shutil
from numpy import array
from random import choice, randrange
from cogent.util.unit_test import TestCase, main
from cogent.util.misc import get_random_directory_name
from cogent.maths.stats.test import G_2_by_2
from qiime.make_otu_network import get_sample_info, get_connection_info, \
get_num_con_cat,get_num_cat,make_table_file,make_stats_files,\
make_props_files
from qiime.util import get_tmp_filename, load_qiime_config
from qiime.format import format_biom_table
from biom.table import table_factory
class OtuNetworkTests(TestCase):
def setUp(self):
self.qiime_config = load_qiime_config()
self.tmp_dir = self.qiime_config['temp_dir'] or '/tmp/'
self.map_file = """#SampleID Day time Description
#This is some comment about the study
1 090809 1200 some description of sample1
2 090809 1800 some description of sample2
3 090909 1200 some description of sample3
4 090909 1800 some description of sample4
5 091009 1200 some description of sample5"""
self.cat_by_sample = {"1":[("Day","090809"),("time","1200")],
"2":[("Day","090809"),("time","1800")],
"3":[("Day","090909"),("time","1200")],
"4":[("Day","090909"),("time","1800")],
"5":[("Day","091009"),("time","1200")]}
self.sample_by_cat = {("Day","090809"):["1","2"],
("Day","090909"):["3","4"],
("Day","091009"):["5"],
("time","1200"):["1","3","5"],
("time","1800"):["2","4"]}
self.num_cats = 2
self.meta_dict = {"1":["090809 1200",0],
"2":["090809 1800",0],
"3":["090909 1200",0],
"4":["090909 1800",0],
"5":["091009 1200",0]}
self.labels = ["from","to","eweight","consensus_lin","Day","time"]
self.node_labels = ["node_name", "node_disp_name", "ntype","degree", \
"weighted_degree","consensus_lin","Day","time"]
self.label_list =[["090809","090909","091009"],["1200","1800"]]
self.otu_table_vals = array([[0,1,0,0,6],
[2,0,0,0,0],
[0,0,3,1,0],
[0,0,0,0,5],
[0,4,2,0,0],
[3,6,0,0,0],
[0,0,4,2,0],
[0,0,0,0,3],
[2,0,0,5,0],
[0,2,0,4,0]])
otu_table_str = format_biom_table(table_factory(self.otu_table_vals,
['1', '2', '3', '4', '5'],
['otu_1', 'otu_2', 'otu_3', 'otu_4', 'otu_5', 'otu_6', 'otu_7', 'otu_8', 'otu_9', 'otu_10'],
[None, None, None, None, None],
[{"taxonomy": ["Bacteria", "Actinobacteria", "Coriobacteridae"]},
{"taxonomy": ["Bacteria", "Bacteroidetes", "Bacteroidales", "Bacteroidaceae"]},
{"taxonomy": ["Bacteria", "Firmicutes", "Clostridia", "Clostridiales"]},
{"taxonomy": ["Bacteria", "Spirochaetes", "Spirochaetales", "Spirochaetaceae"]},
{"taxonomy": ["Bacteria", "Bacteroidetes", "Bacteroidales", "Rikenellaceae"]},
{"taxonomy": ["Bacteria", "Bacteroidetes", "Bacteroidales", "Dysgonomonaceae"]},
{"taxonomy": ["Bacteria", "Bacteroidetes", "Bacteroidales", "Odoribacteriaceae"]},
{"taxonomy": ["Bacteria", "Bacteroidetes", "Bacteroidales", "Dysgonomonaceae", "otu_425"]},
{"taxonomy": ["Bacteria", "Bacteroidetes", "Bacteroidales", "Dysgonomonaceae", "otu_425"]},
{"taxonomy": ["Bacteria", "Firmicutes", "Mollicutes", "Clostridium_aff_innocuum_CM970"]}]))
self.otu_table_fp = get_tmp_filename(tmp_dir=self.tmp_dir,
prefix='test_make_otu_network_otu_table',suffix='.biom')
open(self.otu_table_fp,'w').write(otu_table_str)
self.otu_sample_file = """#Full OTU Counts
#OTU ID 1 2 3 4 5 Consensus Lineage
otu_1 0 1 0 0 6 Bacteria; Actinobacteria; Coriobacteridae
otu_2 2 0 0 0 0 Bacteria; Bacteroidetes; Bacteroidales; Bacteroidaceae
otu_3 0 0 3 1 0 Bacteria; Firmicutes; Clostridia; Clostridiales
otu_4 0 0 0 0 5 Bacteria; Spirochaetes; Spirochaetales; Spirochaetaceae
otu_5 0 4 2 0 0 Bacteria; Bacteroidetes; Bacteroidales; Rikenellaceae
otu_6 3 6 0 0 0 Bacteria; Bacteroidetes; Bacteroidales; Dysgonomonaceae
otu_7 0 0 4 2 0 Bacteria; Bacteroidetes; Bacteroidales; Odoribacteriaceae
otu_8 0 0 0 0 3 Bacteria; Bacteroidetes; Bacteroidales; Dysgonomonaceae; otu_425
otu_9 2 0 0 5 0 Bacteria; Bacteroidetes; Bacteroidales; Dysgonomonaceae; otu_425
otu_10 0 2 0 4 0 Bacteria; Firmicutes; Mollicutes; Clostridium_aff_innocuum_CM970"""
self.con_by_sample = {'1':set(['2','4']),'2':set(['5','3','1','4']),
'3':set(['4','2']),'4':set(['3','1','2']),
'5':set(['2'])}
self.edge_file_str =["2 otu_1 1.0 Bacteria:Actinobacteria:Coriobacteridae 090809 1800",\
"5 otu_1 6.0 Bacteria:Actinobacteria:Coriobacteridae 091009 1200",\
"1 otu_2 2.0 Bacteria:Bacteroidetes:Bacteroidales:Bacteroidaceae 090809 1200",\
"3 otu_3 3.0 Bacteria:Firmicutes:Clostridia:Clostridiales 090909 1200",\
"4 otu_3 1.0 Bacteria:Firmicutes:Clostridia:Clostridiales 090909 1800",\
"5 otu_4 5.0 Bacteria:Spirochaetes:Spirochaetales:Spirochaetaceae 091009 1200",\
"2 otu_5 4.0 Bacteria:Bacteroidetes:Bacteroidales:Rikenellaceae 090809 1800",\
"3 otu_5 2.0 Bacteria:Bacteroidetes:Bacteroidales:Rikenellaceae 090909 1200",\
"1 otu_6 3.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae 090809 1200",\
"2 otu_6 6.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae 090809 1800",\
"3 otu_7 4.0 Bacteria:Bacteroidetes:Bacteroidales:Odoribacteriaceae 090909 1200",\
"4 otu_7 2.0 Bacteria:Bacteroidetes:Bacteroidales:Odoribacteriaceae 090909 1800",\
"5 otu_8 3.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 091009 1200",\
"1 otu_9 2.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 090809 1200",\
"4 otu_9 5.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 090909 1800",\
"2 otu_10 2.0 Bacteria:Firmicutes:Mollicutes:Clostridium_aff_innocuum_CM970 090809 1800",\
"4 otu_10 4.0 Bacteria:Firmicutes:Mollicutes:Clostridium_aff_innocuum_CM970 090909 1800"]
self.node_file_str =["1 1 user_node 3 7.0 other 090809 1200",
"2 2 user_node 4 13.0 other 090809 1800",
"3 3 user_node 3 9.0 other 090909 1200",
"4 4 user_node 4 12.0 other 090909 1800",
"5 5 user_node 3 14.0 other 091009 1200",
"otu_1 otu_node 2 7.0 Bacteria:Actinobacteria:Coriobacteridae otu otu",
"otu_2 otu_node 1 2.0 Bacteria:Bacteroidetes:Bacteroidales:Bacteroidaceae otu otu",
"otu_3 otu_node 2 4.0 Bacteria:Firmicutes:Clostridia:Clostridiales otu otu",
"otu_4 otu_node 1 5.0 Bacteria:Spirochaetes:Spirochaetales:Spirochaetaceae otu otu",
"otu_5 otu_node 2 6.0 Bacteria:Bacteroidetes:Bacteroidales:Rikenellaceae otu otu",
"otu_6 otu_node 2 9.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae otu otu",
"otu_7 otu_node 2 6.0 Bacteria:Bacteroidetes:Bacteroidales:Odoribacteriaceae otu otu",
"otu_8 otu_node 1 3.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 otu otu",
"otu_9 otu_node 2 7.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 otu otu",
"otu_10 otu_node 2 6.0 Bacteria:Firmicutes:Mollicutes:Clostridium_aff_innocuum_CM970 otu otu"]
self.red_edge_file_str =["2 otu_1 1.0 Bacteria:Actinobacteria:Coriobacteridae 090809 1800",\
"5 otu_1 6.0 Bacteria:Actinobacteria:Coriobacteridae 091009 1200",\
"1 @1 1.0 missed 090809 1200",\
"3 otu_3 3.0 Bacteria:Firmicutes:Clostridia:Clostridiales 090909 1200",
"4 otu_3 1.0 Bacteria:Firmicutes:Clostridia:Clostridiales 090909 1800",
"5 @5 1.0 missed 091009 1200",
"2 otu_5 4.0 Bacteria:Bacteroidetes:Bacteroidales:Rikenellaceae 090809 1800",
"3 otu_5 2.0 Bacteria:Bacteroidetes:Bacteroidales:Rikenellaceae 090909 1200",
"1 otu_6 3.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae 090809 1200",
"2 otu_6 6.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae 090809 1800",
"3 otu_7 4.0 Bacteria:Bacteroidetes:Bacteroidales:Odoribacteriaceae 090909 1200",
"4 otu_7 2.0 Bacteria:Bacteroidetes:Bacteroidales:Odoribacteriaceae 090909 1800",
"1 otu_9 2.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 090809 1200",
"4 otu_9 5.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 090909 1800",
"2 otu_10 2.0 Bacteria:Firmicutes:Mollicutes:Clostridium_aff_innocuum_CM970 090809 1800",
"4 otu_10 4.0 Bacteria:Firmicutes:Mollicutes:Clostridium_aff_innocuum_CM970 090909 1800"]
self.red_node_file_str =["1 1 user_node 3 7.0 other 090809 1200",
"2 2 user_node 4 13.0 other 090809 1800",
"3 3 user_node 3 9.0 other 090909 1200",
"4 4 user_node 4 12.0 other 090909 1800",
"5 5 user_node 3 14.0 other 091009 1200",
"otu_1 otu_node 2 7.0 Bacteria:Actinobacteria:Coriobacteridae otu otu",
"@1 otu_collapsed 1 1.0 other otu otu",
"otu_3 otu_node 2 4.0 Bacteria:Firmicutes:Clostridia:Clostridiales otu otu",
"@5 otu_collapsed 2 2.0 other otu otu",
"otu_5 otu_node 2 6.0 Bacteria:Bacteroidetes:Bacteroidales:Rikenellaceae otu otu",
"otu_6 otu_node 2 9.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae otu otu",
"otu_7 otu_node 2 6.0 Bacteria:Bacteroidetes:Bacteroidales:Odoribacteriaceae otu otu",
"otu_9 otu_node 2 7.0 Bacteria:Bacteroidetes:Bacteroidales:Dysgonomonaceae:otu_425 otu otu",
"otu_10 otu_node 2 6.0 Bacteria:Firmicutes:Mollicutes:Clostridium_aff_innocuum_CM970 otu otu"]
self.otu_dc = {1:3,2:7}
self.sample_dc = {3:3,4:2}
self.degree_counts = {1:3,2:7,3:3,4:2}
self.num_con_cat = {"Day":2,
"time":1}
self.num_con = 6
self.num_cat = {"Day":2,
"time":4}
self.num_cat_less = {"Day":1,
"time":3}
self._paths_to_clean_up = [self.otu_table_fp]
self._dir_to_clean_up = ''
def tearDown(self):
map(remove,self._paths_to_clean_up)
if self._dir_to_clean_up != '':
shutil.rmtree(self._dir_to_clean_up)
def test_get_sample_info(self):
cat_by_sample, sample_by_cat, num_cats, meta_dict,labels,node_labels, \
label_list = get_sample_info(self.map_file.split('\n'))
self.assertEqual(cat_by_sample,self.cat_by_sample)
self.assertEqual(sample_by_cat,self.sample_by_cat)
self.assertEqual(num_cats,self.num_cats)
self.assertEqual(meta_dict,self.meta_dict)
self.assertEqual(labels,self.labels)
self.assertEqual(node_labels,self.node_labels)
self.assertEqual(label_list,self.label_list)
def test_get_connection_info(self):
con_by_sample, node_file_str, edge_file_str, red_node_file_str,\
red_edge_file_str,otu_dc, degree_counts,sample_dc = \
get_connection_info(self.otu_table_fp, self.num_cats,\
self.meta_dict)
# get_connection_info(self.otu_sample_file.split('\n'), self.num_cats,\
# self.meta_dict)
self.assertEqual(con_by_sample,self.con_by_sample)
self.assertEqual(set(node_file_str),set(self.node_file_str))
self.assertEqual(set(edge_file_str),set(self.edge_file_str))
self.assertEqual(set(red_node_file_str),set(self.red_node_file_str))
self.assertEqual(set(red_edge_file_str),set(self.red_edge_file_str))
self.assertEqual(otu_dc,self.otu_dc)
self.assertEqual(degree_counts,self.degree_counts)
self.assertEqual(sample_dc,self.sample_dc)
def test_get_num_con_cat(self):
num_con_cat, num_con = get_num_con_cat(self.con_by_sample,self.cat_by_sample)
self.assertEqual(num_con_cat,self.num_con_cat)
self.assertEqual(num_con,self.num_con)
def test_get_num_cat(self):
num_cat = get_num_cat(self.sample_by_cat,self.cat_by_sample.keys())
self.assertEqual(num_cat,self.num_cat)
num_cat = get_num_cat(self.sample_by_cat,self.cat_by_sample.keys()[:-1])
self.assertEqual(num_cat,self.num_cat_less)
def test_make_table_file(self):
random_dir_name=get_random_directory_name(output_dir='/tmp')
foldername = random_dir_name
self._dir_to_clean_up = foldername
try:
os.mkdir(foldername)
except OSError:
pass
obs=foldername
try:
os.mkdir(os.path.join(obs,"otu_network"))
except OSError:
pass
obs = os.path.join(obs,"otu_network")
make_table_file(self.edge_file_str, self.labels, obs,"real_edge_table.txt")
self.assertTrue(exists(foldername+"/otu_network/real_edge_table.txt"),'The file was not created in \
the appropriate location')
def test_make_stats_files(self):
random_dir_name=get_random_directory_name(output_dir='/tmp')
foldername = random_dir_name
self._dir_to_clean_up = foldername
try:
os.mkdir(foldername)
except OSError:
pass
obs=foldername
try:
os.mkdir(os.path.join(obs,"otu_network"))
except OSError:
pass
try:
os.mkdir(os.path.join(obs,"otu_network/stats"))
except OSError:
pass
obs = os.path.join(obs,"otu_network")
make_stats_files(self.sample_dc,self.otu_dc,self.degree_counts,self.num_con_cat, self.num_con,self.num_cat\
,self.cat_by_sample,obs)
self.assertTrue(exists(foldername+"/otu_network/stats/real_dc_otu_degree.txt"),'The file was not created in \
the appropriate location')
self.assertTrue(exists(foldername+"/otu_network/stats/real_dc_sample_degree.txt"),'The file was not created in \
the appropriate location')
self.assertTrue(exists(foldername+"/otu_network/stats/real_dc_sample_otu_degree.txt"),'The file was not created in \
the appropriate location')
self.assertTrue(exists(foldername+"/otu_network/stats/real_cat_stats_Day.txt"),'The file was not created in \
the appropriate location')
self.assertTrue(exists(foldername+"/otu_network/stats/real_cat_stats_time.txt"),'The file was not created in \
the appropriate location')
def test_make_props_files(self):
random_dir_name=get_random_directory_name(output_dir='/tmp')
foldername = random_dir_name
self._dir_to_clean_up = foldername
try:
os.mkdir(foldername)
except OSError:
pass
obs=foldername
try:
os.mkdir(os.path.join(obs,"otu_network"))
except OSError:
pass
try:
os.mkdir(os.path.join(obs,"otu_network/props"))
except OSError:
pass
obs = os.path.join(obs,"otu_network")
self.assertTrue(exists(foldername+"/otu_network/props/"),'The file was not created in \
the appropriate location')
self.assertTrue(exists(foldername+"/otu_network/props"),'The file was not created in \
the appropriate location')
if __name__ =='__main__':
main()