ó
TS!Ic           @   s÷   d  d l  Z  d  d l Td  d l m Z d  d l m Z m Z m Z m Z d Z	 d d d „  ƒ  YZ
 e d k ró d  d	 l m Z d
 d d „  ƒ  YZ x` e d d ƒ D]L Z g  e d ƒ D] Z e ƒ  ^ q³ Z e
 e ƒ Z d Ge e j d ƒ ƒ GHq  Wn  d S(   iÿÿÿÿN(   t   *(   t   PDBException(   t   unfold_entitiest   get_unique_parentst   entity_levelst	   uniqueifys?   Fast atom neighbor lookup using a KD tree (implemented in C++).t   NeighborSearchc           B   s;   e  Z d  Z d d „ Z d „  Z d d „ Z d d „ Z RS(   s^  
    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.
    i
   c         C   s   | |  _  t d „  | ƒ } t j | ƒ j d ƒ |  _ | d k sH t ‚ |  j j d d k sd t ‚ t d | ƒ |  _	 |  j	 j
 |  j ƒ d S(   s  
        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.
        c         S   s
   |  j  ƒ  S(   N(   t	   get_coord(   t   a(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyt   <lambda>$   s    t   fi   i   N(   t	   atom_listt   mapt   numpyt   arrayt   astypet   coordst   AssertionErrort   shapet   KDTreet   kdtt
   set_coords(   t   selfR   t   bucket_sizet
   coord_list(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyt   __init__   s    	c         C   s†   g  } xs | D]k \ } } | j  ƒ  } | j  ƒ  } | | k rC q q | | k  re | j | | f ƒ q | j | | f ƒ q Wt | ƒ S(   N(   t
   get_parentt   appendR   (   R   t	   pair_listt   parent_pair_listt   e1t   e2t   p1t   p2(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyt   _get_unique_parent_pairs.   s    t   Ac   	      C   s™   | t  k r t d | ƒ ‚ n  |  j j | | ƒ |  j j ƒ  } g  } |  j } x% | D] } | | } | j | ƒ qW W| d k rˆ | St | | ƒ Sd S(   s–  Neighbor search.

        Return all atoms/residues/chains/models/structures
        that have at least one atom within radius of center.
        What entitity level is returned (e.g. atoms or residues)
        is determined by level (A=atoms, R=residues, C=chains,
        M=models, S=structures).

        o center - Numeric array 
        o radius - float
        o level - char (A, R, C, M, S)
        s   %s: Unknown levelR#   N(   R   R   R   t   searcht   get_indicesR   R   R   (	   R   t   centert   radiust   levelt   indicest   n_atom_listR   t   iR   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyR$   B   s    	
c         C   sá   | t  k r t d | ƒ ‚ n  |  j j | ƒ |  j j ƒ  } |  j } g  } x; | D]3 \ } } | | } | | }	 | j | |	 f ƒ qT W| d k r› | S| }
 x9 d d d d g D]% } |  j |
 ƒ }
 | | k r´ |
 Sq´ Wd S(   s©   All neighbor search.

        Search all entities that have atoms pairs within
        radius. 

        o radius - float
        o level - char (A, R, C, M, S)
        s   %s: Unknown levelR#   t   Rt   Ct   Mt   SN(   R   R   R   t
   all_searcht   all_get_indicesR   R   R"   (   R   R'   R(   R)   R   t   atom_pair_listt   i1t   i2t   a1t   a2t   next_level_pair_listt   l(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyt
   search_all]   s"    		

(   t   __name__t
   __module__t   __doc__R   R"   R$   R9   (    (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyR      s
   	t   __main__(   t   randomt   Atomc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   d t  d ƒ |  _ d  S(   Nid   i   (   R>   t   coord(   R   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyR   ~   s    c         C   s   |  j  S(   N(   R@   (   R   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyR      s    (   R:   R;   R   R   (    (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyR?   }   s   	i    i   id   s   Found g      @(    (    (   R   t
   Bio.KDTreet   PDBExceptionsR   t	   SelectionR   R   R   R   R<   R   R:   t   numpy.randomR>   R?   t   rangeR+   t   jt   alt   nst   lenR9   (    (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/NeighborSearch.pyt   <module>   s   
"j"