ó
Ú™xTc           @   s¾  d  Z  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
 m Z m Z d d l m Z m Z m Z d d l m Z d d	 l	 m Z d d
 l m Z d d l Z d d l Z d Z d e Z d e f d „  ƒ  YZ i d g d 6d g d 6Z d d „ Z d e f d „  ƒ  YZ e d d d „ Z! d e f d „  ƒ  YZ" d d „ Z# e e j$ d „ Z% d e j& f d „  ƒ  YZ' d d e d  „ Z( d d# e e j$ e d d! d" „ Z) d S($   sÜ  
Prototype for building alternate conformations into difference density.
The actual method is a variant of one that Pavel suggested, in combination
with the procedure in mmtbx.building.extend_sidechains: first, the
backbone atoms for a residue and its neighbors are refined into the
target map using minimization and/or annealing, then the sidechain is
replaced (using an idealized copy) and its placement optimized by a grid
search that also allows for backbone flexibility.
iÿÿÿÿ(   t   division(   t   extend_sidechains(   t   alternate_conformations(   t   building(   t   adopt_init_argst   Autot   slots_getstate_setstate(   t   make_headert   make_sub_headert   format_value(   t   null_out(   t   easy_mp(   t   StringIONsm  
  expected_occupancy = None
    .type = float(value_min=0.0,value_max=1.0)
  omit_waters = False
    .type = bool
  window_size = 2
    .type = int
  backbone_sample_angle = 10
    .type = int
  anneal = False
    .type = bool
  annealing_temperature = 1000
    .type = int
  simple_chi1_sampling = True
    .type = bool
  rmsd_min = 0.5
    .type = float
  map_thresholds {
    omit_two_fofc_min_sc_mean = 0.8
      .type = float
    omit_two_fofc_min_mc = 1.0
      .type = float
    omit_fofc_min_sc_mean = 3.0
      .type = float
    two_fofc_min = 1.0
      .type = float
    fofc_min = 3.0
      .type = float
  }
sq  
residue_fitting {
  %s
  #delete_hydrogens = False
  #  .type = bool
}
prefilter {
  include scope mmtbx.building.alternate_conformations.filter_params_str
}
cleanup {
  rsr_after_build = True
    .type = bool
  rsr_fofc_map_target = True
    .type = bool
  rsr_max_cycles = 3
    .type = int
  include scope mmtbx.building.alternate_conformations.finalize_phil_str
}
t   rebuild_residuec           B   s/   e  Z d  Z d „  Z d d e d e d „ Z RS(   sþ  
  Callable wrapper class for rebuilding a single residue at a time.  This is
  not necessarily limited to modeling disorder, but it has been specifically
  designed to fit to a difference map in the presence of backbone and sidechain
  shifts.  Unlike some of the other tools, this method completely removes all
  sidechains within a sliding window before doing anything else.

  Only the target residue is returned; splitting of adjacent residues will be
  essential in most cases but is not handled here.
  c   	      C   sT   t  |  t ƒ  ƒ d d l m } d d  l } | j ƒ  |  _ | j j j ƒ  |  _	 d  S(   Niÿÿÿÿ(   t   idealized_aa(
   R   t   localst   mmtbx.monomer_libraryR   t   mmtbx.monomer_library.servert   residue_dictt
   ideal_dictt   monomer_libraryt   servert   mon_lib_srv(	   t   selft
   target_mapt   pdb_hierarchyt   xray_structuret   geometry_restraints_managert   rotamer_evalt   d_minR   t   mmtbx(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   __init__X   s
    i   i
   iè  c   *      C   s9  d d  l  } d d l m }	 | d  k	 s. t ‚ |  j j ƒ  }
 |  j j ƒ  } |  j	 } |
 j
 ƒ  } | j ƒ  t j | g ƒ } | | d j ƒ  } | j ƒ  } t j | ƒ } | j ƒ  } t | j ƒ  ƒ d k s× t ‚ t j d | d | ƒ } g  } x* | D]" } | | k rü | j | ƒ qü qü Wt j | ƒ |
 j
 ƒ  } | j ƒ  } | j ƒ  } | j | ƒ } | j | ƒ } | j ƒ  | j d k } | r£t j | g ƒ n  |
 j
 ƒ  } | j ƒ  } | j | ƒ } | j ƒ  t j | ƒ } |	 j | j ƒ  t ƒ j | t ƒ } | j | ƒ } t j d	 | d
 |
 d | d d  d |  j  d | j! d |  j" d t# ƒ  d t ƒ 	} | j$ d | j% d d ƒ | j& d | j' ƒ | j( ƒ  }  | j) |  ƒ t* j+ d | d |  j, d t d |  j- d t ƒ }! | j. j/ j0 d | j ƒ }" xS | j
 ƒ  D]E }# x< |! j
 ƒ  D]. }$ |$ j1 |# j1 k r|" j2 |$ j ƒ  ƒ qqWqWt |" j
 ƒ  ƒ }% t | j
 ƒ  ƒ }& |% |& k r t3 d d | j4 ƒ  |& |% f ƒ ‚ n  | j ƒ  }' |' j5 | ƒ |' j6 |" ƒ |
 j
 ƒ  } | j ƒ  | j ƒ  }( |( j7 | ƒ sýt ‚ t j |" g ƒ }) |	 j | j ƒ  t ƒ j |) t ƒ } | j8 | j9 ƒ  ƒ t j d	 | d
 |
 d | d d  d |  j  d | j! d |  j" d t# ƒ  d t ƒ 	} | j t: k rä| rät; d | j< ƒ  d | j= d | j> d |  j? ƒ | j@ ƒ  n | jA d | ƒ | r| jB d | ƒ n  | j( ƒ  }  |
 j
 ƒ  j) |  ƒ t jC |" ƒ S(    Niÿÿÿÿ(   t   flexi    i   t   residuet   window_sizet   GLYt   ALAR   R   t	   selectiont   processed_pdb_fileR   R   R   t   outt   debugt   reference_sigmagš™™™™™¹?R   t	   hydrogensR   t   match_conformationt   resnames.   Inconsistent atom counts for residue %s after s   building (%d versus %d).t	   unit_cellR   t   backbone_sample_anglet   start_temperature(   R#   R$   (D   t   iotbx.pdb.hierarchyt   scitbx.array_familyR    t   Nonet   AssertionErrorR   t	   deep_copyR   t   deep_copy_scatterersR   t   atomst   reset_i_seqR   t   extract_iselectiont   parentt   detached_copyt   is_stub_residuet   lent   atom_groupst   get_window_around_residuet   appendt   remove_sidechain_atomst   extract_namet   extract_i_seqt   selectR,   t   boolt   sizet   Falset   set_selectedt   Truet   box_build_refine_baseR   t   geometryR   R
   t   restrain_atomst   others_in_boxt   real_space_refinet   selection_in_boxt   update_original_coordinatest   set_xyzR   t   extend_residueR   R   t   pdbt	   hierarchyt
   atom_groupt   namet   append_atomt   RuntimeErrort   id_strt   remove_atom_groupt   append_atom_groupt   all_eqt   set_sites_cartt   extract_xyzt   rotatable_sidechain_atomst   fit_chi1_simplet   only_residuet   unit_cell_boxt   target_map_boxR   t   update_sites_from_pdb_atomst   fit_residue_in_boxt   annealt   atom_group_as_hierarchy(*   R   RT   t   logR"   R.   Re   t   annealing_temperaturet   use_chi1_samplingt   iotbxR    R   R   R   t	   pdb_atomst   iselt   atom_group_startt   needs_rebuildt   residue_groupt   sel_residuest   adjacent_residuest   other_rgt   adjacent_trimmed_atom_namest   adjacent_trimmed_selt   xrs_adjacent_trimmedt   grm_adjacent_trimmedt   truncatet
   all_mc_selt   xrs_mct   window_mc_selR%   t   restraints_managert   boxt	   sites_newt   new_atom_group_baset   new_atom_groupt   atomt   new_atomt   n_atoms_newt   n_atoms_startt   rgt	   new_namest   residue_sel(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   __call__e   sØ    	

			

													
									
(   t   __name__t
   __module__t   __doc__R   RF   R‡   (    (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyR   M   s   
	t   OGt   SERt   SGt   CYSi   c         C   sì  d d l  m } |  j ƒ  } | j ƒ  j ƒ  } g  } d }	 }
 |  j t k sS t ‚ xp |  j ƒ  D]b } | j	 j
 ƒ  } | t |  j k r˜ | j | ƒ q` | d k r­ | }	 q` | d k r` | }
 q` q` Wt | ƒ d k sê d |	 |
 g k rî t Sd } d t | ƒ } | } xÎ | d k  rÚd } xi | D]a } | d |	 j d	 |
 j d
 | j d | d t ƒ | _ | j d | j ƒ } | | j | ƒ 7} q&W| j |  ƒ } | | k rÍ| d k rÍ| j ƒ  j ƒ  } | } n  | | 7} qW| j | ƒ d S(   s3  
  For residues with only a single sidechain chi angle, we can instead sample
  the density at sidechain atoms directly instead of using the more opaque
  RSR infrastructure.  (Basically this is just the Ringer method applied to
  the Fo-Fc map.)  This is somewhat of a hack but should be more sensitive.
  iÿÿÿÿ(   t   rotate_point_around_axist   CAt   CBi    g      @ih  t   axis_point_1t   axis_point_2t   pointt   anglet   degt	   site_cartt   OUTLIERN(   t   scitbx.matrixR�   R6   R]   R4   R2   R,   R^   R3   RU   t   stripR?   R<   RF   t   xyzRH   t   fractionalizet   eight_point_interpolationt   evaluate_residueRP   (   R!   R-   R   R   t   sampling_angleR�   t   residue_atomst   sites_startt   sc_atomst   c_alphat   c_betaR€   RU   R•   t   map_level_bestt
   sites_bestt   map_level_sumt	   site_fract   rotamer(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyR_   ò   sH    
	$				t   residue_trialc        	   B   sJ   e  Z d  d d d d d d d d g	 Z d	 „  Z d
 „  Z d d d „ Z RS(   t   new_hierarchyt
   sc_n_atomst   sc_two_fofc_meant   sc_fofc_meant   two_fofc_valuest   fofc_valuest   statst	   occupancyR©   c         C   s   | |  _  | |  _ g  |  _ g  |  _ d |  _ d  |  _ |  _ | j j	 ƒ  } d }	 }
 xÃ | j
 ƒ  D]µ } | j j ƒ  d k s„ t ‚ | j j ƒ  } | j d | j ƒ } | j | ƒ } | j | ƒ } |  j j | ƒ |  j j | ƒ | d k rc |  j d
 7_ |	 | 7}	 |
 | 7}
 qc qc W|  j d k rN|	 |  j |  _ |
 |  j |  _ n  |  j ƒ  } t j | | ƒ |  _ d  |  _ | j d k rœ| j | ƒ |  _ n  d  S(   Ni    t   Ht   DR—   t   Nt   Ct   OR�   R‘   i   R#   R$   t   PRO(   R³   R´   (   Rµ   R¶   R·   s   CAs   CB(   s   GLYs   ALAR¸   (   R«   R²   R¯   R°   R¬   R2   R®   R­   R   R-   R6   t   elementRš   R3   RU   Rœ   R›   R�   R?   t   as_atom_groupt	   alt_confst   coord_stats_for_atom_groupsR±   R©   R,   Rž   (   R   R!   R«   R²   R   t   fmodelt   two_fofc_mapt   fofc_mapR-   t   sc_fofc_sumt   sc_two_fofc_sumR€   RU   R¨   t   two_fofc_valuet
   fofc_valuet   new_conf(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyR   *  s8    					

	c         C   s   |  j  j ƒ  j ƒ  j ƒ  j ƒ  S(   N(   R«   t
   only_modelt
   only_chaint   only_residue_groupt   only_atom_group(   R   (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyRº   I  s    t    c   
   
   C   s€  | d  k r t ƒ  } n  |  j d k } d  } xx t |  j j ƒ  ƒ D]a \ } } | j j ƒ  } | d k rC |  j | | j	 k  r¤ d | |  j | f } t
 } Pq¤ qC qC W|  j d  k	 r|  j | j k  rÒ t
 } q|  j | j k  r|  j | j d k  rt
 } qn  d	 }	 | rd
 }	 n  | | d |  j |  j t d |  j ƒ t d |  j ƒ |	 f IJ| d  k	 r{| | d | IJn  | S(   NR˜   Rµ   R¶   R·   R�   R‘   s   poor backbone: 2Fo-Fc(%s)=%.2fi   RÉ   s    !!!s7   occupancy=%.2f rotamer=%s 2Fo-Fc(sc)=%s  Fo-Fc(sc)=%s%ss   %5fs     %s(   Rµ   R¶   R·   s   CAs   CB(   R2   R
   R©   t	   enumerateR«   R6   RU   Rš   R¯   t   omit_two_fofc_min_mcRH   R®   t   omit_fofc_min_sc_meanR­   t   omit_two_fofc_min_sc_meanR²   R	   (
   R   t   paramsRg   t   prefixt   rejectt   bad_mc_two_fofc_msgt   i_seqR€   RU   t   flag(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   rescoreM  s<     "		N(   Rˆ   R‰   t	   __slots__R   Rº   R2   RÔ   (    (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyRª   &  s   				c         C   sm  | d  k r t ƒ  } n  t j ƒ  } d d l m }	 |	 j ƒ  }
 t j |  d | j ƒ} x' | D] } |
 j	 | j
 ƒ  j ƒ  ƒ q_ Wt |
 ƒ d k r¤ |
 j d ƒ sª t ‚ g  } | j d  k	 ró d | j k oÙ d k n sä t ‚ | j g } n d d d	 d
 g } g  } | j
 ƒ  j ƒ  j ƒ  } d d l m } | j ƒ  } |  j ƒ  } d  } | j r~t j d | d | j d |
 ƒ } n  xy| D]q} d | j d d ƒ | f } d  } | d k rÆ| d } n  t j d | j ƒ  d |
 d | d t d | d d | ƒ \ } } t d | d | d | j d | d | d | j ƒ  j  ƒ  ƒ } | d |  d | j d | j! d | j" d  | j# d! | j$ d" | ƒ } t% d# |  d$ | d% | d | d | d& | d' | ƒ } | j& | ƒ | d k r…t' d( | d) ƒ j( | j) j* ƒ  ƒ q…q…W| j
 ƒ  j ƒ  j ƒ  } | j | ƒ s't ‚ t j ƒ  } | d k ri| d* t | ƒ |  j ƒ  | | f IJn  | S(+   Niÿÿÿÿ(   R    R"   i    g        g      ð?gš™™™™™É?g333333Ó?gš™™™™™Ù?g      à?(   R   R   R   R%   s   %s_%.2ft    t   _i   s   .mtzR½   t   selection_deletet   negate_surroundingt   map_file_namet   partial_occupancyR   R   R   R   RT   R.   Re   Rh   Ri   Rg   R!   R«   R²   R¾   R¿   s   %s.pdbt   ws     %d build trials (%s): %.3fs(+   R2   R
   t   timeR1   R    t   size_tR   R>   R"   t   extendR6   RB   R<   R[   R3   t   expected_occupancyR]   t	   as_doublet   mmtbx.rotamerR   t   RotamerEvalRX   t   omit_waterst   get_nearby_water_selectionR   t   replaceR»   t   get_partial_omit_mapR4   RH   R   t   f_obsR   R.   Re   Rh   t   simple_chi1_samplingRª   R?   t   opent   writeR«   t   as_pdb_string(   R!   R   R½   R{   RÎ   t   verboseR(   Rg   t   t1R    R%   t   windowt
   pdb_objectt   occupanciest   trialst   sites_start_1dR   t   rotamer_managerRX   t   delete_selectionR²   RÏ   RÚ   R¾   R¿   t   rebuildR«   t   trialt   sites_end_1dt   t2(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   find_alternate_residuen  sŒ    (%												*t   find_all_alternatesc           B   s2   e  Z d  Z e e d e j d „ Z d d „ Z	 RS(   s@   
  Wrapper for parallelizing calls to find_alternate_residue.
  c
         C   sÆ   t  |  t ƒ  ƒ t j | ƒ } |	 d IJ| d k r‡ |	 d IJg  |  _ xw t t | ƒ ƒ D]% }
 |  j j |  j |
 d |	 ƒƒ q[ Wn; |	 d | IJt j	 d |  d t t | ƒ ƒ d | ƒ |  _ d  S(	   NRÉ   i   s     running all residues seriallyRg   s     will use %d processest
   fixed_funct   iterablet	   processes(
   R   R   R   t   get_processest   resultst   rangeR<   R?   R‡   t   pool_map(   R   t   residuesR   R½   R{   RÎ   t   nprocRí   R(   Rg   t   i_res(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyR   Â  s    


	&	c         C   sP   t  d |  j | d |  j d |  j d |  j d |  j d |  j d |  j d | ƒ S(	   NR!   R   R½   R{   RÎ   Rí   R(   Rg   (   Rú   R  R   R½   R{   RÎ   Rí   R(   (   R   R  Rg   (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyR‡   Û  s    						N(
   Rˆ   R‰   RŠ   R   RF   R2   t   syst   stdoutR   R‡   (    (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyRû   ¾  s   c   	      C   sè   | d  k r t ƒ  } n  g  } xE |  D]= } | j d | j d | d d ƒ } | r% | j | ƒ q% q% W| }  t |  ƒ d k r‚ d  St |  ƒ d k rœ |  d Sd } d  } x5 |  D]- } | j j | k r¯ | j j } | } q¯ q¯ W| Sd  S(   NRÎ   Rg   RÏ   s       i    i   (   R2   R
   RÔ   t   map_thresholdsR?   R<   R±   t   rmsd(	   Rò   RÎ   R©   Rg   t   filteredR÷   t   acceptt   rmsd_maxt
   best_trial(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   pick_best_alternateæ  s*    	 c      
   C   sÎ  t  | ƒ t  | ƒ k s t ‚ d d l m } d } | j j ƒ  }	 t j | ƒ \ }
 } | j ƒ  } xct	 | | ƒ D]R\ } } | d  k r¦ | d | j ƒ  IJqt n  t  | ƒ d k r¾ qt n  t ƒ  } | d | j ƒ  IJd  } } | j d$ k r| j | ƒ } | d	 k st ‚ n  t d
 | d | d | d | ƒ } | d  k rKqt n  | j ƒ  } | j | k } t } d } | j } | j | j k  r¯| j | j k  r¯| r¯t } n  | d | j IJt ƒ  } t j d | d |	 d |
 d | ƒ } | j d | j j d | j j d | d d ƒ } | j ƒ  } | d k r>t } n  | rT| rTd } n  | d | j | j | | f IJ| r”| d | | j f IJn  | d k r½| d IJ| | j ƒ  IJn
 | d IJ| sÔ| rê| j  | j ƒ  ƒ n  | röqt n  | j! ƒ  } d | _" d } | j# d  k	 r>t$ d t% d  | j# ƒ ƒ } n  x- | j& ƒ  D] } d! | | _' t( j) | _* qKW| j+ ƒ  } d" | _" x) | j& ƒ  D] } t( j, | _* | | _' q�W| j- | ƒ | d# 7} qt W| S(%   Niÿÿÿÿ(   R   i    s   WARNING: error building %ss     %s:R#   R¸   R$   R˜   Rò   RÎ   R©   Rg   RÉ   s"       selected conformer (occ=%.2f):RT   R-   R¾   R¿   t   two_fofc_cutofft   fofc_cutoffR'   RÏ   s         s    ***s5         RMSD=%5.3f  max. change=%.2f  max(Fo-Fc)=%.1f%ss)         starting rotamer=%s  new rotamer=%ss         atoms outside density:t   Ag      à?gš™™™™™É?gš™™™™™é?g      ð?t   Bi   (   s   GLYs   PROs   ALA(.   R<   R3   Râ   R   R   R-   R   t   get_difference_mapsRã   t   zipR2   RX   R   R,   Rž   R  Rº   R©   RF   R±   R	  t   rmsd_mint   max_devRH   R²   t   residue_density_qualityt   show_atoms_outside_densityR  t   two_fofc_mint   fofc_mint   max_fofc_valuet   getvalueRë   R9   t   altlocRà   t   maxt   minR6   t   occR»   t
   SEGID_MAINt   segidR:   t   SEGID_NEW_REBUILTRZ   (   R   R½   t   residues_int   building_trialsRÎ   Rí   Rg   R   t   n_alternatesR-   R¾   R¿   t   rot_evalt	   main_confRò   t   res_logt   main_rotamert   alt_rotamerR  RÄ   t   changed_rotamert   skipRÓ   R±   t   res_log2t   density_qualityt   n_atoms_outside_densityt   fofc_maxRo   t   new_occR€   (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   process_results  s¤    	
			
							

 		t   rsr_fragments_parallelc           B   s   e  Z d  „  Z RS(   c      
   C   sÕ  d d l  m } | j |  j j ƒ  | ƒ } |  j j j ƒ  } | j | ƒ } |  j | d j	 ƒ  } |  j | d j	 ƒ  } d | j
 | j ƒ  | j ƒ  f } t j d |  j j ƒ  d | |  j @d | |  j @d d	 ƒ \ }	 }
 |	 } |  j rö |
 } n  |  j d | d
 | ƒ } | j d t j d d d t ƒ | j d t j d d d t ƒ | j t j ƒ | j t j ƒ | j ƒ  } | j | ƒ } |  j j j | ƒ |  j j | ƒ t j d | d | d | d | j | ƒ ƒ S(   Niÿÿÿÿ(   R    i    s   %s%s-%sR½   R%   RØ   RÛ   g333333ã?R   R)   g{®Gáz”?t   reset_firstgš™™™™™©?t   labelt
   sites_cartR	  (   R1   R    RD   Rk   RE   R½   R   R7  RC   t   fetch_labelst   chain_idt   residR»   Rç   R4   t   sele_main_conft   rsr_fofc_map_targett   make_boxRK   t   SELECTION_OLDRH   t   SELECTION_NEW_REBUILTRF   RM   t   SELECTION_NEW_SPLITt   SELECTION_NEWRO   R\   RP   t   refined_fragmentt   rms_difference(   R   t   fragmentR    t   frag_selectiont   sites_cart_origR¡   t
   atom_startt   atom_endR6  R¾   R¿   R   R|   t   sites_cart_refinedR}   (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyR‡   _  sH    								(   Rˆ   R‰   R‡   (    (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyR4  ^  s   id   c          C   sÌ  d d l  m } d }	 xu|	 | k  r�| d |	 d IJ|  j ƒ  j ƒ  }
 t j d |  d | d | j d	 t ƒ  ƒ } | j j	 }  |  j ƒ  } | j ƒ  } | j
 d
 t ƒ } | j | ƒ |  j ƒ  } | j t j ƒ } | j t j ƒ } t | ƒ d k sþ t ‚ d } g  } xá | t | ƒ k  rí| | rà| j ƒ  } x% | | r\| j | ƒ | d 7} q8W| | d j ƒ  } | | d j ƒ  } | j | j ƒ  | ƒ } |	 d k rÐ|
 j | ƒ } | j d ƒ rÐqqÐn  | j | ƒ q| d 7} qWt | ƒ d k rn  t d |  d | d | d | d |	 d k o6| j j ƒ } t j d | d | d | ƒ } | j  ƒ  } xL | D]D } | d  k	 s…t ‚ | j" d	 | d d ƒ | j# | j | j$ ƒ qmW| j% | ƒ | j& | ƒ | j | ƒ | sï| d k róPn� x5 |  j ƒ  D]' } | j' t j( k r t j) | _' q q W| d IJt j* d |  d | j+ j, d t d t j- ƒ } | d k r| d | IJn P|	 d 7}	 q W|  j. d | j ƒ } | j | d t/ d t/ ƒt0 j0 ƒ  } |  S(   Niÿÿÿÿ(   R    i    s     Cycle %d:i   R   t   cif_objectst   crystal_symmetryR'   t   show_energiesR½   R&   R;  R<  Rü   Rý   Rþ   RÏ   s       s)       checking for conformational strain...t   new_occupancyt   split_all_adjacentR%   s1       split another %d residue(s) - will re-run RSRt   update_f_maskt   update_f_calc(1   R1   R    R6   t   extract_tmp_as_size_tR   t   reprocess_pdbR   R
   t   all_chain_proxiesR   R   RF   t   update_xray_structuret   atom_selection_cacheR%   R»   t   SELECTION_MODIFIEDR>  R<   R3   RÞ   R?   R8  RD   RE   RC   R[   R4  t   cleanupR<  R   R  R]   R2   t   showRG   R7  RP   R\   R"  t   SEGID_NEW_SPLITR#  t   spread_alternatest   residue_fittingRà   R?  t   extract_xray_structureRH   RÝ   (    R   R½   RJ  RÎ   R'   R  t
   max_cyclest	   remediateR    t   i_cyclet   rebuilt_flagsR&   Rk   R   R   t
   sele_cachet
   sele_splitR;  t   kt	   fragmentst   current_fragmentRG  RH  RE  t   flagst   refine_fragmentst   refinedt   sites_refinedt   resultR€   t   n_splitRù   (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyRM   ˆ  sž    			
			
	
	i    c   &      C   sô  d d l  m } d d l m } t j ƒ  } | j j ƒ  } | j t ƒ } | d k r± t r± | d | IJ| } |  j	 | ƒ }  | j j	 | ƒ } | j
 | ƒ | j	 | ƒ } n  |  j ƒ  } | j ƒ  j ƒ  } | j d ƒ s3| d IJxH t | ƒ D]7 \ } } d | _ | j j ƒ  | } | j ƒ  | _ qõ Wn  t | t ƒ r`|  j ƒ  } | j | ƒ } n  t d |
 d	 d
 | ƒ| j ƒ  j d
 | ƒ |	 d k röd d l m } d d l m } | | d t ƒ\ } } | d | d | d d |
 d	 ƒ n  t j  d |  d | d | d | d | j! d | d | ƒ } t j ƒ  } | d | | IJ| j" d | d t ƒ } t# d d
 | ƒt$ d | d |  d | d | d | j% d | j& d | d  |	 d | ƒ 	j' } t j ƒ  }  | d! |  | IJt# d" d
 | ƒt( d |  d | d# | d$ | d | j% d | d | ƒ }! |! d k r�| d IJ| d% |! IJt j) |  d& | j% j* d' t d | ƒ}" |" d k s~t+ ‚ | d( |" IJn
 | d) IJt j ƒ  }# | d* |# |  IJ| j, j- sé|! d k rÜ| d+ IJqa| d, IJnx t# d- d
 | ƒ| d IJt. d |  d | d. | d | d | j& d/ t d
 | ƒ }  t j ƒ  }$ | d IJ| d0 |$ |# IJ| j ƒ  j/ d
 | d1 d2 ƒ t j ƒ  }% t j0 d |  d3 |  j1 d4 | j ƒ d5 | j, j2 d6 | j, j3 d d7 ƒ t j ƒ  }% | d8 |% | IJ|  |! f S(9   Niÿÿÿÿ(   t
   restraints(   R    i    s+   WARNING: %d hydrogen atoms will be removed!RÉ   s"   WARNING: resetting segids to blanks   Build cycle %di   R'   (   t   get_maps_from_fmodel(   t   write_map_coeffst   exclude_free_r_reflectionst
   fwt_coeffst   delfwt_coeffst	   file_names   cycle_%d_start.mtzR   R½   R   R%   RÎ   Rí   Rg   s   filtering: %.3fsRJ   t   normalizations   Finding alternate conformationsR  R{   R  R(   s     building: %.3fss!   Scoring and assembling alternatesR$  R%  s     %d disordered residues builtRM  RN  s     %d adjacent residues splits   No alternates built this round.s     assembly: %.3fss0   Skipping final RSR step (rsr_after_build=False).s$   No refinement needs to be performed.s   Real-space refinementRJ  R^  s
   RSR: %.3fst   texts   Rebuilt modelR   RK  t	   set_b_isot   convert_to_isotropics   altloc A or altloc Bs   Total runtime for cycle: %.3fs(4   R   Rl  R1   R    RÝ   R   t   hd_selectiont   countRH   RC   RT  R6   t   extract_segidRš   R[   RÊ   R"  t
   scatterersRX   R6  t
   isinstancet   strRU  R%   R   t   infot$   show_rfactors_targets_scales_overallt   mmtbx.maps.utilsRm  t   iotbx.map_toolsRn  R»   t   filter_before_buildt	   prefiltert   managerR   Rû   R[  R  R   R3  RZ  Rà   R3   RW  t   rsr_after_buildRM   t   show_targetst   finalize_modelR\  Ru  Rv  (&   R   R½   R   RÎ   R%   RJ  R  R'   Rí   R(   R_  Rl  R    t   t_startt   hd_selt
   n_hydrogent
   non_hd_selR   Rk   t   segidsRÒ   R€   t   scRa  Rm  Rn  t   two_fofct   fofct   candidate_residuesRî   R{   R%  Rù   R&  Rk  t   t3t   t4t   t_end(    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   build_cycleí  sÜ    
											
	

		
		(    (*   RŠ   t
   __future__R    t   mmtbx.buildingR   R   R»   R   R   t   libtbxR   R   R   t   libtbx.str_utilsR   R   R	   t   libtbx.utilsR
   R   t	   cStringIOR   RÝ   R  t   build_params_strt   master_phil_strt   objectR   R^   R_   Rª   RF   R2   Rú   Rû   R  R  R3  t   rsr_fragments_baseR4  RM   R“  (    (    (    sc   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/alternate_conformations/single_residue.pyt   <module>   sJ   
 
/MI,V0a