ó
¢™xTc        
   @   sî   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	 Z	 d „  Z
 d d „ Z d d d d d	 e e e e	 j d
 „	 Z d	 e e e e e e	 j d „ Z d e f d „  ƒ  YZ d d d „ Z e d „ Z d S(   iÿÿÿÿ(   t   division(   t   percentile_based_spread(   t   slots_getstate_setstate(   t   Sorry(   t	   str_utilsNc         C   sm   g  } x` |  j  ƒ  j ƒ  D]L } | j ƒ  d } x3 | j ƒ  D]% } | j | k r< | j | ƒ q< q< Wq W| S(   Ni    (   t
   only_modelt   chainst
   conformerst   residuest   resnamet   append(   t	   hierarchyt   ligand_codet   copiest   chaint	   main_conft   residue(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   extract_ligand_residue	   s    c   	      C   sÑ   g  } xÄ |  j  ƒ  j ƒ  D]° } x§ | j ƒ  D]™ } x� | j ƒ  D]‚ } | j | k r? | d  k	 r± t } x9 | j ƒ  D]+ } | j j	 ƒ  | j	 ƒ  k rs t
 } Pqs qs W| s± q? q± n  | j | ƒ q? q? Wq, Wq W| S(   N(   R   R   t   residue_groupst   atom_groupsR	   t   Nonet   Truet   atomst   segidt   stript   FalseR
   (	   R   R   t
   only_segidR   R   t   residue_groupt
   atom_groupt
   have_segidt   atom(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   extract_ligand_atom_group   s     	g       @c
         C   sÄ  |  j  ƒ  s t ‚ | | g d  d  g k sH | | g d  d  g k sH t ‚ | d  k rÕ | d  k sf t ‚ d d l m }
 |
 j | d d ƒ} | j d ƒ | j j } |
 j | d d ƒ} | j d ƒ | j j } n  t	 | |  ƒ } t	 | |  ƒ } t
 | ƒ d k st
 | ƒ d k r*t d |  ƒ ‚ n  |	 d t
 | ƒ t
 | ƒ f IJg  } g  } xa | D]Y } t d | d	 | d
 | d | d t d t d |	 ƒ \ } } | j | ƒ | j | ƒ q]W| | f S(   Niÿÿÿÿ(   t   file_readert
   force_typet   pdbi    s(   One or both models missing residue '%s'!s.   %d copies in 1st model, %d copies in 2nd modelt   ligandt   reference_ligandst   verboset   exclude_hydrogenst   implicit_matchingt   quiett   out(   t   isalnumt   AssertionErrorR   t   iotbxR    t   any_filet   check_file_typet   file_objectR   R   t   lenR   t   compare_ligands_implR   R
   (   R   t   hierarchy_1t   hierarchy_2t
   pdb_file_1t
   pdb_file_2t$   max_distance_between_centers_of_massR&   R%   R'   R)   R    t   pdb_1t   pdb_2t	   ligands_1t	   ligands_2t   rmsdst   pbsst   ligand_1t
   rmsds_currt	   pbss_curr(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   compare_ligands"   s@    
$c	   "      C   s  d d l  m }	 d d l m }
 g  } |  j ƒ  } | j ƒ  } | j ƒ  } xd | D]\ } | j ƒ  j ƒ  } | j ƒ  } t |
 | ƒ |
 | ƒ ƒ } | | k  rQ | j | ƒ qQ qQ Wg  } g  } xO| D]G} | j ƒ  } |	 j	 ƒ  } |	 j	 ƒ  } x” t
 |  j ƒ  ƒ D]€ \ } } | j j ƒ  d k r.| r.qn  xP t
 | j ƒ  ƒ D]< \ } } | j | j k rA| j | ƒ | j | ƒ PqAqAWqWt | ƒ d k rÿ| rÄ| d IJxt
 |  j ƒ  ƒ D]\ } } | j j ƒ  d k rç| rçqºn  d } d } t j } x{ t
 | j ƒ  ƒ D]g \ } } | j | j k rt |
 | j ƒ |
 | j ƒ ƒ } | | k  rv| } | j } | } qvqqW| d k	 rº| d | j | | f IJ| j | ƒ | j | ƒ qºqºWn  t | ƒ d k rÿ| rët d	 ƒ ‚ qü| d
 IJd Sqÿn  | j | ƒ } | j ƒ  j ƒ  j | ƒ } | j | ƒ } t | | j ƒ  ƒ }  | sƒ| d |  j ƒ  | j ƒ  | j ƒ  | f IJn  | j | ƒ | j |  ƒ | rÄ | rÄ |  j ƒ  }! | | j ƒ  } xB t | | ƒ D]. \ } } | d | | j ƒ  | | f IJqÖWqÄ qÄ W| | f S(   sæ   
  Given a target ligand and a list of reference ligands, return the RMSD(s)
  for any ligand determined to be approximately equivalent.  (Usually there
  will be just one of these, but this allows for alternate conformations.)
  iÿÿÿÿ(   t   flex(   t   colt   Ht   Di    sI     warning: no atom name matches found - will guess equivalence from sitess!       '%s' : '%s' (distance = %.2f)s   No matching atoms found!s#     WARNING: no matching atoms found!s'     '%s' matches '%s': atoms=%d rmsd=%.3fs       %s: dxyz=%.2f(   RC   RD   (   RC   RD   N(   t   scitbx.array_familyRA   t   scitbx.matrixRB   R   t   extract_xyzt   meant   absR
   t   size_tt	   enumeratet   elementR   t   nameR0   R   t   syst   maxintt   xyzR   t   selectt   rms_differenceR   t   normst   id_strt   sizet   zip("   R#   R$   R6   R&   R'   R%   R(   t    raise_sorry_if_no_matching_atomsR)   RA   RB   t   matchingt   atoms_1t   sites_1t
   xyz_mean_1t   ligand_2t   sites_2t
   xyz_mean_2t   dxyzR;   R<   t   atoms_2t   isel_1t   isel_2t   i_seqt   atom_1t   j_seqt   atom_2t
   j_seq_bestt	   name_bestt	   dxyz_bestt   rmsdt   pbsR   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyR1   N   sˆ    
	"	

(t   ligand_validationc           B   s}   e  Z d  d d d d d d d d d	 d
 d d d d d g Z d d d d „ Z e j d „ Z d d „ Z e j e	 d „ Z
 RS(   t   cct   two_fofc_mint   two_fofc_maxt   two_fofc_meant   fofc_mint   fofc_maxt	   fofc_meant   n_below_two_fofc_cutofft   n_below_fofc_cutofft
   b_iso_meant   occupancy_meanR;   R<   RT   t   atom_selectiont
   xyz_centerg      ø?g      Àc
         C   sa  d d l  m }
 d d l m } d d l m } | j ƒ  j ƒ  } t | ƒ d k sj | j	 d ƒ sj t
 ‚ |
 j d | d | d	 | j ƒ  d
 | j ƒ  d t ƒ } | j d | d | d d ƒ } | j d | d | d d ƒ } | j |  _ | j ƒ  j |  j ƒ } | j ƒ  |  _ | j ƒ  |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _  | j |  _! | j" ƒ  j |  j ƒ } | j | ƒ } | j# | ƒ |  _$ | j% ƒ  j& ƒ  j |  j ƒ } | j | ƒ |  _' d  |  _) |  _* | d  k	 r]t | ƒ d k r]t+ d | d | d d d t, d t, d t ƒ \ |  _) |  _* n  d  S(   Niÿÿÿÿ(   t   real_space_correlation(   t   adptbx(   RA   i   i    Rx   t   xray_structuret
   fft_m_realt
   fft_n_realR&   t   mapt	   model_mapt   ming      ø?g      ÀR#   R$   R6   g       @RW   R%   R(   (-   t   mmtbxRz   t   cctbxR{   RE   RA   R   t   extract_i_seqR0   t   all_eqR+   t    selection_map_statistics_managert   allt   focusR   t   analyze_mapRx   t
   sites_cartRQ   RH   Ry   RT   Rm   R�   Rn   t   maxRo   Rp   Rq   Rr   Rs   t   n_below_minRt   Ru   t   extract_u_iso_or_u_equivt   u_as_bRv   t
   scattererst   extract_occupanciesRw   R   R;   R<   R1   R   (   t   selfR#   t   pdb_hierarchyR|   t   two_fofc_mapt   fofc_mapt
   fmodel_mapR$   t   two_fofc_map_cutofft   fofc_map_cutoffRz   R{   RA   Rx   t   managert   stats_two_fofct
   stats_fofcRŠ   t   u_isot
   u_iso_meant   occ(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   __init__ª   s^    
(						c         C   s?  t  j d | d d d d |  j ƒ } | d t |  j ƒ |  j |  j |  j |  j |  j	 |  j
 |  j |  j |  j f
 IJ|  j d  k	 r+d j g  |  j D] } d | ^ q” ƒ } t |  j ƒ d	 k rË d
 } n  | d | IJt |  j ƒ d	 k r+d j g  |  j D] } d | ^ qþ ƒ } | d | IJq+n  |  j | ƒ ~ d  S(   NR)   t   widthiP   t   titles   Residue: %ssº   Number of non-H atoms = %d   Mean B_iso = %.2f   Mean occ. = %.2f
2mFo-DFc map:  min = %6.2f  max = %6.2f  mean = %6.2f   CC = %5.3f
 mFo-DFc map:  min = %6.2f  max = %6.2f  mean = %6.2fs   , s   %.3fi    s   [none found]s   RMSD to reference ligand(s): %ss       percentile-based spread: %s(   R   t   framed_outputRT   R0   Rx   Rv   Rw   Rn   Ro   Rp   Rm   Rq   Rr   Rs   R;   R   t   joinR<   t   _show_warnings(   R‘   R)   t   boxt   rt   rmsd_formattedt   st   pbs_formatted(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   showä   s"    )	)s       c         C   sk   |  j  d k  r  | | d IJn  |  j  d k  r@ | | d IJn  |  j d k rg | | d |  j IJn  d  S(   Ngš™™™™™é?s+   !!! warning: CC to 2mFo-DFc is poor (< 0.8)gÍÌÌÌÌÌì?s2   !!! warning: CC to 2mFo-DFc is sub-optimal (< 0.9)i    s4   !!! warning: %d atoms have mFo-DFc density < -3sigma(   Rm   Ru   (   R‘   R)   t   prefix(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyR£   ù   s    c         C   sm   | d |  j  |  j |  j |  j |  j |  j |  j |  j |  j |  j	 f
 IJ| ri |  j
 d | d d ƒ n  d  S(   Ns=   %-12s %6.2f %6.2f  %5.3f %5.1f %5.1f %5.1f  %5.1f %5.1f %5.1fR)   Rª   t    i   s           (   RT   Rv   Rw   Rm   Rn   Ro   Rp   Rq   Rr   Rs   R£   (   R‘   R)   t   warnings(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   show_simple  s    N(   t   __name__t
   __module__t	   __slots__R   Rž   RN   t   stdoutR©   R£   R   R­   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyRl   £   s   	1
c         C   s´  d t  | ƒ k  o d k n s( t ‚ | j d d d t d t ƒ j d d ƒ j ƒ  j ƒ  } | j d d	 d t d t ƒ j d d ƒ j ƒ  j ƒ  } | j d d
 d t d t ƒ j d d ƒ j ƒ  j ƒ  } t |  | d | ƒ} t  | ƒ d k rõ d  Sd  }	 | d  k	 rJd d l
 m }
 |
 j | d d ƒ} | j j } t | | ƒ }	 n  g  } x] t | ƒ D]O \ } } t d | d |  d | j d |	 d | d | d | ƒ } | j | ƒ q]W| S(   Ni    i   t   map_types   2mFo-DFct   fill_missingt   merge_anomaloust   resolution_factorg      Ð?s   mFo-DFct   FmodelR   iÿÿÿÿ(   R    R!   R"   R#   R’   R|   R$   R“   R”   R•   (   R0   R+   t   map_coefficientsR   R   t   fft_mapt   apply_sigma_scalingt   real_map_unpaddedR   R   R,   R    R-   R/   R   R   RK   Rl   R|   R
   (   R’   t   fmodelR   R   t   reference_structureR“   R”   R•   t   ligandsR$   R    t   pdb_int   reference_hierarchyt   validationst   i_ligR#   t   v(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   validate_ligands  sH    (			c         C   s›   | sS t  j d | d d d d ƒ } | d d IJ| d d IJ| j ƒ  | } n  xA |  D]9 } | rƒ | j d | ƒ | d IJqZ | j d | ƒ qZ Wd  S(   NR)   R    s   Ligand summaryRŸ   iP   s,   %30s |-----2mFo-DFc-----|    |---mFo-Dfc---|t    s+   %-12s %6s %6s  %5s %5s %5s %5s  %5s %5s %5st   IDt   B_isot   Occt   CCR�   R‹   RH   (
   RÅ   RÆ   RÇ   s   CCs   mins   maxs   means   mins   maxs   mean(   R   R¡   t   add_separatorR©   R­   (   RÀ   R)   R%   R¤   RÂ   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   show_validation_results=  s    
	(   t
   __future__R    t   libtbx.math_utilsR   t   libtbxR   t   libtbx.utilsR   R   RN   R   R   R   R   R   R±   R@   R1   Rl   RÃ   RÊ   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/ligands.pyt   <module>   s6   		%Mn+