ó
Ü™xTc           @   sE  d  d l  m Z d  d l m Z 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 d e e e d „ Z d e f d „  ƒ  YZ e e d	 „ Z d
 „  Z d e f d „  ƒ  YZ e e e d e d „ Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d S(   iÿÿÿÿ(   t   division(   t   ifloort   iceil(   t   Sorryt   null_out(   t   adopt_init_argsNc         C   s  | d k r t j } n  d d l m } d d l m }	 | j |  d d d t ƒj	 }
 |
 j
 d t ƒ } | j ƒ  j ƒ  d k } | j ƒ  | j t ƒ k rµ | d	 | j t ƒ IJn  | j | ƒ } t d
 | d | d | d | d t d t d t d | d | d | ƒ 
d S(   sê   
  Convenience function, used by phenix.fetch_pdb.

  :param remove_unknown_scatterering_type: if True, atoms with unknown
    scattering type will be removed before extracting the X-ray structure
    (which would crash otherwise).
  iÿÿÿÿ(   t   file_reader(   t   flext
   force_typet   pdbt   raise_sorry_if_errorst   enable_scattering_type_unknownt   unknowns8   WARNING: removing %d atoms with unknown scattering type.t	   file_namet   xray_structuret   map_outt   logt   auto_runt   quiett   anomalous_mapt   llg_mapt	   fill_mapst   assume_pdb_dataN(   t   Nonet   syst   stdoutt   iotbxR   t   scitbx.array_familyR   t   any_filet   Truet   file_objectt   xray_structure_simplet
   scattererst   extract_scattering_typest   sizet   countt   Falset   selectt   fast_maps_from_hkl_file(   t   pdb_filet   mtz_filet   output_filet   fillt   outR   t    remove_unknown_scatterering_typeR   R   R   t   pdb_int   xrst	   selection(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyt   create_map_from_pdb_and_mtz   s0     R&   c           B   sP   e  Z d  Z d d d d e j e e e e e e e d „ Z d „  Z	 d „  Z
 RS(   s›  
  Automation wrapper for calculating standard 2mFo-DFc, mFo-DFc, and optional
  anomalous difference map coefficients, starting from an X-ray structure and
  an MTZ file name.  This will attempt to guess the correct data array
  automatically (giving preference to anomalous amplitudes).

  The organization of this class is somewhat messy, as it is designed to be
  run in parallel for multiple structures.
  t   wk1995c   #      C   s¶  t  |  t ƒ  ƒ d d l m } d d l m } | d  k r` |	 r` | d t j j	 | ƒ IJn  d  } } | sHg  } d d d d d	 d
 g } d d d g } g  } t
 j } | j | d d ƒ} xª | j j D]œ } | j ƒ  j ƒ  } | j | ƒ | | k r	| } PqÎ | d  k rN| | k rN| j | ƒ } | | k  rj| } | } qjqÎ | j ƒ  rÎ | j | ƒ qÎ qÎ W| d  k rþt | ƒ d k rÕ| d  k rÕx` | D]% } | j ƒ  rŸ|  j rŸ| } PqŸqŸW| d } qþt d d | | d j | ƒ f ƒ ‚ n  | j ƒ  r| j ƒ  } n  | j ƒ  j ƒ  } | j ƒ  j d | ƒ } | j j d d  d | d d  d d  d t  d t! ƒ } t | ƒ d k rÂ| |  _" | j# d | j$ | j ƒ  j% ƒ  t  ƒ ƒ |  _& q�| d \ } } | j' d | j ƒ  | k ƒ j ƒ  }  | j ƒ  r|  j ƒ  r|  j( ƒ  }  n  |  j) | ƒ |  _& | j) |  j& ƒ |  _" nE d d  l* }! |! j+ j, j- d | d t. ƒ  ƒ }" |" j/ ƒ  \ |  _" |  _& d  |  _0 d  |  _1 | r²|  j2 ƒ  n  d  S(   Niÿÿÿÿ(   R   (   R   s.         no label for %s, will try default labelss   F(+),SIGF(+),F(-),SIGF(-)s   I(+),SIGI(+),I(-),SIGI(-)s   F,SIGFs   FOBS,SIGFOBSt   FOBS_Xs   IOBS,SIGIOBSt
   FreeR_flagt   FREEs   R-free-flagsR   t   hkli   i    s.   Couldn't find %s in %s.  Please specify valid s$   column labels (possible choices: %s)t    R/   R   t   labelt   test_flag_valuet   parameter_scopet   disable_suitability_testt   return_all_valid_arrayst   dataR(   R   (3   R   t   localsR   R   R   R   R   t   ost   patht   basenameR   t   maxintR   t   file_servert   miller_arrayst   infot   label_stringt   appendt   indext   is_xray_amplitude_arrayt   lent   anomalous_flagR   R   t   joint   is_xray_intensity_arrayt	   f_sq_as_ft   sys_absent_flagsR<   t
   map_to_asuR%   t   get_r_free_flagsR$   R   t   f_obst   arrayt   boolR"   t   r_free_flagst   customized_copyt   generate_bijvoet_matest
   common_sett   mmtbx.wwpdb.utilst   wwpdbt   utilst   find_data_arraysR   t   data_and_flagsR   t   fmodelt   run(#   t   selfR   R   t   scattering_tablet   f_labelt   r_free_labelR   R   R   R   R   R   t   save_fmodelR   R   R   R   RQ   RT   t   fallback_f_obst   default_labelst   default_rfree_labelst
   all_labelst
   best_labelt	   data_filet   miller_arrayt   labelst   label_scoreRR   t   sys_abs_flagst   r_freeR8   t	   new_flagst   mmtbxt	   data_info(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyt   __init__B   sŠ    
	
			'			c         C   sÿ   d d  l  } d d l m } | j j d d d |  j d |  j j ƒ  d t ƒ  ƒ | j j	 d |  j g d |  j
 d	 |  j d
 |  j d t d t d t d t ƒ } |  j r³ | |  _ n  t | ƒ \ } } d  } |  j rò |  j j ƒ  rò t | ƒ } n  | | | f S(   Niÿÿÿÿ(   R   R`   t
   n_gaussianR   t   d_minR   t   xray_structuresRQ   RT   t   outliers_rejectiont   skip_twin_detectiont   bulk_solvent_correctiont   anisotropic_scaling(   t   mmtbx.utilsR   R   RZ   t   setup_scattering_dictionariesR   RQ   Rt   R   t   fmodel_simpleR`   RT   R   R$   Rc   R]   t   get_maps_from_fmodelR   R   RJ   t   get_anomalous_map(   R_   Rp   R   R]   t   f_mapt   df_mapt   anom_map(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR}   �   s.    	
					c         C   ss   d d  l  } |  j ƒ  \ } } } |  j d  k rS t j j |  j ƒ d d |  _ n  | j j	 | | |  j | ƒ d  S(   Niÿÿÿÿi    s   _map_coeffs.mtz(
   t   iotbx.map_toolsR}   R   R   R>   R?   t   splitextR   t	   map_toolst   write_map_coeffs(   R_   R   R   R€   R�   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR^   ¶   s
    #N(   t   __name__t
   __module__t   __doc__R   R   R   R   R$   Rr   R}   R^   (    (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR&   8   s   	M	c         C   sˆ   |  j  ƒ  } | j d d d | d | ƒ } | j ƒ  rE | j ƒ  } n  | j d d d | d | ƒ } | j ƒ  r~ | j ƒ  } n  | | f S(   Nt   map_types   2mFo-DFct   exclude_free_r_reflectionst   fill_missings   mFo-DFc(   t   electron_density_mapt   map_coefficientsRJ   t   average_bijvoet_mates(   R]   t   fill_missing_f_obsRŠ   t   map_managert   two_fofc_coeffst   fofc_coeffs(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR}   ½   s    		c         C   s=   |  j  ƒ  } | j d d ƒ } | j ƒ  r9 | j ƒ  } n  | S(   NR‰   t   anom(   RŒ   R�   RJ   RŽ   (   R]   R�   t   anom_coeffs(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR~   Ì   s
    t   generate_water_omit_mapc           B   s8   e  Z d  Z e e e e d d „ Z d „  Z d „  Z RS(   s¸   
  Calculate standard map coefficients (with R-free flags omitted, and missing
  reflections filled) with water occupancies set to zero.  Used for ligand
  fitting after refinement.
  c      	   C   s  | d  k r t ƒ  } n  | j ƒ  } | j d ƒ }	 | j }
 |
 j ƒ  |  _ |	 j ƒ  |
 j ƒ  j ƒ  k so t ‚ |	 j	 t
 ƒ |  _ d  |  _ |  _ |  _ |  _ |  j d k rÊ | rÊ | d IJ| |  _ nC| d IJ| d |  j IJ| d IJ| j |	 ƒ |  _ |
 j ƒ  j ƒ  } |  j d k r<|
 j d d d |	 ƒ n  | j |
 d	 t
 ƒ| j d
 | ƒ | j ƒ  j d d d | d | ƒ |  _ | j ƒ  j d d d | d t d t
 ƒ |  _ | j ƒ  j ƒ  rï| j ƒ  j d d d | d | ƒ |  _ n  | r| j ƒ  j ƒ  |  _ n  d  S(   Ns   resname HOHi    s3     No waters in model, skipping omit map calculations.     Calculating new 2mFo-DFc and mFo-DFc maps...s/       %d waters temporarily set to zero occupancyt    t   valueR/   t   update_f_calcR   R‰   s   2mFo-DFcRŠ   R‹   s   mFo-DFct   merge_anomalousR“   (   R   R   t   atom_selection_cacheR/   R   t   crystal_symmetryR"   R    t   AssertionErrorR#   R   t   n_waterst   two_fofc_mapt   fofc_mapR�   t
   fmodel_mapt   pdb_hierarchy_omitR%   t   extract_occupanciest   set_occupanciest   update_xray_structuret   update_all_scalesRŒ   R�   R$   RQ   RJ   t   f_modelRŽ   (   R_   R]   t   pdb_hierarchyt   skip_if_no_watersRŠ   R�   t   write_f_modelR   t	   sel_cachet	   water_selR.   t   occ(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyRr   Ù   sN    	$



c         C   sJ   |  j  d  k	 s t ‚ t d |  j  d |  j d |  j d |  j d | ƒ d  S(   Nt
   fwt_coeffst   delfwt_coeffsR”   t   fmodel_coeffsR   (   Rž   R   Rœ   R…   RŸ   R�   R    (   R_   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR…     s    				c         C   s    |  j  j d | d |  j ƒ d  S(   NR   R›   (   R¡   t   write_pdb_fileR›   (   R_   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR°     s    N(   R†   R‡   Rˆ   R$   R   Rr   R…   R°   (    (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR•   Ó   s   .		c         C   s‹   d } | s, |  r d | } q, d | } n  d i | d 6| d 6|  oJ | d 6} | d	 k	 rq d | | f } n  | r‡ t j j | ƒ S| S(
   sã   
  Generate the map coefficients PHIL string for phenix.maps, phenix.refine,
  and related applications, based on several toggles which control the
  handling of missing reflections and R-free flags.  Used by the Phenix GUI.
  R–   sä   
      map_coefficients {
        mtz_label_amplitudes = 2FOFCWT_no_fill
        mtz_label_phases = PH2FOFCWT_no_fill
        map_type = 2mFo-DFc
        fill_missing_f_obs = False
        exclude_free_r_reflections = %s
      }sá   
      map_coefficients {
        mtz_label_amplitudes = 2FOFCWT_filled
        mtz_label_phases = PH2FOFCWT_filled
        map_type = 2mFo-DFc
        fill_missing_f_obs = True
        exclude_free_r_reflections = %s
      }so  
    map_coefficients {
      mtz_label_amplitudes = 2FOFCWT
      mtz_label_phases = PH2FOFCWT
      map_type = 2mFo-DFc
      fill_missing_f_obs = %(show_filled)s
      exclude_free_r_reflections = %(exclude_rfree)s
    }
    %(other_two_fofc)s
    map_coefficients {
      mtz_label_amplitudes = FOFCWT
      mtz_label_phases = PHFOFCWT
      map_type = mFo-DFc
      exclude_free_r_reflections = %(exclude_rfree)s
    }
    map_coefficients {
      map_type = anomalous
      format = *mtz phs
      mtz_label_amplitudes = ANOM
      mtz_label_phases = PHANOM
      exclude_free_r_reflections = %(exclude_rfree)s
    }
t   exclude_rfreet   other_two_fofct   show_filleds	   %s {
%s
}N(   R   t   libtbxt   philt   parse(   R³   t   disable_filledRŠ   t   enclosing_scopet   as_phil_objectt   secondary_two_fofc_paramst	   maps_phil(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyt!   get_default_map_coefficients_phil  s    	c          O   s   d d  l  } | j j |  | Ž  S(   Niÿÿÿÿ(   R‚   R„   R…   (   t   argst   kwdsR   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyR…   [  s    c      	   C   sÅ   |  j  d d
 ƒ } | j ƒ  | j ƒ  } g  t | | ƒ D] \ } } t | | ƒ ^ q8 } g  t | | ƒ D] \ } } t | | ƒ ^ qj }	 d g }
 d | } | j d | d |
 d | d	 |	 ƒ | S(   Nt   resolution_factori   g      @s'   REMARK map covering model + 3.0A buffers   %s.mapR   t   title_linest   gridding_firstt   gridding_lastgUUUUUUÕ?(   t   fft_mapt   apply_sigma_scalingt   n_realt   zipR   R   t   as_xplor_map(   t   coeffst   frac_mint   frac_maxt	   file_baseRÃ   RÅ   t   ft   nRÁ   RÂ   RÀ   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyt   write_xplor_map_filec  s    
22	
	i
   c         C   s  d d  l  } |  d  k	 r9 | j d |  d | ƒ \ } } n d d } } g  t | | ƒ D] \ }	 }
 t |	 |
 ƒ ^ qV } g  t | | ƒ D] \ }	 }
 t |	 |
 ƒ ^ qˆ } | j j j d | j	 ƒ  d | d | ƒ } | j j j
 d	 | d
 t d d g d | d | d | d d d d ƒ d  S(   Niÿÿÿÿt
   sites_cartt   bufferg        g      ð?RÍ   t   firstt   lastR   t
   is_p1_cellRÀ   s    Nonet	   unit_cellt   griddingR<   t   averaget   standard_deviation(   g        g        g        (   g      ð?g      ð?g      ð?(   t   iotbx.xplor.mapR   t   box_frac_around_sitesRÆ   R   R   t   xplort   mapRÕ   t   focust   writerR   (   RÏ   RÔ   t   map_dataRÅ   R   RÐ   R   RÉ   RÊ   RÌ   RÍ   RÁ   RÂ   RÕ   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyt   write_xplor_mapr  s(    	22		c          O   s"   d d  l  } | j j j |  | Ž  S(   Niÿÿÿÿ(   t   iotbx.gui_tools.reflectionst	   gui_toolst   reflectionst   extract_map_coeffs(   R½   R¾   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyRã   Œ  s    c          O   s"   d d  l  } | j j j |  | Ž  S(   Niÿÿÿÿ(   Rà   Rá   Râ   t   map_coeffs_from_mtz_file(   R½   R¾   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyRä   �  s    c          O   s"   d d  l  } | j j j |  | Ž  S(   Niÿÿÿÿ(   Rà   Rá   Râ   t    extract_phenix_refine_map_coeffs(   R½   R¾   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyRå   ”  s    c          O   s"   d d  l  } | j j j |  | Ž  S(   Niÿÿÿÿ(   Rà   Rá   Râ   t   get_map_coeff_labels(   R½   R¾   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyRæ   ™  s    c         C   s   t  |  d t ƒS(   Nt
   build_only(   Ræ   R   (   t   server(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyt   get_map_coeffs_for_build�  s    c          O   s"   d d  l  } | j j j |  | Ž  S(   Niÿÿÿÿ(   Rà   Rá   Râ   t   format_map_coeffs_for_resolve(   R½   R¾   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyRê      s    c          O   s"   d d  l  } | j j j |  | Ž  S(   Niÿÿÿÿ(   Rà   Rá   Râ   t   decode_resolve_map_coeffs(   R½   R¾   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyRë   ¥  s    (!   t
   __future__R    t   libtbx.math_utilsR   R   t   libtbx.utilsR   R   R´   R   t   libtbx.philR>   R   R$   R   R0   t   objectR&   R   R}   R~   R•   R¼   R…   RÎ   Rß   Rã   Rä   Rå   Ræ   Ré   Rê   Rë   (    (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/maps/utils.pyt   <module>   s>   	 …	J:								