Back to index

python-biopython  1.60
test_PDB_KDTree.py
Go to the documentation of this file.
00001 # Copyright 2009-2010 by Eric Talevich.  All rights reserved.
00002 # Revisions copyright 2010 by Peter Cock.  All rights reserved.
00003 #
00004 # Converted by Eric Talevich from an older unit test copyright 2002
00005 # by Thomas Hamelryck.
00006 # 
00007 # This code is part of the Biopython distribution and governed by its
00008 # license. Please see the LICENSE file that should have been included
00009 # as part of this package.
00010 
00011 """Unit tests for those parts of the Bio.PDB module using Bio.KDTree."""
00012 import unittest
00013 
00014 try:
00015     from numpy import array
00016     from numpy.random import random
00017 except ImportError:
00018     from Bio import MissingExternalDependencyError
00019     raise MissingExternalDependencyError(\
00020         "Install NumPy if you want to use Bio.PDB.")
00021 
00022 try:
00023     from Bio.KDTree import _CKDTree
00024 except ImportError:
00025     from Bio import MissingExternalDependencyError
00026     raise MissingExternalDependencyError(\
00027         "C module in Bio.KDTree not compiled")
00028 
00029 from Bio.PDB.NeighborSearch import NeighborSearch
00030 
00031 class NeighborTest(unittest.TestCase):
00032     def test_neighbor_search(self):
00033         """NeighborSearch: Find nearby randomly generated coordinates.
00034          
00035         Based on the self test in Bio.PDB.NeighborSearch.
00036         """
00037         class RandomAtom:
00038             def __init__(self):
00039                 self.coord = 100 * random(3)
00040             def get_coord(self):
00041                 return self.coord
00042         for i in range(0, 20):
00043             atoms = [RandomAtom() for j in range(100)]
00044             ns = NeighborSearch(atoms)
00045             hits = ns.search_all(5.0)
00046             self.assertTrue(isinstance(hits, list), hits)
00047             self.assertTrue(len(hits) >= 0, hits)
00048         x = array([250,250,250]) #Far away from our random atoms
00049         self.assertEqual([], ns.search(x, 5.0, "A"))
00050         self.assertEqual([], ns.search(x, 5.0, "R"))
00051         self.assertEqual([], ns.search(x, 5.0, "C"))
00052         self.assertEqual([], ns.search(x, 5.0, "M"))
00053         self.assertEqual([], ns.search(x, 5.0, "S"))
00054 
00055 
00056 if __name__ == '__main__':
00057     runner = unittest.TextTestRunner(verbosity=2)
00058     unittest.main(testRunner=runner)