ó
Ë™xTc           @   s\  d  Z  d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z d d l m Z d d	 l m Z d
 Z e e ƒ \ Z Z Z Z Z Z Z Z Z Z Z Z Z  Z! d d d d d d d d d d d d d d g Z" d e f d „  ƒ  YZ# d e f d „  ƒ  YZ$ d e f d „  ƒ  YZ% d d  e& d! „ Z' d" „  Z( d# „  Z) d$ S(%   sg   
Deals with examing the atoms around a site and recognizing distinct and useful
chemical environments.
iÿÿÿÿ(   t   division(   t   ions(   t   common_residue_names_get_class(   t   sasaki(   t   col(   t   Sorry(   t   slots_getstate_setstate(   t   Counteri   s   Coordinating carboxy groups   Coordinating amide groups   Coordinating backbone atoms   Coordinating water molecules   Coordinating sulfate groups   Coordinating phosphate groups   Coordinating disulfide groups   Coordinating primary nitrogens   Coordinating secondary nitrogens   Coordinating tertiary nitrogens   Coordinating chlorides   Coordinating oxygens   Coordinating nitrogens   Coordinating sulfurt   ScatteringEnvironmentc           B   sP   e  Z d  Z d d d d d d d d d	 d
 d g Z d d d d d d d „ Z RS(   s—  
  Container for information summarizing a site's scattering environment.

  Attributes
  ----------
  d_min : float
  wavelengeth : float
  fp : float
  fpp : float
  b_iso : float
  b_mean_hoh : float
  occ : float
  fo_density : tuple of float, float
  fofo_density : tuple of float, float
  anom_density : tuple of float, float
  pai : float
      Principal axes of inertia at site, currently unused.
  t   d_mint
   wavelengtht   fpt   fppt   b_isot
   b_mean_hoht   occt
   fo_densityt   fofc_densityt   anom_densityt   paic	         C   sÅ  | | g j  d  ƒ d k s! t ‚ | | g j  d  ƒ d k sB t ‚ | | g j  d  ƒ d k sc t ‚ | j | }	 | j j ƒ  j ƒ  |  _ | j |  _ | j | ƒ | j	 | ƒ |  _
 |  _ | j j |	 j ƒ }
 | d  k	 ræ | |  _ n! t j j | j |	 j | ƒ |  _ | d  k	 r| |  _ n | j |
 ƒ d f |  _ | d  k	 rO| |  _ n0 | d  k	 rv| j |
 ƒ d f |  _ n	 d |  _ | j | ƒ |  _ | j |  _ |	 j |  _ | j | ƒ j ƒ  |  _ d  S(   Ni   i    (   NN(   t   countt   Nonet   AssertionErrort	   pdb_atomst   fmodelt   f_obsR	   R
   t   get_fpt   get_fppR   R   t	   unit_cellt   fractionalizet   xyzR   R   t   utilst   fit_gaussianR   t   eight_point_interpolationR   t	   get_b_isoR   R   R   t   principal_axes_of_inertiat   center_of_massR   (   t   selft   i_seqt   managert   fo_mapt   fofc_mapt   anom_mapR   R   R   t   atomt	   site_frac(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   __init__G   s0    	!!!%	N(   t   __name__t
   __module__t   __doc__t	   __slots__R   R-   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyR   2   s   t   atom_contactc           B   sª   e  Z d  Z d d d d d d d g Z d „  Z d	 „  Z d
 „  Z e d „ Z d „  Z	 d „  Z
 e d „  ƒ Z d „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z RS(   sp  
  Container for information about an interacting atom.  Most of the methods
  are simply wrappers for frequently called operations on the atom object, but
  symmetry-aware.

  Attributes
  ----------
  atom : iotbx.pdb.hierarchy.atom
  charge : int
  element : str
  rt_mx : cctbx.sgtbx.rt_mx
  site_cart : tuple of float, float, float
  vector : scitbx.matrix.rec
  R+   t   vectort	   site_cartt   rt_mxt   is_carboxy_terminust   elementt   chargec         C   sg   | j  ƒ  |  _ t | ƒ |  _ | |  _ | |  _ | |  _ t j j	 | ƒ |  _
 t j j | ƒ |  _ d  S(   N(   t   fetch_labelsR+   t   _is_carboxy_terminusR6   R3   R4   R5   R   t   servert   get_elementR7   t
   get_chargeR8   (   R%   R+   R3   R4   R5   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyR-   �   s    			c         C   s   t  |  j ƒ S(   sn   
    Actual distance from target atom.

    Returns
    -------
    float
        Distance, in angstroms.
    (   t   absR3   (   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   distance™   s    	c         C   s   t  |  j | j ƒ S(   sÄ   
    Distance from another coordinating atom.

    Parameters
    ----------
    other : mmtbx.ions.environment.atom_contact

    Returns
    -------
    float
        Distance, in angstroms.
    (   R>   R3   (   R%   t   other(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   distance_from¤   s    c         C   sF   |  j  j ƒ  r5 | r5 |  j j ƒ  d t |  j  ƒ S|  j j ƒ  Sd S(   s  
    Creates a string from the atom's id string and the symmetry operator
    associated with that site.

    Parameters
    ----------
    suppress_rt_mx : bool, optional
        Don't include symmetry operator information in the string.

    Returns
    -------
    str
    t    N(   R5   t
   is_unit_mxR+   t   id_strt   str(   R%   t   suppress_rt_mx(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyRD   ³   s    c         C   s   |  j  j j ƒ  S(   s^   
    Retrieves the coordinating atom's name (i.e. "OX1")

    Returns
    -------
    str
    (   R+   t   namet   strip(   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt	   atom_nameÆ   s    c         C   s   |  j  j ƒ  j j ƒ  j ƒ  S(   sw   
    Retrieves the residue name associated with te coordinating atom (i.e. "ARG")

    Returns
    -------
    str
    (   R+   R9   t   resnameRH   t   upper(   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyRJ   Ð   s    c         C   s
   |  j  j S(   sP   
    Occupancy of the coordinating atom.

    Returns
    -------
    float
    (   R+   R   (   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyR   Ú   s    	c         C   s
   |  j  j S(   s^   
    Retrieves the sequence ID of the coordinating atom.

    Returns
    -------
    int
    (   R+   R&   (   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt
   atom_i_seqå   s    c         C   s   |  j  j ƒ  j j ƒ  S(   s|   
    Retrieves the alternate conformation label, if any, of the coordinating
    atom.

    Returns
    -------
    str
    (   R+   R9   t   altlocRH   (   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyRM   ï   s    	c         C   s^   |  j  j ƒ  } | j | j ƒ  |  j  j } |  j j ƒ  sA | rE | S| d t |  j ƒ Sd S(   sÜ   
    Unique identifier for an atom, ignoring the altloc but taking the symmetry
    operator (if any) into account.

    Parameters
    ----------
    suppress_rt_mx : bool, optional

    Returns
    -------
    str
    RB   N(   R+   R9   t   chain_idt   residRG   R5   RC   RE   (   R%   RF   t   labelst   base_id(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   atom_id_no_altlocú   s
    c         C   s   | j  ƒ  |  j  ƒ  k S(   sÒ   
    Equality operator, taking symmetry into account but ignoring the
    altloc identifier.

    Parameters
    ----------
    other : mmtbx.ions.environment.atom_contact

    Returns
    -------
    bool
    (   RR   (   R%   R@   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   __eq__  s    c         C   s
   |  j  ƒ  S(   N(   R?   (   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   __abs__  s    c         C   s
   |  j  ƒ  S(   N(   RD   (   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   __str__   s    (   R.   R/   R0   R1   R-   R?   RA   t   FalseRD   RI   RJ   t   propertyR   RL   RM   RR   RS   RT   RU   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyR2      s    					
	
	
			t   ChemicalEnvironmentc           B   sA   e  Z d  Z d d d d d g Z d „  Z d	 d „ Z d „  Z RS(
   sY  
  Container for information summarizing a site's chemical environment.

  Attributes
  ----------
  atom : iotbx.pdb.hierarchy.atom
  contacts : list of mmtbx.ions.environment.atom_contact
  contacts_no_alts : list of mmtbx.ions.environment.atom_contact
  chemistry : collections.Counter of int, int
  geometry : list of tuples of str, float
  R+   t   contactst   contacts_no_altst	   chemistryt   geometryc         C   sý   | j  | j ƒ  |  _ | |  _ g  |  _ x˜ t | ƒ D]Š \ } } | j d k rV q5 n  t } xG | | d D]7 } t | j | j ƒ rk | j	 | j	 k rk t
 } Pqk qk W| r5 |  j j | ƒ q5 q5 W|  j |  j | ƒ |  _ t j j |  j d t ƒ|  _ d S(   s˜   
    Parameters
    ----------
    i_seq : int
    contacts : list of mmtbx.ions.environment.atom_contact
    manager : mmtbx.ions.identify.manager
    t   Ht   Di   t   minimizer_methodN(   R]   R^   (   R   R9   R+   RY   RZ   t	   enumerateR7   t   Truet   _same_atom_different_altlocR5   RV   t   appendt   _get_chemical_environmentR[   R   R\   t   find_coordination_geometry(   R%   R&   RY   R'   t   indext   contactt
   no_alt_loct   other_contact(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyR-   1  s$    			c         C   s¥   | d k r! t j j | ƒ } n  t j d | d | ƒ } t j j | |  j ƒ } t d „  | Dƒ ƒ } | d k r• t t | t	 d ƒ ƒ ƒ | } n d } | | f S(   sU  
    Calculates the BVS and VECSUM for a given element / charge identity.

    Parameters
    ----------
    element : str
        The element identity to calculate valences for.
    charge : int, optional
        The charge to calculate valences for.

    Returns
    -------
    float
        Bond-valence sum (BVS).
    float
        Vector sum (VECSUM).

    Notes
    -----
    .. [1] MÃ¼ller, P., KÃ¶pke, S. & Sheldrick, G. M. Is the bond-valence method
           able to identify metal atoms in protein structures? Acta
           Crystallogr. D. Biol. Crystallogr. 59, 32â€“7 (2003).
    R7   R8   c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R>   (   t   .0t   i(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pys	   <genexpr>r  s    i    N(   i    i    i    (
   R   R   R;   R=   t   metal_parameterst   calculate_valencesRY   t   sumR>   R   (   R%   R7   R8   t
   ion_paramst   vectorst   bvst   vecsum(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   get_valenceV  s    "c            s“  ‡  f d †  } ‡  f d †  } t  ƒ  } xe| D]]} | j } | j j } g  | | ƒ D] }	 t j j ˆ  j |	 ƒ ^ qV }
 | | | ƒ ƒ } | d k r¬ | t c d 7<nj| d k r4| t	 c d 7<t
 | j ƒ  ƒ d k ró | t c d 7<qd |
 k r| t c d 7<qd |
 k r| t c d 7<qnâ | d	 k rØ| t c d 7<| d k ro| t c d 7<q| d
 k rŽ| t c d 7<q| d k r­| t c d 7<q| d k rt d | j j ƒ  ƒ ‚ qn> | d k r| t c d 7<d |
 k r| t c d 7<qn  | j j j ƒ  j ƒ  d k r_t
 | j ƒ  ƒ d k r_| t c d 7<n  | d k r. | d k r. x| | ƒ D] } ˆ  j | } t j j | ƒ d k r„xÒ ˆ  j | D]À } | | ƒ } ˆ  j | } | | k r½| | ƒ d k r½t j j | ƒ } t | | g ƒ t d d g ƒ k rC| t c d 7<q}t | | g ƒ t d	 d g ƒ k r}| t c d 7<q}q½q½Wq„q„Wq. q. W| S(   s¡  
    Examines an atom contact object for chemical properties useful to ion
    picking. These properties include degree of connectivitiy, presence in
    carboxy and amide groups, disulfide bonds, and so on.

    Parameters
    ----------
    contacts : list of mmtbx.ions.atom_contact
        The contact objects to examine the chemical environments of. Generally
        created by manager.find_nearby_atoms.
    manager : mmtbx.ions.identify.manager
        The ions manager that created contact. Used for auxillary information
        such as bond connectivity.

    Returns
    -------
    collections.Counter
        The chemical environments found for contacts.
    c            sW   g  } xJ ˆ  j  |  D]; } ˆ  j | } t j j | ƒ d k r | j | ƒ q q W| S(   sæ   
      Parameters
      ----------
      seq : int
          The atom.i_seq to find the neighbors of.

      Returns
      -------
      list of int
          A list of neighboring atoms that does not include any hydrogens.
      R]   R^   (   R]   R^   (   t   connectivityR   R   R;   R<   Rc   (   t   seqt	   neighborsR@   t
   other_atom(   R'   (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   _non_hydrogen_neighbors�  s    c            s‰   g  } xv t  |  ƒ D]h \ } } t } x= |  | d D]- } t ˆ  j | ˆ  j | ƒ r4 t } Pq4 q4 W| r | j | ƒ q q Wt | ƒ S(   st   
      Count the number of neighbors, filtering out those that are alt-locs of
      other neighboring atoms.
      i   (   R`   Ra   Rb   R   RV   Rc   t   len(   t   i_seqst   non_alt_locsRf   R&   Rh   t   j_seq(   R'   (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   _n_non_altlocs   s    t   CLi   t   Ot   common_watert   St   Pt   Ni   i   i    s2   Nitrogen with more than three coordinating atoms: t   Ct   CAt   common_amino_acid(   R€   (   Rƒ   R„   R   R…   (   R†   (   R   Rƒ   (   R„   (   i   (   R   R7   R+   R&   R   R;   R<   R   t   chem_chloridet   chem_oxygent	   get_classRJ   t
   chem_watert   chem_sulfatet   chem_phosphatet   chem_nitrogent   chem_nitrogen_primaryt   chem_nitrogen_secondaryt   chem_nitrogen_tertiaryR   RD   t   chem_sulfurt   chem_disulfideRG   RH   RK   t   chem_backboneRt   t   sett   chem_carboxyt
   chem_amide(   R%   RY   R'   Rx   R}   t   chem_envRg   R7   R&   Rk   t   neighbor_elementst   n_neighborsR|   t   j_atomt   k_seqt   k_neighborst   k_atomt	   k_element(    (   R'   sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyRd   y  sf    		/$$(N(   R.   R/   R0   R1   R-   R   Rs   Rd   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyRX   #  s
   	%#g      @g      ø?c	             sú  g  }	 | j  ƒ  }
 | j ƒ  } | |  } | |  } | |  } | j |  d ƒ j ƒ  } | |  } xg| j ƒ  D]Y\ } } | | } | | } | j j ƒ  j ƒ  d k r¬ qk n  t | | ƒ rÁ qk n  x | D]ø } xï | D]ç } | j	 | j | | ƒ ƒ } | | } |
 j
 | ƒ } t |
 j
 d | ƒ ƒ } t | ƒ } t | | ƒ | d k  sSt ‚ t d | d | | d | d	 | ƒ ‰  | |  k r—| j ƒ  r—qÕ n  ˆ  j ƒ  | k  r¯qÕ n  |	 j ˆ  ƒ qÕ WqÈ Wqk W| rö| d k	 rög  } g  |	 D] ‰  ˆ  j ƒ  ^ qç} xë |	 D]ã ‰  ˆ  j d k r.| j ˆ  ƒ qn  t ‡  f d †  |	 Dƒ ƒ rPqn  g  } x4 | ˆ  j ƒ  D]" } | | k rg| j | ƒ qgqgWxY | D]D } |	 | j | ƒ } | j d k r”t | ƒ t ˆ  ƒ k  r”Pq”q”W| j ˆ  ƒ qW| }	 n  |	 S(   sÈ  
  Given site in the structure, return a list of nearby atoms with the
  supplied cutoff, and the vectors between them and the atom's site. Takes
  into account symmetry operations when finding nearby sites.

  Parameters
  ----------
  i_seq : int
  xray_structure : cctbx.xray.structure.structure
  pdb_atoms : iotbx.pdb.hierarchy.af_shared_atom
  asu_mappings : cctbx.crystal.direct_space_asu.asu_mappings
  asu_table : cctbx.crystal.pair_tables.pair_asu_table
  connectivity : scitbx.array_family.shared.stl_set_unsigned
  far_distance_cutoff : float, optional
  near_distance_cutoff : float, optional
  filter_by_bonding : bool, optional

  Returns
  -------
  list of mmtbx.ions.environment.atom_contact
  i    R]   R^   R,   g      à?R+   R3   R4   R5   R„   R‚   R�   Rƒ   c         3   sH   |  ]> } | ˆ  k o? ˆ  j  | ƒ d  k  o? ˆ  j ƒ  | j ƒ  k Vq d S(   gffffffþ?N(   RA   R?   (   Rj   Ri   (   Rg   (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pys	   <genexpr>B  s   R   (   R]   R^   N(   R„   R‚   R�   Rƒ   (   Rƒ   R   R�   (   R   t
   sites_fract	   get_rt_mxt   inverset   itemsR7   RK   RH   Rb   t   multiplyt   orthogonalizeR   R>   R   R2   RC   R?   Rc   R   RL   t   anyRf   (    R&   t   xray_structureR   t   asu_mappingst	   asu_tableRt   t   far_distance_cutofft   near_distance_cutofft   filter_by_bondingRY   R   RŸ   R,   t   asu_dictt   site_it   rt_mx_i_invt   atom_iR|   t   j_sym_groupst   site_jt   atom_jt   j_sym_groupt   j_sym_idR5   t   site_jit   site_ji_cartt   vec_it   vec_jit   filteredt
   all_i_seqst   bonded_j_seqsRi   (    (   Rg   sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   find_nearby_atomsï  sn    






 
		c         C   s…   d } t |  ƒ j d k r0 |  j ƒ  j ƒ  } n' t |  ƒ j d k sK t ‚ |  j ƒ  } x' | D] } | j j ƒ  d k r^ t Sq^ Wt	 S(   sb  
  Checks if an atom or residue is part of a carboxy terminus.

  Parameters
  ----------
  pdb_object : iotbx.pdb.hierarchy.atom or iotbx.pdb.hierarchy.residue_group or
               iotbx.pdb.hierarchy.atom_group

  Returns
  -------
  bool

  .. note:: Deprecated in favor ChemicalEnvironment._get_chemical_environment in
            the SVM code.
  R+   t   residue_groupt
   atom_groupt   OXTN(   R½   R¾   (
   R   t   typeR.   t   parentt   atomsR   RG   RH   Ra   RV   (   t
   pdb_objectRÂ   R+   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyR:   Y  s    c         C   s˜   g  |  | g D] } | j  ƒ  ^ q \ } } |  j j ƒ  | j j ƒ  } } | j | j } } | j ƒ  | j ƒ  }	 }
 | | k o— | | k o— |	 |
 k S(   sÎ   
  Determines whether atom1 and atom2 differ only by their alternate location.

  Parameters
  ----------
  atom1 : iotbx.pdb.hierarchy.atom
  atom2 : iotbx.pdb.hierarchy.atom

  Returns
  -------
  bool
  (   R9   RG   RH   RN   RO   (   t   atom1t   atom2Rk   t   label1t   label2t   name1t   name2t   chain1t   chain2t   res1t   res2(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyRb   t  s
    +N(*   R0   t
   __future__R    t   mmtbxR   t	   iotbx.pdbR   R‰   t   cctbx.eltbxR   t   scitbx.matrixR   t   libtbx.utilsR   t   libtbxR   t   collectionsR   t   N_SUPPORTED_ENVIRONMENTSt   rangeR•   R–   R“   RŠ   R‹   RŒ   R’   RŽ   R�   R�   R‡   Rˆ   R�   R‘   t   CHEM_ENV_LABELSR   R2   RX   Ra   R¼   R:   Rb   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ions/environment.pyt   <module>   s@   6	M¤Óa	