ó
Ú™xTc        	   @   s  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	 d  d l
 Z
 d Z e e
 j d „ Z d	 e f d
 „  ƒ  YZ d e e d „ Z d e	 d d e d d e d „ Z e e e e e
 j d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   division(   t   col(   t   make_sub_header(   t   null_out(   t   AutoNsâ   
selection = None
  .type = atom_selection
build_hydrogens = Auto
  .type = bool
max_atoms_missing = None
  .type = int
use_rotamers = True
  .type = bool
anneal_residues = False
  .type = bool
skip_rsr = False
  .type = bool
c         C   sÏ  d d l  m } d d l } d d l m } | j j j d |  d | d | ƒ } xU | j D]J } | j | j j j	 k rZ t
 | j ƒ d k r¤ | d	 | j IJq¤ qZ qZ W| j ƒ  }	 i  }
 xY |	 j ƒ  D]K } d
 | k rÇ |	 | } t g  | j ƒ  D] } | j ^ qó ƒ |
 | <qÇ qÇ Wd } xœ|  j ƒ  j ƒ  D]ˆ} | j ƒ  sGq/n  xm| j D]b} | j | j k rQt
 | j ƒ d k rQx2| j ƒ  D]!} | j ƒ  } | | j k r‹| j j | ƒ } | j | } | j | ƒ } | d k	 r¬|
 | j ƒ  } | r	|
 d } n  xm | j ƒ  D]_ } | d 7} d } | | _ x= | j ƒ  D]/ } | j | k rB| j | ƒ | d 7} qBqBWqW| d | j | | j ƒ  d j | | f IJq¬q‹q‹WqQqQWq/W|  j ƒ  j ƒ  | S(   sk  
  Modify the sequence for the pdb hierarchy to match that of the aligned
  sequence.  This will remove incompatible atoms; the sidechains will still
  need to be extended separated.  For proteins only - mismatches in nucleic
  acids will only result in a warning.

  :param pdb_hierarchy: iotbx.pdb.hierarchy.root object
  :param sequences: list of iotbx.bioinformatics.sequence objects
  :param trucate_to_cbeta: chop off entire sidechain to C-beta (default: leave
                           common atoms in place)
  :param out: output filehandle (default = stdout)
  :returns: number of atom_group objects renamed
  iÿÿÿÿ(   t   idealized_aaN(   t   three_letter_given_one_lettert   pdb_hierarchyt	   sequencest   logi    s=     WARNING: will skip %d mismatches in nucleic acid chain '%s't   _ht   alai   s,     chain '%s' %s %s --> %s (%d atoms removed)(    t   mmtbx.monomer_libraryR   t   mmtbx.validation.sequencet   iotbx.pdb.amino_acid_codesR   t
   validationt   sequencet   chainst
   chain_typet   NUCLEIC_ACIDt   lent   mismatcht   chain_idt   residue_dictt   keyst   sett   atomst   namet
   only_modelt
   is_proteint   idt   residue_groupst   residt   indext   actual_codet   gett   Nonet   lowert   atom_groupst   resnamet   remove_atomt   reset_i_seq(   R   R   t   truncate_to_cbetat   outR   t   mmtbxR   t   seq_validationt	   chain_seqt   res_dictt   expected_namesR'   t	   ideal_rest   at	   n_changedt   chaint   residue_groupR    t   idxt   new_codet   new_resnamet   expected_atomst
   atom_groupt	   n_removedt   atom(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   correct_sequence   s`    	
3'
	"t   conformation_scorerc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sQ  
  Stand-in for the conformation scoring class in mmtbx.refinement.real_space;
  instead of calculating fit to the map, this simply uses the change in
  position of the first atom being moved at each rotation.  This allows us to
  superimpose the conformations for those atoms which are present in both the
  old and the new residues.
  c   
      C   sþ   d d l  m } | j ƒ  } | j ƒ  |  _ |  j j ƒ  } | j | t ƒ |  _ | j | d ƒ |  _	 xi t
 | ƒ D][ \ } } xL t
 |  j ƒ D]; \ } }	 | j |	 j k r� t |  j | <| |  j	 | <q� q� Wqq W| j ƒ  |  _ |  j j ƒ  |  _ d  |  _ d  S(   Niÿÿÿÿ(   t   flexi    (   t   scitbx.array_familyR?   R   t   new_residue_atomst   sizet   boolt   Falset   new_residue_selectiont   size_tt   selection_mappingst	   enumerateR   t   Truet   extract_xyzt	   sites_oldt
   sites_cartR$   t   dist_min(
   t   selft   old_residuet   new_residueR?   t   old_residue_atomst   n_atomst   i_seqt   old_atomt   j_seqt   new_atom(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   __init__b   s    c         C   sq   | d } |  j  | rm t t | | ƒ t |  j |  j | ƒ ƒ } | |  j k  rm | |  _ | |  _ t Sn  t S(   Ni    (	   RE   t   absR   RK   RG   RM   RL   RI   RD   (   RN   RL   t	   selectiont   first_i_seqt   dist(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   updater   s    
		c         C   sd   | |  _  | d } |  j | rT t t | | ƒ t |  j |  j | ƒ ƒ |  _ n t j |  _ d  S(   Ni    (	   RL   RE   RX   R   RK   RG   RM   t   syst   maxint(   RN   RL   RY   RZ   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt
   reset_with}   s    	
$c         C   s   |  j  j |  j ƒ d  S(   N(   RA   t   set_xyzRL   (   RN   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   apply_final†   s    (   t   __name__t
   __module__t   __doc__RW   R\   R_   Ra   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyR>   Z   s
   				c         C   sW  d d l  m } |  j j ƒ  } | t k r8 | d 7} n  | | } |  j ƒ  } | j d | d | j ƒ  j ƒ  j	 ƒ  j
 ƒ  d t d t d t ƒ }	 |	 j |  j k s¨ t ‚ | rSd d	 l m }
 d d
 l } | d
 k	 sÜ t ‚ |
 d |	 d | ƒ } t d |  d |	 ƒ } | j j j d | d | d |	 j ƒ  j ƒ  d d d d d d ƒ | j ƒ  n  |	 S(   s�   
  Rebuild a sidechain by substituting an ideal amino acid and (optionally)
  rotating the sidechain to match the old conformation as closely as possible.
  iÿÿÿÿ(   t	   hierarchyR
   t   current_groupt	   new_groupt   backbone_onlyt   is_amino_acidt   exclude_hydrogens(   t   generate_sidechain_clustersNt   residuet   mon_lib_srvRO   RP   t   scorert   clustersRL   t   starti    t   stopic  t   stepi   (   t	   iotbx.pdbRe   R'   R%   RI   t   detached_copyt   substitute_atom_groupR   t
   only_chaint   only_residue_groupt   only_atom_groupRD   t   AssertionErrort   mmtbx.buildingRk   t   mmtbx.refinement.real_spaceR$   R>   t
   refinementt
   real_spacet   torsion_searchR   RJ   Ra   (   Rl   t
   ideal_dictRm   t	   hydrogenst   match_conformationRe   t   res_keyt   idealt   tmp_residueRP   Rk   R,   Ro   Rn   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   extend_residue‰   s>    	 
				c	         C   s­  d d l  m }	 d d l m }
 d d l } d d l m } d d l m } | d k	 rp t
 | d ƒ sy t ‚ n	 d „  } | } | d k rš |	 j ƒ  } n  | d k r² t ƒ  } n  | j j j ƒ  } |  j ƒ  } | d k r÷ | j | j ƒ  t ƒ } n  g  } xö|  j ƒ  j ƒ  D]â} | j ƒ  r@| r@| d	 | j IJqn  x¯| j ƒ  D]¡} | j ƒ  } t | ƒ d
 k r”| d | j | j ƒ  f IJqMn  | d } | j ƒ  j ƒ  } | j | ƒ } | j t ƒ sÔqMn  | d k r| | j ƒ } | d k rR| d | j IJqMqRn= | j j  ƒ  } | t k r=| d } n  | | j! ƒ  k rRn  |
 j" d | d | d t ƒ } t | ƒ d k rM| d t | ƒ | j# ƒ  d j$ | ƒ f IJ| d k sÌt | ƒ | k  rî| | ƒ rë| j% | ƒ qëqîqMqMWqWxŠ | D]‚ } t& d | d | d | d | d t ƒ } | rSx  | j ƒ  D] } d | _' q=Wn  | j( ƒ  } | j) | ƒ | j* | j+ ƒ  ƒ qýW|  j ƒ  j, ƒ  |  j ƒ  j- ƒ  t | ƒ S(   sP   
  Replace all sidechains with missing non-hydrogen atoms in a PDB hierarchy.
  iÿÿÿÿ(   R   (   t   rotamer_evalN(   t   common_residue_names_get_class(   R?   t   __call__c         S   s   t  S(   N(   RI   (   t   r(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   <lambda>Å   s    s#       skipping non-protein chain '%s'i   s.       %s %s has multiple conformations, skippingi    t   common_amino_acids$       skipping non-standard residue %sR
   Rl   Rm   t   ignore_hydrogenss       missing %d atoms in %s: %st   ,R   R€   R�   t   XXXX(.   R   R   t   mmtbx.rotamerR†   t   mmtbx.monomer_library.serverRs   R‡   R@   R?   R$   t   hasattrRy   R   R   t   monomer_libraryt   serverR   RC   RB   RI   R   R   R   R   R   R&   R   R    t   extract_i_seqt   selectt   all_eqR'   R%   R   t   eval_residue_completenesst   id_strt   joint   appendR…   t   segidt   parentt   remove_atom_groupt   append_atom_groupRt   R)   t   reset_serial(   R   RY   R€   t   max_atoms_missingR	   t   modify_segidst   prefilter_callbackt   idealized_residue_dictt   skip_non_protein_chainsR   R†   R,   R‡   R?   R   Rm   t	   pdb_atomst   partial_sidechainsR4   R5   R&   Rl   t   i_seqst   residue_selt	   res_classt   keyt   missing_atomsRP   R<   t   rg(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   extend_protein_model±   sŠ    	 
				c   '         sT  d d l  m } d d l m }	 d d l }
 d d l m } d d l m } |
 j	 j
 j
 ƒ  } |	 j ƒ  } | } } | s� t ƒ  } t ƒ  } n  t d d | ƒ| j d	 |  d
 | j ƒ  j ƒ  d | d | ƒ } | d IJ| j j } | j ƒ  } | j d | d | ƒ } t d d | ƒ| j d d d t ƒ j d d ƒ j ƒ  j ƒ  ‰  | j ƒ  ‰ xò| j ƒ  j ƒ  D]Þ} | j ƒ  rˆ| rˆqhn  | j ƒ  } x¯t | ƒ D]¡\ } } d } } | j! ƒ  } t" | ƒ d k rÛq¡n  | d } | j# ƒ  } | j$ ƒ  | j% ƒ  } | j& d ƒ r¡| j' ƒ  } ‡  ‡ f d †  }  | j' ƒ  j( ƒ  } |  | ƒ }! | j) d ˆ  d | d | d | d | d | d | j ƒ  j* ƒ  d d d  d! d" d# d$ d% | ƒ }" |  | ƒ }# | j' ƒ  }$ d }% |# |! k rìd& }% |" j } n' | j+ | ƒ x | D] }& d |& _, q W| d' | j- ƒ  |$ j. | ƒ |! |# |% f IJq¡q¡WqhW| | f S((   sE   
  Use real-space refinement tools to fit newly extended residues.
  iÿÿÿÿ(   t   fit_residue(   R†   N(   t   building(   R?   s   Processing new modelR+   R   t   crystal_symmetryt   cif_objectst    t   processed_pdb_filet   xray_structures   Fitting residuest   map_types   2mFo-DFct   exclude_free_r_reflectionst   resolution_factorg      Ð?i   i    RŽ   c            s‡   d } d } x^ |  j  ƒ  D]P } | j j ƒ  d k r ˆ j d | j ƒ } | ˆ  j | ƒ 7} | d 7} q q W| d k s t ‚ | | S(   Ni    t   Ht   Dt	   site_carti   (   R¸   R¹   (   R   t   elementt   stript   fractionalizet   xyzt   eight_point_interpolationRy   (   Rl   t   sumRR   R<   t	   site_frac(   t
   target_mapt	   unit_cell(    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   get_two_fofc_mean:  s    RÂ   Rl   Rm   t   rotamer_managert   geometry_restraints_managert   real_space_gradients_deltat   rms_bonds_limitg{®Gáz„?t   rms_angles_limitg      ð?t   backbone_sample_anglei   t   allow_modified_residuess	    <-- keepsA       residue '%s' : rmsd=%5.3f 2fofc_start=%5.3f 2fofc_end=%5.3f%s(/   R{   R®   R�   R†   R�   R,   R¯   R@   R?   R’   R“   t   RotamerEvalR   R   t   reprocess_pdbt   f_obsR°   t   all_chain_proxiesR   R´   t   get_restraints_managert   map_coefficientsRI   t   fft_mapt   apply_sigma_scalingt   real_map_unpaddedRÃ   R   R   R   R   RH   R$   R&   R   R   t	   reset_tmpt   extract_segidR–   RJ   t	   deep_copyt   runt   d_minR`   t   tmpR˜   t   rms_difference('   R   R±   t   fmodelt   use_rotamerst   annealt   verboseRË   R+   R®   R†   R,   R¯   R?   Rm   RÅ   t   ppdb_outt   box_outt   processed_pdbRe   t   xrst   grmR4   R   t   i_rgR5   t   prev_rest   next_resR&   Rl   R   t   segidst   sites_startRÄ   t   two_fofc_mean_startt   refitt   two_fofc_mean_endt	   sites_endt   flagR<   (    (   RÂ   RÃ   sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   refit_residues  s’    
			
			 
 


		t	   prefilterc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   sË   
  Optional filter for excluding residues with poor backbone density from being
  extended.  This is done as a separate callback to enable the main rebuilding
  routine to be independent of data/maps.
  g      ð?c         C   sd   | j  d d d t ƒ j d d ƒ j ƒ  j ƒ  } | j ƒ  j ƒ  |  _ | |  _ | |  _ | |  _	 d  S(   NRµ   s   2mFo-DFcR¶   R·   g      Ð?(
   RÑ   RI   RÒ   RÓ   RÔ   RÎ   RÃ   t   mapR+   t   backbone_min_sigma(   RN   RÜ   R+   Rò   RÂ   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyRW   h  s    			c         C   s´   | j  ƒ  } d } } x^ | D]V } | j j ƒ  d	 k r |  j j d | j ƒ } | |  j j | ƒ 7} | d 7} q q W| d k r� | | } n  | |  j k  r° |  j	 d IJt
 St S(
   Ni    t   Nt   Ct   CAt   CBRº   i   s)         *** poor backbone density, skipping(   Ró   Rô   Rõ   Rö   (   R   R   R¼   RÃ   R½   R¾   Rñ   R¿   Rò   R+   RD   RI   (   RN   Rl   R   t
   sigma_meant   n_bbR<   RÁ   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyRˆ   r  s    
(   Rb   Rc   Rd   RW   Rˆ   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyRð   b  s   
t   extend_and_refinec        	   B   s2   e  Z d  Z d e j d d d e e e d „ Z RS(   s{   
  Run the combined sidechain extension and real-space fitting, and optionally
  write final model and map coefficients.
  c         C   s±  |
 r- |	 d  k	 s- d  | | g k s- t ‚ n  | j t k r] | j ƒ  j t ƒ d k | _ n  t d d | ƒt d | d | ƒ } | j	 ƒ  j
 ƒ  } t d | d | j d | j d | d	 | ƒ |  _ | d
 |  j IJ|  j d k r*| j r*t d | d | d | d | j d | j d | ƒ \ } } n | j d | ƒ } | j | d t ƒ| j	 ƒ  j
 ƒ  } | j ƒ  |  _ | j ƒ  |  _ | | |  _ d  |  _ |  _ | rÅx  | j ƒ  D] } d | _ q¯Wn  |
 r­| d  k rä|	 d } n  t | d ƒ } | j | j | j ƒ ƒ | j ƒ  | |  _ | d | IJ| d  k rF|	 d } n  d d l  m! } d d  l" } | | ƒ \ } } | j# j$ d | d | d | ƒ | d | IJ| |  _ n  d  S(   Ni    s   Filling in partial sidechainsR+   RÜ   R   R€   R    R¢   R	   s     %d sidechains extended.R±   RÝ   RÞ   R°   t   update_f_calcR²   s   _extended.pdbt   ws     wrote new model to %ss	   _maps.mtziÿÿÿÿ(   t   get_maps_from_fmodelt
   fwt_coeffst   delfwt_coeffst	   file_names     wrote map coefficients to %s(%   R$   Ry   t   build_hydrogensR   t   hd_selectiont   countRI   R   Rð   RL   RB   R­   R    t
   n_extendedt   skip_rsrRï   RÝ   t   anneal_residuest   extract_xray_structuret   update_xray_structuret   r_workt   r_freet   n_new_atomst   pdb_filet   map_fileR   R›   t   opent   writet   as_pdb_stringR´   t   closet   mmtbx.maps.utilsRü   t   iotbx.map_toolst	   map_toolst   write_map_coeffs(   RN   R   R´   RÜ   t   paramsR±   R+   t   output_modelt   output_map_coeffst   prefixt   write_filest   reset_segidRß   R¢   t   n_atoms_startt   n_atoms_endR<   t   fRü   t   iotbxt   two_fofc_mapt   fofc_map(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyRW   †  sn    !							
	(    N(   Rb   Rc   Rd   R]   t   stdoutR$   RI   RW   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyRù   �  s   (   t
   __future__R    t   scitbx.matrixR   t   libtbx.str_utilsR   t   libtbx.utilsR   t   libtbxR   R]   t   master_paramsRD   R!  R=   t   objectR>   R$   RI   R…   R­   Rï   Rð   Rù   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/extend_sidechains.pyt   <module>   s6   ?1%OV