ó
±™xTc           @   s  d  d l  m Z d  d l m 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 m Z d  d l Z d  d l Z e j j d d e ƒZ e Z d	 „  Z e j e d
 „ Z d „  Z d e	 j f d „  ƒ  YZ d „  Z e d k re e j d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   utilsN(   t   file_reader(   t   runtime_utils(   t   Sorryt   Usages	  
mtz_file = None
  .type = path
  .short_caption = MTZ file
  .style = bold file_type:hkl OnChange:extract_map_coeffs_for_fft
  .help = MTZ file containing map coefficients
pdb_file = None
  .type = path
  .short_caption = Model file
  .help = Model file around which to draw the map.  If not supplied, the map     will fill the unit cell.
labels = None
  .type = strings
  .multiple = True
  .help = Map column labels.  Common examples are "2FOFCWT,PH2FOFCWT" and     "FP,SIGFP" "PHIM" "FOMM".  If left blank, all maps present in the input     file will be used.
buffer = 5.0
  .type = float
  .short_caption = Region padding
  .help = Extra padding (in Angstroms) around the selected region (or unit     cell)
selection = None
  .type = str
  .input_size = 400
  .short_caption = Atom selection
  .help = Atom selection around which to draw map (plus buffer).  If left     blank, the entire model file will be used.
d_min = None
  .type = float
  .help = High-resolution cutoff
d_max = None
  .type = float
  .help = Low-resolution cutoff
grid_resolution_factor = 0.25
  .type = float
  .help = Grid spacing (multiplied by the high-resolution limit)
scale = *sigma volume
  .type = choice(multi=False)
  .expert_level = 1
  .short_caption = Map scaling
  .help = Scaling method for map values
output {
  directory = None
    .type = path
    .short_caption = Output directory
    .help = Output directory (defaults to current)
    .style = output_dir bold
  prefix = None
    .type = str
    .input_size = 400
    .short_caption = Output file prefix
    .help = Output file prefix (defaults to the MTZ file name base)
  include scope libtbx.phil.interface.tracking_params
  format = xplor *ccp4 dsn6
    .type = choice
    .caption = XPLOR CCP4 DSN6
  extension = *Auto ccp4 xplor map dsn6
    .type = choice
}
r_free_flags {
  remove = False
    .type = bool
    .short_caption = Remove R-free set from map
  file_name = None
    .type = path
    .short_caption = R-free flags
    .style = OnChange:extract_r_free_flags_for_fft
  label = None
    .type = str
    .input_size = 120
    .short_caption = R-free label
    .style = renderer:draw_rfree_label_widget
  test_flag_value = None
    .type = int
}
include_fmodel = False
  .type = bool
  .short_caption = Include F(model) if present
show_maps = False
  .type = bool
t   process_includesc         C   s1   x* |  D]" } | j  ƒ  j ƒ  | k r | Sq Wd  S(   N(   t   infot   label_stringt   None(   t   miller_arrayst   labelst   array(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/mtz2map.pyt
   find_arraye   s    c   ,      C   sr  d d  l  } d  } d  } g  } t |  ƒ d k rc | d IJt j d | d d ƒ t d d ƒ ‚ n  t j d	 d
 ƒ } x·|  D]¯} t j j	 | ƒ rÆt
 j | ƒ }	 |	 j d k rÿ | d  k	 rÍ t d ƒ ‚ n  | j | j j d t j j | ƒ ƒ ƒ |	 } q+|	 j d k ry| j d ƒ s,t d ƒ ‚ n | d  k	 rGt d ƒ ‚ n  | j | j j d t j j | ƒ ƒ ƒ |	 } q+y | j j d | ƒ }
 Wn! t k
 rµt d | ƒ ‚ q+X| j |
 ƒ q| | j d ƒ ræ| d d } n  y | j d | ƒ } Wn t k
 r| d | IJq| X| j | ƒ q| Wt j d | ƒ } | j ƒ  } | d  k rw| j d  k rwt d ƒ ‚ n  | s| j j d  k r¤t j ƒ  | j _ n  t j j | j j ƒ sÒt j | j j ƒ n1 t j j | j j ƒ st d | j j ƒ ‚ n  | j j } n t j ƒ  } | j j d  k r^t j j  t j j! | j ƒ ƒ d | j _ n  | d  k r‹t
 j | j d d d t" ƒ} n  g  } | j# s¯t | j$ ƒ d k r¹t% j& | j' d t( d  | j) d! t" ƒ} | j) st% j& | j' d t( d  t( d! t" ƒ} n | } t | ƒ d k r�| d" IJx} | D]f } t* | t+ ƒ rQ| g } n | } | | k rld# } n d
 } | j$ j | ƒ | d$ | | f IJq0Wn t d% ƒ ‚ | j# r¹t( Sn  | d  k rü| j, d  k	 rüt
 j | j, d d ƒ} | j- d ƒ n  | j. j/ ƒ  } d  } | j0 j1 rÂ| j0 j2 d  k	 rMt
 j | j0 j2 d d ƒ} n | } | j' j3 d | j2 d& | j0 j4 d' | j0 j5 d( t( d) d* ƒ \ } } | j6 d+ | j7 ƒ  | k ƒ j8 ƒ  j9 ƒ  } n  d  } | d  k	 rU| j. j: } | j; ƒ  j< ƒ  } | j= d  k	 r_| j> ƒ  } | j= | j= ƒ } | j? | ƒ } t | ƒ d k rRt d, ƒ ‚ qRq_n
 | d- IJg  } g  } x t@ | j$ ƒ D]ï\ } } d  }  t | ƒ d. k rŒtA | | d ƒ }  |  d  k rJt% j& | j' d! t" d t( d  | j) ƒ} g  }! t | ƒ d k rnxC | D]; } t* | t+ ƒ r&| g } n  |! j d d/ jB | ƒ ƒ qWt d0 d1 | d d2 jB |! ƒ f ƒ ‚ q‰t d3 d4 | d ƒ ‚ qJn¾ t | ƒ d k r®| j d  ƒ n  t% jC d5 | d6 | d d7 | d. d8 | d ƒ \ }" }# }$ |" jD ƒ  sùtE ‚ |# jF ƒ  stE ‚ |$ d  k s)|$ jF ƒ  s)tE ‚ | jG jH d9 |" d: |# d; |$ ƒ }  | d< d/ jB | ƒ IJ|  jI ƒ  sstE ‚ |  j8 ƒ  j9 ƒ  }  |  jJ d= | jK d> | jL ƒ }  | d  k	 rý|  jM d? | ƒ \ }  }% | d@ |% j7 ƒ  jN t" ƒ IJ|  j? |% j7 ƒ  ƒ }  n  |  jO dA | jP ƒ }& | jQ dB k r8	| dC IJ|& jR ƒ  n& | jQ dD k r^	| dE IJ|& jS ƒ  n  d  }' | dF dG dH g k r‚	d
 }' n| d j dI ƒ rº	| d j dJ ƒ r±	dK }' q™
dL }' nß | d j dM ƒ rÖ	dN }' nÃ | d dO k rï	dL }' nª | d dP k r
dN }' n‘ | d j dQ ƒ r$
dR }' nu | d j dS ƒ r@
dT }' nY | d j dU ƒ sf
| d j dV ƒ ro
dW }' n* | d j dX ƒ r‹
dY }' n dZ | d. }' d[ | d k r¶
|' d[ 7}' n d\ | d k rÓ
|' d] 7}' n  |' | k r| j |' ƒ | jN |' ƒ }( |' dZ |( 7}' n | j |' ƒ | j jT }) | j jU d^ k rj|) d_ k rLd_ }* qÌ|) d` k rada }* qÌdb }* nb | j jU }* |) db k r¡|* do k r¡t dd |* ƒ ‚ n+ |) d_ k rÌ|* dp k rÌt de |* ƒ ‚ n  t j jB | | j j |' df |* ƒ }+ |) db k rXt% jV dg | dh |  jW ƒ  di |& jX ƒ  dj |& jY ƒ  d |+ dk | jZ ƒ | j |+ dl f ƒ n|) d` k rØ| d  k	 rÅd d  l[ } | jG j\ dg | dh |  jW ƒ  di |& jX ƒ  dj |& jY ƒ  d |+ dk | jZ ƒ q\|& j] d |+ ƒ n„ | d  k	 r9d d  l[ } | jG j^ dg | dh |  jW ƒ  di |& jX ƒ  dj |& jY ƒ  d |+ dk | jZ ƒ n |& j_ d |+ ƒ | j |+ dm f ƒ | dn |+ IJq{W| S(q   Niÿÿÿÿi    s   Parameter syntax:t   outt   prefixs     s2   phenix.mtz2map [mtz_file] [pdb_file] [param_file] s   [--show_maps]t
   home_scopet    t   pdbs&   A model file has already been defined.s   pdb_file=%st   hkls   .mtzs3   Only MTZ files are supported for reflections input.s%   An MTZ file has already been defined.s   mtz_file=%st	   file_names-   The file %s was not in a recognizable format.s   --i   s   =Truet   args   Unknown argument '%s'.t   sourcess7   Please specify an MTZ file containing map coefficients.s2   The specified output path '%s' is not a directory!t
   force_typet   raise_sorry_if_errorst   exclude_anomaloust   exclude_fmodelt   keep_array_labelss,   Available map coefficients in this MTZ file:s/    (skipping, add include_fmodel=True to include)s     %s%ss+   No map coefficients found in this MTZ file.t   labelt   test_flag_valuet   disable_suitability_testt   parameter_scopet   r_free_flagst   datas0   No atoms found matching the specified selection.s1   No model input - will output map(s) in unit cell.i   t    s4   No map coefficients found with labels %s.  Possible s   choices are:
%ss   
s4   No map coefficients found with labels %s; this file s6   does not appear to contain any other map coefficients!R
   t   f_labt   phi_labt   fom_labt   ft   phit   foms   Processing map: %st   d_mint   d_maxt   others(     removing %d R-free flagged reflectionst   resolution_factort   sigmas     applying sigma-scalingt   volumes     applying volume-scalings   FP,SIGFPt   PHIMt   FOMMt   2FOFCWTt   no_fills   _2mFo-DFc_no_fills	   _2mFo-DFct   FOFCWTs   _mFo-DFcs   FWT,PHWTs   DELFWT,PHDELWTt   ANOMt   _anomt   LLGt   _llgt   FMODELs   F-modelt   _fmodelt   FCt   _fcalcs   _%dt   _no_fillt   _fillt   _filledt   Autot   ccp4t   dsn6t   omapt   xplort   maps2   %s is not an appropriate extension for Xplor maps.s1   %s is not an appropriate extension for CCP4 maps.t   .t
   sites_cartt	   unit_cellt   map_datat   n_realt   buffers	   XPLOR maps   CCP4 maps
     wrote %s(   RC   s   map(   R@   s   map(`   t
   iotbx.philR	   t   lent   master_philt   showR   t!   command_line_argument_interpretert   ost   patht   isfileR   t   any_filet	   file_typeR   t   appendt   philt   parset   abspatht   endswitht   RuntimeErrort
   startswitht   processt   fetcht   extractt   mtz_filet   outputt	   directoryt   getcwdt   existst   makedirst   isdirR   t   splitextt   basenamet   Truet	   show_mapsR   R   t   get_map_coeff_labelst   file_servert   Falset   include_fmodelt
   isinstancet   strt   pdb_filet   assert_file_typet   file_objectt   as_miller_arraysR    t   removeR   t   get_r_free_flagsR   R   R   R!   t
   map_to_asut   average_bijvoet_matest	   hierarchyt   atomst   extract_xyzt	   selectiont   atom_selection_cachet   selectt	   enumerateR   t   joint   extract_map_coeffst   is_xray_amplitude_arrayt   AssertionErrort   is_real_arrayt	   map_toolst   combine_f_phi_and_fomt   is_complex_arrayt   resolution_filterR)   R*   t   common_setst   countt   fft_mapt   grid_resolution_factort   scalet   apply_sigma_scalingt   apply_volume_scalingt   formatt	   extensiont   write_xplor_mapRG   t   real_mapRI   RJ   t   iotbx.map_toolst   write_dsn6_mapt   as_dsn6_mapt   write_ccp4_mapt   as_ccp4_map(,   t   argst   logt    run_in_current_working_directoryt   iotbxRp   R_   t
   input_philt   parameter_interpreterR   t
   input_filet	   file_philt   arg_philt   working_philt   paramst
   output_dirt
   all_labelst
   map_labelsR   t   labels_listt   extraR
   R    t
   rfree_filet	   raw_arrayt
   flag_valueRF   t   pdb_hierarchyt   selection_cacheR{   t	   file_infot   suffixest   it
   map_coeffst
   labels_outR&   R'   R(   t   flagsRD   t   suffixt   nR�   R�   t   map_file_name(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/mtz2map.pyt   runk   sì   
					%
		
		 		$
$

!

														
c         C   s
   |  g  f S(   N(    (   t   result(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/mtz2map.pyt
   finish_jobx  s    t   launcherc           B   s   e  Z d  „  Z RS(   c         C   sE   t  j |  j ƒ t  j |  j ƒ t d t |  j ƒ d t j d t	 ƒ S(   NR˜   R™   Rš   (
   RP   Rd   R£   t   chdirR¶   t   listR˜   t   syst   stdoutRh   (   t   self(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/mtz2map.pyR¶   |  s
    	(   t   __name__t
   __module__R¶   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/mtz2map.pyR¹   {  s   c         C   s"   |  j  d  k r t d ƒ ‚ n  d  S(   Ns   No MTZ file was provided.(   R_   R	   R   (   R¢   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/mtz2map.pyt   validate_paramsƒ  s    t   __main__i   (   t
   __future__R    t
   mmtbx.mapsR   R“   R›   RK   R   t   libtbxR   t   libtbx.philt   libtbx.utilsR   R   R¼   RP   RV   RW   Rh   RM   t   master_paramsR   R½   Rl   R¶   R¸   t   target_with_save_resultR¹   RÁ   R¿   t   argv(    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/mtz2map.pyt   <module>   s&   	Q	ÿ 		