Search code examples
pythonbioinformaticsbiopythonkdtree

'search' and all other methods from Biopython.KDTree library gives output as 'None' Although KDTree is created succesfully


Following is my code snippets for creating a KDTree and searching points withing a radius of given center:

code snippet:

threed_array = np.array(my_list, np.float_)
atom_kdtree.set_coords(threed_array)

try:

    print(atom_kdtree.built)
    print(atom_kdtree.search(threed_array[1], 100.00000000))

except Exception, err:
    print traceback.format_exc()
    print sys.exc_info()[0]

output:

1
None

my_list is a list of coordinates of points in 3d space. threed_array is numpy nd array.

Thus KDTree.built gives output as '1'. 'search' gives 'None'. Am i missing any step in between to built the KDTree?


Solution

  • The problem is that atom_kdtree.search() returns nothing, it only does the search.

    You have to do the search, and then ask for the points in question. I. e.

    my_list = ([0, 0, 0], 
               [1, 1, 1], 
               [3, 3, 3]) 
    
    threed_array = np.array(my_list, np.float_)
    atom_kdtree = KDTree(3)
    atom_kdtree.set_coords(threed_array)
    
    # The search is done here. Every point within a radious < 2 from [0, 0, 0].
    atom_kdtree.search(threed_array[0], 2.00000000)
    
    # Here we print the points
    print(atom_kdtree.get_indices())
    print(atom_kdtree.get_radii())
    

    Outputs:

    [0 1]
    [ 0.          1.73205078]
    

    That is: the points 0 and 1 are within radious < 2 from the point (0, 0, 0), with a distance of 0 and 1.73205078 from that point. The point (3, 3, 3) is farthest, thus not shown.