ó
Õ™xTc           @   s  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z m	 Z	 d d l m
 Z
 d d l Z d d l Z d d l Z e j j d ƒ Z d	 e f d
 „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d Z d d d d e e e e e e e e e e d d d d „ Z d S(   s4  
This module provides a way to identify a suitably isomorphous initial model
for directly refining against experimental data.  This is commonly done when
solving a series of ligand-bound structures, partly to save time, and partly
to avoid changing the frame of reference when running molecular replacement.
iÿÿÿÿ(   t   division(   t   make_sub_header(   t   slots_getstate_setstate(   t   null_outt   Sorry(   t   easy_mpNsà   
d_min = None
  .type = float
rigid_body_refine = True
  .type = bool
optimize_b_factors = False
  .type = bool
max_cell_angle_rmsd=1.0
  .type = float
max_cell_edge_rmsd=1.0
  .type = float
max_r_free = 0.4
  .type = float
t   evaluate_modelc           B   sJ   e  Z d  Z d d d d d g Z e e e d d „ Z e j d d	 „ Z RS(
   sŒ   
  Create an fmodel object (including bulk solvent correction) and calculate
  R-factors, with or without optional rigid-body refinement.
  t   r_workt   r_freet   r_work_startt   r_free_startt   xray_structuret
   n_gaussianc	         C   sö  d  |  _ d  |  _ d  |  _ d d l m }	 d d l m }
 |
 j d | j	 ƒ  d | j
 ƒ  ƒ } | j d | ƒ } | j d | ƒ j ƒ  } | j d | ƒ j ƒ  } |	 d | d | d	 | g d
 | d | ƒ } | j ƒ  |  _ | j ƒ  |  _ | s|  j |  _ |  j |  _ | |  _ nß d d l m } | j d | d | d t d t d t ƒ  ƒ } g  } x6 | D]. } | j ƒ  j | ƒ } | j | j ƒ  ƒ q]W| j d | d | d | j j ƒ  d t ƒ  ƒ } | j j |  _ | j j ƒ  |  _ | j j ƒ  |  _ d  S(   Niÿÿÿÿ(   t   fmodel_simple(   t   crystalt	   unit_cellt   space_groupt   crystal_symmetryt   f_obst   r_free_flagst   xray_structurest   skip_twin_detectiont   scattering_table(   t
   rigid_bodyt   pdb_hierarchyR   t   group_all_by_chaint$   check_for_atoms_on_special_positionst   logt   fmodelt
   selectionst   params(   t   NoneR   R   R   t   mmtbx.utilsR   t   cctbxR   t   symmetryR   R   t   customized_copyt   eliminate_sys_absentR	   R
   t   mmtbx.refinementR   t   rigid_groups_from_pdb_chainst   TrueR   t   atom_selection_cachet	   selectiont   appendt
   iselectiont   managert   master_paramst   extractR   (   t   selfR   R   R   R   t   rigid_body_refinet   optimize_b_factorsR   R   R   R   t   combined_symmetryR   R   t   selection_stringsR   t   sele_strt   selet   refined(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   __init__(   sZ    													t    c         C   s.   | | d |  j  IJ| | d |  j IJd  S(   Ns   r_work = %6.4fs   r_free = %6.4f(   R   R   (   R/   t   outt   prefix(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   show`   s    (	   t   __name__t
   __module__t   __doc__t	   __slots__t   FalseR7   t   syst   stdoutR;   (    (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyR   !   s   	0c         C   s   d |  j  ƒ  S(   Ns   %g %g %g %g %g %g(   t
   parameters(   R   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   ucfd   s    t   select_modelc           B   sƒ   e  Z d e d  e j d „ Z e j e d „ Z d „  Z d „  Z	 d „  Z
 e d „ Z d d „ Z d	 d
 „ Z d „  Z d „  Z RS(   i   c	   '      C   sƒ  | d  k r t j ƒ  } n  | |  _ | d  k rÂ d d l m }	 g  } xy | D]n }
 t j j |
 ƒ sx t	 d |
 ƒ ‚ n  |	 |
 d d d t
 ƒj } | j } | j ƒ  } | j | | f ƒ qM Wn  g  |  _ g  |  _ g  |  _ | j d | j ƒ |  _ | j d |  j ƒ |  _ | |  _ | |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d d	 l m } d d
 l  m! } | j" ƒ  } | j# ƒ  } | j$ ƒ  } | j% ƒ  } | j& ƒ  d d !} | j& ƒ  d d !} t' d d | ƒ| d IJ| d | j( ƒ  IJ| d t) | ƒ IJg  } g  } x,t* | ƒ D]\ } }
 | | \ } } | j+ ƒ  j, ƒ  | j | ƒ | j" ƒ  } |  j j | ƒ | d  k r±| d | d IJ| d |
 IJ| j d  ƒ qn  | j% ƒ  } | j# ƒ  } t- } | | k rät
 } n! | j$ ƒ  } | | k rt
 } n  | sR| d | d IJ| d |
 IJ| d | j( ƒ  IJ| j d  ƒ qn  t- } | j. | ƒ rpt
 } nk | j& ƒ  d d !} | j& ƒ  d d !}  | | | ƒ }! | |  | ƒ }" |! | j/ k rÛ|" | j0 k rÛt
 } n  | s.| d | d IJ| d |
 IJ| d t) | j& ƒ  ƒ IJ| j d  ƒ qq| j | ƒ qW| j1 d  ƒ t2 | ƒ k ri| d IJ| d | IJt3 j4 d |  j5 d t6 | | ƒ d | ƒ }# g  }$ xQ t* |# ƒ D]C \ } }% |% d  k	 r¯|% j7 | j8 k rò|$ j | |% f ƒ qòq¯q¯Wt2 |$ ƒ d k ri|$ j9 d „  ƒ |$ d \ }& }% |$ |  _ |% j: |  _ | |& |  _ |% |  _ |  j |& |  _ qin  |  j; d | d  t
 ƒ d  S(!   Niÿÿÿÿ(   t   any_files)   model_data is None, but %s is not a file.t
   force_typet   pdbt   raise_sorry_if_errorst   d_mint   other(   t   rms_difference(   t   file_readeri    i   i   s   Evaluating modelsR9   s   Experimental data:s     space group:  %ss     unit cell:    %ss%   Model %d is missing symmetry records:i   s     source:  %ss&   Model %d has incompatible space group:s     space group: %ss     source: %ss     model:  %sR8   s/   Calculating R-factors - will use %s processors.t   funct   iterablet	   processesc         S   s   t  |  d j | d j ƒ S(   Ni   (   t   cmpR   (   t   at   b(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   <lambda>Õ   s    t   verbose(<   R   t   master_philR.   t   model_namest   iotbx.file_readerRF   t   ost   patht   isfilet   RuntimeErrorR'   t   file_objectt	   hierarchyt   xray_structure_simpleR*   t   model_symmetriest   models_acceptedt   model_r_freest   resolution_filterRJ   R   t
   common_setR   R   R   t   evaluationst   best_xray_structuret   best_pdb_hierarchyt   best_resultt   best_model_namet   mmtbx.pdb_symmetryRL   t   iotbxRM   R   R   t   build_derived_point_groupR   RC   R   t   infoRD   t	   enumeratet   atomst   reset_i_seqR@   t   is_similar_tot   max_cell_edge_rmsdt   max_cell_angle_rmsdt   countt   lenR   t   parallel_mapR   t   zipR   t
   max_r_freet   sortR   R;   ('   R/   RW   t
   model_dataR   R   R   R   t   nprocR   RF   t	   file_namet   model_inR   R   RL   RM   t   data_symmetryt   data_space_groupt   data_point_groupt   data_unit_cellt   data_cell_edgest   data_cell_anglest   pdb_hierarchiesR   t   kt   model_symmetryt   model_unit_cellt   model_space_groupt   is_compatible_sgt   model_point_groupt   is_similar_cellt   model_cell_edgest   model_cell_anglest   cell_edge_rmsdt   cell_angle_rmsdRe   t   passedt   resultt   i_result(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyR7   h   sÒ    														
				
					c         C   sã   |  j  d  k r | d IJnÃ | d IJ| d IJ| d |  j IJ|  j  j d | d d ƒ | d IJ| rß t |  j ƒ d k rß | d	 IJxF |  j d D]7 \ } } | d |  j | IJ| j d | d d ƒ q— W| d IJn  d  S(
   Ns)   No models accepted - will need to run MR.R8   s   Best starting model:s     source: %sR9   R:   s       i   s   Other suitable models:(   Rh   R   Ri   R;   Ru   Re   RW   (   R/   R9   RU   t   i_otherRK   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyR;   Þ   s    



c         C   s   |  j  d  k	 S(   N(   Rg   R   (   R/   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   successî   s    c         C   s   t  |  j d d  ƒ S(   NR   (   t   getattrRh   R   (   R/   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyR   ñ   s    c         C   s   t  |  j d d  ƒ S(   NR   (   R•   Rh   R   (   R/   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyR   ô   s    c         C   sE   |  j  d  k r d  S|  j } |  j } | r; | j | ƒ n  | | f S(   N(   Rh   R   Rf   Rg   t   adopt_xray_structure(   R/   t   update_structureR   R   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   get_best_model÷   s    		s   best_model.pdbc         C   s{   |  j  ƒ  s t ‚ |  j ƒ  \ } } t | d ƒ } | j d ƒ | j d |  j ƒ | j | j d | ƒ ƒ | j ƒ  d  S(   Nt   ws   REMARK original PDB file:
s   REMARK   %s
R   (   R”   t   AssertionErrorR˜   t   opent   writeRi   t   as_pdb_stringt   close(   R/   R|   R   R   t   f(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   save_best_model   s    s   best_model_data.mtzc         C   s–   |  j  ƒ  s t ‚ |  j ƒ  \ } } |  j j d | ƒ j ƒ  } |  j j d | ƒ j ƒ  } | j d d ƒ } | j | d d ƒ| j	 ƒ  j
 | ƒ d  S(   NR   t   column_root_labelt   Ft
   FreeR_flag(   R”   Rš   R˜   R   R#   R$   R   t   as_mtz_datasett   add_miller_arrayt
   mtz_objectRœ   (   R/   R|   R   R   R   R   t   mtz_data(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   save_updated_data	  s    c         C   sV   | \ } } | d  k r d  St d | d | d |  j d |  j d |  j j d |  j ƒ S(   NR   R   R   R   R0   R   (   R   R   R   R   R   R0   R   (   R/   t   argsR   R   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyR     s    		c         C   s   |  j  ƒ  \ } } | j ƒ  S(   N(   R˜   t   space_group_info(   R/   R   R   (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyRª      s    N(   R<   R=   R   R@   RA   RB   R7   R;   R”   R   R   R'   R˜   R    R¨   R   Rª   (    (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyRE   g   s   n						sŸ  
  remove_waters = True
    .type = bool
    .help = Remove all water molecules (HOH)
  remove_hydrogens = True
    .type = bool
    .help = Remove explicit hydrogen atoms
  remove_alt_confs = True
    .type = bool
    .help = Remove alternate conformations
  convert_semet_to_met = True
    .type = bool
    .help = Change MSE residues to MET
  convert_to_isotropic = True
    .type = bool
    .help = Convert atoms to anisotropic
  reset_occupancies = True
    .type = bool
    .help = Set occupancies to 1.0
  remove_ligands = False
    .type = bool
    .help = Remove all ligands
  reset_hetatm_flag = False
    .type = bool
    .help = Change HETATM records to ATOM
c   #      C   s÷  | d! k	 rW | j } | j } | j } | j } | j } | j }	 | j }
 | j } n  | d! k ro t	 ƒ  } n  t
 d d | ƒd d l m } d! } | d! k	 r5| d | IJ|  | g d! d! g k sÍ t ‚ d d l m } | j | d d d	 t ƒ} | j d ƒ | j j j ƒ  } | j j }  | j j ƒ  } n |  j ƒ  }  | j ƒ  } |  j ƒ  j ƒ  t |  j ƒ  ƒ d
 k r„t d ƒ ‚ n  | rô| j ƒ  } | j  t! ƒ } | d k rô|  j" | ƒ }  | j" | ƒ } | d | IJ|  j ƒ  j ƒ  qôn  | rl|  j# ƒ  j$ d ƒ } | j  t! ƒ } | d k rl|  j" | ƒ }  | j" | ƒ } | d | IJ|  j ƒ  j ƒ  qln  t } | rþ| j% ƒ  j& ƒ  } | j |  ƒ |  j ƒ  j' ƒ  } | j& ƒ  } | | k rÜ| d | | IJt! } n  | j" | ƒ } |  j ƒ  j ƒ  n  | rš|  j ƒ  }  | j d |  d | ƒ } | d k rš| d | IJt! } t( d d ƒ j) |  j* ƒ  ƒ |  j# ƒ  j$ } | d ƒ j  t ƒ d k s—t ‚ qšn  | rÊ| j ƒ  |  j+ | d | ƒ| d IJn  |	 r| sÜt ‚ | j, d d d d ƒ |  j+ | d | ƒ| d IJn  | r;x  |  j ƒ  D] } t! | _- q%Wn  |
 rõ|  j ƒ  j ƒ  |  j. ƒ  } xa | j/ ƒ  D]S }  |  j0 ƒ  rj|  j1 ƒ  rj| d t |  j ƒ  ƒ |  j2 f IJ| j3 |  ƒ qjqjW|  j ƒ  j' ƒ  } | j" | ƒ } |  j ƒ  j ƒ  n  | j% ƒ  j& ƒ  |  j ƒ  j& ƒ  k st ‚ | d! k	 rít( | d ƒ }! | d! k	 rl|! j) d j4 | ƒ ƒ |! j) d ƒ n  | r¤| d! k	 r¤|! j) d j4 | ƒ ƒ |! j) d ƒ n  d! }" | r¹| }" n  |! j) |  j* d |" ƒ ƒ |! j5 ƒ  | d  | IJn  |  | f S("   sÏ   
  Utility for removing extraneous records from a model intended for use in
  molecular replacement, etc., including waters, alternate conformations,
  and other features specific to a particular dataset.
  s   Processing input modelR9   iÿÿÿÿ(   t   pdbtoolss   Reading model from %s(   RM   RG   RH   RI   i   s   Multiple models not supported.i    s     removed %d hydrogenss   not (resname HOH)s     removed %d waterss-     removed %d atoms in alternate conformationsR   R   s,     removed %d selenomethionine (MSE) residuess   tmp1.pdbR™   s   resname MSEt   assert_identical_id_strs,     converted all atoms to isotropic B-factorst   occ_maxg      ð?t   occ_mins&     reset occupancy to 1.0 for all atomss(     removing %d ligand atoms in chain '%s's   
R   s     wrote model to %sN(6   R   t   remove_waterst   remove_hydrogenst   remove_alt_confst   convert_semet_to_mett   convert_to_isotropict   reset_occupanciest   remove_ligandst   reset_hetatm_flagR   R   t   mmtbxR«   Rš   Rk   RM   RF   R'   t   check_file_typeR]   t   inputt   remark_sectionR^   R_   t	   deep_copyt   deep_copy_scatterersRo   Rp   Ru   t   modelsR   t   hd_selectionRt   R@   t   selectR(   R)   t
   scattererst   sizet   extract_i_seqR›   Rœ   R�   R–   t   adjust_occupancyt   heterot
   only_modelt   chainst
   is_proteint   is_nat   idt   remove_chaint   joinRž   (#   R   R   R|   R   R¯   R°   R±   R²   R³   R´   Rµ   R¶   t   preserve_remarkst   preserve_symmetryt   add_remarkst   output_fileR   R«   t   remarksRM   t   pdb_inR5   t   n_hdt   n_watR¬   t   n_atoms_startt   i_seqst   n_atoms_endt   n_mset   selt   atomt   modelt   chainRŸ   t   symm(    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   strip_model?  sÚ    											'
*	
(   R>   t
   __future__R    t   libtbx.str_utilsR   t   libtbxR   t   libtbx.utilsR   R   R   t   libtbx.philt   os.pathRY   RA   t   philt   parseRV   R   RD   t   objectRE   t   strip_model_paramsR   R'   R@   RÝ   (    (    (    sY   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/select_best_starting_model.pyt   <module>   s>   		C	Ö