-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_util.py
102 lines (85 loc) · 3.78 KB
/
test_util.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
# ----------------------------------------------------------------------------
# Copyright (c) 2016--, Calour development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------
from unittest import main
from copy import deepcopy
from os.path import basename, join
from tempfile import mkdtemp
import shutil
import calour as ca
from calour import util
from calour._testing import Tests
class IOTests(Tests):
def setUp(self):
super().setUp()
self.test1 = ca.read(self.test1_biom, self.test1_samp, normalize=None)
def test_iter_fasta(self):
seqs = []
heads = []
for chead, cseq in util._iter_fasta(self.seqs1_fasta):
seqs.append(cseq)
heads.append(chead)
self.assertListEqual(heads, ['real_seq_6', 'not real seq'])
self.assertListEqual(seqs, ['TT', 'AACGGAGGATGCGAGCGTTATCTGGAATCATTGGGTTTAAAGGGTCCGTAGGCGGGTTGATAAGTCAGAGGTGAAAGCGCTTAGCTCAACTAAGCAACTGCCTTTGAAACTGTCAGTCTTGAATGATTGTGAAGTAGTTGGAATGTGTAG'])
def test_get_taxonomy_string(self):
orig_tax = list(self.test1.feature_metadata['taxonomy'].values)
# test string taxonomy
tax = util._get_taxonomy_string(self.test1)
self.assertListEqual(tax, orig_tax)
# test using a biom table with list taxonomy, not removing the X__ parts
exp = deepcopy(self.test1)
exp.feature_metadata['taxonomy'] = exp.feature_metadata['taxonomy'].str.split(';')
tax = util._get_taxonomy_string(exp, remove_underscore=False)
self.assertListEqual(tax, orig_tax)
# and test with removing the X__ parts and lower case
tax = util._get_taxonomy_string(exp, to_lower=True)
self.assertEqual(tax[1], 'bacteria;tenericutes;mollicutes;mycoplasmatales;mycoplasmataceae;mycoplasma;')
def test_get_file_md5(self):
md5 = util.get_file_md5(self.test1_samp)
self.assertEqual(md5, '36c9dc4dd389e82689a2a53ca9558c6b')
def test_get_data_md5(self):
exp = deepcopy(self.test1)
# try on dense matrix
exp.sparse = True
md5 = util.get_data_md5(exp.data)
self.assertEqual(md5, '561ba229f4a4c68979e560a10cc3fe42')
# try on sparse matrix
exp.sparse = False
md5 = util.get_data_md5(exp.data)
self.assertEqual(md5, '561ba229f4a4c68979e560a10cc3fe42')
def test_get_config_file(self):
fp = util.get_config_file()
self.assertEqual(basename(fp), 'calour.config')
def test_get_config_sections(self):
sections = util.get_config_sections()
self.assertIn('dbbact', sections)
self.assertIn('sponge', sections)
self.assertNotIn('username', sections)
def test_config_file_value(self):
# test the set and get config file values
# create the tmp config file path
d = mkdtemp()
f = join(d, 'config.txt')
util.set_config_value('test1', 'val1', config_file_name=f)
res = util.get_config_value('test1', config_file_name=f)
self.assertEqual(res, 'val1')
# test the fallback if a key doesn't exist
res = util.get_config_value('test2', fallback='na', config_file_name=f)
self.assertEqual(res, 'na')
shutil.rmtree(d)
def test_to_list(self):
self.assertEqual(util._to_list(5), [5])
self.assertEqual(util._to_list([5]), [5])
self.assertEqual(util._to_list('test'), ['test'])
self.assertEqual(util._to_list(range(5)), range(5))
def test_format_docstring(self):
@util.format_docstring([], a=1, b='a')
def foo():
'''{} {a} {b}'''
self.assertEqual(foo.__doc__, '[] 1 a')
if __name__ == "__main__":
main()