ó
¼™xTc           @   sò   d  Z  d d l m Z d d l Z d d e e d „ Z d „  Z d „  Z e j	 d „ Z
 d	 „  Z d d
 „ Z d „  Z d „  Z e d k rî e j d Z d d l m Z e j e ƒ j Z e j Z e j ƒ  Z e e e ƒ Z e
 e e ƒ n  d S(   sP  
Utility functions for analyzing lattice contacts.  Used in:

`Fraser JS, van den Bedem H, Samelson AJ, Lang PT, Holton JM, Echols N, Alber
T. Accessing protein conformational ensembles using room-temperature X-ray
crystallography. Proc Natl Acad Sci U S A. 2011 Sep 27;108(39):16247-52.
<http://www.ncbi.nlm.nih.gov/pubmed/21918110>`_
iÿÿÿÿ(   t   divisionNg      @c         C   sª  d d l  m } |  j ƒ  } |  j ƒ  } |  j d | ƒ }	 |	 j ƒ  }
 g  } | d  k rs | j t | ƒ t	 ƒ } n  x0t
 |
 ƒ D]"\ } } | | sœ q€ n  | | } | | } | j } | j } | j } xÔ | j ƒ  D]Æ \ } } | | } | | } | j } | j } | j } xˆ | D]€ } | j ƒ  rM| r8qqM| | k rMqqMn  | d k re| reqn  | | } | j | | ƒ } | j | | | | f ƒ qWqØ Wq€ W| S(   Niÿÿÿÿ(   t   flext   distance_cutofft   HOHt   WAT(   R   R   (   t   scitbx.array_familyR   t
   sites_fract	   unit_cellt   pair_asu_tablet   extract_pair_sym_tablet   Nonet   boolt   lent   Truet	   enumeratet   resnamet   namet   chain_idt   itemst
   is_unit_mxt   distancet   append(   t   xray_structuret	   pdb_atomst   selected_atomsR   t   ignore_same_asut   ignore_watersR   R   R   R   t   pair_sym_tablet   contactst   i_seqt   pair_sym_dictt   site_it   atom_it	   resname_it	   atmname_it	   chainid_it   j_seqt   sym_opst   site_jt   atom_jt	   resname_jt	   atmname_jt	   chainid_jt   sym_opt   site_jiR   (    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   find_crystal_contacts   sF    		


			

				
%c         K   sb  i  } t  | j ƒ  ƒ } t |  | | � } x~ | D]v \ } } } }	 | | j ƒ  }
 |
 j |
 j |
 j ƒ  |
 j f } | | k r� g  | | <n  | | j | | |	 f ƒ q1 Wg  } xª | j	 ƒ  d j
 ƒ  D]’ } | j } x€ | j ƒ  D]r } | j ƒ  } x] | j ƒ  D]O } | j } | j } | | | | f } | j | g  ƒ } | j | | f ƒ qWqä WqÈ W| S(   Ni    (   t   listt   atoms_with_labelsR-   t   fetch_labelsR   R   t   residt   altlocR   t   modelst   chainst   idt   residue_groupst   atom_groupst   get(   R   t   pdb_hierarchyt   kwdst   contacts_by_residuet   atomsR   R   R$   R+   R   t   atom_rect   residue_keyt   all_residuest   chainR   t   residue_groupR1   t
   atom_groupR   R2   t   residue_contacts(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt    find_crystal_contacts_by_residue9   s,    			c   
      C   s¼   g  } x¯ |  D]§ \ } } t  | ƒ d k rG | j | d  d  d  f ƒ q | j d „  ƒ | d \ } } } | | j ƒ  } | j | j | j ƒ  | j f }	 | j | |	 | | f ƒ q W| S(   Ni    c         S   s   t  |  d | d ƒ S(   Ni   (   t   cmp(   t   xt   y(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   <lambda>Z   s    (	   R   R   R
   t   sortR0   R   R   R1   R2   (
   RC   R   t   reduced_contactsR>   R   R$   R+   R   R=   t   contact_key(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt#   extract_closest_contacting_residuesS   s    c         C   s  d d l  m } t |  | j ƒ  ƒ } | d d | ƒ| d d IJ| d
 d IJx­ | D]¥ \ } } } } | \ }	 }
 } } d |	 | |
 | f } | d  k r» | d | d d d f IJqX | \ }	 }
 } } d |	 | |
 | f } | d | | | | f IJqX Wd  S(   Niÿÿÿÿ(   t   make_headers   Crystal contacts by residuet   outs     %-16s %-16s %-16s %-16st   residues   closest contactt   symops   distance (A)t   -iH   s   %s%5s %3s %ss     %-16s %-16s %-16s %-4st   *s     %-16s %-16s %-16s %-4.2f(   RO   s   closest contactRP   s   distance (A)(   t!   mmtbx.refinement.print_statisticsRM   RL   R<   R
   (   RC   R9   RN   RM   t   summaryR>   RK   R+   R   R   R   R1   R2   t   id_strt   id_str_2(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   summarize_contacts_by_residueb   s     c         C   s|   xu |  D]m } | \ } } } } | | } | | } | j  ƒ  d d !}	 | j  ƒ  d d !}
 d |	 |
 | t | ƒ f GHq Wd  S(   Ni   i   s   %s %s %5.2f %s(   RU   t   str(   R   R   t   contactR   R$   R+   R   R    R'   t   fmt_it   fmt_j(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   show_contactsw   s    

c         C   s˜   x‘ |  D]‰ } | \ } } } } | | }	 | | }
 d | |	 j  |	 j ƒ  |	 j f } d | |
 j  |
 j ƒ  |
 j f } d | | | d | f GHq Wd  S(   Ns+   (%s and chain '%s' and resi %d and name %s)s3   ((not %s) and (chain '%s' and resi %d and name %s))s   dist %s, %s within %.1f of %sgš™™™™™¹?(   R   t   resseq_as_intR   (   R   R   t   object_nameR   RY   R   R$   R+   R   R    R'   t   s1t   s2(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   show_contacts_for_pymol„   s    

c   	      C   s   | j  ƒ  } | j ƒ  } |  j ƒ  } |  j ƒ  } | j d | j ƒ  ƒ } | | j ƒ  | j ƒ  } | j | j d | ƒ ƒ | S(   Nt
   sites_cartR   (	   t   rt   tt	   deep_copyR<   t   fractionalizet   extract_xyzt	   as_doublet   set_xyzt   orthogonalize(	   R9   R+   R   Rc   Rd   t   new_hierarchyR<   R   t	   new_sites(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   apply_sym_op_to_pdb�   s    c         C   s=   |  j  ƒ  } |  j ƒ  } | j ƒ  d k r9 t d ƒ ‚ n  t S(   Ni    s#   No REMARK records in this PDB file.(   R<   t   remark_sectiont   sizet   Sorryt   pdb_out(   t   pdb_inR<   t   remark(    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   apply_biological_unitœ   s
    t   __main__i   (   t   file_reader(   t   __doc__t
   __future__R    t   sysR
   R   R-   RD   RL   t   stdoutRW   R\   Ra   Rm   Rt   t   __name__t   argvt   pdb_filet   iotbxRv   t   any_filet   file_objectRr   t	   hierarchyR9   t   xray_structure_simplet   xrsRC   (    (    (    s;   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/lattice.pyt   <module>	   s*   &						