ó
¼™xTc           @   sê   d  d l  m Z d  d l m Z d  d l Z d  d l m Z d  d l Z d  d l	 m
 Z
 d  d l m Z m Z m Z d  d l Z e j j d ƒ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d S(   iÿÿÿÿ(   t   division(   t   millerN(   t   Sorry(   t   xray_structure(   t   fa_estimationt   pair_analysest   relative_scalingsê  
      task = *get_dano get_diso lsq_scale sfcalc custom None
      .type=choice
      .help="Possible tasks"
      output_label_root=None
      .type=str
      .help="Output label root"
      get_dano
      .help="Get ||F+| - |F-|| from input data."
      {
        input_data = None
        .type=str
      }

      get_diso
      .help="Get |Fder|-|Fnat|"
      {
        native = None
        .type=str
        .help="Name of native data"
        derivative = None
        .type=str
        .help="Name of derivative data"
        use_intensities=True
        .type=bool
        .help="Scale on intensities"
        use_weights=True
        .type=bool
        .help="Use experimental sigmas as weights in scaling"
        scale_weight=True
        .type=bool
        .help="Whether or not to scale the sigmas during scaling"
      }

      lsq_scale{
        input_data_1 = None
        .type=str
        .help="Reference data"
        input_data_2 = None
        .type=str
        .help="Data to be scaled"
        use_intensities=True
        .type=bool
        .help="Scale on intensities"
        use_weights=True
        .type=bool
        .help="Use experimental sigmas as weights in scaling"
        scale_weight=True
        .type=bool
        .help="Whether or not to scale the sigmas during scaling"
      }

      sfcalc{
        fobs = None
        .type=str
        .help = "Data name of observed data"
        output = 2mFo-DFc mFo-DFc *complex_fcalc abs_fcalc intensities
        .type=choice
        .help="Output coefficients"
        use_bulk_and_scale = *as_estimated user_upplied
        .type=choice
        .help = "estimate or use parameters given by user"
        bulk_and_scale_parameters
        .help = "Parameters used in the structure factor calculation. Ignored if experimental data is given"
        {
          d_min = 2.0
          .type=float
          .help = "resolution of the data to be calculated."
          overall
          .help = "Bulk solvent and scaling parameters"
          {
            b_cart
            .help = "Anisotropic B values"
            {
              b_11 = 0
              .type=float
              b_22 = 0
              .type=float
              b_33 = 0
              .type=float
              b_12 = 0
              .type=float
              b_13 = 0
              .type=float
              b_23 = 0
              .type=float
            }
            k_overall = 0.1
            .type=float
            .help = "Overall scalar"
          }
          solvent
          .help = "Solvent parameters"
          {
            k_sol = 0.3
            .type=float
            .help="Solvent scale"
            b_sol = 56.0
            .type=float
            .help="Solvent B"
          }
        }
      }

     custom
     .help = "A custom script that uses miller_array data names as variables."
     {
       code = None
       .help = "A piece of python code"
       .type=str
       show_instructions = True
       .help = "Some instructions"
       .type = bool
     }


      c         C   s_   g  } xR t  |  t t |  ƒ ƒ ƒ D]5 \ } } d | | f } | j t | d d ƒ ƒ q" W| S(   Ns%   %s =  miller_arrays[ %i ].deep_copy()s   <string>t   exec(   t   zipt   ranget   lent   appendt   compile(   t   namest   resultt   namet   numbert
   tmp_result(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt   patch_miller_arrays_as_names�   s
    (c         C   sè   d  } | j d  k r7 t | ƒ d k ri | d } qi n2 |  j | j ƒ r] | |  | j } n t d ƒ ‚ | j ƒ  r„ | j ƒ  } n  | j ƒ  s– t ‚ t	 j
 | ƒ } | j j ƒ  } | j j ƒ  } t j | | ƒ }	 |	 j j ƒ  }
 |
 S(   Ni   i    s   Unknown data name.(   t   Nonet
   input_dataR
   t   has_keyR   t   is_xray_intensity_arrayt	   f_sq_as_ft   is_xray_amplitude_arrayt   AssertionErrorR   t   ano_scalingt   x1pt	   deep_copyt   x1nR   t   delta_generatort   abs_delta_f(   R   t   miller_arraysR   t
   parameterst   outt   miller_arrayt   pair_generatort   plust   minust	   delta_gent   deltas(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt   get_dano‹   s"    		c   
      C   sd  | j  d  k r t d ƒ ‚ n  | j d  k r< t d ƒ ‚ n  d  } d  } |  j | j  ƒ rt | |  | j  j ƒ  } n t d | j  ƒ ‚ |  j | j ƒ r³ | |  | j j ƒ  } n t d | j ƒ ‚ t j d | d | d | j d | j	 d | j
 ƒ } | j d	 | ƒ | j ƒ  r$| j ƒ  } n  | j ƒ  r?| j ƒ  } n  t j | | ƒ } | j j ƒ  }	 |	 S(
   Ns   Please define native data names"   Please define derivative data names   Unknown data name: >>%s<<t   miller_nativet   miller_derivativet   use_intensitiest   scale_weightt   use_weightsR"   (   t   nativeR   R   t
   derivativeR   R   R   t   ls_rel_scale_driverR,   R-   R.   t   showR   R   R   R   t   delta_f(
   R   R    R   R!   R"   R/   R0   t   scalerR'   R(   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt   get_diso¤   s6    					c         C   s  | j  d  k r t d ƒ ‚ n  | j d  k r< t d ƒ ‚ n  d  } d  } |  j | j  ƒ rt | |  | j  j ƒ  } n t d | j  ƒ ‚ |  j | j ƒ r³ | |  | j j ƒ  } n t d | j ƒ ‚ t j d | d | d | j d | j	 d | j
 ƒ } | j d	 | ƒ | j j ƒ  S(
   Ns   Please define input_data_1s   Please define input_data_2s   Unknown data name: >>%s<<R*   R+   R,   R-   R.   R"   (   t   input_data_1R   R   t   input_data_2R   R   R   R1   R,   R-   R.   R2   t   scaled_original_derivative(   R   R    R   R!   R"   R6   R7   R4   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt	   lsq_scaleË   s(    			c         C   s~  d d l  m } d  } | j d  k rp | j d k rC t d ƒ ‚ q‡ t | j d | j j	 d t
 ƒ j ƒ  ƒ } n | |  | j j ƒ  } | j ƒ  r¢ | j ƒ  } n  | j d	 d
 d d ƒ } | j j j j | j j j j | j j j j | j j j j | j j j j | j j j j g } | j d | d | d d d | d | d | j j j d | j j j ƒ }	 | j d k r–| j d  k	 r–|	 j d | ƒ q–n  d  }
 | j d k r|	 j ƒ  }
 | j d k rÏ|
 }
 n  | j d k rít |
 ƒ }
 n  | j d k rht |
 ƒ j ƒ  }
 qhnT | j d k r>|	 j ƒ  j  d d ƒ }
 n  | j d k rh|	 j ƒ  j  d d ƒ }
 n  |
 d  k	 szt! ‚ |
 S(   Niÿÿÿÿ(   t   f_modelt   complex_fcalct	   abs_fcalct   intensitiessD   Experimental data is needed for %s coefficients.
 Please supply Fobst   d_mint   anomalous_flagt   fractiongš™™™™™¹?t   max_freeiÿàõR   t   r_free_flagst   target_namet   ls_wunit_k1t   f_obst   b_cartt   k_solt   b_solt   as_estimatedR"   s   2mFo-DFct   map_types   2m*Fobs-D*Fmodels   mFo-DFc(   R;   R<   R=   (   R;   R<   R=   ("   t   mmtbxR:   R   t   fobst   outputR   t   abst   structure_factorst   bulk_and_scale_parametersR>   t   Falset   f_calcR   R   R   t   generate_r_free_flagst   overallRF   t   b_11t   b_22t   b_33t   b_12t   b_13t   b_23t   managert   solventRG   RH   t   use_bulk_and_scalet   update_solvent_and_scalet	   f_as_f_sqt   electron_density_mapt   map_coefficientsR   (   R   R    R   R!   R"   R:   RE   t   flagsRF   t   fmodelR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt   sfcalcé   sX    		c         C   s-   | d IJx |  D] } | d | IJq Wd  S(   Ns   Restricted data set names are:s
       -   %s(    (   t   restricted_namesR"   t   rn(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt   show_restricted_custom_names#  s    
c         C   s^   |  d IJ|  d IJ|  d IJ|  d IJ|  d IJ|  d IJ|  d IJ|  d IJ|  d	 IJd  S(
   Nsc   The custom function in the manipulate miller task of xmanip allows one to submit a small (or large)se   snippet of python code, have it executed and have a single miller array returned and written to file.sd   If one is familiar with python and the cctbx in general, this function allows one to quickly performs`   complex tasks relating reflection files without having the overhead of writing a user interface.sl   Data set names given to the miller arrays in the main (user specified) input file, are actual variable namessj   and are stored as a cctbx.miller.array object. A pdb file that was read in, is stored in the object named si   xray_structure. Note that not many safeguards are in place: make sure your code snippet is proper python!sn   Please note that there are some restriction on variable names: the should not contains spaces or have the namesR   of local variables or functions. By default, a variable named 'result' is returned(    (   R"   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt   print_custom_instructions(  s    







c         B   s#  d d d d d d d d d	 d
 d d d d g } | j  rS e | ƒ e | | ƒ n  xV |  D]N } d | k r e d | ƒ ‚ n  | | k rZ e | ƒ e d | ƒ ‚ qZ qZ We |  ƒ } x | D] } | d  Uq¿ Wd  }	 | d IJ| d IJ| | j IJ| d IJe | j d d ƒ }
 |
 d  U|	 S(   NRe   R   R    t   paramsR"   R)   R5   t   customt   sfalcR   R9   t   manipulate_millerRg   Rh   t    sM   Sorry, no spaces allowed in data set name >%s< to avoid compilation problems.sC   The data set name >%s< is restricted to avoid compilation problems.s*   Trying to evaluate the code as shown belows*   ------------------------------------------s   <string>R   (   t   show_instructionsRh   Rg   R   R   R   t   codeR   (   R   R    R   Ri   R"   Re   R   t	   tmp_namest   instructionR   t	   user_code(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyRj   5  s.    	




c   	      B   s‹   | d  k r e j } n  i e d 6e d 6e d 6e d 6e d 6} d  } e d | j	 d d ƒ } | d  U| | j	 |  | | | | ƒ } | S(	   NR)   R5   R9   Rd   Rj   s   function_arguments = params.%ss   <string>R   (
   R   t   syst   stdoutR)   R5   R9   Rd   Rj   R   t   task(	   R   R    R   Ri   R"   t   function_pointert   function_argumentst   patchR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyRl   X  s"    
	(   t
   __future__R    t   cctbxR   t   cctbx.xray.structure_factorst   libtbx.utilsR   t
   iotbx.philt   iotbxt	   iotbx.pdbR   t   mmtbx.scalingR   R   R   Rs   t   philt   parset   master_paramsR   R)   R5   R9   Rd   Rg   Rh   Rj   R   Rl   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/xmanip_tasks.pyt   <module>   s$   	t		
		'		:			#