ó
±™xTc        	   @   s   d  d l  m Z d  d l m Z d  d l m 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 m 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 Z d	 e j e d
 „ Z e e e e e e e e j d „ Z e e e e e j d „ Z e j j d d e ƒZ e j e 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   iotbx_option_parser(   t   crystal_symmetry_from_anyN(   t   flex(   t   crystal(   t   runtime_utils(   t   Sorrys   phenix.cif_as_mtzc   
      C   sV  t  |  ƒ d k r d g }  n  ywt d d | d d | ƒ j ƒ  j d  d d d	 d
 t d d d d ƒj d  d d d	 d
 d  d d d d ƒj d  d d d	 d
 d  d d d d ƒj d  d d d	 d
 d d d d d ƒj d  d d d d d ƒj d  d d d d d ƒj d  d d d d d ƒj d  d d d d d ƒj d  d d d d! ƒj d" d d d d# ƒj d$ d d d d% ƒj d& d d d d' ƒj d( |  ƒ } Wn@ t k
 r×} t | ƒ d) k rÇt | ƒ GHn  t	 j
 d ƒ n X| j } t  | j ƒ d* k r%| d+ t  | j ƒ I| j IJt d, ƒ ‚ n  | j d } t j j | ƒ sWt d- | ƒ ‚ n  | j j } | d t j k sˆt j d. | ƒ r£t d/ d0 d1 | ƒ ‚ n  t d2 | d3 | d4 | j j d5 | j j d6 | j j d7 | j j d8 | j j d9 | j j d: | j j d; | j j d< | j j d= | d> | j j  d? | j j! d@ | ƒ }	 | rR|	 Sd  S(A   Ni    s   --helpt   usages"   %s [reflection_cif_file] [options]t   descriptions.   Example: %s r1o9ksf.ent --symmetry=pdb1o9k.ents   --output_file_namet   actiont   storet   defaultt   typet   stringt   helps   Output mtz file name.s   --wavelength_idt   ints*   Extract data set with given wavelength_id.s   --crystal_ids'   Extract data set with given crystal_id.s   --output_r_free_labels   R-free-flagss@   MTZ column label to use for R-free flags (default: R-free-flags)s   --merget
   store_trues$   Merge non-unique data where present.s    --incompatible_flags_to_work_setsL   When merging place reflections with incompatible flags into the working set.s   --remove_systematic_absencess%   Remove systematic absent reflections.s   --map_to_asus   Map to asymmetric unit.s   --show_details_if_errors"   Show data details for some errors.s
   --show_logs   Show some output.s   --ignore_bad_sigmassK   Set sigmas to None instead of raising an error when bad sigmas are present.s   --extend_flagss:   Extend R-free flags to cover all reflections if necessary.t   argst   0i   s-   %d arguments are given from the command line:s'   Please specify one reflection cif file.s   File is not found: %ss   [^a-zA-Z0-9_\-]s8   %s is not a suitable column label.  MTZ format requires sB   an uppercase letter as the first character, and only alphanumeric s0   characters or hyphens in the rest of the string.t	   file_namet   crystal_symmetryt   output_file_namet   wavelength_idt
   crystal_idt   show_details_if_errort   output_r_free_labelt   merge_non_unique_under_symmetryt
   map_to_asut   remove_systematic_absencest   incompatible_flags_to_work_sett   return_as_miller_arrayst   ignore_bad_sigmast   extend_flagst   log("   t   lenR   t   enable_symmetry_comprehensivet   optiont   Nonet   Falset   processt	   Exceptiont   strt   syst   exitt   symmetryR   R   t   ost   patht   isfilet   optionsR   R   t	   uppercaset   ret   searcht   process_filesR   R   R   R   t   mergeR   R   R   R   R    (
   R   t   command_namet   outR   t   command_linet   eR   R   R   t   result(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyt   run-   s®     
																			 		c         C   s  t  d |  d | d | d | d | d | d | d | d	 |	 d
 |
 d | d | d | d | ƒ } | rg | S| d  k	 r| s t j j |  ƒ } | d d !d k r¯ | d  d } q | d d !d k rÓ | d  d } q | j d ƒ ró | d  d } q | d } n  | j d | ƒ | j ƒ  Sd  S(   NR   R   R   R   R   R   R   R   R   R   R   R   R    R!   iüÿÿÿiýÿÿÿt   .s   .mtziûÿÿÿs   .ent.gziùÿÿÿ(   t   extractR%   R-   R.   t   basenamet   endswitht   writet   n_reflections(   R   R   R   R   R   R   R   R   R   R   R   R   R   R    R!   t
   mtz_objectR>   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyR4   Š   s:    	   c   <         se
  d d  l  } d d l m } | j d | ƒ } | j j d |  ƒ j d | ƒ } t | ƒ d k rx t d d d	 ƒ ‚ n  t	 ƒ  } | r� t
 } n  ‡  f d
 †  } xì | j ƒ  D]Þ \ } } xÏ | j ƒ  D]Á } | } y" | j d | j ƒ  d t
 ƒ } WqÅ t k
 r…} t | ƒ } d d l m } | ƒ  } d | k r| j d | ƒ | j ƒ  j d | ƒ | d | j ƒ  7} t | ƒ ‚ q†‚  qÅ XqÅ Wq¬ W| j ƒ  d  k s²| j ƒ  d  k rÁt d ƒ ‚ n  | j j ƒ  j d d ƒ j d | j ƒ  ƒ } | j ƒ  } i  } | j d | ƒ d d l m } d d l m }  g  }! x> | j ƒ  D]0 \ } } x! | j ƒ  D] } |! j  | ƒ q\WqCW|  |! ƒ }" | r’g  }# n  x¾t! | j ƒ  ƒ D]ª\ }$ \ } } x•| j ƒ  D]‡} | j" d | ƒ j# | j$ ƒ  ƒ } | j$ ƒ  j% }% | | ƒ }& |& d  k r2| d | j$ ƒ  j& ƒ  IJqÄnù |& j' ˆ  ƒ rË| | d d ƒ\ } }' t( | j) ƒ  t* j+ ƒ r+| j) ƒ  j, ƒ  }( |( j- ƒ  j. | j) ƒ  ƒ s¤t ‚ | j" d |( ƒ j# | j$ ƒ  ƒ } q+n` | j/ ƒ  sã| j0 ƒ  r+t( | j) ƒ  t* j1 ƒ r+| j" d | j) ƒ  j- ƒ  ƒ j# | j$ ƒ  ƒ } n  d }) x7 |% D]/ }* d |* k r8t1 |* j2 d ƒ d ƒ }) Pq8q8W|) d k r”| d  k r”|& d |) 7}& n  | d  k	 r¾|) d k r¾|) | k r¾qÄn  |) | k rù| j3 d d  |) d! d" d | ƒ i  f | |) <n  | |) \ }+ }, d }- x7 |% D]/ }* d# |* k rt1 |* j2 d ƒ d ƒ }- PqqW| d  k	 rs|- d k rs|- | k rsqÄn  |- d$ k r«| d  k r«|& | k r«|& d |- 7}& q«n  |- |, k rú| j$ ƒ  j4 }. |. d  k rÛd }. n  |+ j5 d d% d& |. ƒ |, |- <n  |, |- }/ | j6 ƒ  d  k	 rf| j6 ƒ  d k j7 t8 ƒ d k rf|
 rW| d' IJ| j9 d  ƒ qft d( ƒ ‚ n  | j: ƒ  s$| rf| d) IJ|& j' ˆ  ƒ rÏ|	 rÏ| j; d* d ƒ }0 |0 j< d k r<| d+ |0 j< IJq<nm y | j; ƒ  }0 WnZ t k
 r;} d, t | ƒ k r<t t | ƒ d- | j$ ƒ  j% d d. d/ ƒ ‚ ‚  q<n X|0 j= ƒ  j" d | ƒ j# | j$ ƒ  ƒ } q$| roq$| j> ƒ  j? ƒ  }1 | j@ ƒ  }2 t* jA |1 |2 ƒ }3 |3 j7 t
 ƒ }4 |2 j? ƒ  }5 d0 |  d1 |1 |5 |1 f d2 }6 | r|6 GH| jB d3 d4 ƒ | jC ƒ  jD ƒ  jE d3 d4 ƒ n  t |6 ƒ ‚ n  | rH| jC ƒ  j# | j$ ƒ  ƒ } n  | r]| jF ƒ  } n  |& j' ˆ  ƒ r	t |" jG d5 | ƒ j> ƒ  ƒ }7 |7 d k r	| rèd d6 l mH }8 |8 jI d7 | d d d8 |& d9 |" d: t
 d; t
 d< tJ jK ƒ } q	tL jM d= d> d? d@ dA |7 |& f ƒ q	n  | jN dB t* jO dO ƒ dC t
 ƒ j# | j$ ƒ  ƒ } | r^	|# j  | ƒ qÄn  d  }9 dD |& k r‚	| j jP ƒ  }9 n  d  }: dE |& k r©	| jQ ƒ  r©	dF }: n? |& j' dG ƒ rè	| jQ ƒ  rè	| j6 ƒ  d  k	 rß	dH }: qè	dI }: n  |& }; d$ }$ x( |& | k r
|; dJ |$ }& |$ d$ 7}$ q÷	W| jR |& ƒ |/ jS | dK |& dL |9 dM |: ƒqÄWq¥W| r]
|# S| Sd  S(P   Niÿÿÿÿ(   t   millert   crystal_symmetry_from_filet	   file_patht   base_array_infoi    s9   No data arrays were found in this CIF file.  Please make sE   sure that the file contains reflection data, rather than the refined s   model.c            sÐ  d  } xÃ|  j ƒ  j D]²} d | k rn |  j ƒ  r= d } n- |  j ƒ  rR d } n | j d ƒ rj d } n  Pq d | k rØ |  j ƒ  r� d } nE |  j ƒ  r¤ d } n0 | j d	 ƒ r¼ d } n | j d
 ƒ rÔ d } n  Pq |  j ƒ  r|  j ƒ  rù d } n |  j ƒ  rd } n  Pq d | k s*d | k r4ˆ  } Pq |  j ƒ  rId } q |  j ƒ  rŠ| j d ƒ rnd } PqÈ| j d ƒ rÈd } PqÈq |  j	 ƒ  r d | k r¬d } PqÈ| j d ƒ rÈd } PqÈq q W| S(   Nt   _meast   FOBSt   IOBSs   .phase_meast   PHIMt   _calct   FCt   ICALCs   .F_calcs   .phase_calct   PHICt   Ft   It   statust   _freet   HLt   DELFWTt   FWTt   pdbx_anom_differencet   DANOs   .fomt   FOM(
   R%   t   infot   labelst   is_xray_amplitude_arrayt   is_xray_intensity_arrayR?   t   anomalous_flagt   is_hendrickson_lattman_arrayt   is_complex_arrayt   is_real_array(   t   miller_arrayt   labelt   l(   R   (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyt	   get_labelÑ   s\    										t   other_symmetryt   force(   t   StringIOs6   Space group is incompatible with unit cell parameters.t   fs   
%ssB   Crystal symmetry is not defined. Please use the --symmetry option.t   titles   phenix.cif_as_mtzt   space_group_infot	   unit_cell(   t$   cif_status_flags_as_int_r_free_flags(   t   make_joined_setR   s/   Can't determine output label for %s - skipping.t   test_flag_valuet   dataR   t   =s   %it   names
   crystal_%it   project_namet   projectR   i   t   datasett
   wavelengths,   Warning: bad sigmas, setting sigmas to None.s{   Bad sigmas: all sigmas are equal to zero.
  Add --ignore_bad_sigmas to command arguments to leave out sigmas from mtz file.s    Warning: merging non-unique datat   incompatible_flags_replacementsi   Warning: %i reflections were placed in the working set because of incompatible flags between equivalents.s%   merge_equivalents_exact: incompatibles    for %ss   
sj   Add --incompatible_flags_to_work_set to command line arguments to place incompatible flags to working set.s*   Miller indices not unique under symmetry: s    (%d redundant indices out of %d)s7   Add --merge to command arguments to force merging data.t   prefixs     t   other(   t   r_free_utilst   r_free_flagst   array_labelt   complete_sett   preserve_input_valuest   allow_uniform_flagsR!   s/   %d reflections do not have R-free flags in the s   array '%s' - this may s=   cause problems if you try to use the MTZ file for refinement s<   or map calculation.  We recommend that you extend the flags s>   to cover all reflections (--extend-flags on the command line).t   indicest   negateRU   t   PHIt   PRW   t   DQt   Ds   -%it   column_root_labelt   label_decoratort   column_types(   i    i    i    (   (   i    i    i    (T   t	   iotbx.cift   cctbxRC   t
   array_infot   cift   readert   build_miller_arraysR"   R   t   sett   Truet	   iteritemst   valuest   join_symmetryR   t   AssertionErrorR)   t	   cStringIORg   t   show_summaryt   getvalueRk   R%   Rj   t   mtzt   objectt	   set_titlet   set_space_group_infot   set_hkl_baset   iotbx.reflection_file_utilsRl   Rm   t   appendt	   enumeratet   customized_copyt   set_infoRY   RZ   t   label_stringt
   startswitht
   isinstanceRo   R   t   doublet   iroundt	   as_doublet   all_eqR[   R\   R   t   splitt   add_crystalRu   t   add_datasett   sigmast   countR&   t
   set_sigmast   is_unique_set_under_symmetryt   merge_equivalentst   n_incompatible_flagst   arrayR   t   sizet   unique_under_symmetry_selectiont   boolt   show_comprehensive_summaryR   t   sortt
   show_arrayR   t   lone_setRy   R    R*   t   stdoutt   libtbxt   warnt   select_indicest   miller_indext   ccp4_label_decoratorR`   t   addt   add_miller_array(<   R   R   R   R   R   R   R   R   R   R   R   R    R   R!   t   iotbxRC   RF   t   all_miller_arrayst   column_labelsRd   t	   data_namet   miller_arrayst   maRe   R9   t   str_eRg   t   sRB   Rk   t   mtz_crystalsRl   Rm   t
   all_arraysR|   t   miller_array_listt   iRZ   Rb   t   _t   data_intt   crys_idRc   R   t   datasetst   w_idRu   Rt   t   mergingt   n_allt
   sel_uniquet   sel_dupt   n_duplicatet   n_uust   msgt	   n_missingRy   t   decR‡   t
   label_base(    (   R   sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyR=   ¶   s€   					0			%	!'0$	
$		
3

		%

				s‘  
input
  .caption = This program will convert CIF-formatted structure factors (used     by the PDB) to an MTZ file.  Other CIF types (restraints, etc.) will be     ignored.  Because the data in the PDB often contains mistakes or lacks     symmetry, an optional model file is strongly recommended.  If you want the     program to generate an MTZ file for a specific PDB ID, you may specify     that instead of input files, and the CIF and PDB will be fetched from     www.rcsb.org.
  .style = caption_img:icons/custom/phenix.reflection_file_editor.png
{
  pdb_id = None
    .type = str
    .short_caption = PDB ID to retrieve
    .input_size = 80
    .style = bold noauto
  cif_file = None
    .type = path
    .short_caption = CIF data file
    .style = bold noauto OnChange:extract_symm_for_cif
  pdb_file = None
    .type = path
    .short_caption = Model file
    .style = bold noauto file_type:pdb OnChange:extract_symm_for_cif
  wavelength_id = None
    .type = str
    .short_caption = Wavelength ID
    .help = Not required when only one wavelength is present
    .input_size = 120
    .style = noauto
  crystal_id = None
    .type = str
    .short_caption = Crystal ID
    .help = Not required when only one crystal is present
    .input_size = 120
    .style = noauto
}
crystal_symmetry {
  space_group = None
    .type = space_group
    .style = bold
  unit_cell = None
    .type = unit_cell
    .style = bold
}
output_file_name = None
  .type = path
  .style = new_file bold
include scope libtbx.phil.interface.tracking_params
options {
  merge = False
    .type = bool
    .short_caption = Merge non-unique data
  map_to_asu = True
    .type = bool
    .short_caption = Map HKL indices to ASU
  eliminate_sys_absent = False
    .type = bool
    .short_caption = Remove systematic absences
  show_details_if_error = True
    .type = bool
    .short_caption = Show data details for some errors
  incompatible_flags_to_work_set = False
    .type = bool
    .short_caption = Move incompatible flags to work set
  ignore_bad_sigmas = False
    .type = bool
  extend_flags = False
    .type = bool
    .short_caption = Extend incomplete R-free flags
  show_log = True
    .type = bool
}
t   process_includesc         C   s¦  d d  l  } t j j t j ƒ t j d d ƒ } d  } d  } g  } x |  D]}	 t	 j
 j |	 ƒ rt j j |	 ƒ r‰ t	 j
 j |	 ƒ }
 qb|	 j d ƒ s§ |	 j d ƒ r¼ t	 j
 j |	 ƒ } qby t j j d |	 ƒ } Wn t k
 rñ d |	 GHqbX| j | ƒ qJ |	 j d ƒ r"|	 d	 d
 }	 n  y# | j d |	 ƒ } | j | ƒ WqJ t k
 rad |	 GHqJ XqJ W| d  k r�t j d | ƒ j ƒ  } n  d  } | j j d  k	 rJ| j j j d | j j g d | ƒ | j _ | j j j d d | j j g d | ƒ | j _ t  j! | j j ƒ } | j" ƒ  | j# _$ | j% ƒ  | j# _% d  | j _ n  | r]t& | ƒ n  | j' d  k r¯t	 j
 j( | j j ƒ \ } } t	 j
 j) t	 j* ƒ  | d ƒ | _' n  | d  k rt+ | j# j$ ƒ j, d k sÜt- ‚ t. j/ d | j# j$ d | j# j% ƒ } n  t0 d | j j d | d | j' d | j j1 d | j j2 d | j3 j4 d d d | j3 j5 d | j3 j6 d | j3 j7 d | j3 j8 d | j3 j9 d  | j3 j: ƒ } | j' | f S(!   Niÿÿÿÿt
   home_scopet    s   .cifs   .cif.txtR   s!   Unrecognizable file format for %ss   --i   s   =Truet   args   Unrecognizable parameter %st   sourcesR   R!   s   -xs   .mtzRj   Rk   R   R   R   R   R   R   t
   FreeR_flagR   R   R   R   R   R    (;   t   mmtbx.command_line.fetch_pdbRº   t	   call_backt   set_warning_logR*   t   stderrt   master_philt!   command_line_argument_interpreterR%   R-   R.   R/   RÁ   t   pdbt   is_pdb_filet   abspathR?   t   philt   parset   RuntimeErrorR�   R¢   R'   t   fetchR=   t   inputt   pdb_idR8   t	   fetch_pdbt   run2t   pdb_filet   cif_fileR   t   extract_fromRj   R   t   space_groupRk   t   validate_paramsR   t   splitextt   joint   getcwdR   t   __name__R“   R   R,   R4   R   R   R0   R   R5   R   t   eliminate_sys_absentR   R   R    (   R   R!   t   check_paramst   paramst   mmtbxt   parameter_interpreterRó   Rô   Rà   Rß   t	   pdb_filest	   user_philt   symmt   baset   extt   n_refl(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyRò     s‚    		%		c         C   sA  |  j  j d  k r3 |  j  j d  k r3 t d ƒ ‚ n  |  j  j d  k	 rº |  j  j d  k	 rf t d ƒ ‚ n  d d  l } y | j j j |  j  j ƒ Wqí t	 k
 r¶ } t t
 | ƒ ƒ ‚ qí Xn3 |  j j d  k sÞ |  j j d  k rí t d ƒ ‚ n  |  j d  k	 r=t j j |  j ƒ } t j j | ƒ s=t d d | ƒ ‚ q=n  t S(   Ns   No CIF file provided!s7   Please specify either a PDB ID or a CIF file, not both.iÿÿÿÿs'   Crystal symmetry missing or incomplete.s3   The output directory %s does not exist or is not a s
   directory.(   Rï   Rô   R%   Rð   R   t   iotbx.pdb.fetchRè   Rî   t   validate_pdb_idRí   R)   R   Rö   Rk   R   R-   R.   t   dirnamet   isdirR�   (   Rþ   RÁ   R9   t
   output_dir(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyR÷   g  s&    $t   launcherc           B   s   e  Z d  „  Z RS(   c         C   s/   t  j |  j ƒ t d t |  j ƒ d t j ƒ S(   NR   R!   (   R-   t   chdirR  Rò   t   listR   R*   R¹   (   t   self(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyR;   ~  s    (   Rû   t
   __module__R;   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyR  }  s   c         C   se   |  \ } } | d  k r! d } n  | d  k	 r[ t j j | ƒ r[ d | f g d | f g f Sg  g  f S(   Ni    s   MTZ files   Number of reflections(   R%   R-   R.   R/   (   t   resultst   mtz_fileR  (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyt
   finish_job‚  s    	t   __main__i   ('   t
   __future__R    t   iotbx.option_parserR   RÁ   R   t
   iotbx.philt	   iotbx.mtzt	   iotbx.pdbt   cctbx.array_familyR   R‰   R   Rº   R   t   libtbx.utilsR   t   libtbx.callbacksR   R2   R-   R*   R¹   R&   R;   R4   R=   Rë   Rì   R�   Ræ   R%   Rò   R÷   t   target_with_save_resultR  R  Rû   t   argv(    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/cif_as_mtz.pyt   <module>   sP   	c'ÿ 	IG		