ó
‘™xTc           @   s¦  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z	 d d l
 m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m  Z  d d l! m" Z" d d l! m# Z# d d l$ m% Z% d d l& m' Z' m( Z( d d l) Z) d d l* Z* d d l+ Z* d d l, Z, d „  Z- d „  Z. d e/ f d „  ƒ  YZ0 e1 e2 d d d „ Z4 d „  Z5 d S(   sÎ  
This module provides a generic frontend to all of the reflection file formats
supported in ``iotbx``.  Note that this module can also be used indirectly via
the even more generic :py:mod:`iotbx.file_reader` module, which provides
a unified API for reading in any file (but calls
:py:class:`iotbx.reflection_file_reader.any_reflection_file` internally).
Currently, the supported formats include:

- **CIF**: Crystallographic Information Format, the common syntax for
  specifying most structured data encountered in crystallography (but more
  widely used in small-molecule versus macromolecular crystallography), usually
  as ASCII text.  May encapsulate a variety of other data types, but only
  reflection data (of any type) is handled by this particular module.  Uses
  :py:mod:`iotbx.cif` internally.
- **MTZ**: Binary file format established by `CCP4 <http://www.ccp4.ac.uk>`_
  capable of storing any numerical data, and used by most major macromolecular
  crystallography software packages.
  Because of its speed and broad compatibility, this is the primary interchange
  format for reflection data in Phenix.  Uses :py:mod:`iotbx.mtz` internally.
- **Scalepack**: Fixed-format ASCII text produced by the program of the same
  name and the HKL2000 graphical interface.  This is actually two separate
  formats: one for merged intensities (with or without Friedel mates), another
  for unmerged intensities and associated processing metadata.  Uses either
  :py:mod:`iotbx.scalepack.merge` or :py:mod:`iotbx.scalepack.no_merge_original_index` internally.
- **CNS**: ASCII format, not as flexible as MTZ or CIF but able to store
  either amplitudes or intensities, R-free flags, and Hendrickson-Lattman
  coefficients.  Uses :py:mod:`iotbx.cns.reflection_reader` internally.
- **SHELX**: Fixed-format ASCII used by the eponymous software suite.  This
  format has significan disadvantages, discussed below.  Uses
  :py:mod:`iotbx.shelx.hklf` internally.
- **XDS**: ASCII format for processed intensities (both merged and unmerged).
  Uses :py:mod:`iotbx.xds.read_ascii` internally.
- **D*Trek**: ASCII format produced by software sold by Rigaku.

Independently, the :py:class:`cctbx.miller.array` class defines output methods
for CIF, MTZ, CNS, SHELX, and unmerged Scalepack files (although only the first
two are recommended for routine use).

Note that the underlying formats do not always contain complete information
about the crystal or even the data type.  SHELX format is especially
problematic as it not only omits crystal symmetry, but the same format may be
used to store either amplitudes or intensities, without any distinguishing
features.  As a crude workaround for the latter problem, the data type may be
specified as part of the file name::

  hkl_file = any_reflection_file("data.hkl=hklf4")
  hkl_file = any_reflection_file("data.hkl=intensities")

Other formats (CNS, unmerged Scalepack) may have incomplete or missing crystal
symmetry.  MTZ, XDS, and (usually) CIF files will be more complete.
iÿÿÿÿ(   t   division(   t   mtz(   t   merge(   t   no_merge_original_index(   t   reader(   t   reflection_reader(   t   index_fobs_sigma_reader(   t   reflnlist_reader(   t   hklf(   t   crystal_symmetry_from_ins(   t   option_parser(   t   miller(   t   crystal(   t   easy_pickle(   t   Sorryt   detect_binary_fileNc         C   s_   t  j |  ƒ } t | t j ƒ r( | g St | ƒ } x$ | D] } t | t j ƒ s; d  Sq; W| S(   N(   R   t   loadt
   isinstanceR   t   arrayt   listt   None(   t	   file_namet   resultt   miller_array(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt   unpickle_miller_arraysK   s    c         C   s}  y t  j d |  ƒ } Wn t k
 r) n Xd | f St j d |  ƒ rŽ y t d |  ƒ } Wn$ t k
 ro ‚  n t k
 r n Xd | f Sd Sy t	 j
 t |  ƒ ƒ } Wn t	 j k
 r½ n Xd | f Sy t j d |  ƒ } Wn t k
 rñ n Xd | f Sy t j t |  ƒ ƒ } Wn t j k
 r+n Xd | f Sy t j |  ƒ } Wn$ t k
 r_‚  n t k
 ron Xd | f Sy t j t |  ƒ ƒ } Wn$ t k
 r©‚  n t k
 r¹n Xd | f Sy t j d |  ƒ } Wn$ t k
 rð‚  n t k
 r n Xd	 | f Syg t d
 |  ƒ } t } x< | j ƒ  j ƒ  D]( } d | k sTd | k r6t } Pq6q6W| sqt ‚ n  Wn$ t k
 rˆ‚  n t k
 r˜n Xd | f Sy t t |  ƒ ƒ } Wn$ t k
 rÏ‚  n t k
 rßn Xd | f Sy t ƒ  } | j |  ƒ Wn$ t k
 r‚  n t k
 r*n Xd | f Sy t d |  ƒ } Wn$ t k
 r^‚  n t k
 rnn Xd | f Sd S(   NR   t   ccp4_mtzs   cctbx.miller.arrayt   cns_reflection_filet   cns_index_fobs_sigmat   scalepack_merget!   scalepack_no_merge_original_indext   dtrek_reflnlistt
   shelx_hklft	   file_patht   _refln_index_hs   _refln.index_ht   cift	   xds_asciit   xds_integrate_hklt
   solve_fpfm(   NN(   NN(   R   t   objectt   RuntimeErrorR   t   from_initial_blockR   t   KeyboardInterruptt	   ExceptionR   t   cns_reflection_readerR   t   opent   CNS_input_Errort   cns_index_fobs_sigma_readerR   R   t   FormatErrort   scalepack_no_merget   dtrek_reflnlist_readert	   reflnlistR   t
   cif_readert   Falset   modelt   valuest   Truet   xds_ascii_readert   xds_integrate_hkl_readert	   read_filet   solve_fpfm_reader(   R   t   contentt   looks_like_a_reflection_filet   block(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt   try_all_readersU   sº      
   
  
 	 
  
   
   
 	  
  
   
	  
   
t   any_reflection_filec           B   sP   e  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d e	 e d d d „ Z
 RS(   s³  
  Proxy object for reading a reflection file of unspecified format, and
  extracting the Miller arrays contained therein.

  Examples
  --------
  >>> from iotbx.reflection_file_reader import any_reflection_file
  >>> hkl_file = any_reflection_file("data.mtz")
  >>> print hkl_file.file_type()
  'ccp4_mtz'
  >>> print type(hkl_file.file_content())
  <class 'iotbx_mtz_ext.object'>
  >>> miller_arrays = hkl_file.as_miller_arrays()
  c         C   s  | j  d ƒ sK | j  d ƒ sK | j  d ƒ sK | j  d ƒ sK | j  d ƒ rl | j d d ƒ \ |  _ |  _ n~ | j d ƒ s· | j d	 ƒ s· | j d
 ƒ s· | j d ƒ s· | j d ƒ rØ | j d d ƒ \ |  _ |  _ n | |  _ d  |  _ |  j d k rd |  _ n |  j d k r d |  _ n  d  |  _ |  j } y t | ƒ Wn/ t k
 rq} | rmt	 t
 | ƒ ƒ ‚ n  d  SX|  j d  k	 ræy t j d | ƒ |  _ Wn= t k
 r°‚  qt k
 rÙt	 d | |  j f ƒ ‚ qXd |  _ n t d | ƒ \ |  _ |  _ d  S(   Ns   amplitudes=s   hklf3=s   intensities=s   hklf4=s   hklf+ins/res=t   =i   s   =amplitudess   =hklf3s   =intensitiess   =hklf4s   =hklf+ins/rest   hklf3t
   amplitudest   hklf4t   intensitiesR   sR   Not a SHELX reflection file: %s
  =%s can only be used for SHELX reflection files.R   (   t
   startswitht   splitt   _observation_typet
   _file_namet   endswithR   t
   _file_typeR,   t   IOErrorR   t   strR   R   t   _file_contentR)   R*   R?   (   t   selfR   t   ensure_read_accesst   strictt   e(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt   __init__§   sL    !!				 	 c         C   s   |  j  S(   s   Returns the file name.(   RI   (   RO   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyR   Ñ   s    c         C   s   |  j  S(   s=   Return a string specifying the format type (e.g. 'ccp4_mtz').(   RK   (   RO   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt	   file_typeÕ   s    c         C   s   |  j  S(   s-   Return the underlying format-specific object.(   RN   (   RO   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt   file_contentÙ   s    c         C   s  | d' k s t ‚ |  j d& k r% g  S|  j } | j d ƒ sL | j d ƒ rY | d } n  | d& k rƒ t j d | d |  j ƒ } n  |  j d k rcg  } xÄ |  j D]¹ } | j ƒ  }	 |	 d& k sÓ t	 |	 t j ƒ rÜ | }	 n  | |	 _
 t j d	 | j ƒ  d
 | j ƒ  d t ƒ |	 _ | j | j d | j d | d | d t ƒ ƒ j |	 ƒ j | j ƒ  ƒ ƒ q¢ W| S| d& k s�| j ƒ  d& k r|  j d k rt j j |  j ƒ \ }
 } | d k rÒt j j |
 ƒ \ }
 } n  x[ d |
 d |
 f D]. } y t | ƒ } PWqçt k
 rqçqçXqçWt d |
 f d ƒ ‚ t j d | ƒ } | j  d ƒ | j! ƒ  } | j" ƒ  t# j$ d | t# j% t# j& Bt# j' Bƒ } | d& k r­t d | j( ƒ ‚ n  | j) d ƒ d k rÎd |  _ q| j) d ƒ d k rïd |  _ qt d | j) d ƒ ƒ ‚ n  |  j j* d | d | d | d | ƒ } |  j+ ƒ  d k r|  j d k s_| d k r�| d j | d j ƒ  j d  d! d" g ƒ ƒ | d j, ƒ  q|  j d k s¸| d k rö| d j | d j ƒ  j d  d# d$ g ƒ ƒ | d j- ƒ  qt d% |  j ƒ ‚ n  | S((   sŽ  
    Convert the contents of the reflection file into a list of
    :py:class:`cctbx.miller.array` objects, each of which may contain multiple
    columns of data from the underlying file.  By default this will
    automatically merge redundant observations to obtain a unique set under
    symmetry.

    :param crystal_symmetry: :py:class:`cctbx.crystal.symmetry` object
      (defaults to using internally specified symmetry, if any)
    :param force_symmetry: TODO
    :param merge_equivalents: merge redundant obervations (default=True)
    :param base_array_info: :py:class:`cctbx.miller.array_info` object
      containing basic information to be propagated to the arrays
    :param assume_shelx_observation_type_is: if specified, instead of raising
      an exception if the SHELX file type is not known from the file name plus
      data type tag, the function will force the specified data type.
    RC   RE   s   ./s   .\i   t   sourcet   source_types   cctbx.miller.arrayt	   unit_cellt   space_group_infot%   raise_sorry_if_incompatible_unit_cellt   crystal_symmetryt   other_symmetryt   forces   hklf+ins/ress   .hkls   %s.inss   %s.ressD   Can't open files %s.ins or %s.resrequired by the option hklf+ins/rest   filei    s   ^HKLF\s*(\d)s2   %s does not contain the mandatory HKLF instructioni   t   4t   3s   HKLF %s not supportedt   force_symmetryt   merge_equivalentst   base_array_infoR   t   labelst   Iobst   SigIobst   Fobst   SigFobss  Unresolved amplitude/intensity ambiguity: %s
  SHELX reflection files may contain amplitudes or intensities.
  Please append   =amplitudes
             or   =hklf3
             or   =intensities
             or   =hklf4
  to the file name argument or parameter to resolve the ambiguity.
  If a corresponding .ins file is available, look for the HKLF codeword.
  Alternatively, run the phenix.reflection_statistics command twice,
  once with =amplitudes and once with =intensities. Inspect the <I^2>/(<I>)^2
  statistics. For acentric structures the values should fluctuate around
  2.0, for centric structures around 3.0. If the statistics are not conclusive
  it will be best to recover the original reflection data, such as SCALEPACK,
  SCALA MTZ, XDS, or d*TREK files.N(   Ns
   amplitudess   intensities(.   R   t   AssertionErrorRK   RI   RF   R   t
   array_infoRN   t   infoR   RV   R   t   symmetryRX   RY   R7   t   crystal_symmetry_from_filet   appendt   customized_copyt   join_symmetryt   set_infot   set_observation_typet   observation_typeRH   t   ost   patht   splitextR,   RL   R   R	   t   extract_fromt   seekt   readt   closet   ret   searcht   Xt   Mt   St   namet   groupt   as_miller_arraysRT   t#   set_observation_type_xray_intensityt#   set_observation_type_xray_amplitude(   RO   R[   Ra   Rb   Rc   t    assume_shelx_observation_type_ist   info_sourceR   R   Rk   R€   t   extt   shelx_file_namet
   shelx_filet	   remainingt   m(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyR‚   Ý   sš    									
&	N(   t   __name__t
   __module__t   __doc__R7   RS   R   RT   RU   R   R4   R‚   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyR@   —   s   *			i   c         C   sJ  | d  k r t j } n  | r' d  } n g  } x|  D]} | d k rg | J| d I| IJ| j ƒ  n  t | ƒ }	 | d k rœ | d I|	 j ƒ  IJ| Jn  |	 j d | d | d | ƒ }
 xo |
 D]g } | d k rÁ | d k rò | j d | ƒ n | j d | ƒ | d	 k r!| j	 d | ƒ n  | JqÁ qÁ W| s4 | j
 |
 ƒ q4 q4 W| S(
   Ni    s
   file_name:s
   file_type:R[   Ra   Rb   i   t   fi   (   R   t   syst   stdoutt   flushR@   RT   R‚   t   show_comprehensive_summaryt   show_summaryt
   show_arrayt   extend(   t
   file_namesR[   Ra   Rb   t   discard_arrayst   verboset
   report_outR   R   t   reflection_filet   miller_arraysR   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt   collect_arraysY  s:    			c         C   s  t  d d d d ƒ j ƒ  j d  d d d d t d	 d
 ƒj d  d d d d t d	 d ƒj d  d d d d d d	 d d d ƒj d |  ƒ } t | j ƒ d k r­ | j j	 ƒ  d  S| j
 j rÂ d } n d } t d | j d | j d | j
 j d | j
 j d  k d | d t j ƒ } | d  k	 r
t | ƒ d k r
t | ƒ d k rN| d } n  | j
 j } | d k rÊt | j ƒ d k rŠt d  ƒ ‚ n  t j j | j d ƒ } | j ƒ  j d! ƒ rÊt d" ƒ ‚ qÊn  | j ƒ  j d! ƒ sì| d! 7} n  Hd# G| GHt j | | ƒ Hn  d  S($   Nt   usages:   iotbx.reflection_file_reader [options] reflection_file ...t   descriptions:   Example: iotbx.reflection_file_reader w1.sca w2.mtz w3.cnss   --weak_symmetryt   actiont
   store_truet   defaultt   helps?   symmetry on command line is weaker than symmetry found in filess   --show_datas*   show Miller indices and data of all arrayss   --picklet   storet   typet   strings?   write all data to FILE ('--pickle .' copies name of input file)t   metavart   FILEt   argsi    i   i   R—   R[   Ra   R˜   R™   Rš   i   t   .s:   Ambiguous name for pickle file (more than one input file).s   .pickles$   Input file is already a pickle file.s"   Writing all Miller arrays to file:(   R
   t   enable_symmetry_comprehensivet   optionR   R4   t   processt   lenR©   t   parsert	   show_helpt   optionst	   show_dataR�   Rl   t   weak_symmetryt   pickleR�   R‘   R   Rt   Ru   t   basenamet   lowerRJ   R   t   dump(   R©   t   command_lineR™   t   all_miller_arrayst   pickle_file_name(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt   run�  s^    										(6   RŽ   t
   __future__R    t   iotbxR   t   iotbx.scalepackR   R   R   R0   t	   iotbx.cifR   R3   t	   iotbx.cnsR   R+   R   R.   t   iotbx.dtrekR   R1   t   iotbx.shelxR   R   R	   t   iotbx.xds.read_asciiR8   t   iotbx.xds.integrate_hklR9   t   iotbx.solve.fpfm_readerR;   t   iotbx.option_parserR
   t   cctbxR   R   t   libtbxR   t   libtbx.utilsR   R   R�   Rt   t   os.pathR{   R   R?   R&   R@   R7   R4   R   R�   R»   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/reflection_file_reader.pyt   <module>4   s8   $	
	BÆ!