ó
�™xTc        	   @   s¬   d  Z  d d l m Z d d l Z d d l m Z d d l Z d Z e d Z	 e
 d d e e d d e d „ Z d „  Z d	 „  Z e j d
 e
 d „ Z d „  Z d „  Z d S(   s  
Module for querying the RCSB web server using the REST API, as described here:
http://www.rcsb.org/pdb/software/rest.do

There is some overlap with iotbx.pdb.fetch, which really should have gone here
instead, but this module is intended to be used in higher-level automation
pipelines.
iÿÿÿÿ(   t   divisionN(   t   parseStrings   http://www.rcsb.org/pdb/rests   /searchc	         C   s�  | d k r t j j ƒ  } n  g  }	 |  d k	 r@ |	 j |  ƒ n  | d IJ| r‡ | d IJd d }
 | rw |
 d 7}
 n  |	 j |
 ƒ n  | d k	 sŸ | d k	 r=d } | d d 7} | d k	 rð | d	 k sÑ t ‚ | d
 | IJ| d | 7} n  | d k	 r-| d	 k st ‚ | d | IJ| d | 7} n  |	 j | ƒ n  | ra| d IJ|	 j d d ƒ n  | d k	 r½| d t | ƒ IJ| d	 k r™| d k sŸt ‚ |	 j d d t | ƒ ƒ n  t |	 ƒ d	 k rÞt d ƒ ‚ n  d } d	 } x, |	 D]$ } | d | | f 7} | d 7} qñWd | } t	 | ƒ } t
 } | rR| d 7} | d IJn  | d IJt j j | | ƒ j ƒ  } | j ƒ  S(   s”  
  Generate the full XML for a multi-part query with generic search options,
  starting from the basic query passed by another function, post it to the
  RCSB's web service, and return a list of matching PDB IDs.

  Parameters
  ----------
  query_xml : str or None
  xray_only : bool, optional
  d_max : float, optional
  d_min : float, optional
  protein_only : bool, optional
  data_only : bool, optional
  identity_cutoff : int, optional
      Apply sequence filtering to remove structures with greater than this
      percentage sequence identity. Must be one of 100, 95, 90, 70, 50, 40, 30.
  log : file, optional
  sort_by_resolution : bool, optional
  s   Setting up RCSB server query:s     limiting to X-ray structuress9   <queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
s@   <mvStructure.expMethod.value>X-RAY</mvStructure.expMethod.value>sQ   
<mvStructure.hasExperimentalData.value>Y</mvStructure.hasExperimentalData.value>s;   <queryType>org.pdb.query.simple.ResolutionQuery</queryType>s)   
<refine.ls_d_res_high.comparator>betweens"   </refine.ls_d_res_high.comparator>i    s'     applying resolution cutoff d_min > %fs8   
<refine.ls_d_res_high.min>%f</refine.ls_d_res_high.min>s'     applying resolution cutoff d_min < %fs8   
<refine.ls_d_res_high.max>%f</refine.ls_d_res_high.max>s     excluding non-protein modelss;   <queryType>org.pdb.query.simple.ChainTypeQuery</queryType>
s$   <containsProtein>Y</containsProtein>s.     filtering based on sequence identity < %d %%id   sD   <queryType>org.pdb.query.simple.HomologueReductionQuery</queryType>
s#   <identityCutoff>%d</identityCutoff>s   No queries specified!t    sà   
        <queryRefinement>
          <queryRefinementLevel>%d</queryRefinementLevel>
          <conjunctionType>and</conjunctionType>
          <orgPdbQuery>
            %s
          </orgPdbQuery>
        </queryRefinement>i   sB   <orgPdbCompositeQuery version="1.0">
  %s
</orgPdbCompositeQuery>
s   /?sortfield=Resolutions     will sort by resolutions     executing HTTP request...N(   t   Nonet   libtbxt   utilst   null_outt   appendt   AssertionErrort   intt   lent   RuntimeErrorR   t
   url_searcht   urlopent   readt
   splitlines(   t	   query_xmlt	   xray_onlyt   d_maxt   d_mint   protein_onlyt	   data_onlyt   identity_cutofft   logt   sort_by_resolutiont   queriest
   xray_queryt   base_clauset   queries_stringt   kt   clauset	   query_strt   parsedt	   url_finalt   result(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/rcsb_web_services.pyt
   post_query   sr    





c         K   sŠ   | j  d d ƒ } | j  d d ƒ } | j  d d ƒ } | d k rO | d 9} n  | d k sa t ‚ d | | |  | | | f } t | | � S(   Nt   search_typet   blastt   expectg{®Gáz„?t   min_identityg        i   id   t   fastat   psiblasts      <queryType>org.pdb.query.simple.SequenceQuery</queryType>
      <description>Sequence Search (expect = %g, search = %s)</description>
      <sequence>%s</sequence>
      <eCutOff>%g</eCutOff>
      <sequenceIdentityCutoff>%g</sequenceIdentityCutoff>
    <searchTool>%s</searchTool>(   R%   R(   R)   (   t   popR   R#   (   t   sequencet   kwdsR$   R&   R'   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/rcsb_web_services.pyt   sequence_searchm   s    c         K   sv   d t  |  ƒ k o d k n s( t ‚ | j d d ƒ } | d	 k sL t ‚ d | } d |  |  | f } t | | � S(
   sH  
  Find all entry IDs with the specified chemical ID.

  Parameters
  ----------
  resname : str
  kwds : dict
      Keyword arguments passed to post_query.

  Examples
  --------
  >>> from mmtbx.wwpdb.rcsb_web_services import chemical_id_search
  >>> len(chemical_id_search("ZN", data_only=True, identity_cutoff=70))
  2874
  i   i   t   polymeric_typet   Anyt   Freet	   Polymerics!   <polymericType>%s</polymericType>s•       <queryType>org.pdb.query.simple.ChemCompIdQuery</queryType>
    <description>Chemical ID: %s</description>
    <chemCompId>%s</chemCompId>
    %s(   R/   R0   R1   (   R
   R   R*   R#   (   t   resnameR,   R.   t   polymer_limitR   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/rcsb_web_services.pyt   chemical_id_searchƒ   s    (
t   dimStructurec         C   sù  t  | ƒ d k s t ‚ t  |  ƒ d k r. g  Sd } | d d j |  ƒ } d g | } | d d j | ƒ 7} t j j | ƒ j ƒ  } t | ƒ }	 g  }
 t g  ƒ } |	 j	 d ƒ } xÙ | D]Ñ } | j	 d | ƒ } t  | ƒ d	 k sø t | j
 ƒ  ƒ ‚ | d j d j } | j | ƒ | g } xZ | D]R } | j	 d
 | | f ƒ } t  | ƒ d	 k s`t ‚ | j | d j d j ƒ q)W|
 j | ƒ q» W| rõt |  ƒ | } t  | ƒ d k rõ| d t  | ƒ IJ| d d j t t | ƒ ƒ ƒ IJqõn  |
 S(   sy   Given a list of PDB IDs and a list of attribute identifiers, returns a
  Python list of lists for the IDs and attributes.i    s*   http://www.rcsb.org/pdb/rest/customReport?s	   pdbids=%st   ,t   structureIds   &customReportColumns=%st   records   %s.structureIdi   s   %s.%ss(   WARNING: missing report info for %d IDs:s     %st    (   R
   R   t   joinR   R   R   R   R   t   sett   getElementsByTagNamet   toxmlt
   childNodest   datat   addR   t   sortedt   list(   t   pdb_idst   columnsR   t   prefixt   check_for_missingt   url_baset   urlt   all_columnsR"   t   xmlrect   tablet
   report_idst   recordsR8   t   pdb_id_nodest   pdb_idt   rowt   col_namet   row_colt   missing_ids(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/rcsb_web_services.pyt   get_custom_report_tablež   s:     $	)c         C   s   t  |  d d g ƒS(   NRD   t   highResolutionLimit(   RT   (   RC   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/rcsb_web_services.pyt"   get_high_resolution_for_structuresÍ   s    c      	   C   s.   t  |  d d d d d d d g d d	 d
 t ƒS(   sW   Return a list of ligands in the specified structure(s), including the
  SMILES strings.RD   t   chainIdt   ligandIdt   ligandMolecularWeightt   ligandFormulat
   ligandNamet   ligandSmilesRE   t	   dimEntityRF   (   RT   t   False(   RC   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/rcsb_web_services.pyt   get_ligand_info_for_structuresÐ   s
    	(   t   __doc__t
   __future__R    t   libtbx.utilsR   t   xml.dom.minidomR   t   sysRG   R   t   TrueR   R^   R#   R-   R4   t   stdoutRT   RV   R_   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/rcsb_web_services.pyt   <module>	   s   
	X		.	