# Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk) # This code is part of the Biopython distribution and governed by its # license. Please see the LICENSE file that should have been included # as part of this package. """Fast atom neighbor lookup using a KD tree (implemented in C++).""" import numpy from Bio.KDTree import KDTree from Bio.PDB.PDBExceptions import PDBException from Bio.PDB.Selection import unfold_entities, entity_levels, uniqueify class NeighborSearch: """ This class can be used for two related purposes: 1. To find all atoms/residues/chains/models/structures within radius of a given query position. 2. To find all atoms/residues/chains/models/structures that are within a fixed radius of each other. NeighborSearch makes use of the Bio.KDTree C++ module, so it's fast. """ def __init__(self, atom_list, bucket_size=10): """ o atom_list - list of atoms. This list is used in the queries. It can contain atoms from different structures. o bucket_size - bucket size of KD tree. You can play around with this to optimize speed if you feel like it. """ self.atom_list=atom_list # get the coordinates coord_list = [a.get_coord() for a in atom_list] # to Nx3 array of type float self.coords=numpy.array(coord_list).astype("f") assert(bucket_size>1) assert(self.coords.shape[1]==3) self.kdt=KDTree(3, bucket_size) self.kdt.set_coords(self.coords) # Private def _get_unique_parent_pairs(self, pair_list): # translate a list of (entity, entity) tuples to # a list of (parent entity, parent entity) tuples, # thereby removing duplicate (parent entity, parent entity) # pairs. # o pair_list - a list of (entity, entity) tuples parent_pair_list=[] for (e1, e2) in pair_list: p1=e1.get_parent() p2=e2.get_parent() if p1==p2: continue elif p1