ó
Ÿ™xTc           @   s3  d  Z  d d l m Z d d l m Z m Z d d l m Z d d l m	 Z	 m
 Z
 d d l Z d d l m Z d d l Z e Z d Z d	 „  Z d
 „  Z d e f d „  ƒ  YZ d e d d „ Z d e f d „  ƒ  YZ d „  Z d „  Z d e f d „  ƒ  YZ d d d „ Z d d „ Z d e f d „  ƒ  YZ d S(   s3  
Post-fitting cleanup of ligand positions to match NCS operations present in
protein model.  This can be used to recover cases where one copy is placed
successfully but another misses due to interfering protein atoms, weak density,
false positive in empty protein density, etc.  It can also accelerate ligand
placement for large structures if at least one copy is already placed with high
confidence.  Should only be used when the initial placement (e.g. LigandFit CC)
is sufficiently good.

Usually this will be called via the command mmtbx.apply_ncs_to_ligand.
iÿÿÿÿ(   t   division(   t   Sorryt   null_out(   t   make_sub_header(   t   adopt_init_argst
   group_argsN(   t   sqrts>  
d_min = 2.5
  .type = float
max_rmsd = 2.0
  .type = float
min_cc = 0.7
  .type = float
min_cc_reference = 0.85
  .type = float
min_2fofc = 1.0
  .type = float
min_dist_center = 5
  .type = float
remove_clashing_atoms = True
  .type = bool
clash_cutoff = 2.0
  .type = float
write_sampled_pdbs = False
  .type = bool
c         C   s#   |  j  ƒ  } d | j | j ƒ  f S(   Ns   %s %s(   t   fetch_labelst   resnamet   resid(   t   atomt   labels(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt	   resid_str/   s    c         C   sH   |  \ } } } | \ } } } t  | | d | | d | | d ƒ S(   Ni   (   R   (   t   xyz1t   xyz2t   x1t   y1t   z1t   x2t   y2t   z2(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   xyz_distance3   s    t   group_operatorsc           B   s8   e  Z d  Z d „  Z d „  Z d „  Z d d d „ Z RS(   s  
  Object for storing information about NCS relationships relative to an
  atom selection, i.e. given four identical chains A, B, C, and D, this might
  store information about chain B and the NCS operators for approximate
  superpositioning on chains A, C, and D.
  c         C   s@   | |  _  | |  _ | j | ƒ j ƒ  |  _ g  |  _ g  |  _ d  S(   N(   t	   selectiont   selection_stringt   selectt   meant   center_of_masst	   operatorst   op_selections(   t   selfR   t   sele_strt
   sites_cart(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   __init__?   s
    			c         C   s$   |  j  j | ƒ |  j j | ƒ d S(   s@   
    Save an NCS operator and corresponding atom selection.
    N(   R   t   appendR   (   R   t   opsR   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   add_operatorF   s    c         C   s   | j  ƒ  } t | |  j ƒ S(   s˜   
    Compute the distance between centers-of-mass of this selection and the
    given sites.  Used to determine ligand-protein chain relationships.
    (   R   R   R   (   R   t   sitest   c_o_m(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   distance_from_centerM   s    t    c         C   s  | d k r t j } n  | | d |  j IJxÐ t |  j |  j ƒ D]¹ \ } } | | d | IJ| | d IJ| | d | j j d d !IJ| | d | j j d d !IJ| | d | j j d d !IJ| | d	 IJ| | d | j	 j IJ| d
 IJqC W| d
 IJd S(   s1   
    Print out selections and NCS operators.
    s   Reference selection: %ss     Selection: %ss     Rotation:s       %6.4f  %6.4f  %6.4fi    i   i   i	   s     Translation:R(   N(
   t   Nonet   syst   stdoutR   t   zipR   R   t   rt   elemst   t(   R   t   outt   prefixt   opt   op_sele(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   show_summaryU   s     "N(   t   __name__t
   __module__t   __doc__R!   R$   R'   R)   R4   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR   8   s
   			g       @c   &   
      sS  d d l  m } d d l m } d d l m } | j |  d | ƒ} t | ƒ d k rf t d ƒ ‚ n  xK t	 | ƒ D]= \ } }	 | d | d	 IJx |	 D] }
 | d
 |
 IJq˜ Wqs W|  j
 ƒ  ‰  |  j ƒ  } | j ƒ  } g  } ‡  f d †  } x_| D]W} g  } t | ƒ d k st ‚ x#t	 | ƒ D]\ } } | | ƒ } t | | | ƒ }	 t | ƒ d k sgt ‚ g  } x: | D]2 } t | | ƒ } | | k rt| j | ƒ qtqtWx€t	 | ƒ D]r\ } } | | k rÕq·n  | | ƒ } | j ƒ  } t g  ƒ } t | ƒ d k st ‚ xY | D]Q } t | | ƒ } | | k r@qn  | j | ƒ | | k r| j | ƒ qqW| j ƒ  } g  } x9 t | | ƒ D]( \ } } | | k r�| j | ƒ q�q�Wt | ƒ t | ƒ k sÙt ‚ t | ƒ d k sñt ‚ | j | ƒ } | j | ƒ j ƒ  } | j d | d | ƒ }  |  j j | |  j j }! | j |! ƒ }" | rÕ| j d | d | d d ƒ }# |# j j | |# j j }$ | j |! ƒ }% |% |" k  rÕ| d |% |" f IJ|# }  |% }" qÕn  | d | d	 | d	 |" f IJ|" | k r|	 j |  j ƒ  j ƒ  | ƒ q·| d IJq·W| j |	 ƒ q%W| j | ƒ qô W| S(   s;  
  Determines all possible NCS transformation matrices for the input structure,
  based on sequence alignemnt and simple C-alpha superposition.  There may be
  multiple sets of operators but these will eventually become a flat list.

  :param max_rmsd: maximum allowable RMSD between NCS-related chains for use
    in ligand superposition
  :param try_sieve_fit: also perform a sieve fit between chains and use the
    resulting operator if the RMSD is lower than the global fit
  :param log: filehandle-like object
  :returns: list of lists of group_operators objects
  iÿÿÿÿ(   t   torsion_ncs(   t	   superpose(   t   flext   logi    s"   No NCS present in the input model.s	   Group %d:i   s     %sc            s   d |  }  ˆ  j  |  ƒ j ƒ  S(   Ns-   (%s) and name CA and (altloc ' ' or altloc A)(   R   t
   iselection(   R   (   t   selection_cache(    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   get_selectionƒ   s    
i   t   reference_sitest   other_sitest   sites_fixedt   sites_movingt   frac_discardg      Ð?s1     using sieve fit (RMSD = %.3f, RMSD(all) = %.3f)s     %d versus %d RMSD = %.3fs,     exceeds cutoff, will not use this operator(    t   mmtbx.torsion_restraintsR8   t   scitbx.mathR9   t   scitbx.array_familyR:   t   determine_ncs_groupst   lenR   t	   enumeratet   atom_selection_cachet   atomst   extract_xyzt   AssertionErrorR   R   R"   t   size_tt   sett   addR,   R   t	   deep_copyt   least_squares_fitR-   R.   R/   t   rms_differencet	   sieve_fitR$   t   rtt   inverse(&   t   pdb_hierarchyt   max_rmsdt   try_sieve_fitR;   R8   R9   R:   t
   ncs_groupst   kt   groupt   selet	   pdb_atomsR    R   R>   t   restraint_groupt	   group_opst   jR   t   sele_jt
   calpha_idst   i_seqR	   t
   sele_str_kt   sele_kt
   group_selet	   group_idst   id_strt   first_sele_copyt   delete_indicest	   sites_reft   sites_groupt   lsq_fitt	   sites_fitt   rmsdt	   lsq_fit_2t   sites_fit_2t   rmsd_2(    (   R=   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   find_ncs_operatorsf   s”     				t   sample_operatorsc           B   s8   e  Z d  Z d d „ Z d „  Z d „  Z d d „ Z RS(   sÌ   
  Determines an appropriate "reference" ligand, and samples the density around
  sites transformed by each operator, applying the best operator to the
  other ligand copies if meeting cutoff criteria.
  c   #         sý  ˆ  d  k r t j ‰  n  | d  k r< t ƒ  j ƒ  j ƒ  } n  t |  t ƒ  ƒ | j j	 ƒ  |  _ | j j	 ƒ  } d d l
 m } |  j ƒ  d }	 d }
 d  } g  } ˆ  d IJ‡  f d †  } x€ t | ƒ D]r \ } } | j ƒ  } |  j | ƒ } | d | d | ƒ | j |	 k rÅ | j | j k rÅ | } | }
 |	 }	 qÅ qÅ W| d  k r]t d | j ƒ ‚ n  | j ƒ  } x6 t | ƒ D]( \ } } | | k	 rv| j | ƒ qvqvWˆ  d	 |
 d IJˆ  d
 IJ| j ƒ  j ƒ  } t j } d  } x8 | D]0 } | j | ƒ } | | k  ræ| } | } qæqæW| d  k	 r:ˆ  d | j IJn  g  |  _ x³t | j ƒ D]¢\ } } | j ƒ  } | j ƒ  } | j j | | j j } | j ƒ  } xU| D]X } | j ƒ  j ƒ  j ƒ  } t | | j ƒ  ƒ } | | j  k  r¤ˆ  d | d IJPq¤q¤W| j! | ƒ |  j | | ƒ } | d | d | ƒ | j" rµ| j# ƒ  }  | d |  _$ |  j% | ƒ t& d | d d ƒ }! x+ | j ƒ  D] }" |! j' |" j( ƒ  d ƒ q‡W|! j) ƒ  n  | j | j* k rSˆ  d | d | j f IJ|  j j | ƒ qSqSWd  S(   Niÿÿÿÿ(   t	   hierarchyi    s   Identifying reference ligand...c            s!   ˆ  d |  | j  | j f IJd  S(   Ns      %s: CC = %5.3f  mean = %6.2f(   t   cct   map_mean(   R1   t   stats(   R;   (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   show_map_statsà   s    s	   Ligand %di   s+   No ligand with acceptable CC (>%.2f) found.s.   Copy #%d was the best, using that as referenceR(   s.   This appears to be bound to the selection "%s"s*     operator %d specifies an existing ligands   NCS op. %2ds   ncs_ligand_%d.pdbt   ws   
s&     operator %d has acceptable CC (%.3f)(+   R)   R*   R+   t   master_philt   fetcht   extractR   t   localst   xray_structuret   deep_copy_scattererst	   iotbx.pdbRv   t
   setup_mapsRI   RK   t   get_sites_ccRw   t   min_cc_referenceR   R"   RL   t   maxintR'   R   t   new_ligandsR   t   detached_copyR-   R.   R/   R   R   t   min_dist_centert   set_xyzt   write_sampled_pdbst   residue_groupt   resseqt   append_atom_groupt   opent   writet   format_atom_recordt   closet   min_cc(#   R   RW   t   fmodelt   ncs_operatorst   ligandst   paramsR;   t   xrs_ncsRv   t   best_cct   best_kt   best_ligandt   other_ligandsRz   R[   t   ligandRK   t   startt
   best_atomsRl   t   min_distt
   best_groupt   op_groupt   dxyzRa   t   operatort
   new_ligandt	   sites_newt
   sites_meant   othert   sites_other_meant	   stats_newt   lig_rgt   fR
   (    (   R;   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR!   Ì   sŒ     

!
			c         C   sô   |  j  j ƒ  } | j d ƒ } | j d ƒ } | j d d ƒ } | j d d	 ƒ } | j ƒ  | j ƒ  | j d ƒ } | j d d
 ƒ } | j ƒ  | j ƒ  |  _ | j ƒ  |  _ | j ƒ  |  _ |  j j ƒ  |  _	 |  j j
 ƒ  |  _ | j ƒ  |  _ d S(   s0   
    Create 2mFo-DFc, mFo-DFc, and Fc maps.
    s   2mFo-DFcs   mFo-DFct   resolution_factori   g      @t   FcNgUUUUUUÕ?gUUUUUUÕ?gUUUUUUÕ?(   R”   t   electron_density_mapt   map_coefficientst   fft_mapt   apply_sigma_scalingt	   unit_cellt   real_mapt   diff_mapt   focust   n_realt   allt   m_realt   fcalc_real_map(   R   t
   map_helpert
   map_coeffst   diff_map_coeffsR±   t   diff_fft_mapt   fcalct	   fcalc_map(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyRƒ      s     


c         C   s·   |  j  j ƒ  } | j ƒ  } | j | | ƒ | j | ƒ |  j j d | d t d t ƒ |  j j ƒ  j	 d ƒ } | j
 d d ƒ } | j ƒ  |  j j d |  j  d t d t ƒ | j ƒ  S(	   NR€   t   update_f_calct   update_f_maskR®   R­   i   g      @gUUUUUUÕ?(   R€   R�   R    t   set_selectedt   set_sites_cartR”   t   update_xray_structuret   TrueR¯   R°   R±   R²   R´   (   R   R¦   t   i_seqst   xrs_newt	   all_sitesR¿   RÀ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   get_new_fcalc_map5  s     
	c         C   sE  d d l  m } d d l m } | j ƒ  } x@ | D]8 } | j j ƒ  d k r^ | j d ƒ q3 | j d ƒ q3 W|  j } | d  k r“ | j
 ƒ  } n |  j d | d	 | j ƒ  ƒ } | j d
 |  j d |  j d |  j d | d | ƒ } |  j j | ƒ }	 | j | ƒ }
 | j d |	 d |
 ƒ j ƒ  } t d | d | j |	 j ƒ  ƒ ƒ S(   Niÿÿÿÿ(   t   maptbx(   R:   t   Ht   Dg      ð?g      ø?R¦   RÇ   R³   t
   fft_n_realt
   fft_m_realR    t
   site_radiit   xt   yRw   Rx   (   RÌ   RÍ   (   t   cctbxRË   RF   R:   t   doublet   elementt   stripR"   Rº   R)   RL   RÊ   t   extract_i_seqt   grid_indices_around_sitesR³   R·   R¹   R´   R   t   linear_correlationt   coefficientR   R   t   as_1d(   R   RK   R%   RË   R:   t   radiiR
   RÀ   t   selt   m1t   m2Rw   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR„   H  s2    							N(   R5   R6   R7   R)   R!   Rƒ   RÊ   R„   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyRu   Æ   s
   N		c         C   sR   |  j  ƒ  } | j  ƒ  } | j ƒ  | j ƒ  k oQ | j | j k oQ | j | j k S(   N(   R   R	   t   chain_idt   altloc(   t   atom1t   atom2t   labels1t   labels2(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   _is_same_residuee  s
    c      
      s  d d l  m } | j ƒ  } | j | j ƒ  t ƒ } |  j d | j ƒ } | j ƒ  }	 t	 g  ƒ }
 x | D]w } | j ƒ  j
 ƒ  } x\ | D]T } |	 | } xA | j ƒ  D]3 \ } } t | | | | ƒ s¤ |
 j | ƒ q¤ q¤ Wq‡ Wqh Wt |
 ƒ d k r	‡  f d †  } g  } g  |
 D] } | | ^ q} x˜| D]�} | j | k rIq.n  | j ƒ  } | j ƒ  } | j ƒ  } | j ƒ  } | j ƒ  } | j d k rÁ| j | j
 ƒ  ƒ | j | ƒ | | ƒ q.| j ƒ  d j ƒ  rr| j j ƒ  d k r| | ƒ | j | j
 ƒ  ƒ | j | ƒ q¾x¢ | D]K } | j j ƒ  d k rAq q | | g ƒ | j | j ƒ | j | ƒ q Wq.| j | j ƒ | | g ƒ | j | ƒ t | ƒ d k r.| j | ƒ q.q.Wt | ƒ } ˆ  d | IJx | D] } t | | <qãW|  j | ƒ } n |  } | S(   s�   
  Since the transformed ligands will very frequently overlap with existing
  atoms, these need to be deleted if we are confident about the new positions.
  iÿÿÿÿ(   R:   t   distance_cutoffi    c            s)   x" |  D] } ˆ  d | j  ƒ  IJq Wd  S(   Ns     warning: deleting atom %s(   Ri   (   RK   R
   (   R;   (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   show_removedˆ  s    t   HOHt   Nt   Ct   CAt   CBt   ORÌ   t   HAi   s/   %d atoms removed due to clashes with ligand(s).(   Rê   Rë   Rì   Rí   Rî   RÌ   Rï   (   Rê   Rë   Rì   Rí   Rî   RÌ   Rï   (   RF   R:   RK   t   boolt   sizeRÆ   t   pair_asu_tablet   clash_cutofft   tableRO   R×   t   itemsRæ   RP   RH   Rd   R   t   parentR   t   extendt   remove_residue_groupt
   conformerst
   is_proteint   nameRÖ   R"   t   remove_atomt   FalseR   (   R€   RW   R–   R—   R;   R:   R^   R   Rò   t	   asu_tablet   remove_atomsR�   RÇ   Rd   t   asu_dictt   j_seqt   sym_opsRè   t   deletedt	   bad_atomsR
   R   t
   atom_groupRŒ   t   other_atomst   chainRã   t	   n_removedRÈ   (    (   R;   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   remove_clashing_atomsl  sj    
	
 
t   get_final_maps_and_ccc           B   s   e  Z d  „  Z d „  Z RS(   c         C   sö  d d l  m } d d l m } | j ƒ  } | j d ƒ |  _ | j d ƒ |  _ |  j j d d ƒ } | j	 ƒ  | j d ƒ }	 |	 j d d ƒ }
 |
 j	 ƒ  | j
 ƒ  } |
 j
 ƒ  } g  } x$t | ƒ D]\ } } | j ƒ  } | j ƒ  } | j ƒ  } xC | D]; } | j j ƒ  d k rü | j | j ƒ | j d ƒ qü qü W| j d |  j j ƒ  d | j ƒ  d | j ƒ  d | d | ƒ } | j | ƒ } | j | ƒ } | j d | d | ƒ j ƒ  } | j | ƒ | d | d | f IJqÅ W| d IJ| |  _ d  S(   Niÿÿÿÿ(   RË   (   R:   s   2mFo-DFcs   mFo-DFcR­   g      Ð?R®   RÌ   RÍ   g      ø?R³   RÎ   RÏ   R    RÐ   RÑ   RÒ   s     Ligand %d: CC = %5.3fi   R(   (   RÌ   RÍ   (   RÓ   RË   RF   R:   R¯   R°   t   two_fofc_map_coeffst   fofc_map_coeffsR±   R²   R´   RI   RK   t   vec3_doubleRÔ   RÕ   RÖ   R"   t   xyzRØ   R³   R¶   R¸   R   RÙ   RÚ   t   final_cc(   R   R”   R–   R—   R;   RË   R:   R»   R±   R¿   RÀ   R´   Rº   R  R[   R�   RK   R%   RÜ   R
   RÝ   RÞ   Rß   Rw   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR!   ¶  sD    

		
c         C   so   d d  l  } | j j d d ƒ } |  j j d d d | ƒ } | j |  j d d d | ƒ| j ƒ  j | ƒ d  S(   Niÿÿÿÿt   phases_prefixt   PHt   column_root_labelt   2FOFCWTt   label_decoratort   FOFCWT(	   t	   iotbx.mtzt   mtzR  R  t   as_mtz_datasett   add_miller_arrayR  t
   mtz_objectR�   (   R   t	   file_namet   iotbxt   dect   mtz_dat(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt
   write_mapsÞ  s    	(   R5   R6   R!   R  (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR
  µ  s   	(c   
      C   s'  t  |  j ƒ  ƒ d k s t ‚ g  } xü |  j ƒ  d j ƒ  D]ä } xÛ | j ƒ  D]Í } xÄ | j ƒ  D]¶ } | j | k ra t } | d k	 r» x0 | j	 ƒ  D] }	 |	 j
 | k r• t } Pq• q• Wn  | rþ | d k	 rþ x. | j	 ƒ  D] }	 | |	 j sÚ t } PqÚ qÚ Wn  | r| j | ƒ qqa qa WqN Wq; W| S(   s\  
  Extract the atom_group object(s) with given 3-character residue name.

  :param pdb_hierarchy: input model
  :param ligand_code: 3-letter residue ID
  :param atom_selection: optional flex.bool object specifying ligand selection
      to use
  :param only_segid: optional segid which ligand(s) must match
  :returns: list of atom_group objects
  i   i    N(   RH   t   modelsRM   t   chainst   residue_groupst   atom_groupsR   RÆ   R)   RK   t   segidRý   Rd   R"   (
   RW   t   ligand_codet   atom_selectiont
   only_segidR–   R  RŒ   R  t
   use_ligandR
   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   extract_ligand_residuesé  s(    c         C   sí  d d l  m } | d  k r( t ƒ  } n  i  } |  j ƒ  d } x¨t | ƒ D]š\ } } | j ƒ  j ƒ  j ƒ  } d  }	 t	 j
 }
 x° | j ƒ  D]¢ } | j ƒ  d j ƒ  } | j | k sÉ | | j | k  rÙ | | | j <n  | j ƒ  së q‹ n  | j ƒ  j ƒ  j ƒ  } t | | ƒ } | |
 k  r‹ | }
 | }	 q‹ q‹ Wd } |	 d  k	 rO|	 j } n  | j d | ƒ } | j ƒ  } d } | | k r�| | d } n  | d | d | | f IJ| | _ | j | ƒ | j | ƒ | j | ƒ | | | <qK Wd  S(   Niÿÿÿÿ(   Rv   i    t    t   idi   s!     ligand %d: chain='%s' resseq=%s(   R‚   Rv   R)   R   R   RI   RK   RL   R   R*   R†   R!  R"  t   resseq_as_intR+  Rú   R   R  RŒ   R�   RŽ   t   append_residue_groupt   append_chain(   RW   R–   R;   Rv   t   chain_id_countst   modelt   i_ligR�   t   xyz_meant
   best_chainR    R  t   last_resseqt   chain_xyz_meant   distt   best_chain_idt	   new_chaint   new_rgt
   new_resseq(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   combine_ligands_and_hierarchy  sH     	 	t   apply_ligand_ncsc           B   sD   e  Z d  Z d e d d d „ Z e d „  ƒ Z d „  Z d „  Z	 RS(   s™  
  Wrapper class; this should be the primary entry point for external calling
  routines.

  :param pdb_hierarchy: initial model with one or more copies of the target
    ligand
  :param fmodel: mmtbx.f_model.manager object corresponding to the model
  :param ligand_code: three-letter residue name of ligand
  :param params: phil scope_extract object for ncs_ligand_phil
  :param atom_selection: selection for reference ligand (default: search for
    all copies and pick one with the best CC)
  :param add_new_ligands_to_pdb: generate combined PDB hierarchy and fmodel
    with new ligands incorporated
  :param only_segid:
  :param log: filehandle-like object
  c	         C   s§  | d  k r t j } n  | d  k	 r6 t | ƒ d k s< t ‚ t d | ƒ t | d | j d | ƒ}	 g  }
 x |	 D] } |
 j | ƒ qq W| d IJx> |	 D]6 } x- t	 | ƒ D] \ } } | j
 | d d ƒq¬ Wq™ W| d | IJt | | d	 | ƒ} t | ƒ d
 k rt d ƒ ‚ n  | j ƒ  j ƒ  t d | ƒ t d | d | d |
 d | d | d | ƒ } t | j ƒ d
 k r1| j rØt d | ƒ t d | j d | d | j d | d | ƒ } | j d | d t d t ƒ n  | r1t d | d | j d | ƒ | j d | j ƒ } | j d | d t d t ƒ q1n  t | | d	 | ƒ|  _ t d | d |  j d | d | ƒ |  _ t | ƒ |  _ t | j ƒ |  _ | |  _ | |  _ d  S(   Ni   s   Determining NCS operatorsRX   R;   s   Summary of NCS operators:R1   s     s   Looking for ligands named %s...R'  i    s   No ligands found!s   Applying NCS to ligandsR”   RW   R•   R—   R–   s   Removing clashing atomsR€   RÁ   RÂ   t   crystal_symmetry(   R)   R*   R+   RH   RM   R   Rt   RX   R÷   RI   R4   R)  R   RK   t   reset_i_seqRu   R‡   R	  R€   RÅ   RÆ   R;  t   extract_xray_structuret   final_ligandsR
  R  t   n_ligands_startt   n_ligands_newRW   R”   (   R   RW   R”   R%  R—   R&  t   add_new_ligands_to_pdbR'  R;   t   ncs_opst   ncs_ops_flatR¢   t	   ncs_groupR[   R\   R–   t   samplerRÈ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR!   C  s~    	 $			
							
					c         C   s   |  j  |  j S(   N(   RA  RB  (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt	   n_ligands‹  s    c         C   s?   t  | d ƒ } | j |  j j d |  j j ƒ ƒ | j ƒ  d  S(   NR{   R=  (   R�   R�   RW   t   as_pdb_stringR”   R€   R’   (   R   R  R¬   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt	   write_pdb�  s    c         C   s   |  j  j | ƒ d  S(   N(   R  R  (   R   R  (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR  •  s    N(
   R5   R6   R7   R)   Rý   R!   t   propertyRH  RJ  R  (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyR<  2  s   @	(   R7   t
   __future__R    t   libtbx.utilsR   R   t   libtbx.str_utilsR   t   libtbxR   R   t   copyt   mathR   R*   RÆ   t   debugt   ncs_ligand_philR   R   t   objectR   R)   Rt   Ru   Ræ   R	  R
  R)  R;  R<  (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/ncs/ligands.pyt   <module>   s,   		._Ÿ		I7 %