ó
±™xTc           @   sÃ   d  Z  d d l m Z d d l Z d d l m Z m Z d d l m Z d d l	 Z	 d d l
 Z
 e j j d ƒ Z d d e	 j d „ Z e	 j d „ Z d	 „  Z e d
 k r¿ e e	 j d ƒ n  d S(   sx   
Provides a command-line utility for fetching PDB files and their associated
reflection data and electron density maps.
iÿÿÿÿ(   t   divisionN(   t   Sorryt   Usage(   t   easy_runs  
fetch_pdb
  .caption = PHENIX can automatically retrieve data from the PDB via the RCSB     web server.  If you intend to re-refine or re-build the structure we     recommend creating a new project, but this is not required.  Note that     you may also use this tool to generate an MTZ file from the mmCIF     structure factors (if available) and even calculate maps, but the options     are more limited than what is available in the phenix.cif_as_mtz and     phenix.maps GUIs.
  .style = auto_align caption_img:icons/custom/pdb_import64.png     caption_width:400
{
  pdb_ids = None
    .type = strings
    .short_caption = PDB ID(s)
    .input_size = 400
    .style = bold
  action = *pdb_only all_data all_plus_mtz all_plus_maps
    .type = choice
    .caption = Download_PDB_file(s) Download_all_data Download_all_data_and_convert_CIF_to_MTZ Download_all_data_and_create_maps
    .style = bold
  site = *rcsb pdbe pdbj
    .type = choice
    .caption = RCSB PDBe PDBj
    .short_caption = Mirror site
    .style = bold
}c   
      C   s^  | d k	 s t ‚ g  } g  } d | j j } x#| j j D]} | | g }  | j j d k r|  j d d ƒ | j j d k r” |  j d d ƒ n% | j j d k r¹ |  j d d	 ƒ n  y9 t d
 |  d | ƒ } | d j | ƒ IJ| j	 | ƒ WqPt
 k
 r} | j t | ƒ ƒ qPXq; t d
 | | g d | ƒ }	 | |	 IJ| j |	 ƒ q; W| | f S(   s=  
  For use in PHENIX GUI only, fetches pdb files and their associated maps and/or
  reflection data from the PDB.

  Parameters
  ----------
  args : list of str, optional
  params : libtbx.phil.scope_extract, optional
  out : file, optional

  Returns
  -------
  output_files : list of str
  errors : list of str
  s   --mirror=%st   all_datat   all_plus_mtzt   all_plus_mapsi    s   --alli   s   --mtzs   --mapst   argst   logs   
N(   R   R   R   (   t   Nonet   AssertionErrort	   fetch_pdbt   sitet   pdb_idst   actiont   insertt   run2t   joint   extendt	   Exceptiont   appendt   str(
   R   t   paramst   outt   output_filest   errorst   mirrort   idt
   data_filest   et   pdb_file(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fetch_pdb.pyt   run0   s.    		
c         C   s6  t  |  ƒ d k  r! t d ƒ ‚ n  d d l m } t } t } } } d } d } d }	 g  }
 x|  D]ü } | d k r d } qd | d	 k r” d
 } qd | d k r© d } qd | d k r¾ t } qd | d k rÙ t } d } qd | d k rú t } d } t } qd | d k rd } qd | j d ƒ rS| j d ƒ d }	 |	 d4 k r`t d |	 ƒ ‚ q`qd |
 j	 | ƒ qd Wt  |
 ƒ d k r…t d ƒ ‚ n  | d k règ  } x0 |
 D]( } | j	 | | | |	 | d | ƒƒ qžWt  | ƒ d k rä| d S| Sg  } x=|
 D]5} xB d5 d6 d7 d8 g D]. \ } } | j	 | | | |	 | d | ƒƒ qW| rqd d d d g } t
 j d | | d j | ƒ f ƒ t j j d  | ƒ rºt j d  | d! | ƒ | d" | IJn  t j j d! | ƒ | d <t j j d! | ƒ st d# d$ | ƒ ‚ n  d d% l m } | d! | ƒ } | j d& ƒ x@ | j j D]/ } | j ƒ  r;| d' | j ƒ  j ƒ  IJq;q;Wn  | rõt j j d! | ƒ s“t ‚ d d( l m } | d) d* | d+ d! | d, d- | d. | d/ | d0 t d1 t ƒ t j j d- | ƒ süt ‚ | d2 | IJ| j	 t j j d- | ƒ ƒ qõqõW| Sd3 S(9   sø   
  Fetches pdb files and their associated maps and/or reflection data from the
  PDB.

  Parameters
  ----------
  args : list of str
  log : file, optional

  Returns
  -------
  str or list of str
      List of file names that were downloaded.
  i   s  phenix.fetch_pdb [-x|-f|--all] [--mtz] [--maps] [-q] ID1 [ID2, ...]

Command-line options:
  -x      Get structure factors (mmCIF file)
  -c      Get model file in mmCIF format
  -f      Get sequence (FASTA file)
  --all   Download all available data
  --mtz   Download structure factors and PDB file, and generate MTZ
  --maps  As for --mtz, plus create 2mFo-DFc and mFo-DFc map coefficients (and
          anomalous map, if possible)
  --fill  Fill missing F(obs) with F(calc) in map coefficients
  -q      suppress printed output
iÿÿÿÿ(   t   get_pdbt   pdbt   rcsbs   --allt   alls   -xt   xrays   -ft   fastas   -qs   --mtzs   --mapss   -ct   cifs	   --mirror=t   =t   pdbet   pdbjs9   Unrecognized mirror site '%s' (choices: rcsb, pdbe, pdbj)i    s   No PDB IDs specified.t   formats   --merges   --map_to_asus   --extend_flagss   --ignore_bad_sigmass0   phenix.cif_as_mtz %s-sf.cif --symmetry=%s.pdb %st    s	   %s-sf.mtzs   %s.mtzs+   Converted structure factors saved to %s.mtzs6   MTZ conversion failed - try running phenix.cif_as_mtz s1   manually (and check %s-sf.cif for format errors).(   t   any_filet   hkls     %s is anomalous(   t   create_map_from_pdb_and_mtzR   s   %s.pdbt   mtz_filet   output_files   %s_maps.mtzt   fillR   t    remove_unknown_scatterering_typet   assume_pdb_datas%   Map coefficients saved to %s_maps.mtzN(   R"   R(   R)   (   R!   R!   (   R%   R!   (   R$   R!   (   R!   R&   (   t   lenR   t   iotbx.pdb.fetchR    t   Falset   Truet
   startswitht   splitR   R   R   t   callR   t   ost   patht   isfilet   renamet   abspatht   iotbx.file_readerR,   t   assert_file_typet   file_servert   miller_arrayst   anomalous_flagt   infot   label_stringR
   t   mmtbx.maps.utilsR.   (   R   R   R    t   quiett   convert_to_mtzt   mapst	   fill_mapst	   data_typeR*   R   t   idst   argt   filesR   t
   data_type_t   data_formatt	   misc_argsR,   t   mtz_int   arrayR.   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fetch_pdb.pyR   Y   s¤    							&	&			$


$c         C   s=   |  j  j d k s* t |  j  j ƒ d k r9 t d ƒ ‚ n  t S(   s	  
  Validates that the input parameters specify a PDB file to download.

  Parameters
  ----------
  params : libtbx.phil.scope_extract

  Returns
  -------
  bool

  Raises
  ------
  Sorry
      Raised if a pdb file is not specified to be fetched within params.
  i    s   No PDB IDs specified!N(   R   R   R	   R4   R   R7   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fetch_pdb.pyt   validate_paramsË   s    *t   __main__i   (    (   t   __doc__t
   __future__R    t   libtbx.philt   libtbxt   libtbx.utilsR   R   R   t   sysR;   t   philt   parset   master_philR	   t   stdoutR   R   RU   t   __name__t   argv(    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fetch_pdb.pyt   <module>   s   		)r	