
    Ri               	          d 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
  ej                  d      Zdededz  fd	Zdedee   fd
ZdedefdZ G d d      Z	 ddedee   dz  dedefdZedk(  rddlmZ  e        yy)a  Code for dealing with assorted UniProt file formats and interacting with the UniProt database.

This currently include parsers for the GAF, GPA and GPI formats
from UniProt-GOA as the module Bio.UniProt.GOA.

See also Bio.SwissProt and the "swiss" support in Bio.SeqIO for
the legacy plain text sequence format still used in UniProt.

See also Bio.SeqIO.SwissIO for the "uniprot-xml" support in
Bio.SeqIO.
    N)HTTPResponse)Optional)urlopenz<(.+)>; rel="next"responsereturnc                     | j                   }d|v r0|d   r+t        j                  |d         }|r|j                  d      S y )NLink   )headers_re_next_linkmatchgroup)r   r   r   s      L/home/agent/.friday_env/lib/python3.12/site-packages/Bio/UniProt/__init__.py_get_next_linkr      sC    GWV_##GFO4;;q>!    c                 j    t        j                  | j                         j                               d   S )Nresults)jsonloadsreaddecode)r   s    r   _get_resultsr   (   s%    ::hmmo,,./	::r   c                 J    | j                   }d|v r|d   rt        |d         S y)Nzx-total-resultsr   )r   int)r   r   s     r   _get_search_result_countr   ,   s1    GG#0A(B7,-..r   c                   \    e Zd ZdZdefdZdefdZd Zde	fdZ
de	dd	fd
ZdefdZd Zy	)_UniProtSearchResultszA sequence over the results of a UniProt search.

    Do not use this class directly. Instead, use the :meth:`UniProt.search` method.
    r   c                     | j                   J t        | j                         5 }| xj                  t        |      z  c_        t	        |      | _         |cd d d        S # 1 sw Y   y xY wN)next_urlr   results_cacher   r   )selfr   s     r   _fetch_next_batchz'_UniProtSearchResults._fetch_next_batch;   sZ    }}(((T]]# 	x,x"88*84DM	 	 	s   0AA'	first_urlc                 n    || _         g | _        d| _        | j                         }t	        |      | _        y )Nr   )r    r!   next_result_indexr#   r   search_result_count)r"   r$   r   s      r   __init__z_UniProtSearchResults.__init__B   s4    !)+!"))+#;H#E r   c                     | S r    r"   s    r   __iter__z_UniProtSearchResults.__iter__I   s    r   c                     | j                   S )zHReturn the total number of search results, regardless of the batch size.)r'   r+   s    r   __len__z_UniProtSearchResults.__len__L   s    '''r   indexNc                     |t        t        |             v sJ |t        | j                        k\  r*| j                          |t        | j                        k\  r)yy)z4Fetch batches until the given index is in the cache.N)rangelenr!   r#   )r"   r/   s     r   
_fetch_forz _UniProtSearchResults._fetch_forP   sM    c$i((((s4--..""$ s4--..r   c                     | j                   t        |       k  rL| j                  | j                          	 | j                  | j                      }| xj                   dz  c_         |S t
        # t        $ r t
        w xY w)Nr
   )r&   r2   r3   r!   
IndexErrorStopIteration)r"   next_results     r   __next__z_UniProtSearchResults.__next__V   sw    !!CI-OOD223$"001G1GH&&!+&""    $##$s   /A+ +A;c                    t        |t              r|j                  t        |             \  }}}d|cxk  rt        |       k  rn J d|cxk  rt        |       k  sJ  J |dkD  r||k  r|dkD  r| j	                  |dz
         nu||k\  rp|dkD  rk| j	                  |       nYt        |t
              rI|t        t        |        t        |             vrt        d      | j	                  |t        |       z         | j                  |   S )Nr   r
   zIndex out of bounds.)	
isinstancesliceindicesr2   r3   r   r1   r5   r!   )r"   r/   startstopsteps        r   __getitem__z!_UniProtSearchResults.__getitem__b   s    eU# %c$i 8E4)D	)DDa4.D3t9.DDD.DDDaxD=TAXOOD1H- D=UQYOOE*s#E3t9*c$i88 !788OOECI-.!!%((r   )__name__
__module____qualname____doc__r   r#   strr(   r,   r   r.   r3   dictr8   r@   r*   r   r   r   r   5   sV    
< F# F( (% % %
 $ 
 )r   r   queryfields
batch_sizec                     | |dd}|rdj                  |      |d<   dt        j                  j                  |       }t	        |      S )aa  Search the UniProt database.

    Consider using `query syntax <https://www.uniprot.org/help/text-search>`_ and
    `query fields <https://www.uniprot.org/help/query-fields>`_ to refine your search.

    See the API details `here <https://www.uniprot.org/help/api_queries>`_.

    >>> from Bio import UniProt
    >>> from itertools import islice
    >>> # Get the first 10 results
    >>> results = UniProt.search("(organism_id:2697049) AND (reviewed:true)")[:10]

    :param query: The query string to search UniProt with
    :type query: str
    :param fields: The columns to retrieve in the results, defaults to all fields
    :type fields: List[str], optional
    :param batch_size: The number of results to retrieve in each batch, defaults to 500
    :type batch_size: int
    :return: An iterator over the search results
    :rtype: _UniProtSearchResults
    r   )rG   sizeformat,rH   z*https://rest.uniprot.org/uniprotkb/search?)joinurllibparse	urlencoder   )rG   rH   rI   
parametersurls        r   searchrT   u   sS    2 J
 "xx/
86v||7M7Mj7Y6Z
[C %%r   __main__)run_doctest)Ni  )rD   r   reurllib.parserO   http.clientr   typingr   urllib.requestr   compiler   rE   r   listrF   r   r   r   r   rT   rA   
Bio._utilsrV   r*   r   r   <module>r_      s   
  	  $  "

01\ cDj ;< ;DJ ;|  =) =)B DG!&!&S	D(!&=@!&!&H z&M r   