ó
±™xTc           @   sº  d  d l  m Z d  d l m Z d  d l Z d  d l m Z d  d l m Z d  d l	 m
 Z
 d  d l Z d  d l m Z d  d l m Z m Z d  d	 l m Z d  d
 l m Z d  d l Z d  d l m Z d  d l m Z m Z d  d l Z d  d l Z d e j j e j j e j j  e j! f Z" d „  Z# e j$ e% d „ Z& d e j f d „  ƒ  YZ' d „  Z( d e j) f d „  ƒ  YZ* d e+ f d „  ƒ  YZ, e- d k r¶e ƒ  e& e j. d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   density_modificationN(   t   flex(   t   crystal(   t   option_parser(   t   any_reflection_file(   t   show_times_at_exitt   Sorry(   t   runtime_utils(   t   adopt_init_args(   t   ncs(   t   nearest_phaset   phase_errorsÔ  density_modification {
  input
    .style = noauto
  {
    reflection_data {
      %s
    }
    experimental_phases {
      %s
    }
    map_coefficients
      .optional=True
      .help = Optional starting map coefficients
    {
      %s
    }
    ncs_file_name = None
      .type = path
      .optional = True
      .short_caption = NCS file
      .help = .ncs_spec file produced by phenix.find_ncs or phenix.find_ncs_from_density
    unit_cell = None
      .type = unit_cell
      .optional = False
      .style = bold noauto
    space_group = None
      .type = space_group
      .optional = False
      .style = bold noauto
  }
  output
    .style = noauto
  {
    map {
      file_name = None
        .type = path
        .help = The file name for the final density-modified map
        .short_caption = Output map file
        .style = bold noauto new_file file_type:mtz
      format = xplor *ccp4
        .type = choice
        .short_caption = Map format
      scale = *sigma volume
        .type = choice(multi=False)
        .short_caption = Map scaling
        .expert_level=2
    }
    mtz {
      file_name = None
        .type = path
        .help = The file name for the coefficients of the final density-modified map
        .short_caption = Output map coefficients
        .style = bold noauto new_file
      output_hendrickson_lattman_coefficients = False
        .type = bool
        .help = Output density modified phase probability distributions
      skip_output_if_worse = False
        .type = bool
        .help = Skip output if FOM gets worse
        .style = hidden
    }
  }
  job_title = None
    .type = str
    .input_size = 400
    .help = Job title in PHENIX GUI, not used on command line
    .style = noauto
%s
}
c         C   s    t  j j t d t ƒ} |  J| S(   Nt   process_includes(   t   iotbxt   philt   parset   master_params_including_IO_strt   True(   t   logt   parsed(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyt   defaults\   s    	c   5      C   s
  t  |  ƒ d k r; t d | ƒ } | j d d d | ƒ d  St ƒ  j ƒ  j d d d d	 d
 t d d ƒj d d d	 d
 t ƒj d |  ƒ } t d | ƒ } t j	 j
 d | j d | j d | d | d t ƒ } | j j d | ƒ | j j ƒ  j } | j j } t j d | j j d | j j ƒ } i  }	 x~ | j j j | j j j | j j j f D]R }
 t j j t |
 ƒ ƒ rQ|
 |	 k rQ|	 j |
 t  j! j" d |
 d t ƒ ƒ qQqQWt  j# j$ d | d |	 j% ƒ  ƒ } t j	 j& | d | j j d t d | ƒj' } t j	 j( | | j j d | d d d d  d t d t ƒ} | j j j d  k	 rŽ| j* d | j j j d | j j j+ d  t d! d  d d d" d ƒ j, ƒ  } n d  } d  } | j j- d  k	 rÞt. j. ƒ  } | j/ | j j- ƒ | j0 d | ƒ n  | j, ƒ  } | j, ƒ  } | j1 ƒ  j2 ƒ  } | j1 ƒ  j2 ƒ  } d  } d  } t  | j3 ƒ rŽt j	 j4 d# | j3 ƒ } | j5 j6 ƒ  } | | } } | j7 rº| j8 ƒ  } | j9 | ƒ } | j9 | ƒ j, ƒ  } | j9 | ƒ j, ƒ  } n  t j	 j: d$ | d% d& d' | g d( t d) t d* | j; d+ t< j= | j> ƒ  t ƒ ƒ ƒ } | j? d, | ƒ t j@ jA ƒ  } | jB t  jC jD d- ƒ ƒ j ƒ  j d } t j@ jE d. | d/ | ƒ } | jF d0 | jG ƒ jH ƒ  } n  d1 d  lI } | jI ƒ  } tJ | | | d2 | d3 | d4 | d | d5 | ƒ} | jI ƒ  | } | d6 | IJ| rxdb } d1 dD lK mL } d1 dE lM mN }  | jO ƒ  }! | dF ƒ }" t  | jP ƒ dG k r| jP }# |  jQ ƒ  }$ |$ jR dG dG dG ƒ }% |% jS dH ƒ |% jT tU dG | jV dI ƒ |# ƒ |" jW |$ ƒ | jX }# |  jQ ƒ  }$ |$ jR dG dG dG ƒ }% |% jS dJ ƒ |% jT tU dG | jV dI ƒ |# ƒ |" jW |$ ƒ n  x° | D]¨ }& g  tU dG | jV dI ƒ D] }' tY |! jZ |' ƒ |& ƒ ^ q<}# |  jQ ƒ  }$ |$ jR dG dG dG ƒ }% |% jS |& j[ dK dL ƒ ƒ |% jT tU dG | jV dI ƒ |# ƒ |" jW |$ ƒ qWg  tU dG | jV dI ƒ D]( }' |! jZ |' ƒ j\ |! jZ |' ƒ j] ^ qâ}# |  jQ ƒ  }$ |$ jR dG dG dG ƒ }% |% jS dM ƒ |% jT tU dG | jV dI ƒ |# ƒ |" jW |$ ƒ |" j^ ƒ  n  | j_ }( | j` }) | ja jb } | j d  k	 rO|( jF d0 | jG ƒ }* | jc dN k rÖ|* jd ƒ  n
 |* je ƒ  d  }+ }, g  }- | jf dO k r'|* jg d | j dP |- dQ |+ dR |, ƒ qO|* jh d | j dQ |+ dR |, d |- ƒ n  | ja ji }. |. j d  k	 r²| jj ƒ  \ }/ }0 |. jk r©|0 |/ k  r©t }1 dS |0 |/ f GHq¸t }1 n t }1 |. j d  k	 rø	|1 rø	t  ji jl ƒ  }2 | jm | jn ƒ jo |( ƒ } | jp dT dU dV |2 ƒ }3 |3 jq |( dT dW dV |2 ƒ| jm | jr ƒ jo |( ƒ }4 |3 jq |4 j; d+ t< js |4 jt ƒ  ƒ ƒ dT dX dY dZ dV |2 ƒ|3 jq |4 j; d+ |4 ju d[ t ƒ jt ƒ  ƒ dT d\ dY d] dV d  ƒ|. jv rß	|3 jq |) dT d^ dV |2 ƒn  |3 jw ƒ  jx |. j ƒ n  ty d_ | j d` |. j da | jO ƒ  ƒ S(c   Ni    R   t   prefixs     t   outs   -qs   --quiett   actiont
   store_truet   defaultt   helps   suppress outputs   --output_plotst   argst   cmd_cst   master_paramst    suppress_symmetry_related_errorst	   unit_cellt   space_group_infot	   file_namet   ensure_read_accesst   crystal_symmetryt   reflection_filest
   parameterst   extract_r_free_flagst   parameter_scopet    t   working_point_groupt   symmetry_safety_checkt   ignore_all_zerost   labelst   convert_to_phases_if_necessaryt   original_phase_unitst   parameter_namet   pdb_file_namest   f_obst   scattering_tablet   wk1995t   xray_structurest   bulk_solvent_correctiont   anisotropic_scalingt   r_free_flagst   datat   abcds?   map_coefficients {
  map_type = 2mFo-DFc
  isotropize = True
}
t   fmodelt   paramst   resolution_factoriÿÿÿÿt
   ncs_objectt
   map_coeffst   model_map_coeffst   as_gui_programs*   Time taken for density modification: %.2fst   fomt   skewnesst	   r1_factort   r1_factor_fomt   mean_solvent_densityt   mean_protein_densityt   f000_over_vt   k_flipt   rms_solvent_densityt   rms_protein_densityt   standard_deviation_local_rmst   mean_delta_phit   mean_delta_phi_initial(   t   PdfPages(   t   pyplots   density_modification.pdfi   s   correlation coefficienti   s   Mean effective phase errorst   _t    s!   RMS solvent/protein density ratiot   sigmat   xplort   title_linest   gridding_firstt   gridding_lasts?   Not writing out mtz. Final FOM (%7.3f) worse than start (%7.3f)t   column_root_labelt   Ft   label_decoratort   FWTt   FOMt   column_typest   Wt   degt   PHIBt   Pt   HLt   map_filet   mtz_filet   stats(   s   foms   skewnessRD   RE   RF   RG   RH   RI   s   rms_solvent_densitys   rms_protein_densityRL   RM   RN   (z   t   lenR   t   showR   t   enable_symmetry_comprehensivet   optiont   Falset   processt   mmtbxt   utilst   process_command_line_argsR   t   symmetryR   R<   t   extractR   t   optionst   output_plotsR   t   inputR    t   space_groupt   reflection_dataR"   t   experimental_phasest   map_coefficientst   ost   patht   isfilet   strt
   setdefaultR   t   reflection_file_readerR   t   reflection_file_utilst   reflection_file_servert   valuest   determine_data_and_flagsR2   t   determine_experimental_phasest   Nonet   get_phases_degR-   t
   map_to_asut   ncs_file_nameR
   t   read_ncst   display_allt   eliminate_sys_absentt   average_bijvoet_matesR1   t   pdb_filet   pdb_inpt   xray_structure_simplet   change_basis_to_niggli_cellt!   change_of_basis_op_to_niggli_cellt   change_basist   fmodel_simplet   arrayR   t   boolt   sizet   updatet   mapst   map_and_map_coeff_master_paramst   fetchR   R   t   map_coefficients_from_fmodelt   fft_mapt   grid_resolution_factort   real_map_unpaddedt   timet   density_modifyt   matplotlib.backends.backend_pdfRO   t   libtbxRP   t	   get_statst   correlation_coeffst   figuret   add_subplott	   set_titlet   plott   ranget   i_cyclet   savefigt   mean_phase_errorst   getattrt   get_cycle_statst   replaceRJ   RK   t   closet   map_coeffs_in_original_settingt   hl_coeffs_in_original_settingt   outputt   mapt   scalet   apply_sigma_scalingt   apply_volume_scalingt   formatt   as_xplor_mapt   as_ccp4_mapt   mtzt   start_and_end_fomt   skip_output_if_worset   ccp4_label_decoratort    miller_array_in_original_settingt   f_obs_completet
   common_sett   as_mtz_datasett   add_miller_arrayt   phase_sourcet   absR9   t   phasest'   output_hendrickson_lattman_coefficientst
   mtz_objectt   writet   result(5   R   R   RA   R   t   command_linet   processed_argsR<   Rr   R$   R%   t   rfnt   servert   fot	   hl_coeffsR?   R>   t	   model_mapR@   R‹   t   xst   fo_t   hl_t   change_of_basis_opt   fmodel_refinedt   master_philt
   map_paramsR�   t   t0t   dmt   time_dmt   plots_to_makeRO   RP   Re   t   pdfR9   t   figt   axR¦   t   it   dm_map_coeffst   dm_hl_coeffsRš   RV   RW   RU   t
   mtz_paramst   orig_fomt	   final_fomt   ok_to_write_mtzRZ   t   mtz_datasetRÂ   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyt   runb   sš   							$						'		   "	 	 ; B 		

		
		
			!$	
		Rž   c           B   s)   e  Z d d d d e d  „ Z d „  Z RS(   c	         C   s*  | |  _  t j ƒ  |  _ t j ƒ  |  _ t j j |  | | | d | d | d | d | ƒt |  j ƒ d k r&|  j  j |  j	 ƒ \ }	 }
 |	 j
 d |  j j ƒ j ƒ  } |
 j
 d |  j j ƒ j ƒ  } t j | j ƒ  j ƒ  | j ƒ  j ƒ  ƒ } d | j ƒ  GHd |  j d	 GH| j d
 d d g  ƒ n  d  S(   NR>   R?   R   RA   i   R=   s#   Starting dm/model correlation: %.6fs#   Final dm/model correlation:    %.6fiÿÿÿÿR"   s   starting.mapR-   (   R@   R   t   doubleR¢   Rª   R   t   __init__Rf   t   common_setst   map_coeffs_startRš   R<   R›   R´   t   linear_correlationRœ   t   as_1dt   coefficientR¸   (   t   selfR<   RÍ   RÎ   R>   R?   R@   R   RA   t   model_coeffst   start_coeffst   model_fft_mapRš   t   corr(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyRè   ’  s(    				$c         C   s  t  j  j |  ƒ |  j d  k	 r|  j j |  j ƒ \ } } | j d |  j j ƒ j	 ƒ  } | j d |  j j ƒ j	 ƒ  } Ht
 j | j ƒ  j ƒ  | j ƒ  j ƒ  ƒ } d GH| j ƒ  |  j j | j ƒ  ƒ |  j j t
 j t t
 j | j ƒ  ƒ t
 j | j ƒ  ƒ ƒ ƒ t  j ƒ n  d  S(   NR=   s   dm/model correlation:(   R   t   compute_mapR@   Rƒ   Ré   R?   Rš   R<   R›   R´   R   Rë   Rœ   Rì   t   show_summaryR¢   t   appendRí   Rª   t   meanR   t   argR9   t   pi_180(   Rî   Rï   t	   dm_coeffsRš   t   dm_mapRò   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyRó   °  s     		$
N(   t   __name__t
   __module__Rƒ   Rj   Rè   Ró   (    (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyRž   �  s   c         C   sø   |  j  } | j j j d  k r- t d ƒ ‚ n  | j j j d  k rQ t d ƒ ‚ n  | j j j d  k ry t d d ƒ ‚ n  | j j j d  k r� t d ƒ ‚ n  | j j	 j d  k rÖ | j j
 j d  k rÖ t d ƒ ‚ n  | j d  k rô t d ƒ ‚ n  d  S(   Ns   No reflection data provided.s   Data labels not specified.s6   Experimental phases (Hendrickson-Lattman coefficients s   not specified.s(   Experimental phase labels not specified.s   No output requested!s$   Please specify the solvent fraction!(   R   Rs   Ru   R"   Rƒ   R   R-   Rv   R±   R²   R¹   t   solvent_fraction(   R<   t   params_(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyt   validate_paramsÂ  s    	t   launcherc           B   s   e  Z d  „  Z RS(   c         C   s%   t  d t |  j ƒ d t j d t ƒ S(   NR   R   RA   (   Ræ   t   listR   t   syst   stdoutR   (   Rî   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyRæ   Ô  s    	(   Rû   Rü   Ræ   (    (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyR   Ó  s   RÈ   c           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   t  |  t ƒ  ƒ d  S(   N(   R	   t   locals(   Rî   Rc   Rd   Re   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyRè   Ú  s    c         C   s   |  j  j ƒ  S(   N(   Re   t   extract_loggraph(   Rî   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyR  Ý  s    c         C   s:   d |  j  j d ƒ j f d |  j  j d ƒ j f g } | S(   NR\   iÿÿÿÿt   Skewness(   Re   R¬   RB   RC   (   Rî   Re   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyt   get_final_job_statisticsà  s    c         C   sl   g  } |  j  d  k	 r. | j |  j  d f ƒ n  |  j d  k	 rV | j |  j d f ƒ n  |  j ƒ  } | | f S(   Ns   Map coefficientss   Real-space map(   Rd   Rƒ   Rõ   Rc   R  (   Rî   t   output_filesRe   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyt
   finish_jobç  s    (   Rû   Rü   Rè   R  R  R	  (    (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyRÈ   Ù  s   			t   __main__i   (/   t
   __future__R    Rl   R   t   mmtbx.utilst   cctbx.array_familyR   t   cctbxR   t   iotbx.option_parserR   t
   iotbx.philR   t   iotbx.reflection_file_readerR   t   libtbx.utilsR   R   R    R   R	   t
   mmtbx.mapst	   mmtbx.ncsR
   t   scitbx.mathR   R   Rx   R  Rm   t   data_and_flags_strt   experimental_phases_params_strt   map_coefficents_params_strt   master_params_strR   R   R  Rj   Ræ   Rž   Rÿ   t   target_with_save_resultR   t   objectRÈ   Rû   t   argv(    (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/density_modification.pyt   <module>   s8   H			ÿ /2	