ó
ž™xTc           @   sb   d  d l  m Z d  d l m Z d  d l m Z d  d l Z d  d l Z	 d e
 f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   division(   t   flex(   t	   superposeNt   asu_ncs_converterc           B   sA   e  Z d  Z d e d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sŠ  
  Simple method of converting multiple NCS copy ASU content into one NCS copy
  and NCS transformations. Limitations:
    - Assumes each chain to be individual NCS copy.
    - Takes first NCS copy as the master copy.
    - Assume all copies are identical (number and order of atoms)
    - Anisotropic ADP are not handlesd.
  Further improvements: use atom selections to identify NCS copies.
  g{®Gáz„?c         C   s¶  | |  _  t j ƒ  } d  } g  |  _ g  |  _ g  |  _ g  |  _ d  |  _ x3 t	 | j
 ƒ  ƒ D] \ } } | j | j ƒ  ƒ q[ Wt } | j | d ƒ rŸxt	 | j
 ƒ  ƒ D]î\ } } | j ƒ  sÎ | j ƒ  rª | j | j ƒ  ƒ | d  k r¨| d k r¨| j ƒ  j ƒ  } t j t | j ƒ  ƒ ƒ }	 | j |	 ƒ |  _ | r¨t j j d ƒ }
 t j j d ƒ } |  j j |
 ƒ |  j j | ƒ |  j j |
 ƒ |  j j | ƒ q¨n  | d k r˜t j d | d | j ƒ  j ƒ  ƒ } |  j j | j j ƒ  ƒ |  j j | j ƒ t j  | | j! ƒ  j" ƒ  ƒ j# ƒ  j$ ƒ  } | d d k rHt% } n  t j d | j ƒ  j ƒ  d | ƒ } |  j j | j ƒ |  j j | j ƒ q˜qª qª Wn  | r²|  j& ƒ  n  d  S(   Ni    i   t   reference_sitest   other_sitesi   (	   i   i    i    i    i   i    i    i    i   (   i    i    i    ('   t   pdb_hierarchyR   t   intt   Nonet   rotation_matricest   translation_vectorst   back_rotation_matricest   back_translation_vectorst   ph_first_chaint	   enumeratet   chainst   appendt
   atoms_sizet   Falset   all_eqt   is_nat
   is_proteint   atomst   extract_xyzt   size_tt   xranget   sizet   selectt   scitbxt   matrixt   sqrt   colR   t   least_squares_fitt   rt	   transposet   tt   sqrtt   other_sites_best_fitt   dott   min_max_meant   as_tuplet   Truet   _init(   t   selfR   t   epst   add_identityt   n_atoms_per_chaint   sites_cart_chain_0t   i_chaint   chaint   outlier_foundt   selt   umt   zvt   lsq_fit_objt   d(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/__init__.pyt   __init__   s\    							  			"			  c         C   s1   g  |  _  g  |  _ g  |  _ g  |  _ d  |  _ d  S(   N(   R	   R
   R   R   R   R   (   R+   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/__init__.pyR*   E   s
    				c         C   s   t  |  j ƒ d k S(   Ni   (   t   lenR	   (   R+   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/__init__.pyt   is_ncs_presentL   s    c         C   sØ   t  j | j ƒ  ƒ t  j |  j j ƒ  j ƒ  j ƒ  ƒ } t  j | j ƒ  t | ƒ ƒ } x| t	 |  j
 j ƒ  ƒ D]e \ } } | d k rœ |  j j ƒ  j | ƒ n  | j ƒ  j |  j | j | | |  j | | ƒ qk Wd  S(   Ni    (   R   t   doublet   meanR   R   R   t   vec3_doubleR   t   listR   R   R   t   set_xyzR   t   elemsR   (   R+   t   sites_cart_master_ncs_copyt   cmR0   R1   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/__init__.pyt   update_sites_cartO   s    %"c         C   s•   | d k s t  ‚ t | d ƒ } | d k rn | t j j d |  j d |  j ƒ IJ| |  j j d | ƒ IJn | |  j	 j d | ƒ IJ| j
 ƒ  d  S(   Nt   asut   ncst   wR	   R
   t   crystal_symmetry(   RD   RE   (   t   AssertionErrort   opent   iotbxt   pdbt   format_MTRIX_pdb_stringR   R   R   t   as_pdb_stringR   t   close(   R+   t	   file_nameRG   t   modet   of(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/__init__.pyt   write_pdb_fileZ   s    		(	   t   __name__t
   __module__t   __doc__R)   R8   R*   R:   RC   RR   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/__init__.pyR      s   	3			(   t
   __future__R    t   scitbx.array_familyR   t   scitbx.mathR   t	   iotbx.pdbRJ   t   scitbx.matrixR   t   objectR   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/__init__.pyt   <module>   s
   