ó
Ó™xTc           @   s  d  d l  m Z d  d l m Z d  d l Z d  d l Z d  d l Z e j Z e j	 Z	 d e e	 f Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 e j f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d d e d d „ Z d S(   iÿÿÿÿ(   t   division(   t   fit_rotamersNsf  %s
ignore_water_when_flipping = True
  .type = bool
skip_approximate_helices = True
  .type = bool
residue_iteration
  .style = box auto_align
{
  poor_cc_threshold = 0.8
    .type = float
    .short_caption = Poor CC threshold
  real_space_refine_peptide = True
    .type = bool
  real_space_refine_window = 1
    .type = int
  real_space_refine_max_iterations = 25
    .type = int
  real_space_refine_target_weight = 100.
    .type = float
  torsion_grid_search = True
    .type = bool
    .expert_level = 2
  ignore_alt_conformers = True
    .type = bool
    .short_caption = Ignore alternate conformers
  %s
}
c           C   s   t  j j d t ƒ S(   Nt   input_string(   t   libtbxt   philt   parset   torsion_search_params_str(    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   torsion_search_params,   s    c           C   s   t  j j d t ƒ S(   NR   (   R   R   R   t   master_params_str(    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   master_params/   s    c         C   s‚   d \ } } x3 |  D]+ } | | j ƒ  } | d k r | } q q Wx3 | D]+ } | | j ƒ  } | d k rI | } qI qI W| | f S(   Nt   CA(   NN(   t   Nonet   strip(   t   residue1_iselt   residue2_iselt
   atom_namest   ca1t   ca2t
   atom_i_seqt	   atom_name(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   extract_calpha_atoms2   s    c         C   s$  d d l  m } d \ } } } } | j ƒ  } xH |  D]@ }	 | |	 j ƒ  }
 |
 d k r` |	 } q5 |
 d k r5 |	 } q5 q5 WxH | D]@ }	 | |	 j ƒ  }
 |
 d k r« |	 } q€ |
 d	 k r€ |	 } q€ q€ Wd  | | | g k rÝ d  S| j | ƒ | j | ƒ | j | ƒ | d  k	 r | j | ƒ n  | S(
   Niÿÿÿÿ(   t   flext   Ct   Ot   Nt   Ht   D(   NNNN(   R   R   (   t   scitbx.array_familyR   R   t   size_tR   t   append(   R   R   R   R   t   c1t   o1t   n2t   h2t   iselR   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   extract_peptide_atoms>   s.    		c         C   s•   d d l  m } d d l m } | |  } | | } | j ƒ  }	 xN | D]F }
 | d | |  d | | d | |
 d | d t ƒ } |	 j | ƒ qG W|	 S(	   Niÿÿÿÿ(   R   (   t   rotate_point_around_axist   axis_point_1t   axis_point_2t   pointt   anglet   deg(   R   R   t   scitbx.matrixR$   t   vec3_doublet   TrueR   (   R   R   t
   sites_cartt   selectedt	   angle_degR   R$   R%   R&   t	   new_sitest   i_seqt   xyz_new(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   rotate_peptideW   s    




t
   select_mapc           B   s   e  Z d  „  Z RS(   c         C   sÁ   d d l  m } xª | D]¢ } | | j j ƒ  j ƒ  d k r | | } |  j d | j | g ƒ ƒ } |  j j | ƒ }	 |  j j | ƒ }
 | j	 d |	 d |
 ƒ j
 ƒ  } | | k  r¹ t Sq q Wt S(	   Niÿÿÿÿ(   R   t   ht   dR-   t   xt   y(   R5   R6   (   R   R   t   elementR   t   lowert   selectR+   t   target_map_datat   model_map_datat   linear_correlationt   coefficientR,   t   False(   t   selft   peptide_iselR-   t   atomst   cc_limitR   R1   t   xyzt   sel_mapt   m1t   m2t   cc(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   is_refinement_neededg   s    
 (   t   __name__t
   __module__RJ   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyR4   f   s   c         C   sž   d  } d  } t j d d | j d d | j d | j ƒ } xV | D]N }	 t d | d | d | d | d	 |	 ƒ }
 |  j d |
 ƒ rB |	 } |
 } qB qB W| | f S(
   Nt   starti´   t   stopt   stepR   R   R-   R.   R/   (   R   R   t   generate_ranget   range_startt
   range_stopRO   R3   t	   is_better(   t   residue_evaluatorR   R   R-   RB   t   paramst   angle_deg_bestt   sites_cart_bestt   anglesR/   t   sites_cart_flipped(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   rotation_searchs   s"    		t   regularizerc           B   s   e  Z d  „  Z d d „ Z RS(   c         C   s   | |  _  d  S(   N(   t   geometry_restraints_manager(   RA   R\   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   __init__ˆ   s    c         C   s7   d d  l  } | j j j d | d |  j d | ƒ } d  S(   NiÿÿÿÿR-   R\   t   sites_cart_selection(   t   cctbx.geometry_restraints.lbfgst   geometry_restraintst   lbfgsR\   (   RA   R-   t	   selectiont   cctbxRa   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   __call__‹   s
    	N(   RK   RL   R]   R   Rd   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyR[   ‡   s   	t   ramachandran_validatorc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s#   d d l  m } | j ƒ  |  _ d  S(   Niÿÿÿÿ(   t   ramachandran_eval(   t   mmtbx.rotamerRf   t   RamachandranEvalt   rama_lookup(   RA   Rf   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyR]   “   s    c         C   s,   d  | k r t S|  j j | | | ƒ d k  S(   Ngü©ñÒMb`?(   R   R@   Ri   t   evaluate_sites(   RA   t   res_typet   i_seqsR-   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyRd   —   s     c         C   s   |  j  j | | | f ƒ d k  S(   Ngü©ñÒMb`?(   Ri   t   evaluate(   RA   Rk   t   phit   psi(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   eval_phi_psi›   s    (   RK   RL   R]   Rd   Rp   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyRe   ’   s   		c   C      C   sA  | j  ƒ  | j  ƒ  k s t ‚ | j ƒ  | j ƒ  k s< t ‚ d d l m } d d  l } d d  l } d d l m } | j	 ƒ  } d } d } d } |
 | IJ|
 | IJ| j
 ƒ  } t d | j
 ƒ  d | d	 | ƒ } t | j ƒ } | j j } d
 } d
 } d
 } | j ƒ  } |  j ƒ  } | j ƒ  } g  } x|  j ƒ  D]õ} xì| j ƒ  D]Þ} xÕ| j ƒ  D]Ç}  |  j ƒ  }! x²t t |! ƒ d ƒ D]š}" d  }# |" d
 k r¯|! |" d }# n  |! |" }$ |! |" d }% |$ j ƒ  }& |% j ƒ  }' |' |& d k p
|' |& k o
|$ j |% j k sq†n  | |$ j ƒ d k r†|$ j d d ƒ d }( |$ j ƒ  j ƒ  }) |% j ƒ  j ƒ  }* t d |) d |* d | ƒ \ }+ }, d  |+ |, g k r›q†n  t  d |) d |* d | ƒ }- |- d  k rÈq†n  d }. |$ j d5 k ræd }. nE |$ j d k rûq†n0 |$ j d k rd }. n |% j d k r+d }. n  d  g d }/ d6 \ }0 }1 |# d  k	 rá|# j ƒ  }2 |2 |& d k sŠ|2 |& k rá|# j |$ j k rá| j! j" d |# d |$ d |% ƒ }/ d  |/ k rÞ| j! j# d |/ d | ƒ \ }0 }1 qÞqán  t$ }3 |0 d  k	 r„|1 d  k	 r„| j% |) ƒ }4 |4 j& ƒ  d
 k rl| j' |0 |1 ƒ r8q†q�d  |0 k  oOd! k  n r�|1 d" k r�t( }3 q�q„|	 j) |. |0 |1 ƒ }3 n  | j* ƒ  }5 |5 j+ |) ƒ |5 j+ |* ƒ |3 s×| j, d# |- d | d$ | d% | j- ƒ r | j. |- ƒ }6 t/ j0 d& | d' |5 ƒ \ }7 }8 t/ j1 d( |6 d | d) | d* | ƒ }9 | j2 d |6 d' |5 ƒ }: t/ j3 d+ |( d& |- j4 ƒ  d |6 j4 ƒ  d, |9 j5 d- |9 j6 d. |: d/ |. d0 |/ ƒ }; | j7 r£| j8 }< n! t9 ƒ  j: ƒ  }< d
 |< _; d
 |< _< t= d1 |9 d2 |+ d3 |, d | d# |- d4 | j8 ƒ \ }= }> |> d  k rq†n  | j2 d |= d' |5 ƒ }? d  }@ | j> sN| j? |- |= ƒ } |? }@ n~ | j? |- |= ƒ }A | j@ |A j. |7 ƒ |7 |8 ƒ }B |9 jA |B ƒ rº| j? |- |= ƒ } | j2 d |B d' |5 ƒ }@ n | j? |- |6 ƒ } |@ d  k	 r| jB |; ƒ |
 | |( |: |9 j5 |9 j6 |@ |9 jC |9 jD |> f IJqq q†q†Wq]WqJWq7W| jE | ƒ | S(7   Niÿÿÿÿ(   t   proteins(   R   s>                  |--------START--------| |--------FINAL--------|sG        residue   map_cc 2mFo-DFc mFo-DFc map_cc 2mFo-DFc mFo-DFc rotations3     %12s%7.4f %8.2f %7.2f %6.4f  %7.2f %7.2f %4.1fdegt	   unit_cellR<   R=   i    i   t   common_amino_acidt   suppress_segidiôÿÿÿR   R   R   t   generalt   ILEt   VALs   isoleucine or valinet   PROt   GLYt   glycines   pre-prolinei   t   prev_rest   residuet   next_resRl   R-   i`ÿÿÿiìÿÿÿi<   RB   RC   RD   Rb   t   residue_iselectiont   sites_cart_startt   two_mfo_dfc_mapt   mfo_dfc_mapt   residue_id_strt	   twomfodfct   mfodfcRI   t   residue_typet   validate_iselectionRT   R   R   RU   (   Rv   Rw   (   NN(F   t   focust   AssertionErrort   allt   mmtbx.secondary_structureRq   Rg   t	   iotbx.pdbR   R   t
   iselectionRr   R4   R[   R\   t   pdbt   common_residue_names_get_classR-   RC   t   extract_namet   modelst   chainst
   conformerst   residuest   ranget   lenR   t   resseq_as_intt   icodet   resnamet   id_strt   extract_i_seqR   R#   t   rotamert   get_phi_psi_indicest   phi_psi_from_sitesR@   t   intersectiont   sizet   is_approximately_helicalR,   Rp   R   t   extendRJ   t   poor_cc_thresholdR;   R   t   include_residue_selectiont   rotamer_evaluatort   get_cct   residue_rsr_monitort	   deep_copyt   t1_startt   t2_startt   torsion_grid_searcht   torsion_searchR   t   extractRM   RN   RZ   t   real_space_refine_peptidet   set_selectedt   refine_restrainedRS   R   t   t1_bestt   t2_bestt   set_sites_cart(C   t   pdb_hierarchyt   xray_structureRb   t   alpha_selectionR<   R=   t   residual_map_datat   rsr_managerRU   t	   validatort   logRq   t   mmtbxt   iotbxR   t
   alpha_iselt   fmt1t   fmt2t   fmt3Rr   t   map_selectort   minimizet	   get_classt   n_other_residuest   n_amino_acids_ignoredt   n_amino_acids_scoredR   RC   R   t   resultt   modelt   chaint	   conformerR“   t   i_rest   residue0t   residue1t   residue2t   resseq1t   resseq2R‚   t   residue1_iselectiont   residue2_iselectionR   R   RB   t   rama_res_typet   phi_psi_i_seqsRn   Ro   t   resseq0t   bad_phi_psit   common_i_seqst   residues_iselectiont   sites_cart_peptidet   rselt   rst   revt   cc_startt   rmt   torsion_paramsRW   t
   angle_bestt
   cc_flippedt   cc_endt   tmpt   sites_cart_refined(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   residue_iterationž   s@   

	
					(																		2c   #      C   s§  d d l  m } d d l m }	 d d  l }
 d d  l }
 d d l m } | d  k r` t	 j
 } n  | d  k r~ t ƒ  j ƒ  } n  | d  k r® d d  l }
 |
 j j d | ƒ } n  | j d d d | ƒ t ƒ  } |
 j j d	 | d
 t ƒ } |
 j j d | d |  j d | ƒ } | j ƒ  } | j r1| j ƒ  } n! | j | j j ƒ  j ƒ  t ƒ } | | M} d } | | d |  j ƒ  |  j ƒ  f IJ| j } | d  k r£d } n  xýt d | d ƒ D]è} t j  d |  d | j! d t" ƒ \ } } t j  d |  d | j# ƒ \ } } t j  d |  d | j$ d t" ƒ \ } } | j% d  k	 rh| j& | ƒ | j% k  } | j' | d ƒ } n  | j( d  k	 r¤| j& | ƒ | j( k  } | j' | d ƒ } n  t j) d | d | d | d | j* j+ d |  j, ƒ  j- ƒ  d d | j* j. ƒ } | | _/ | j0 d t1 j2 ƒ  ƒ | j3 d t ƒ } | j4 r§| j5 ƒ  } | d IJ|	 j6 | ƒ } | d IJ| j7 ƒ  } | d  k	 r§x0 | j8 D]" }  | j9 |  j9 ƒ }! | |! O} q{Wq§n  t* d | d |  j d | d | d | d  | d! | d" | d# | j* d$ | d | ƒ }" |  j: d% t d& t ƒ | d' I| | |  j ƒ  |  j ƒ  f IJ~ ~ ~ ~ ~ ~ | j; rUn  | j< rƒt j= d |  d( |" d) | d | ƒ n  | j> ƒ  j? |  j j@ ƒ  ƒ q·Wd  S(*   Niÿÿÿÿ(   t   print_statistics(   Rq   (   R   R³   t   texts   Peptide bond flipst   outt   geometryt   normalizationt   restraints_managerR´   s*   Macro-cycle %2d: r_work=%6.4f r_free=%6.4fi    i   t   fmodelt   map_typet   kickt
   target_mapR\   t   real_space_target_weightt   real_space_gradients_deltai   t   max_iterationsR¹   t
   alpha_onlys)   Looking for roughly helical residues. . .t    Rb   Rµ   R<   R=   R¶   R·   RU   R¸   t   update_f_calct   update_f_masks   1:R¦   t   validate_method(A   t   mmtbx.refinementRå   RŠ   Rq   t   mmtbx.restraintst   mmtbx.modelR   R   R   t   syst   stdoutR	   R¬   t   secondary_structuret   managert   make_sub_headerRe   t
   restraintsR,   RÇ   R´   t   backbone_selectionst   ignore_water_when_flippingt   solvent_selectiont   boolt
   scatterersRŸ   t   r_workt   r_freet   number_of_macro_cyclesR”   R   t   get_map_dataRî   R@   t	   model_mapt   residual_mapt   filter_residual_map_valuet   absR®   t   filter_2fofc_mapt   refinerRä   t   real_space_refine_target_weightt   f_obst   d_mint    real_space_refine_max_iterationsR³   t   find_automaticallyt	   cStringIOt   StringIOt   helix_selectiont   skip_approximate_helicest   atom_selection_cachet   find_helices_simplet   as_restraint_groupst   helixRb   t   update_xray_structuret   real_space_refine_overallt   validate_changet   validate_changesRC   t   set_xyzR-   (#   Rë   R\   R³   R  Rü   RU   t   optimize_hdR¹   Rå   Rq   Rº   R   R¸   Rê   RÇ   R   Rb   t   fmtt   n_mact   macro_cycleR<   t	   fft_map_1R=   t	   fft_map_2R¶   t	   fft_map_3t   map_selR·   Rµ   t   cachet   find_helicest	   ss_paramsR  R  R¦   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   runW  s¼      					!
#	 									

			)	
		
(   t
   __future__R    R÷   R   t   libtbx.philR   R  Rú   t   local_fix_params_strR   R   R   R	   R   R#   R3   R4   RZ   t   objectR[   Re   Rä   R   R@   R,  (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/flip_peptides.pyt   <module>   s*   									½