This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_hierarchical_cluster.py
83 lines (64 loc) · 2.28 KB
/
test_hierarchical_cluster.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
#!/usr/bin/env python
__author__ = "Justin Kuczynski"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["justin kuczynski"]
__license__ = "GPL"
__version__ = "1.9.1"
__maintainer__ = "Justin Kuczynski"
__email__ = "justinak@gmail.com"
from tempfile import mkstemp
from os import close
from qiime.hierarchical_cluster import single_file_upgma, single_file_nj
from qiime.format import format_distance_matrix
from unittest import TestCase, main
import os
import numpy
def remove_files(list_of_filepaths, error_on_missing=True):
missing = []
for fp in list_of_filepaths:
try:
os.remove(fp)
except OSError:
missing.append(fp)
if error_on_missing and missing:
raise OSError("Some filepaths were not accessible: %s"
% '\t'.join(missing))
class FunctionTests(TestCase):
"""Tests"""
def setUp(self):
self._paths_to_clean_up = []
def test_single_file_upgma(self):
""" single_file_upgma should throw no errors"""
titles = ['hi', 'ho']
distdata = numpy.array([[0, .5], [.5, 0.]])
fd, fname = mkstemp(prefix='upgma_', suffix='.txt')
close(fd)
f = open(fname, 'w')
self._paths_to_clean_up.append(fname)
f.write(format_distance_matrix(titles, distdata))
f.close()
fd, fname2 = mkstemp(prefix='upgma_', suffix='.txt')
close(fd)
self._paths_to_clean_up.append(fname2)
single_file_upgma(fname, fname2)
assert(os.path.exists(fname2))
def test_single_file_nj(self):
""" single_file_nj should throw no errors"""
titles = ['hi', 'ho', 'yo']
distdata = numpy.array([[0, .5, .3], [.5, 0., .9], [.3, .9, 0.]])
fd, fname = mkstemp(prefix='nj_', suffix='.txt')
close(fd)
f = open(fname, 'w')
self._paths_to_clean_up.append(fname)
f.write(format_distance_matrix(titles, distdata))
f.close()
fd, fname2 = mkstemp(prefix='nj_', suffix='.txt')
close(fd)
self._paths_to_clean_up.append(fname2)
single_file_nj(fname, fname2)
assert(os.path.exists(fname2))
def tearDown(self):
remove_files(self._paths_to_clean_up)
# run tests if called from command line
if __name__ == '__main__':
main()