Back to index

python-biopython  1.60
test_trie.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 
00007 import unittest
00008 
00009 try:
00010     from Bio import trie
00011 except ImportError:
00012     import os
00013     from Bio import MissingPythonDependencyError
00014     if os.name=="java":
00015         message = "Not available on Jython, Bio.trie requires compiled C code."
00016     else:
00017         message = "Could not import Bio.trie, check C code was compiled."
00018     raise MissingPythonDependencyError(message)
00019 
00020 
00021 class TestTrie(unittest.TestCase):
00022 
00023     def test_get_set(self):
00024         trieobj = trie.trie()
00025         trieobj["hello world"] = "s1"
00026         trieobj["bye"] = "s2"
00027         trieobj["hell sucks"] = "s3"
00028         trieobj["hebee"] = "s4"
00029         self.assertEqual(trieobj["hello world"], "s1")
00030         self.assertEqual(trieobj["bye"], "s2")
00031         self.assertEqual(trieobj["hell sucks"], "s3")
00032         self.assertEqual(trieobj["hebee"], "s4")
00033         trieobj["blah"] = "s5"
00034         self.assertEqual(trieobj["blah"], "s5")
00035         self.assertEqual(trieobj.get("foobar"), None)
00036         self.assertEqual(len(trieobj), 5)
00037         trieobj["blah"] = "snew"
00038         self.assertEqual(trieobj["blah"], "snew")
00039 
00040     def test_prefix(self):
00041         trieobj = trie.trie()
00042         trieobj["hello"] = 5
00043         trieobj["he"] = 7
00044         trieobj["hej"] = 9
00045         trieobj["foo"] = "bar"
00046         k = trieobj.keys()
00047         k.sort()
00048         self.assertEqual(k, ["foo", "he", "hej", "hello"])
00049         self.assertEqual(trieobj["hello"], 5)
00050         self.assertEqual(trieobj.get("bye"), None)
00051         self.assertEqual(trieobj.has_key("hello"), True)
00052         self.assertEqual(trieobj.has_key("he"), True)
00053         self.assertEqual(trieobj.has_key("bye"), False)
00054         self.assertEqual(trieobj.has_prefix("h"), True)
00055         self.assertEqual(trieobj.has_prefix("hel"), True)
00056         self.assertEqual(trieobj.has_prefix("foa"), False)
00057         self.assertEqual(trieobj.has_prefix("hello world"), False)
00058         self.assertEqual(len(trieobj), 4)
00059         k = trieobj.with_prefix("he")
00060         k.sort()
00061         self.assertEqual(k, ["he", "hej", "hello"])
00062         k = trieobj.with_prefix("l")
00063         self.assertEqual(k, [])
00064         k = trieobj.with_prefix("hej")
00065         self.assertEqual(k, ["hej"])
00066         k = trieobj.with_prefix("hejk")
00067         self.assertEqual(k, [])
00068 
00069     def test_save(self):
00070         import StringIO
00071         trieobj = trie.trie()
00072         trieobj["foo"] = 1
00073         k = trieobj.keys()
00074         self.assertEqual(k, ["foo"])
00075         v = trieobj.values()
00076         self.assertEqual(v, [1])
00077         self.assertEqual(trieobj.get("bar", 99), 99)
00078         trieobj["hello"] = '55a'
00079         self.assertEqual(trieobj.get_approximate("foo", 0), [("foo", 1, 0)])
00080         self.assertEqual(trieobj.get_approximate("foo", 1), [("foo", 1, 0)])
00081         self.assertEqual(trieobj.get_approximate("foa", 0), [])
00082         self.assertEqual(trieobj.get_approximate("foa", 1), [("foo", 1, 1)])
00083         x = trieobj.get_approximate("foa", 2)
00084         x.sort()
00085         self.assertEqual(x, [("foo", 1, 1), ("foo", 1, 2), ("foo", 1, 2)])
00086         # foo  foo-  foo-
00087         # foa  f-oa  fo-a
00088         # mismatch a->o
00089         # insertion after f, deletion of o
00090         # insertion after o, deletion of o
00091         x = trieobj.get_approximate("foo", 4)
00092         y = {}
00093         for z in x:
00094             y[z] = y.get(z, 0) + 1
00095         x = y.items()
00096         x.sort()
00097         self.assertEqual(x,[(('foo', 1, 0), 1), (('hello', '55a', 4), 6)])
00098         h = StringIO.StringIO()
00099         trie.save(h, trieobj)
00100         h.seek(0)
00101         trieobj = trie.load(h)
00102         k = trieobj.keys()
00103         self.assertTrue("foo" in k)
00104         self.assertTrue("hello" in k)
00105         self.assertEqual(repr(trieobj["foo"]), '1')
00106         self.assertEqual(repr(trieobj["hello"]), "'55a'")
00107 
00108     def test_get_approximate(self):
00109         # Found bug, doesn't handle insertions and deletions at end properly.
00110         trieobj = trie.trie()
00111         trieobj["hello"] = 1
00112         self.assertEqual(trieobj.get_approximate('he', 2), [])
00113         self.assertEqual(trieobj.get_approximate('he', 3), [('hello', 1, 3)])
00114         self.assertEqual(trieobj.get_approximate('hello me!', 3), [])
00115         self.assertEqual(trieobj.get_approximate('hello me!', 4), [('hello', 1, 4)])
00116         self.assertEqual(trieobj.get_approximate('hello me!', 5), [('hello', 1, 4)])
00117 
00118 
00119 class TestTrieFind(unittest.TestCase):
00120 
00121     def test_find(self):
00122         from Bio import triefind
00123         trieobj = trie.trie()
00124         trieobj["hello"] = 5
00125         trieobj["he"] = 7
00126         trieobj["hej"] = 9
00127         trieobj["foo"] = "bar"
00128         trieobj["wor"] = "ld"
00129         self.assertEqual(triefind.match("hello world!", trieobj), "hello")
00130         k = triefind.match_all("hello world!", trieobj)
00131         k.sort()
00132         self.assertEqual(k, ["he", "hello"])
00133         k = triefind.find("hello world!", trieobj)
00134         k.sort()
00135         self.assertEqual(k, [("he", 0, 2), ("hello", 0, 5), ("wor", 6, 9)])
00136         k = triefind.find_words("hello world!", trieobj)
00137         k.sort()
00138         self.assertEqual(k, [("hello", 0, 5)])
00139         trieobj["world"] = "full"
00140         k = triefind.find("hello world!", trieobj)
00141         k.sort()
00142         self.assertEqual(k, [("he", 0, 2), ("hello", 0, 5), ("wor", 6, 9), ("world", 6, 11)])
00143         k = triefind.find_words("hello world!", trieobj)
00144         k.sort()
00145         self.assertEqual(k, [("hello", 0, 5), ("world", 6, 11)])
00146 
00147 
00148 if __name__ == "__main__":
00149     runner = unittest.TextTestRunner(verbosity = 2)
00150     unittest.main(testRunner=runner)