ó
±™xTc           @   sj  d  d l  m Z d  d l Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l	 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 d  d l
 m Z d  d	 l m Z e j d
 ƒ e j d
 ƒ d Z d Z d „  Z e j e e d „ Z d e j f d „  ƒ  YZ d d „ Z! d „  Z" d „  Z# e$ d k rfe d e j% d ƒ n  d S(   iÿÿÿÿ(   t   divisionN(   t   flex(   t   Sorry(   t   runtime_utils(   t   reflection_file_reader(   t   reflection_file_utils(   t   crystal_symmetry_from_any(   t   crystali    s   
phenix.maps: a command line tool to compute various maps and save them in most
             of known formats.

How to run the command line version:

  1. Run phenix.maps without any arguments: just type phenix.maps in the command
     line and hit Enter. This will creare a parameter file called maps.params,
     which can be renamed if desired.

  2. Edit maps.params file to specify input/output file names, data labels and
     the desired maps. It is possible to request as many maps as desired. By
     default, the file maps.params specifies 5 maps to be created: 2mFo-DFc,
     2mFo-DFc with missing Fobs filled with DFcalc, mFo-DFc and anomalous
     difference maps will be output in MTZ format, and one 2mFo-DFc map will be
     output in CCP4 format.
     NOTE: the anomalous difference map will only be created if the input
     reflection data file contains Bijvoet maps (F+/F- or I+/I-).

  3. Run this command to compute requested maps: phenix.maps maps.params

Alternately, you may specify input files (and additional parameters) directly
on the command line:

  % phenix.maps model.pdb data.mtz

and it will automatically generate the default maps as described above.

Important Facts:

  - phenix.maps is available in PHENIX GUI.

  - The scope of parameters 'map_coefficients' defines the map that will be
    output as Fourier map coefficients. The scope of parameters 'map' defines
    the maps that will be output as CCP4 or X-plor format.

  - To create several maps: duplicate either 'map_coefficients' or 'map' or both
    scopes of parameters as many times as many maps is desired. Then edit each
    of them to define the maps.

  - A map is defined by specifying a map type using 'map_type' keyword available
    within each scope of parameters: 'map_coefficients' or 'map'. The general
    supported format for 'map_type' is: [p][m]Fo+[q][D]Fc[_kick][_filled]. For
    example: 2Fo-Fc, 2mFobs-DFcalc, 3Fobs-2Fmodel, Fo-Fc, mfobs-Dfcalc, anom,
    llg.  The 'map_type' parser will automatically recognize which map is
    requested.

  - The program creates as many files with CCP4 or X-plor formatted maps as
    is requested, and it creates only one MTZ formatted file with
    all Fourier map coefficients in it.

  - The CCP4 or X-plor formatted maps can be computed in the entire unit cell
    or around selected atoms only.

  - Kick maps and missing Fobs filling is done (if requested) as described in
    Adams et al. (2010). Acta Cryst. D66, 213-221.

  - Twinning (if detected) will be accounted for automatically. This can be
    disabled by using "skip_twin_detection=True" keyword.

  - All arrays used in map calculation, for example: Fobs, Fmodel, Fcalc, Fmask,
    m, D, etc., can be output into a CNS or MTZ formatted reflection file.

  - For those who likes to experiment: bulk solvent correction and anisotropic
    scaling can be turned off, the data can be filtered by sigma and resolution.

  - For some map types certain 'map_coefficients' or 'map' scope parameters may
    not be applicable. For example, for "map_type=anomalous" the keywords
    "kicked", "fill_missing_f_obs" and some other are not applicable.

  - For LLG map calculation, if you specify the wavelength any existing heavy
    atoms (P or heavier) will be modeled as anomalous scatterers using the
    theoretical values of f' and f''.
sø  maps {
  map_coefficients {
    map_type = 2mFo-DFc
    format = *mtz phs
    mtz_label_amplitudes = 2FOFCWT
    mtz_label_phases = PH2FOFCWT
    fill_missing_f_obs = False
  }
  map_coefficients {
    map_type = 2mFo-DFc
    format = *mtz phs
    mtz_label_amplitudes = 2FOFCWT_fill
    mtz_label_phases = PH2FOFCWT_fill
    fill_missing_f_obs = True
  }
  map_coefficients {
    map_type = mFo-DFc
    format = *mtz phs
    mtz_label_amplitudes = FOFCWT
    mtz_label_phases = PHFOFCWT
    fill_missing_f_obs = False
  }
  map_coefficients {
    map_type = anomalous
    format = *mtz phs
    mtz_label_amplitudes = ANOM
    mtz_label_phases = PHANOM
  }
  map {
    map_type = 2mFo-DFc
    fill_missing_f_obs = False
    grid_resolution_factor = 1/4.
  }
}
c         C   sV  |  j  j } d } x” | t | ƒ k  r¨ | | } | j d  k rM | | =q n  t j | j ƒ j r› t | _	 t | _
 d | _ d | _ t | _ d  | _ n  | d 7} q W|  j  j } d } x” | t | ƒ k  rQ| | } | j d  k rö | | =q¾ n  t j | j ƒ j rDt | _	 t | _
 d | _ d | _ t | _ d  | _ n  | d 7} q¾ Wd  S(   Ni    g       @g      ð?i   (   t   mapst   map_coefficientst   lent   map_typet   Nonet   mmtbxt	   map_namest	   anomaloust   Falset   kickedt   fill_missing_f_obst   acentrics_scalet   centrics_pre_scalet
   sharpeningt   sharpening_b_factort   map(   t   paramst   mcpt   it   mcp_t   mpt   mp_(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyt   analyze_input_params„   s<    
					
					c   .      C   s•
  | t  IJ| d d IJt j j ƒ  } t |  ƒ d k s[ t |  ƒ d k r+|  d d k r+t |  ƒ d k oz |  d d k sà d } | d | t j j d ƒ f IJt j j | ƒ rÎ d	 } t	 | | ƒ ‚ n  t
 | d
 ƒ } n | } | d IJ| j t j j t ƒ ƒ } | j d | d d d d ƒ d  St j j d |  d | d | ƒ } | j }	 |	 j ƒ  }
 |
 j j j } t |
 j j ƒ d k rÝt |
 j j ƒ d k rÝ| d IJ| j d |	 t j j t ƒ g ƒ }	 |	 j ƒ  }
 n  | d  k sï| rd  |
 j j _ n  t d |
 ƒ t | j ƒ d k } t | j ƒ d k rGt	 d ƒ ‚ n  |
 j j j d  k rŠt | j ƒ d k rŠ| j d |
 j j _ n  t j j t |
 j j j ƒ ƒ sÍt	 d t |
 j j j ƒ ƒ ‚ n  |
 j j j  j! d  k r1|
 j j j  j" j! d  k r1t | j# ƒ d k r1| j# d |
 j j j  _! n  | d I|
 j j j IJ| j$ d |
 ƒ }	 | d d IJ| d IJ|	 j d | d d ƒ t j% j d |
 j j j ƒ } | j& ƒ  g } g  } x‹ |
 j j j  j! |
 j j j  j" j! g D]b } t j j t | ƒ ƒ råy | j' t( j) | ƒ ƒ WqGt* k
 r3‚  qGt+ k
 rCqGXqåqåWd  } y t, j- d | d | ƒ } Wn< t. k
 r«} d t | ƒ k r¬t	 d d  d! ƒ ‚ q¬n Xg  } g  } x‚ |
 j j j  j! |
 j j j  j" j! g D]Y } t j j t | ƒ ƒ rà| | k rà| j' t/ j0 d | d" t1 ƒ ƒ | j' | ƒ qàqàWt2 j3 d# | d$ t4 d% | d& | ƒ } t j j5 d' d( ƒ } | j j6 d) ƒ } | j | ƒ j ƒ  j  } t j j7 d* | d+ | d, d) d- d. d/ d0 d1 t4 d | ƒ } | j8 } | j" } | j9 } | d  k r3| j: d2 t; j< | j= ƒ  j> ƒ  t1 ƒ ƒ } d  } n  | d d IJ| d3 I|
 j j j IJ| j? d4 t4 ƒ } | j@ ƒ  } | jA d# | ƒ } |
 j jB jC d  k	 rñ| jC d5 |
 j jB jC ƒ }  |  }! | jD d6 |! ƒ } | jD |! ƒ } | j@ ƒ  } n  t j jE d7 |
 j jF d8 | d9 | jG ƒ  d | ƒ |
 j jH d  k	 rx|
 j jF d: k rYt	 d; d< ƒ ‚ n  | jI d= |
 j jH d> d? ƒ n  | jJ d@ | d dA ƒ | d d IJ| dB IJt j jK dC | g d7 |
 j jF dD | dE | dF |
 j j j  jL dG |
 j jM dH |
 j jN dI |
 j jO ƒ }" |" jP ƒ  }# |# jQ d | ƒ | d d IJ| dJ IJ|
 j j jR d  k	 rŽ| rŽt j jS |
 j j jR ƒ s|t. ‚ |
 j j jR }$ n t jT ƒ  }$ |
 j j jU d  k	 rÜt j jV |$ t j jW |
 j j jU ƒ ƒ }% n: |
 j j j }% |% jX d ƒ d k r	|% |% jY d ƒ  }% n  t j jZ dK |" d |
 j j dL | dM d  dN |% dO | ƒ }& t j j[ dK |" d |
 j j dO | d | ƒ }' |% dP }( d  }) |
 j j j\ r°	|" j" ƒ  j] ƒ  }) n  |' j^ d |( dE |) ƒ }* |
 j j j d  k	 r=
|% dQ |
 j j j }+ | dR |+ IJt
 |+ d
 ƒ }, |" j_ d |, dS |
 j j j ƒ |, j` ƒ  n  | dT IJ|* r^
| dU |( IJn  x |& D] }- | dV |- IJqe
W| d d IJ|( |& f S(W   Nt   -iO   i    i   t   NO_PARAMETER_FILEs   maps.paramss;   Creating parameter file '%s' in the following directory:
%st   .sG   File '%s' exists already. Re-name it or move and run the command again.t   ws   
All phenix.maps parameters::
t   outt   prefixt    t   expert_levelt   argst   logt   master_paramss0   No map input specified - using default map typest   sourcesR   s(   Only one model file is allowed as input.sB   model file is not given: maps.input.pdb_file_name=%s is not a files   FORMAT:t   python_objects   
Parameters to compute maps::
t	   file_namet   from_coordinate_filest   from_reflection_filess.   No unit cell and symmetry information supplieds:   Missing or incomplete symmetry information.  This program s>   will only work with reflection file formats that contain both s5   unit cell and space group records, such as MTZ files.t   ensure_read_accesst   crystal_symmetryt   force_symmetryt   reflection_filest   errt   master_scope_namet   reflection_datas   maps.input.reflection_datat   reflection_file_servert
   parameterst   data_parameter_scopet   flags_parameter_scopes'   maps.input.reflection_data.r_free_flagst   data_descriptions   Reflection datat
   keep_goingt   datas   
Input model file:t   set_atom_i_seqt   stringt	   selectiont   scattering_tablet   xray_structuret   d_mint   neutrons4   Wavelength parameter not supported when the neutron s   scattering table is used.t   photont   tablet   sasakit   fs     s0   Bulk solvent correction and anisotropic scaling:t   xray_structurest   f_obst   r_free_flagst   outliers_rejectiont   skip_twin_detectiont   bulk_solvent_correctiont   anisotropic_scalings   Compute maps.t   fmodelt   atom_selection_managert   file_name_prefixt   file_name_baset   pdb_hierarchys   _map_coeffs.mtzs   _fmodel.s7   Writing fmodel arrays (Fobs, Fcalc, m, ...) to %s file.t   formats	   All done.s   Map coefficients: %ss   CCP4 or XPLOR map: %s(a   t   legendR   R   t   maps_including_IO_master_paramsR
   t   ost   patht   abspatht   isfileR   t   opent   fetcht   iotbxt   philt   parset   default_paramst   showt   utilst   process_command_line_argsR   t   extractt   outputt   fmodel_data_file_formatR	   R   R   R   t   phil_file_namest   pdb_file_namest   inputt   pdb_file_namet   strR5   R,   RJ   t   reflection_file_namesRT   t   pdbt   crystal_symmetry_from_cryst1t   appendR   t   extract_fromt   KeyboardInterruptt   RuntimeErrorR   t   select_crystal_symmetryt   AssertionErrorR   t   any_reflection_fileR   R   R6   t   Truet   data_and_flags_master_paramst   gett   determine_data_and_flagsRI   t   test_flag_valuet   arrayR   t   boolR<   t   sizet   construct_hierarchyt   atom_selection_cachet   xray_structure_simplet   omitR?   t   selectt   setup_scattering_dictionariesR@   RB   t
   wavelengtht   set_inelastic_form_factorst   show_summaryt   fmodel_simpleRK   RL   RM   RN   t   infot$   show_rfactors_targets_scales_overallt	   directoryt   isdirt   getcwdR$   t   joint   basenamet   countt   indext   compute_xplor_mapst   compute_map_coefficientst   include_r_free_flagst   average_bijvoet_matest   write_mtz_filet   exportt   close(.   R'   R(   t   use_output_directoryt   suppress_fmodel_outputR)   t   parameter_file_namet   msgt   pfot   processed_argst   working_philR   Rf   t   have_phil_file_inputt   pdb_inpt   cs_from_coordinate_filest   cs_from_reflection_filest   rfnR0   t   eR2   Rl   R6   t   reflection_data_master_paramst   reflection_data_input_paramst   reflection_data_paramst   determine_data_and_flags_resultRI   RJ   Rz   RS   RP   RA   t   omit_selectiont   keep_selectionRO   t   fmodel_infot
   output_dirRR   t
   xplor_mapst   cmot   map_coeff_file_namet   r_free_flags_outputt   write_mtz_file_resultt   fmodel_file_namet   fmodel_file_objectR,   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyt   run¦   s~   
4"
		0
!
  	$									*					

	
!!		
	
t   launcherc           B   s   e  Z d  „  Z RS(   c      	   C   sK   t  j |  j ƒ t  j |  j ƒ t d t |  j ƒ d t j d t	 d t
 ƒ S(   NR'   R(   R˜   R™   (   RW   t   mkdirR¬   t   chdirR´   t   listR'   t   syst   stdoutR   Rv   (   t   self(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyR´   u  s    	(   t   __name__t
   __module__R´   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyRµ   t  s   c         C   sM  |  j  j j d  k r$ t d ƒ ‚ nÞ |  j  j j j d  k rK t d ƒ ‚ n· |  j  j j j d  k rr t d ƒ ‚ n� t |  j  j	 ƒ d k r± t |  j  j
 ƒ d k r± t d ƒ ‚ nQ |  j  j j d  k	 rt j j |  j  j j ƒ rt d d |  j  j j ƒ ‚ n  |  j  j d  k	 r<|  j  j d k r<t d	 d
 ƒ ‚ q<n  t |  j  ƒ t S(   Ns   No model file defined.s   No reflection file defined.s%   No labels chosen for reflection data.i    s+   You have not requested any maps for output.s8   The output directory %s does not exist; please choose a s/   valid directory, or leave this parameter blank.RC   s4   Wavelength parameter not supported when the neutron s   scattering table is used.(   R   Ri   Rj   R   R   R5   R,   t   labelsR
   R   R	   Re   RŠ   RW   RX   R‹   R„   R@   t   validate_map_paramsRv   (   R   t   callback(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyt   validate_params}  s&    0c   	      C   sM  d d l  m } g  } xè |  j D]Ý } | j d  k	 r  y | | j ƒ } Wn% t k
 ro } t t | ƒ ƒ ‚ n X| j } | j	 } | | k sš | | k r¿ t d d d d | | f ƒ ‚ n( d  | | g k rç t d | j ƒ ‚ n  | j
 | | g ƒ q  q  Wt |  d ƒ rIx6 |  j D]( } | j d	 k rt d
 d ƒ ‚ qqWn  t S(   Niÿÿÿÿ(   R   s8   The map coefficients with MTZ labels %s,%s duplicates atsC    least one previously defined label.  You may output multiple sets sC   of coefficients with the same map type, but the column labels must s
   be unique.s8   Please specify both MTZ column labels for map_type '%s'.R   g      à?s4   The grid resolution factor for CCP4 and X-PLOR maps s   must be 0.5 or less.(   R   R   R	   R   R   Rr   R   Rk   t   mtz_label_amplitudest   mtz_label_phasest   extendt   hasattrR   t   grid_resolution_factorRv   (	   R   R   R¾   t
   map_coeffst
   decode_mapR¤   RG   t   phiR   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyR¿   —  s0    		c         C   s‰   |  \ } } g  } | d  k	 rF t j j | ƒ rF | j | d f ƒ n  x6 | D]. } t j j | ƒ rM | j | d f ƒ qM qM W| g  f S(   Ns   MTZ files	   XPLOR map(   R   RW   RX   RZ   Ro   (   t   resultst   mtz_filet	   map_filest   output_filest   map_file(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyt
   finish_job³  s    t   __main__R'   i   (&   t
   __future__R    t
   mmtbx.mapsR   t   scitbx.array_familyR   RW   R¹   t   randomt	   iotbx.pdbR]   t   libtbx.utilsR   t   libtbxR   t   mmtbx.utilsR   R   R   t   cctbxR   t   seedt   set_random_seedRU   R`   R   Rº   Rv   R   R´   t   target_with_save_resultRµ   R   RÁ   R¿   RÏ   R¼   t   argv(    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/maps.pyt   <module>   s0   $L%	"	Í			
