ó
Ÿ™xTc           @   sÖ  d  d l  m Z d  d l m Z d  d l Z e j j d ƒ Z d  d l	 Td  d l
 m Z d  d l Z d  d l m Z d  d l m Z d  d l 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 e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d „  Z d „  Z  d „  Z! d e f d „  ƒ  YZ" d e f d „  ƒ  YZ# d e f d „  ƒ  YZ$ d e j% j& f d „  ƒ  YZ' d e f d „  ƒ  YZ( d e j% j& f d  „  ƒ  YZ) d! e f d" „  ƒ  YZ* d S(#   iÿÿÿÿ(   t   division(   t   flexNt   mmtbx_ncs_restraints_ext(   t   *(   t   adptbx(   t	   superpose(   t   matrix(   t   show_string(   t   Sorry(   t   adopt_init_args(   t   countt   selection_propertiesc           B   s   e  Z d  „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   stringt
   iselection(   t   selfR   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   __init__   s    	(   t   __name__t
   __module__R   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR      s   c   	      C   sú  g  } t  |  ƒ } | d k r" | St  | ƒ } | d k r> | Sd } d } x©t rõ| } | } x\t r½|  | | | k r™ | j | | f ƒ | } Pqb | | k rÖ | | |  | k rÖ | j | | f ƒ | } Pqb | d 7} | | k rHxU t rC| d 7} | | k rPn  |  | | | k rï | j | | f ƒ | } Pqï qï WPn  | d 7} | | k rb xU t rµ| | |  | k r˜| j | | f ƒ | } Pn  | d 7} | | k raPqaqaWPqb qb W| d 7} | | k rØPn  | d 7} | | k rM PqM qM W| S(   Ni    i   (   t   lent   Truet   append(	   t   at   bt   resultt   nat   nbt   iat   ibt   jat   jb(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   match_ordered_chain_members   sd      		 
	
 
	
 
 
 t	   resconfidc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   sj   | |  _  | j ƒ  |  _ i  } x< | j ƒ  D]. } | j } | | k sL t ‚ | j | | <q+ W| |  _ d  S(   N(   t   residue_groupt   residt   atom_groupst   altloct   AssertionErrort   resnamet   confs(   R   R    R&   t   agR#   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   K   s    		c         C   s&   |  j  | j  k r t S|  j d | ƒ S(   Nt   other(   R!   t   Falset   confs_eq(   R   R(   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   __eq__U   s     c         C   sX   xQ |  j  j ƒ  D]@ \ } } | j  j | ƒ } | d  k r@ q n  | | k r t Sq Wt S(   N(   R&   t   itemst   gett   NoneR)   R   (   R   R(   R#   R%   t   other_resname(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR*   Y   s      c         C   s   t  |  j ƒ d t  |  j ƒ S(   Nt   :(   t   strR!   R&   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   __repr__`   s    (   R   R   R   R+   R*   R2   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   I   s   	
		c         C   sV   t  |  ƒ t  | ƒ k r t Sx3 t |  | ƒ D]" \ } } | j d | ƒ s, t Sq, Wt S(   NR(   (   R   R)   t   zipR*   R   (   t   resconfids_it   resconfids_jt   it   j(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   all_confs_eqc   s      c         C   s7   g  } x* |  j  ƒ  D] } | j t d | ƒ ƒ q W| S(   NR    (   t   residue_groupsR   R   (   t   modelR   t   rg(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   get_resconfidsi   s    c	      
   C   sH  t  d | ƒ }	 t  d | ƒ }
 t d |	 d |
 ƒ ra g  t t |	 ƒ ƒ D] } | | f ^ qF } n t d |	 d |
 ƒ } xË| D]Ã\ } } |	 | j } |
 | j } i  } x< | j ƒ  D]. } | j ƒ  } | | k sÚ t ‚ | | | <q¶ WxU| j ƒ  D]G} | j	 | j ƒ  ƒ } | d  k r"qõ n  i  } x9 | j ƒ  D]+ } | j } | | k sVt ‚ | | | <q5WxÕ| j ƒ  D]Ç} | j	 | j | ƒ } | | k ržqqn  | d  k	 s°t ‚ d  | | j <d  } x’ d | f | | f g D]x \ } } | j |  k rÜd d t | | ƒ d	 | j ƒ  } | s2t | ƒ ‚ qT| d  k	 rT| d
 I| IJqTqÜqÜW| d  k	 rjqqn  | j } | j } | | k rÉt d d t | d ƒ d t | | ƒ d	 | j ƒ  ƒ ‚ n  | j d | d | d | ƒ \ } } | d k rl| | k st ‚ t d d t | d ƒ d t | | ƒ d t | | ƒ d | j ƒ  d	 | j ƒ  ƒ ‚ n  | d k r¹t d d t | d ƒ d t | | ƒ d | j ƒ  ƒ ‚ n  | d k r&t d d t | d ƒ d t | | ƒ d | j ƒ  d | | j ƒ  d | j ƒ  ƒ ‚ n  | d k sqt ‚ qqWqõ Wq} Wd  S(    NR:   R4   R5   R   R   i    s6   NCS selection includes an atom on a special position:
s     Selection: %s
s       %ss   WARNING:s(   NCS selections restrain atom to itself:
s     Reference selection: %s
s         Other selection: %s
t   i_seqt   j_seqt   j_ncsi   s;   Two different NCS operators applied to same pair of atoms:
s          Reference selection: %s
s     Previous other selection: %s
s      Current other selection: %s
s       %s
i   s.   Current reference atom previously other atom:
s"     Current reference selection: %s
s"        Previous other selection: %s
i   s/   Two other atoms mapped to same reference atom:
s       Reference: %s
s         Other 1: %s
s         Other 2: %s(   R<   R8   t   xrangeR   R   R    R"   t   confidR$   R-   R.   t   atomst   nameR=   R   t   quoteR   t   enter(   t   special_position_indicest   special_position_warnings_onlyt	   pdb_atomst   selection_stringst   registryt   reference_modelR?   t   other_modelt   logt   r_resconfidst   o_resconfidsR6   t
   pairs_listR7   t   r_rgt   o_rgt	   r_ag_dictt   r_agRA   t   o_agt   r_atom_dictt   r_atomt	   atom_namet   o_atomt   msgt   i_ncst   atomt   i_seq_it   i_seq_jt   statt   i_diag(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   match_atomso   sp    
. 	 %& 		>		^>^t$   adhoc_revertable_altloc_manipulationc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s÷   
  Ad hoc work-around to allow NCS selections consisting of one conformer only (
  with blank or non-blank altloc) per user-defined NCS selection. It should be
  called before match_atoms with .set() and then after match_atoms with
  .revert().
  c         C   st   | |  _  | |  _ | |  _ t |  _ | j ƒ  |  _ |  j |  _ |  j  d  k	 rg |  j |  j  g |  _ n  g  |  _	 d  S(   N(
   t   reference_selection_stringRI   t   pdb_hierarchyR)   t   statust   atom_selection_cachet   asct   all_selectionsR.   t   altlocs(   R   Rc   RI   Rd   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   Ñ   s    				c         C   s¦   xŸ |  j  D]” } |  j j d | ƒ } |  j j | ƒ } y | j ƒ  } Wn t k
 r[ d  SX|  j j	 | j
 ƒ x# | j ƒ  D] } d | j ƒ  _
 q| Wt |  _ q
 Wd  S(   NR   t    (   Rh   Rg   t	   selectionRd   t   selectt   only_conformerR$   R.   Ri   R   R#   RB   t   parentR   Re   (   R   t   sst   selt   phst   confR'   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   setà   s       c         C   sƒ   |  j  r xs t |  j |  j ƒ D]Y \ } } |  j j d | ƒ } |  j j | ƒ } x# | j ƒ  D] } | | j	 ƒ  _
 q_ Wq Wn  d  S(   NR   (   Re   R3   Rh   Ri   Rg   Rk   Rd   Rl   RB   Rn   R#   (   R   Ro   R#   Rp   Rq   R'   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   revertê   s    	" (   R   R   t   __doc__R   Rs   Rt   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRb   É   s   		
t   pair_lists_generatorc           B   s   e  Z d  „  Z RS(   c         C   sÿ  t  d | d | d | j j ƒ } | j ƒ  | |  _ ~ | |  _ ~ | |  _ ~ |  j d  k	 r} |  j g t |  j ƒ |  _ n  t	 |  j ƒ } | d k  r¸ t
 d t |  j d ƒ ƒ ‚ n  |  j j } | j j ƒ  |  _ g  |  _ xy |  j D]n }	 | j d |	 ƒ }
 |
 j ƒ  } | j ƒ  d k r8t
 d t |	 ƒ ƒ ‚ n  |  j j t d |	 d	 | ƒ ƒ qé Wt d
 |  j d | ƒ |  _ t j j j t | j ƒ  j ƒ  ƒ ƒ } | j } | j d |  j d j ƒ } x‹t d | ƒ D]z} | j d |  j | j ƒ } | j ƒ  } | j ƒ  } t	 | ƒ t	 | ƒ k ryt
 d d t |  j d ƒ d t |  j | ƒ d t	 | ƒ d t	 | ƒ ƒ ‚ n  xÐ t | | ƒ D]¿ \ } } | j | j k rt
 d d t |  j d ƒ d t |  j | ƒ d t | j ƒ d t | j ƒ ƒ ‚ n  t  d | d | d | j d |  j d |  j d | d | d | d | ƒ 	q‰WqÒWxž t! |  j j" ƒ  ƒ D]‡ \ } } | d j ƒ  d k  rfd d g | d j ƒ  } t
 d  j# d! | d" t |  j d ƒ d# t |  j | d ƒ g ƒ ƒ ‚ qfqfW| j$ ƒ  d  S($   NRc   RI   Rd   i   sM   Only one NCS restraints selection: %s
  At least two selections are required.i    R   s"   Empty NCS restraints selection: %sR   t   n_seqt   n_ncst   atom_selectioni   s@   NCS restraints selections yield different number of PDB MODELs:
s     reference selection: %s
s         other selection: %s
s!     number of reference MODELs: %d
s      number of     other MODELs: %ds8   NCS restraints selections lead to PDB MODEL mismatches:
s     reference MODEL id: %s
s         other MODEL id: %sRF   RG   RH   RJ   RK   R?   RL   RM   s   do not produce any pairss   produce only one pairs   
s/   NCS restraints selections %s of matching atoms:s     Reference selection: %ss         Other selection: %s(%   Rb   t   all_chain_proxiesRd   Rs   t   processed_pdbRc   RI   R.   t   listR   R   R   RH   t   sizeRw   R   Rk   R   R   t   pair_registryRJ   t   scitbxt   stlt   unsignedt   itert   site_symmetry_tableRF   Rl   R@   t   modelsR3   t   idRa   t	   enumeratet   selection_pairst   joinRt   (   R   R{   Rc   RI   RG   RM   t   aramRx   Rz   t   selection_stringRk   R   RF   Rd   t   reference_hierarchyR?   t   other_hierarchyt   r_modelst   o_modelst   r_modelt   o_modelt   i_pairt   pairt   detail(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   ó   sˆ    
								RX			"	%(   R   R   R   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRv   ñ   s   t   groupc           B   sV   e  Z d  d d „ Z e e ƒ Z d „  Z d „  Z d „  Z d „  Z e	 d d „ Z
 RS(   g�íµ ÷Æ°>c   	      C   sR   t  d |  d | d | d | d | ƒ } t d | j d | j d | d | d	 | ƒ S(
   NR{   Rc   RI   RG   RM   RJ   t   coordinate_sigmat   b_factor_weightt   u_average_min(   Rv   R”   RI   RJ   (	   R{   Rc   RI   R•   R–   RG   R—   RM   t   g(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   from_atom_selectionsR  s    				c         C   s#   t  |  t ƒ  ƒ | j ƒ  |  _ d  S(   N(   R	   t   localsR‡   (   R   RI   RJ   R•   R–   R—   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   i  s    c         C   s   |  j  j d | ƒ d  S(   Nt   number(   RJ   t"   register_additional_isolated_sites(   R   R›   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRœ   r  s    c         C   sa   t  | t j ƒ s! | j ƒ  } n  t d |  j d |  j j d | ƒ d |  j d |  j	 d |  j
 ƒ S(   NRI   RJ   R   R•   R–   R—   (   t
   isinstanceR   t   size_tR   R”   RI   RJ   t   proxy_selectR•   R–   R—   (   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRl   u  s    			c         C   s   t  d |  d | ƒ S(   NR”   t
   sites_cart(   t
   _operators(   R   R    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt	   operators  s    c         C   s%   t  d |  d | d | d | d | ƒ S(   NR”   t   u_isost   average_powert   compute_gradientst	   gradients(   t   _energies_adp_iso(   R   R£   R¤   R¥   R¦   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   energies_adp_iso‚  s    N(   R   R   R.   R™   t   staticmethodR   Rœ   Rl   R¢   R   R¨   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR”   P  s   					
	R§   c           B   s    e  Z d  „  Z d d d „ Z RS(   c            sŸ  t  j j j ˆ  d | d | d | j ƒ  d t j d t ƒ| ˆ  _ | ˆ  _	 | ˆ  _
 d } x+ ˆ  j j D] } t | | d d ƒ } qh Wt j | d d ƒ } t j | d d ƒ }	 xI ˆ  j j D]; } | j | d | j | d ƒ ƒ |	 j | d d ƒ qÂ WxZ ˆ  j j D]L } | j | d | j | d ƒ ƒ |	 j | d |	 j | d ƒ d ƒ qW|	 d k }
 |	 j |
 d ƒ | |	 j ƒ  } |	 j |
 d ƒ |
 j ƒ  }
 t j ƒ  ˆ  _ ‡  f d	 †  } | d
 | j |
 ƒ d | j |
 ƒ ƒ xA ˆ  j j D]3 } | d
 | j | d ƒ d | j | d ƒ ƒ q÷Wˆ  j j ˆ  _ ˆ  j j j d ˆ  j d ˆ  j
 d | d ˆ  j j d ˆ  j ƒ ˆ  _ ˆ  j ƒ  |	 ˆ  _ | ˆ  _ d  S(   NR¥   R¦   t   gradients_sizet   gradients_factoryt   normalizationi    iÿÿÿÿi   c            sI   |  | } ˆ  j  j t j t j | ƒ d ƒ ƒ ˆ  j | j ƒ  7_ d  S(   Ng      à?(   t   rms_with_respect_to_averageR   R   t   u_as_bR   t   mean_sqt   number_of_restraintsR}   (   t   u_isos_currentt   u_isos_averaget   diff(   R   (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   residual_contribution³  s    
R±   R²   t   weightR¤   R£   R—   (   R   t
   restraintst   energiesR   R}   R   t   doubleR)   R”   R£   R¤   R‡   t   maxRž   t   set_selectedRl   t   add_selectedt	   as_doubleR   R­   R–   Rµ   RJ   t   adp_iso_residual_sumR—   R¦   t   residual_sumt   finalize_target_and_gradientst   u_isos_countR²   (   R   R”   R£   R¤   R¥   R¦   t	   max_indexR’   t
   u_isos_sumRÀ   Rp   R²   R´   (    (   R   sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   �  sX    				!!)		
	Rj   c   	         sþ   ˆ d  k r t j ‰ n  t ˆ ƒ ˆ j j ƒ  k s9 t ‚ d ‰ x# ˆ D] } t ˆ t | ƒ ƒ ‰ qF Wd ˆ ‰  ‡  ‡ ‡ ‡ ‡ ‡ f d †  } ˆ j d k j	 ƒ  } | d d d | | g ƒ x< t
 t d ƒ ˆ j j ƒ D] \ } } | d | d | ƒ q× Wd  S(   Ni   s!     %%%ds: %%7.2f - %%7.2f = %%8.4fc      
      sÜ   ˆ ˆ d It  ˆ j j |  ƒ IJˆ ˆ d ˆ d d IJˆ j j | d ƒ } ˆ j j | d ƒ } xk t | d | | ƒ D]S \ } } } t j | ƒ } t j | ƒ } ˆ ˆ ˆ  ˆ | | | | | f IJq� Wd  S(   Ns   NCS selection:t    i   s       B-iso   NCS ave  Differencei   i    (	   R   R”   RI   R£   Rl   R²   R3   R   R®   (   R[   R’   R±   R²   R6   t   cR   (   t   fmtt   max_label_sizet   outt   prefixR   t   site_labels(    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   show_selectionÑ  s    &i    R[   R’   (   R.   t   syst   stdoutR   R£   R}   R$   R¹   RÀ   R   R3   R
   R”   R‡   (	   R   RÉ   RÇ   RÈ   t   labelRÊ   Rp   R[   R’   (    (   RÅ   RÆ   RÇ   RÈ   R   RÉ   sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   show_differences_to_averageÊ  s     !
(N(   R   R   R   R.   RÎ   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR§   Ž  s   	:R¡   c           B   s5   e  Z d  „  Z d d d d „ Z e d d d „ Z RS(   c   	      C   sê   | |  _  g  |  _ g  |  _ xÈ |  j  j D]º } t j d | j | d ƒ d | j | d ƒ ƒ } t j | j	 | j
 f ƒ } |  j j | ƒ | j | d ƒ } | | j | d ƒ } | | j ƒ  } |  j j t j | ƒ d ƒ q( Wd  S(   Nt   reference_sitesi    t   other_sitesi   g      à?(   R”   t   matricest   rmsR‡   R   t   least_squares_fitRl   R   t   rtt   rt   tR   t   dotR   t   mean(	   R   R”   R    R’   t   superpositiont   rtmxt   xt   yt   d_sq(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   ã  s    				i   Rj   c         C   sÔ  | d  k r t j } n  |  j j } x©t t d ƒ |  j j |  j |  j	 ƒ D]€\ } } } }	 | | d | IJ| | d It
 | d ƒ IJ| | d It
 | | ƒ IJ| | d It | d ƒ IJ| | j j d d d	 d
 d t d | d ƒ IJ| | j j d d d	 d
 d | d ƒ IJ| j | d ƒ }
 | | j | d ƒ } |
 | j ƒ  } | d  k	 rº| | d IJt j d t j | ƒ d | ƒ } | j d | d | d d d ƒ n  | | d |	 IJqL Wd  S(   Ni   s   NCS operator %d:s     Reference selection:i    s         Other selection:s     Number of atom pairs:RÍ   t   Rotationt   formats   %.6gt   one_row_per_lineRÈ   s     t   Translations     Histogram of differences:t   datat   n_slotst   fs       t   format_cutoffss   %8.6fs5     RMS difference with respect to the reference: %8.6f(   R.   RË   RÌ   R”   RI   R3   R
   R‡   RÑ   RÒ   R   R   RÕ   t   mathematica_formR   RÖ   Rl   R×   R   t	   histogramt   sqrtt   show(   R   R    t   n_slots_difference_histogramRÇ   RÈ   RI   t   i_opR’   t   mxRÒ   RÛ   RÜ   RÝ   t   diff_histogram(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRé   ò  s<     				c         C   s%   t  d |  d | d | d | d | ƒ S(   NR¢   R    R¥   R¦   t   sites_average(   t   _energies_sites(   R   R    R¥   R¦   Rî   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   energies_sites  s    N(   R   R   R   R.   Ré   R   Rð   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR¡   á  s   	!Rï   c           B   s,   e  Z d  „  Z d „  Z d d d d „ Z RS(   c         C   s‰  t  j j j |  d | d | d | j ƒ  d t j d t ƒ| |  _ | |  _	 |  j j
 j } d } x% | D] } t | | d d ƒ } qh Wt j | d d ƒ }	 t j | d d ƒ }
 xC | D]; } |	 j | d | j | d ƒ ƒ |
 j | d d ƒ q¼ Wxm t | |  j j ƒ D]V \ } } |	 j | d | | j | d ƒ ƒ |
 j | d |
 j | d ƒ d ƒ qW|
 d k } |
 j | d ƒ | d  k	 r´| j ƒ  |
 j ƒ  k sÄt ‚ n |	 |
 j ƒ  } |
 j | d ƒ | j ƒ  } |  j j
 j d k süt ‚ d |  j j
 j d	 |  _ t j ƒ  |  _ |  j d  k	 rá|  j j | |  j d
 | j | ƒ d | j | ƒ ƒ ƒ xû t | |  j j ƒ D]Y \ } } |  j j | d |  j d
 | j | d ƒ d | j ƒ  | j | d ƒ ƒ ƒ q�Wnˆ |  j d
 | j | ƒ d | j | ƒ ƒ x] t | |  j j ƒ D]F \ } } |  j d
 | j | d ƒ d | j ƒ  | j | d ƒ ƒ qW|  j ƒ  |
 |  _ | |  _  d  S(   NR¥   R¦   Rª   R«   R¬   i    iÿÿÿÿi   i   t   sites_currentRî   (   i    i    i    (!   R   R¶   R·   R   R}   R   t   vec3_doubleR)   R¢   R    R”   R‡   R¹   Rž   Rº   Rl   R3   RÑ   R»   R.   R$   R¼   R   R•   Rµ   R¸   R­   R¦   R´   t   inverseR¿   t   sites_countRî   (   R   R¢   R    R¥   R¦   Rî   R‡   RÁ   R’   t	   sites_sumRô   t   opRp   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   #  sh    			!"%)!		"		(	"	"
	c         C   s€   | | } |  j  j t j | j ƒ  ƒ d ƒ |  j | j ƒ  7_ |  j |  j | j	 ƒ  7_ |  j
 d  k	 r| d |  j | Sd  S(   Ng      à?i   (   R­   R   R   RØ   R×   R°   R}   R¾   Rµ   t   sum_sqR¦   R.   (   R   Rñ   Rî   R³   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR´   `  s    
#Rj   c      	      s$  ˆ d  k r t j ‰ n  t ˆ ƒ ˆ j j ƒ  k s9 t ‚ d ‰ x# ˆ D] } t ˆ t | ƒ ƒ ‰ qF Wd ˆ ‰ ‡  ‡ ‡ ‡ ‡ ‡ ‡ f d †  } ˆ j d k j	 ƒ  } | d d d | | g d d  ƒ } xW t
 t d ƒ ˆ j j j ˆ j j ƒ D]. \ }	 }
 } | | d |	 d |
 d | ƒ 7} qî W| S(   Ni   s     %%%ds: %%8.4fc   
         sA  ˆ ˆ d It  ˆ j j j |  ƒ IJˆ ˆ d ˆ d d IJˆ j j | d ƒ } | d  k r| ˆ j j | d ƒ } n  | j ƒ  ˆ j j | d ƒ } d } x˜ t	 | d | | ƒ D]€ \ } } } t
 t j | ƒ t j | ƒ ƒ }	 ˆ ˆ ˆ ˆ | |	 f Iˆ  d  k	 r5|	 ˆ  k r5ˆ d I| d 7} n  ˆ Jq¹ W| S(   Ns   NCS selection:RÃ   i   s     Distance to NCS averagei   i    t	   EXCESSIVE(   R   R¢   R”   RI   R    Rl   R.   Rî   Ró   R3   t   absR   t   col(
   R[   R’   Rö   Rñ   Rî   t   n_excessiveR6   RÄ   R   t   abs_diff(   t   excessive_distance_limitRÅ   RÆ   RÇ   RÈ   R   RÉ   (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRÊ   s  s$     &"
i    R[   R’   Rö   (   R.   RË   RÌ   R   R    R}   R$   R¹   Rô   R   R3   R
   R¢   R”   R‡   RÑ   (   R   RÉ   Rý   RÇ   RÈ   RÍ   RÊ   Rp   Rû   R[   R’   Rö   (    (   Rý   RÅ   RÆ   RÇ   RÈ   R   RÉ   sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   show_distances_to_averageh  s     !
!!#N(   R   R   R   R´   R.   Rþ   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRï   !  s
   	=	
t   groupsc           B   s˜   e  Z d d  „ Z d „  Z d „  Z e d e d „ Z d d d „ Z	 d „  Z
 e d e d „ Z d d d „ Z d	 „  Z d d d d
 „ Z d d „ Z RS(   c         C   s.   | d  k r g  |  _ n	 | |  _ d  |  _ d  S(   N(   R.   t   membersR¢   (   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR   ‘  s    	c         C   s(   x! |  j  D] } | j d | ƒ q
 Wd  S(   NR›   (   R   Rœ   (   R   R›   R”   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRœ   ˜  s    c         C   s@   g  } x* |  j  D] } | j | j d | ƒ ƒ q Wt d | ƒ S(   NR   R   (   R   R   Rl   Rÿ   (   R   R   R   R”   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRl   œ  s    c   	      C   sÖ   t  j j d | d | d | j ƒ  d t j d | ƒ } g  | _ x† |  j D]{ } | j d  k	 r´ | j d k r´ | j
 d | d | d | d | j ƒ } | | 7} | j j | j ƒ qI | j j d  ƒ qI W| j ƒ  | S(	   NR¥   R¦   Rª   R«   R¬   i    R£   R¤   (   R   R¶   R·   R}   R   R¸   t   rms_with_respect_to_averagesR   R–   R.   R¨   R¦   R   R­   R¿   (	   R   R£   R¤   R¥   R¦   R¬   R   R”   t   contribution(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyR¨   ¢  s*    				
	
Rj   c      	   C   sÉ   xÂ t  |  j ƒ D]± \ } } | | d | d IJ| j d  k	 r¦ | j d k r¦ | j d | d d d t ƒ } | | d | j IJ| j d | d	 | d
 | d ƒ q | | d t | j ƒ IJq Wd  S(   Ns   NCS restraint group %d:i   i    R£   R¤   R¥   s     weight: %.6gRÉ   RÇ   RÈ   s     s.     b_factor_weight: %s  =>  restraints disabled(	   R†   R   R–   R.   R¨   R)   Rµ   RÎ   R1   (   R   R£   RÉ   RÇ   RÈ   t   i_groupR”   R¨   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt#   show_adp_iso_differences_to_average¿  s    			c         C   s=   g  |  _  x- |  j D]" } |  j  j | j  d | ƒ ƒ q Wd  S(   NR    (   R¢   R   R   (   R   R    R”   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   compute_operatorsÔ  s    	c         C   sæ   |  j  d | ƒ t j j d | d | d | j ƒ  d t j d | ƒ } g  | _ x† |  j D]{ } | j	 j
 d  k	 rÄ | j	 j
 d k rÄ | j d | d | d | j ƒ } | | 7} | j j | j ƒ qY | j j d  ƒ qY W| j ƒ  | S(   NR    R¥   R¦   Rª   R«   R¬   i    (   R  R   R¶   R·   R}   R   Rò   R  R¢   R”   R•   R.   Rð   R¦   R   R­   R¿   (   R   R    R¥   R¦   R¬   R   R¢   R  (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRð   Ù  s*    				
	
c      	   C   sl   xe t  |  j ƒ D]T \ } } | | d | d IJ| j d | ƒ } | j d | d | d | d ƒ q Wd  S(   Ns   NCS restraint group %d:i   R    RÇ   RÈ   s     (   R†   R   R¢   Ré   (   R   R    RÇ   RÈ   R  R”   t   ncs_operators(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   show_operatorsõ  s    c         C   s=   g  } x0 |  j  D]% } | j d | ƒ } | j | ƒ q W| S(   NR    (   R   R¢   R   (   R   R    R   R”   R  (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   extract_ncs_groupsû  s
    c         C   sü   d } xï t  |  j ƒ D]Þ \ } } | | d | d IJ| j d  k	 rÙ | j d k rÙ | | d | j IJ| j d | ƒ }	 |	 j d | d t ƒ }
 | | d |
 j IJ| |
 j d | d	 | d
 | d | d ƒ 7} q | | d t	 | j ƒ IJq W| S(   Ni    s   NCS restraint group %d:i   s     coordinate_sigma: %.6gR    R¥   s     weight:  %.6gRÉ   Rý   RÇ   RÈ   s     s/     coordinate_sigma: %s  =>  restraints disabled(
   R†   R   R•   R.   R¢   Rð   R)   Rµ   Rþ   R1   (   R   R    RÉ   Rý   RÇ   RÈ   Rû   R  R”   R¢   Rð   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   show_sites_distances_to_average  s*    
		c         C   sÏ   | d  k rp d } xN |  j D]C } x: | j D]/ } x& | D] } t | t j | ƒ ƒ } q9 Wq, Wq W| d 7} n  t j | t ƒ } xF |  j D]; } x2 | j D]' } x | D] } | j | t ƒ q© Wqœ WqŒ W| S(   Niÿÿÿÿi   (	   R.   R   R‡   R¹   R   t   boolR)   Rº   R   (   R   Rw   R”   R’   Rp   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   selection_restrained  s    $N(   R   R   R.   R   Rœ   Rl   R   R)   R¨   R  R  Rð   R  R  R	  R  (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyRÿ   �  s$   					
(+   t
   __future__R    t   scitbx.array_familyR   t   boost.pythont   boostt   pythont
   import_extt   extR   t   cctbxR   t   scitbx.restraintsR   t   scitbx.mathR   R   t
   scitbx.stlt   libtbx.str_utilsR   t   libtbx.utilsR   t   libtbxR	   t	   itertoolsR
   RË   t   objectR   R   R   R8   R<   Ra   Rb   Rv   R”   R¶   R·   R§   R¡   Rï   Rÿ   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/restraints.pyt   <module>   s6   
	/			Z(_>S@n