ó
Ù™xTc           @   s7  d  d l  m Z d  d l m Z m Z d  d l Z e j d d „ Z d „  Z e j d „ Z	 d d	 „ Z
 d
 „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d d „ Z d e f d „  ƒ  YZ d „  Z d d „ Z e d „ Z d e f d „  ƒ  YZ e f  d e d e e d e e d „
 Z d „  Z d S(    iÿÿÿÿ(   t   division(   t
   group_argst   adopt_init_argsNt    c         C   s·  i  } d
 } } xç t |  ƒ D]Ù \ } } | } t | ƒ j d k rS | j ƒ  } n  | j ƒ  }	 |	 j | k r„ g  | |	 j <d
 } n  | j ƒ  }
 | d
 k s¬ |
 | d k rÏ | |	 j j |
 | j f g ƒ n! | |	 j d j |
 | j f ƒ |
 } q Wd „  } x­ t	 | j
 ƒ  ƒ D]™ } g  } xi | | D]] } t | ƒ d k rs| j d | | d Œ  | | d Œ  f ƒ q-| j | | d Œ  ƒ q-W| | d | d	 j | ƒ f IJqWd
 S(   sˆ   
  Given a list of residues (either residue_group or atom_group objects),
  print a summary of the chains and residue ranges present.
  t
   atom_groupi   iÿÿÿÿc         S   s   d |  | f j  ƒ  S(   Ns   %s%s(   t   strip(   t   resseqt   icode(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   format_resid   s    i   s   %s-%si    s   chain '%s': %st   ,N(   t   Nonet	   enumeratet   typet   __name__t   parentt   idt   resseq_as_intt   appendR   t   sortedt   keyst   lent   join(   t   residuest   outt   prefixt   rangest
   prev_chaint   prev_resseqt   i_rest   residuet   rgt   chainR   R   t   chain_idt   clausest   range(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   show_chain_resseq_ranges   s0    
	#!
	c   
      C   s¤   d d l  m } d d l  m } | j ƒ  } | j ƒ  } x5 | D]- } x$ | | g D] }	 |	 j d | ƒ qR Wq? W| j d | d | d |  j d | ƒ d | d	 | ƒ S(
   Niÿÿÿÿ(   t   pdb_interpretation(   t   servert
   cif_objectt   mon_lib_srvt   ener_libt   raw_recordst   crystal_symmetryt   log(   t   mmtbx.monomer_libraryR$   R%   R(   t   process_cif_objectt   processt   as_pdb_string(
   t   pdb_hierarchyR*   t   cif_objectsR   R$   R%   R'   R(   R&   t   srv(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   reprocess_pdb)   s    	c         C   s¿   d d  l  } d  } | d  k	 rQ | j ƒ  j ƒ  j ƒ  } d | k pK d | k } n  |  j d t d t d d  d d d	 d  d
 | ƒ } | j j d | d t	 ƒ } | d  k	 r» | j
 ƒ  | _
 n  | S(   Niÿÿÿÿt   Ht   Dt   show_energiest   show_nonbonded_clashscoret   params_editst   plain_pairs_radiusi   t   hydrogen_bond_proxiest   assume_hydrogens_all_missingt   geometryt   normalization(   t   mmtbx.restraintsR
   t   scattering_type_registryt   type_count_dictR   t   geometry_restraints_managert   Falset
   restraintst   managert   TrueR*   (   t   processed_pdb_filet   xray_structureR+   t   mmtbxt   has_hdt	   sctr_keysR<   t   restraints_manager(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   get_restraints_manager8   s$    	
	i   c         C   s;   |  j  ƒ  } | j d ƒ } | j d | d | ƒ } | | @S(   Ns   resname HOHt   radiust	   selection(   t   atom_selection_cacheRN   t   selection_within(   R0   RG   RN   t   selection_buffer_radiust	   sel_cachet   water_selectionRP   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   get_nearby_water_selectionM   s    		c         c   s=   x6 |  j  ƒ  j ƒ  D]" } x | j ƒ  D] } | Vq& Wq Wd  S(   N(   t
   only_modelt   chainst   residue_groups(   t	   hierarchyR   t   residue_group(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   iter_residue_groupsY   s    c         C   s`   d d l  m } | j ƒ  } x' |  D] } | j | j ƒ  j ƒ  ƒ q# W| j d ƒ s\ t ‚ | S(   Niÿÿÿÿ(   t   flexi    (   t   scitbx.array_familyR[   t   size_tt   extendt   atomst   extract_i_seqt   all_eqt   AssertionError(   t   pdb_objectsR[   t   iselt   pdb_obj(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   extract_iselection^   s    c         C   s]   xV |  D]N } | j  ƒ  } x9 | D]1 } | j j ƒ  d k r  | j ƒ  j | ƒ q  q  Wq Wd  S(   Nt   CAt   Ct   Nt   Ot   CB(   Rg   Rh   Ri   Rj   Rk   (   R_   t   nameR   R   t   remove_atom(   Rc   Re   R_   t   atom(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   remove_sidechain_atomsf   s
    c      	   C   sS   |  j  d
 k r t St } x2 |  j ƒ  D]$ } | j j ƒ  d k r& t } q& q& W| S(   Nt   GLYt   ALAR4   t   HARh   Rg   Rk   Ri   Rj   (   Rp   Rq   (   R4   Rr   Rh   s   CAs   CBRi   Rj   (   t   resnameRE   RB   R_   Rl   R   (   R   t   has_sidechain_atomsRn   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   is_stub_residuem   s    c         C   s_   d d l  m } | j ƒ  } x< |  j ƒ  D]. } | j j ƒ  d k r) | j | j ƒ q) q) W| S(   Niÿÿÿÿ(   R[   R4   R5   (   R4   R5   (   R\   R[   R]   R_   t   elementR   R   t   i_seq(   t
   pdb_objectR[   t   i_seqs_no_hdRn   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   get_non_hydrogen_atom_indicesv   s    i   c   
      C   sÍ   |  j  ƒ  } | j  ƒ  } | j ƒ  } g  } x„ t | ƒ D]v \ } } | | k r7 x[ t | | d ƒ D]B } | | }	 |	 d k rd |	 t | ƒ k  rd | j | |	 ƒ qd qd Wq7 q7 Wt | ƒ d k sÉ t ‚ | S(   Ni   i    (   R   RW   R   R"   R   R   Rb   (
   R   t   window_sizeRY   R   t   all_residuest   sel_residuesR   t   other_rgt   j_rest   k_res(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   get_window_around_residue~   s    
c   	      C   s÷   d d l  } |  j ƒ  } | d k	 s* t ‚ | j ƒ  } | j j j ƒ  } | j j j ƒ  } | j j j d | j	 ƒ } | j j j
 d | j d | j ƒ } | j |  j ƒ  ƒ | j | ƒ | j | ƒ | j | ƒ | j ƒ  } | j ƒ  | j ƒ  | S(   sÌ   
  Convert an iotbx.pdb.hierarchy.atom_group object to a separate hierarchy
  (leaving the original hierarchy untouched), which allows us to use the
  pickling capabilities of iotbx.pdb.hierarchy.root.
  iÿÿÿÿNR   R   R   (   t   iotbx.pdb.hierarchyR   R
   Rb   t   pdbRX   t   roott   modelR   R   RY   R   R   t   append_atom_groupt   detached_copyt   append_residue_groupt   append_chaint   append_modelR_   t   reset_i_seqt   reset_serial(	   R   t   iotbxt   old_rgt	   old_chainR„   R…   R   RY   R_   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   atom_group_as_hierarchyŒ   s$    	

g      @c         C   sï   t  |  ƒ j d k r( |  j ƒ  d }  n  t  | ƒ j d k rP | j ƒ  d } n  d  } } x3 |  j ƒ  D]% } | j j ƒ  d k rg | } Pqg qg Wx3 | j ƒ  D]% } | j j ƒ  d k r� | } Pq� q� Wd  | | g k rÜ t S| j | ƒ | k  S(   NRY   i    Rh   Ri   (	   R   R   t   atom_groupsR
   R_   Rl   R   RB   t   distance(   t   residue1t   residue2t   max_sept   c_pept   n_pepRn   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   residues_are_adjacent¥   s     
t   local_density_qualityc           B   s�   e  Z d d d d d d  d „ Z d d „ Z d d „ Z d d e d „ Z d „  Z d  d	 „ Z	 d
 „  Z
 d „  Z d „  Z d „  Z d „  Z RS(   g      @c	         C   sV  d d l  m }	 d d l m }
 t | ƒ t | ƒ k rP | j ƒ  | j ƒ  k sV t ‚ | |  _ | |  _ | d  k	 r› t
 | ƒ j d k r› |
 j | ƒ } n  | |  _ | |  _ | d  k	 r4| d  k	 rÚ t |  j ƒ d k sà t ‚ | j ƒ  j |  j ƒ |  _ | j ƒ  |  _ | j ƒ  j ƒ  } | j |  j ƒ |  _ n  |	 j d |  j d | j ƒ  d | j ƒ  d	 |  j d
 |
 j |  j j ƒ  | ƒ ƒ |  _ t |  j ƒ d k s£t ‚ | j |  j ƒ |  _ | j |  j ƒ |  _ |
 j ƒ  |  _ |
 j ƒ  |  _ xd |  j D]Y } |  j j d | ƒ } | j | ƒ } | j | ƒ } |  j j | ƒ |  j j | ƒ qõWd  S(   Niÿÿÿÿ(   t   maptbx(   R[   t
   std_stringi    t	   unit_cellt
   fft_n_realt
   fft_m_realt
   sites_cartt
   site_radiit	   site_cart(   t   cctbxRš   R\   R[   R   t   focusRb   t   atom_selectionRŸ   R
   R   R   R›   t
   atom_namesRœ   t   selectt
   scattererst   extract_labelst   grid_indices_around_sitest   allt   doublet   sizet   map_selt   fofc_map_selt   two_fofc_map_selt   fofc_map_levelst   two_fofc_map_levelst   fractionalizet   tricubic_interpolationR   (   t   selft   fofc_mapt   two_fofc_mapRŸ   R¥   Rœ   R¤   RG   RM   Rš   R[   t   labelsR¡   t	   site_fract   fofc_map_valuet   two_fofc_map_value(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   __init__º   sB    			!		'			!g      Àc         C   s   |  j  | k } |  j j | ƒ S(   N(   R°   R¥   R¦   (   R´   t   sigma_cutoffRN   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   atoms_below_fofc_map_levelå   s    c         C   s   |  j  | k } |  j j | ƒ S(   N(   R±   R¥   R¦   (   R´   R¼   RN   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   atoms_below_two_fofc_map_levelé   s    g      @g      ð?c         C   sG   |  j  | k } | s' |  j | k  } n |  j t t j ƒ k  } | | @S(   N(   R°   R±   t   floatt   syst   maxint(   R´   t   fofc_cutofft   two_fofc_cutofft   require_difference_map_peakst   sel_outside_fofct   sel_outside_two_fofc(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt    _atoms_outside_density_selectioní   s
    c         K   s   |  j  | �  } |  j j | ƒ S(   N(   RÇ   R¥   R¦   (   R´   t   kwdsRN   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   atoms_outside_density÷   s    c         C   s#   |  j  | k j t ƒ t |  j  ƒ S(   N(   R®   t   countRE   R   (   R´   R¼   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt+   fraction_of_nearby_grid_points_above_cutoffû   s    c         O   s   t  |  j | | Ž  ƒ S(   N(   R   R½   (   R´   t   argsRÈ   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt$   number_of_atoms_below_fofc_map_levelþ   s    c         O   s   t  |  j | | Ž  ƒ S(   N(   R   R¾   (   R´   RÌ   RÈ   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt(   number_of_atoms_below_two_fofc_map_level  s    c         O   s   t  |  j | | Ž  ƒ S(   N(   R   RÉ   (   R´   RÌ   RÈ   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   number_of_atoms_outside_density  s    c      	   K   s‡   | j  d t j ƒ } | j  d d ƒ } |  j | �  j ƒ  } x> | D]6 } | | d |  j | |  j | |  j | f IJqC Wt | ƒ S(   NR   R   R   s!   %s: 2mFo-DFc=%6.2f  mFo-DFc=%6.2f(	   t   popRÀ   t   stdoutRÇ   t
   iselectionR¥   R±   R°   R   (   R´   RÈ   R   R   Rd   Rw   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   show_atoms_outside_density  s    
c         C   s    d d l  m } | j |  j ƒ S(   Niÿÿÿÿ(   R[   (   R\   R[   t   maxR®   (   R´   R[   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   max_fofc_value  s    N(   R   t
   __module__R
   R»   R½   R¾   RB   RÇ   RÉ   RË   RÍ   RÎ   RÏ   RÓ   RÕ   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyR™   ¹   s$   #					
c   	      C   sÍ   d d l  m } | j ƒ  } | j ƒ  } xL |  j ƒ  D]> } | j j ƒ  d k r5 | j | j ƒ | j | j	 ƒ q5 q5 Wt
 | ƒ d k r¢ t d |  j ƒ  ƒ ‚ n  t d | d | d	 | d
 | d | ƒ } | S(   Niÿÿÿÿ(   R[   R4   R5   i    s   No non-hydrogen atoms in %sRµ   R¶   RŸ   R¥   Rœ   (   R4   R5   (   R\   R[   t   vec3_doubleR›   R_   Rv   R   R   t   xyzRl   R   t   RuntimeErrort   id_strR™   (	   R   Rœ   R¶   Rµ   R[   RŸ   R¥   Rn   t   density(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   residue_density_quality  s     	g      Ð?c         C   s‚   |  j  d d d t ƒ } | j d | ƒ } | j ƒ  j ƒ  } |  j  d d d t ƒ } | j d | ƒ } | j ƒ  j ƒ  } | | f S(   Nt   map_types   mFo-DFct   exclude_free_r_reflectionst   resolution_factors   2mFo-DFc(   t   map_coefficientsRE   t   fft_mapt   apply_sigma_scalingt   real_map_unpadded(   t   fmodelRß   t   fofc_coeffst   fofc_fftRµ   t   two_fofc_coeffst   two_fofc_fftR¶   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   get_difference_maps+  s    				c         C   si  t  | ƒ t  | ƒ k s t ‚ |  } t |  ƒ j d k rH |  j ƒ  } n  d d l m } | j |  ƒ }	 | j ƒ  }
 | j	 ƒ  } | j	 ƒ  } t
 j } d } } d } xÆ t | |	 ƒ D]µ \ } } | | j j ƒ  d k r´ |
 j | ƒ | j d | ƒ } | j | ƒ } | | k  r&| } | | } n  | | 7} | d 7} | si| j | ƒ | j | j | ƒ ƒ qiq´ q´ W| d k st ‚ | rþd d l m } | j d	 | d
 | j ƒ  d | j ƒ  d |
 d | j	 |
 j ƒ  d ƒ ƒ } | j | ƒ } | j | ƒ } n  t  | ƒ d k r(t  | ƒ t  | ƒ k s.t ‚ | j d | d | ƒ j ƒ  } t d | d | d | | ƒ S(   s¡   
  Collect basic statistics for a model map and some target map (usually an
  mFo-DFc map), including CC, mean, and minimum density at the atomic
  positions.
  t   booliÿÿÿÿ(   R[   i    R4   R¡   i   (   Rš   Rœ   R�   Rž   RŸ   R    g      ð?t   xt   yt   cct   mint   meanN(   R   Rb   R   R   RÒ   R\   R[   R¦   R×   R«   RÀ   RÁ   R
   t   zipRv   R   R   R²   R³   R¢   Rš   R©   R£   Rª   R¬   t   linear_correlationt   coefficientR   (   RN   t
   target_mapt	   model_mapRœ   RŸ   t	   pdb_atomst   local_samplingRÒ   R[   t   sites_cart_refinedt   sites_selectedt   map1t   map2t   min_densityt   sum_densityt   n_sitest
   worst_atomRw   RØ   R¸   t   target_valueRš   R­   Rí   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   get_model_map_stats9  sT    	


 	0t   box_build_refine_basec           B   sì   e  Z d  Z d d d d d d e d „ Z d e d „ Z d „  Z d „  Z	 d d „ Z
 d d	 d
 „ Z d e e d „ Z d d „ Z d d d d „ Z d „  Z d „  Z d e e e e e e e d d d d d „ Z d d d d d „ Z RS(   s—   
  Base class for handling functions associated with rebuilding and refinement
  of atoms in a box, with the building methods implemented elsewhere.
  g      Ð?i   i   c         C   sþ  t  |  t ƒ  ƒ d d  l } d d  l } d d l m } | j ƒ  |  _ | d  k r¤ |  j d  k r‰ t	 d | d | d | d | ƒ |  _ n  |  j j
 d t ƒ |  _
 n  | j j d |  j
 d	 t ƒ } | j d
 |
 d | ƒ |  _ | j j d | d | d | d | d |  j d | d t ƒ |  _ |  j j j ƒ  |  _ |  j j j ƒ  j ƒ  |  _ |  j j |  _ |  j j |  _ |  j d  k rˆ|  j |  _ n  |  j j j ƒ  |  _ | j j  |  j j ƒ j! d |  j ƒ |  _" |  j" j# j$ |  _% | j j d |  j" d	 t ƒ |  _& | j' |  j t ƒ |  _( | j  |  j ƒ j ƒ  |  _) | j' |  j t ƒ j* |  j) t ƒ j ƒ  |  _+ t, |  j) j- |  j+ ƒ ƒ d k s�t. ‚ |  j j/ j  |  j) ƒ |  _0 |  j0 j1 ƒ  j2 ƒ  |  j j j ƒ  } i  |  _3 x- |  j0 j1 ƒ  D] } | j4 |  j3 | j5 ƒ  <qÚWd  S(   Niÿÿÿÿ(   R[   R0   R1   R*   R   R6   R<   R=   RM   RN   RG   t   map_datat
   map_data_2t   box_cushiont-   assert_pdb_hierarchy_and_xray_structure_equalt   new_unit_celli    (6   R   t   localsR>   t   mmtbx.utilsR\   R[   RÒ   R
   RF   R3   RA   RB   RC   RD   RE   RP   t   utilst    extract_box_around_model_and_mapt   boxt   xray_structure_boxt   hd_selectiont   hd_selection_boxRŸ   R¬   t   n_sites_boxt   map_boxt   target_map_boxt	   map_box_2t   target_map_rsr_boxRœ   t   unit_cell_boxR<   R¦   t   discard_symmetryt   geo_boxt   generic_restraints_managert   reference_managert   reference_manager_boxt   box_restraints_managerRê   t   selection_all_boxt   selection_in_boxt   set_selectedt   others_in_boxR   t   intersectionRb   t   pdb_hierarchy_boxt   box_selected_hierarchyR_   R‹   t   atom_id_mappingRw   RÚ   (   R´   R0   RG   RF   Ró   RN   t   d_minR   R1   Rß   RQ   R  t   target_map_rsrRA   t   debugRH   R[   t   grmt   sites_cart_boxRn   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyR»   z  sh    					'	c         C   s‘   d d l  m } | d  k r( |  j } n  t | ƒ j d k ra | j |  j t ƒ j	 | t
 ƒ } n  | sx | |  j M} n  |  j j j ƒ  j | ƒ S(   Niÿÿÿÿ(   R[   R]   (   R\   R[   R
   R  R   R   Rê   R  RB   R  RE   R  R  R  RŸ   R¦   (   R´   RN   t	   hydrogensR[   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   get_selected_sitesÀ  s    $c         C   sr   d } |  j j j ƒ  } | j |  j ƒ } xA | D]9 } | j ƒ  } | d k rX | } q1 | | k s1 t ‚ q1 W| S(   sž   
    Returns the atom_group object corresponding to the original selection.
    Will raise an exception if the selection covers more than one atom_group.
    N(   R
   R  R   R_   R¦   R  R   Rb   (   R´   R   t	   box_atomst	   sel_atomsRn   R   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   only_residueÊ  s    	c         C   s/   |  j  j j ƒ  } |  j  j j | j ƒ  ƒ d S(   sU   
    Update the xray_structure coordinates after manipulating the PDB hierarchy.
    N(   R  R   R_   R  t   set_sites_cartt   extract_xyz(   R´   R*  (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   update_sites_from_pdb_atomsÚ  s    c         C   s    | d k r |  j } n  |  j j j ƒ  } d } } xO |  j d | d t ƒ D]5 } | j | ƒ } | |  j j	 | ƒ 7} | d 7} qM W| d k s˜ t
 ‚ | | S(   s_   
    Compute the mean density of the target map at coordinates for non-hydrogen
    atoms.
    i    RN   R(  i   N(   R
   R  R  R  Rœ   R)  RB   R²   R  R³   Rb   (   R´   RN   Rœ   Rü   t   n_heavyt   siteR¸   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   mean_density_at_sitesá  s    
g      ø?c         C   s2  d d l  m } | d	 k r( |  j } n  |  j j j d |  j ƒ j ƒ  } | j	 d |  j
 ƒ } | j ƒ  j ƒ  } |  j | d t ƒ} t | ƒ d k s� t ‚ | j ƒ  } | j ƒ  }	 |  j j j ƒ  }
 xL | D]D } |
 j | ƒ } | j | j | ƒ ƒ |	 j |  j j | ƒ ƒ qÎ W| j d |	 d | ƒ j ƒ  S(
   sb  
    Calculate the correlation coefficient for the current model (in terms of
    F(calc) from the xray structure) and the target map, calculated at atomic
    positions rather than grid points.  This will be much
    less accurate than the CC calculated in the original crystal environment,
    with full F(model) including bulk solvent correction.
    iÿÿÿÿ(   R[   R#  Rß   R(  i    Rë   Rì   N(   R\   R[   R
   R  R  R  t   structure_factorsR#  t   f_calcRá   Rß   Râ   Rã   R)  RB   R   Rb   R«   Rœ   R²   R   R³   R  Rñ   Rò   (   R´   RN   RM   R[   t   fcalct
   fc_fft_mapt   fc_mapRø   t	   fc_valuest
   map_valuesRœ   R1  R¸   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   cc_model_mapñ  s$    !	g      ð?c         C   sÃ   | d k s t  ‚ t | t ƒ r] |  j j j ƒ  j | ƒ j ƒ  } t | ƒ d k s] t  ‚ n  |  j j	 j
 ƒ  } | j | ƒ } | r” |  j j ƒ  n  |  j j d | d | d | d | d | ƒ d S(   s€   
    Apply harmonic reference restraints to the selected atoms, wiping out
    any previously existing harmonic restraints.
    i    RŸ   RN   t   sigmat   limitt   top_out_potentialN(   Rb   t
   isinstancet   strR  R   RO   RN   RÒ   R   R  RŸ   R¦   R  t   remove_coordinate_restraintst   add_coordinate_restraints(   R´   RN   t   reference_sigmaR<  t   reset_firstR=  R'  t   reference_sites(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   restrain_atoms  s    
c         C   s  d d l  } | d k r$ |  j } n3 t | t ƒ rW |  j j j ƒ  j | ƒ j	 ƒ  } n  | j
 j j j d |  j d | d |  j |  j d d d |  j ƒ } | j
 j j j d	 | d
 |  j j d d d d d d ƒ } |  j | j ƒ |  j d | j | j f IJ| j S(   sg   
    Run simple real-space refinement, returning the optimized weight for the
    map target (wx).
    iÿÿÿÿNRó   RN   t   real_space_gradients_deltat   max_iterationsi–   RA   t   refinerRG   t   start_trial_weight_valueg      ð?t   rms_bonds_limitg{®Gáz„?t   rms_angles_limits>     after real-space refinement: rms_bonds=%.3f  rms_angles=%.3f(   t,   mmtbx.refinement.real_space.individual_sitesR
   R  R>  R?  R  R   RO   RN   RÒ   t
   refinementt
   real_spacet   individual_sitest   simpleR  R#  Rß   R  t   refineryR  t   update_coordinatest   sites_cart_resultR   t   rms_bonds_finalt   rms_angles_finalt   weight_final(   R´   RN   RH   t   rsr_simple_refinert   refined(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   real_space_refine&  s.    		i   c         C   sü   d d  l  } | d  k r9 d d  l } | j j j ƒ  } n  | d  k rd d d l m } | j ƒ  } n  |  j ƒ  } |  j	 d |  j
 d d ƒ |  j ƒ  | j j j j d |  j d | d |  j j d	 | d
 | d |  j d |  j |  j d d d d d | ƒ 
d  S(   Niÿÿÿÿ(   t   rotamer_evalRN   RB  gš™™™™™¹?Ró   R   RG   R'   t   rotamer_managerRA   RF  RJ  g{®Gáz„?RK  g      ð?t   backbone_sample_angle(   t'   mmtbx.refinement.real_space.fit_residueR
   t   mmtbx.monomer_library.servert   monomer_libraryR%   t   mmtbx.rotamerRZ  t   RotamerEvalR,  RE  R  RY  RM  RN  t   fit_residuet   runR  R  R  R  R#  Rß   (   R´   R'   R[  R\  RH   RZ  R   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   fit_residue_in_boxC  s.    		
		c         C   s0   |  j  j j | ƒ |  j  j j ƒ  j | ƒ d S(   sQ   
    Set coordinates for the boxed xray_structure and pdb_hierarchy objects.
    N(   R  R  R-  R   R_   t   set_xyz(   R´   RŸ   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyRR  _  s    c         C   sq   |  j  j j ƒ  |  j  j } | j |  j ƒ } t |  j ƒ t | ƒ k sO t ‚ |  j	 j ƒ  j
 |  j | ƒ } | S(   s¦   
    Copies the new coordinates from the box to the selected atoms in the
    original xray_structure object, and returns the (complete) list of
    coordinates.
    (   R  R  RŸ   t   shift_to_map_boxed_sites_backR¦   R  R   RÒ   Rb   RG   R  (   R´   t#   sites_cart_box_refined_shifted_backR÷   t   sites_cart_moving(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   update_original_coordinatesf  s    !iô  i    c         C   s‡  d d l  m } d d l m } d d l m } d d l } | d k rT |  j } n  t	 | ƒ j
 d k r� | j |  j t ƒ j | t ƒ } n  | j j d |	 ƒ } | j j d | d	 | d
 | d | d | d | d | ƒ } |  j j j ƒ  j ƒ  } x� t |
 ƒ D] } |  j j j ƒ  } | j j d t ƒ } | j d | d |  j d | d | d | d | d | d | d | ƒ 	} |  j | ƒ q Wd S(   sF   
    Perform geometry minimization on a selection of boxed atoms.
    iÿÿÿÿ(   t   geometry_minimization(   t   geometry_restraints(   R[   NR]   RG  t   bondt	   nonbondedt   anglet   dihedralt	   chiralityt	   planarityt   generic_restraintst-   ignore_line_search_failed_step_at_lower_boundRŸ   RA   t   geometry_restraints_flagst   lbfgs_termination_paramst   lbfgs_exception_handling_paramst   sites_cart_selectiont   rmsd_bonds_termination_cutofft   rmsd_angles_termination_cutofft   site_labels(   t   mmtbx.refinementRj  R¢   Rk  R\   R[   t   scitbx.lbfgsR
   R  R   R   Rê   R  RB   R  RE   t   lbfgst   termination_parameterst   flagsR  R  R§   R¨   t   xrangeRŸ   t   exception_handling_parametersR  RR  (   R´   RN   Rl  Rm  Rn  Ro  Rp  Rq  Rr  t   max_number_of_iterationst   number_of_macro_cyclesRx  Ry  Rj  Rk  R[   t   scitbxRu  Rt  Rz  t   iRŸ   t   exception_handling_paramst	   minimized(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyRj  v  sD    $						i2   c   	      C   sƒ  d d l  m } d d l } |  j d |  j ƒ } |  j rM |  j j d ƒ n  d } |  j r† | j	 j
 d |  j j d |  j j ƒ } n  | d k r§ | j ƒ  j ƒ  } n  | d k	 r¿ | | _ n  | d k	 r× | | _ n  | d k	 rï | | _ n  | j d | d	 d d |  j j d
 t d |  j d |  j d | d d d |  j d t d | ƒ | d k	 rf| j d ƒ n  |  j |  j j j ƒ  ƒ d S(   s  
    Run real-space simulated annealing using the target map (not the RSR map,
    if this is different).  In practice, the non-selection atoms in the box
    should almost always be restrained to their current positions, but the
    setup is left to the calling code.
    iÿÿÿÿ(   t   simulated_annealingNRN   s   box_start.pdbRG   R0   t   paramsRä   RN  Ró   RK   t   wxt   wcg      ð?R+   t   verboset   states_collectors   box_traj.pdb(   t   mmtbx.dynamicsRˆ  R  RY  R  R%  R  t   write_pdb_fileR
   R	  t   statesR  R   t   master_paramst   extractt   start_temperaturet	   cool_ratet   number_of_stepsRc  RE   R  R  R   t   writeRR  RŸ   (	   R´   t   simulated_annealing_paramsR“  R”  R•  Rˆ  RH   RŠ  R�  (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   anneal©  sB    						(    N(   R   RÖ   t   __doc__R
   RB   R»   RE   R)  R,  R/  R2  R:  RE  RY  Rd  RR  Ri  Rj  R˜  (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyR  u  sL   	8
				(i   g      à?c         C   sï   d d l  m } d d l } | d k rF | j j | j ƒ j ƒ  } n  t d |  d | d | d | d | d	 | d
 | d | d |
 d | d | ƒ } | d k	 r¹ | j	 d | j
 d | ƒ n  | j d | ƒ | rå | j d | j ƒ n  | j ƒ  S(   sš   
  Run simulated annealing with a real-space target.  For maximum flexibility,
  a separate map may be used for the initial real-space refinement step.
  iÿÿÿÿ(   Rˆ  NRG   R0   RN   Ró   R#  RF   R1   Rß   R$  R   R%  RB  R—  (   RŽ  Rˆ  t
   iotbx.philR
   t   philt   parset   master_params_strR’  R  RE  R  R˜  RY  R  Ri  (   RG   R0   RN   Ró   R#  RF   R1   Rß   R‰  R‹  R$  t   rsr_after_annealRB  R   R%  Rˆ  R�   R  (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   run_real_space_annealingÖ  s0    			
c         C   s}  d d l  m } d d l m } d d l m } |  j ƒ  } | j d |  d | ƒ } g  } | d k	 ryxt	 | ƒ D]ÿ \ } }	 d }
 x8 |	 d D], } | | j
 j ƒ  d	 k r� |
 d 7}
 q� q� W|
 d k rÒ qs n  | t | ƒ d k r+| j | j d
 |	 d d |	 d d | j |	 d ƒ d d ƒ ƒ qs | j | j d
 |	 d d |	 d d | j |	 d d g ƒ d d ƒ ƒ qs Wn  | S(   sM   
  Extract Chi angle indices (including rotation axis) from the atom_group
  iÿÿÿÿ(   Rb  (   t   rotatable_bonds(   R[   R   R'   i    i   R4   t   axist   atoms_to_rotateRN   t   vectorN(   t   mmtbx.refinement.real_spaceRb  R  R   R\   R[   R_   t   axes_and_atoms_aa_specificR
   R   Rv   R   R   R   t   clusterR]   (   R   R'   Rb  R   R[   R_   R¥  t   resultt   i_aat   aaR0  Rw   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   generate_sidechain_clusters  s6    	 



(   t
   __future__R    t   libtbxR   R   RÀ   RÑ   R#   R3   RL   RT   RZ   Rf   Ro   Ru   Rz   R�   R�   R˜   t   objectR™   RÜ   Ré   RB   R   R  R
   RŸ  Rª  (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/__init__.pyt   <module>   s>   !								\	5ÿ h 