ó
xTc           @   s  d  d l  m Z d  d l m Z m Z 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 Z d  d l Z d  d l Z d   Z d   Z d	   Z d
 d
 d d e d d  Z d
 e d d  Z e d
 d  Z e d  Z d   Z d   Z d S(   i˙˙˙˙(   t   division(   t   Sorryt   null_out(   t
   smart_open(   t   AutoN(   t   StringIOc         C   s"   t  |   d k o! t j d |   S(   Ni   s   [1-9]{1}[a-zA-Z0-9]{3}(   t   lent   ret   match(   t   id(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   looks_like_pdb_id    s    c         C   s'   t  |   s# t d d |    n  d  S(   Ns/   Invalid PDB ID '%s'.  IDs must be exactly four s9   alphanumeric characters, starting with a number from 1-9.(   R
   t   RuntimeError(   R	   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   validate_pdb_id#   s    c         C   sK   xD |  D]< } y t  |  Wq t k
 rB } t t |    q Xq Wd  S(   N(   R   R   R   t   str(   t   id_listR	   t   e(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   validate_pdb_ids(   s
    t   pdbt   rcsbc         C   s  | d1 k s t   | d2 k s$ t   | d3 k s6 t   t |   | d- k rX t   } n  |  j   }  | sÉ| d- k	 rh| d k rhd
 d l m } | t k r­ t j	   } n  t j
 |  } xŠ | D] }	 t |	  d k rĂ t j d d |	  d d !}
 |
 j   |  k ra| |	  d k r^t j j | |	  }	 | d IJ| d |	 IJt j |	  } | SqaqĂ qĂ Wn  | d k rd t j k rt j j t j d |  d d ! } t j j |  rt j j | d |   }	 t j j |	  r| d IJ| d |	 IJt j |	  } | Sqn  | d k rÉd t j k rÉt j d } t j j | |  d d ! } t j j |  rĆt j j | d |   }	 t j j |	  rĂ| d IJ| d |	 IJt j |	  } | SqĆqÉn  d- } t } | d k rüd } d } d } d } n| d k r#d } d } d } d } nň | d	 k rd  } | d k rt } | d k rq| d! |  d d !|  f } qç| d k rç| d" |  d d !|  f } qçnI | d k rÎt } | d# |  d d !|  f } n | d4 k rçd$ |  } n  | d- k r| d k rt d% d&   qn  | d5 k r| d- k r:d' |  } n  y t j j |  } Wq°t j k
 r} | j   d( k rt d) |    q  q°Xn| d k r| d- k rÁ| | |  | } n  y t j j |  } Wq°t j k
 r} | j   d( k rt d* |    q  q°Xn | d- k rY| d k rD| |  | } qY| |  d+ | } n  y t j j |  } Wn> t j k
 rŻ} | j   d( k rŠt d, |    q°  n X| r	y d
 d- l } Wn! t  k
 rét d. d/   q	X| j! d0 t" | j#     Sn  | S(6   s  
  Locate and open a data file for the specified PDB ID and format, either in a
  local mirror or online.

  :param id: 4-character PDB ID (e.g. '1hbb')
  :param data_type: type of content to download: pdb, xray, or fasta
  :param format: format of data: cif, pdb, or xml
  :param mirror: remote site to use, either rcsb or pdbe

  :returns: a filehandle-like object (with read() method)
  R   t   xrayt   fastat   seqt   cift   xmlR   t   pdbet   pdbji˙˙˙˙(   t   guess_file_typei   s   ^pdbt    i    s   Reading from cache directory:s     t   PDB_MIRROR_PDBi   i   s   pdb%s.ent.gzs   Reading from local mirror:t   PDB_MIRROR_STRUCTURE_FACTORSs   r%ssf.ent.gzs   http://www.rcsb.org/pdb/files/s   .pdbs   -sf.cifs)   http://www.ebi.ac.uk/pdbe-srv/view/files/s   .entt   rs   sf.ents3   ftp://ftp.pdbj.org/pub/pdb/data/structures/divided/s   pdb/%s/pdb%s.ent.gzs   mmCIF/%s/%s.cif.gzs!   structure_factors/%s/r%ssf.ent.gzs1   http://pdbj.org/app//downloadFasta4PDBID?pdbid=%ss7   Can't determine PDBj download URL for this data/format s   combination.s_   http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=FASTA&compression=NO&structureId=%si  s"   Couldn't download sequence for %s.s+   Couldn't download structure factors for %s.t   .s   Couldn't download model for %s.Ns7   gzip module not available - please use an uncompressed s   source of PDB data.t   fileobj(   s   pdbR   R   R   (   R   s   pdbR   (   R   R   R   (   R   R   (   R   R   ($   t   AssertionErrorR   t   NoneR   t   lowert   iotbx.file_readerR   R   t   ost   getcwdt   listdirR   R   t   subt   patht   joinR   t   for_readingt   environt   isdirt   isfilet   Falset   TrueR   t   libtbxt   utilst   urlopent   urllib2t	   HTTPErrort   getcodeR   t   gzipt   ImportErrort   GzipFileR   t   read(   R	   t	   data_typet   formatt   mirrort   logt   force_downloadt   local_cacheR   t   cache_filest	   file_namet   file_idt   ft   subdirt   sf_dirt   urlt
   compressedt   url_baset   pdb_extt	   sf_prefixt   sf_extt   dataR   R7   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   fetch/   sÖ    
 
#


		!

c   	   	   C   s   t  d |  d | d t   d |  } d d l } | j j j d | j    } | j } | j   j   | j j	 d |  } | | f S(	   sy   
  Simple utility method to load the PDB hierarchy and xray structure objects
  directly (without intermediate files).
  R	   R<   R>   R@   i˙˙˙˙Nt
   pdb_stringt   enable_scattering_type_unknown(
   RN   R   t   iotbx.pdb.hierarchyR   t	   hierarchyt   inputR:   t   atomst   reset_i_seqt   xray_structure_simple(	   R	   R<   t   allow_unknownsR@   RM   t   iotbxt   pdb_inRR   t   xray_structure(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   load_pdb_structure´   s    $		c   	   	   C   sq  y% t  |  | d | d | d | } Wn% t k
 rL } t t |    n Xd } | d k r´ t j j t j   d |   } t	 | d  j
 | j    | sm| d | IJqmnš | d k rt j j t j   d
 |   } t	 | d  j
 | j    | sm| d | IJqmnX t j j t j   d |  | f  } t	 | d  j
 | j    | sm| d | IJn  | S(   s=   
  Frontend for fetch(...), writes resulting data to disk.
  R=   R<   R>   R   s	   %s-sf.cift   ws   Structure factors saved to %sR   R   s   %s.fas   Sequence saved to %ss   %s.%ss   Model saved to %sN(   s   fastas   seq(   RN   R   R   R   R"   R%   R)   R*   R&   t   opent   writeR:   (	   R	   R;   R=   R>   t   quietR<   RM   R   RB   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   get_pdbĹ   s(    %%c         C   s<  |  d  k	 s t  t |   d k s6 t |   d k rM t d d |    n  |  d j   } |  j   }  t j j d d | |  f d t	 j
 j  } | d  k r.d |  } y t j j |  } Wn> t j k
 r } | j   d	 k rú t d
 t   q+  q8Xd |  } t | d  j | j    | Sn
 | s8| Sd  S(   Ni    i   s;   Bad code '%s': PDB residue codes must be at least 1 but no s   more than 3 characters.t   relative_paths,   chem_data/chemical_components/%s/data_%s.cift   tests+   http://www.rcsb.org/pdb/files/ligand/%s.cifi  s"   Couldn't download sequence for %s.s   %s.cifR\   (   R"   R!   R   R   R#   t   upperR1   t   envt   find_in_repositoriesR%   R)   R.   R2   R3   R4   R5   R6   R   R	   R]   R^   R:   (   t   codet   return_none_if_already_presentt
   first_chart   chem_comp_cifRG   RM   R   RB   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   get_chemical_components_cifß   s0    $


c          O   s"   d d  l  } | j j j |  |   S(   Ni˙˙˙˙(   t   iotbx.bioinformatics.structuret   bioinformaticst	   structuret   get_ncbi_pdb_blast(   t   argst   kwdsRX   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyRn   ü   s    c          O   s"   d d  l  } | j j j |  |   S(   Ni˙˙˙˙(   Rk   Rl   Rm   t   get_ebi_pdb_wublast(   Ro   Rp   RX   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyRq      s    (   t
   __future__R    t   libtbx.utilsR   R   R1   R   R   t   libtbx.load_envt	   cStringIOR   R4   R   R%   R
   R   R   R"   R/   RN   R[   R`   Rj   Rn   Rq   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/pdb/fetch.pyt   <module>   s*   				