
:>"^6                 @   s!  d  Z  d d l m Z d d l Z d d l m Z d d d d d	 d
 d d d d d d d d d d d g Z d d d d d	 d
 d d d d d d d d d g Z d d d d	 d
 d d d d d d d g Z d d d d	 d
 d d d d d d d g Z	 d d d d d d d d d  d! d" g Z
 d d d d d d d" d# d$ g	 Z d% d&   Z d' d(   Z d) d*   Z d+ d,   Z d- d.   Z d/ d0   Z d1 d2   Z d3 d4   Z d5 d6   Z d7 d8   Z d9 d:   Z d; d<   Z e d= d>  Z e d? d@  Z dA dB   Z e dC k rd dD l m Z e dE d  d S)Fa  Parsers for the GAF, GPA and GPI formats from UniProt-GOA.

Uniprot-GOA README + GAF format description:
ftp://ftp.ebi.ac.uk/pub/databases/GO/goa/UNIPROT/README

GAF formats:
http://geneontology.org/docs/go-annotation-file-gaf-format-2.1/
http://geneontology.org/docs/go-annotation-file-gaf-format-2.0/

gp_association (GPA format) README:
http://geneontology.org/docs/gene-product-association-data-gpad-format/

gp_information (GPI format) README:
http://geneontology.org/docs/gene-product-information-gpi-format/
    )print_functionN)zipZDBDB_Object_IDZDB_Object_SymbolZ	QualifierZGO_IDzDB:ReferenceZEvidenceWithZAspectZDB_Object_NameZSynonymZDB_Object_TypeZTaxon_IDDateZAssigned_ByZAnnotation_ExtensionZGene_Product_Form_IDzEvidence codeZInteracting_taxon_IDZAssigned_byZSpliceform_IDZECO_Evidence_codezAnnotation ExtensionZAnnotation_PropertiesZ	DB_subsetZDB_Object_SynonymZTaxonZAnnotation_Target_SetZAnnotation_CompletedZParent_Object_IDZDB_XrefZGene_Product_Propertiesc             c   s   x |  D] } | d d k r  q | j  d  j d  } t |  d k rM q | d j d  | d <| d j d  | d <t t t |   Vq Wd	 S)
zRead GPI 1.0 format files (PRIVATE).

    This iterator is used to read a gp_information.goa_uniprot
    file which is in the GPI 1.0 format.
    r   !
	      |   N)rstripsplitlendictr   GPI10FIELDS)handleinlineinrec r   4/tmp/pip-build-ww9dw3qa/biopython/Bio/UniProt/GOA.py_gpi10iterator   s    r   c             c   s   x |  D] } | d d k r  q | j  d  j d  } t |  d k rM q | d j d  | d <| d j d  | d <| d	 j d  | d	 <| d
 j d  | d
 <t t t |   Vq Wd S)zRead GPI 1.0 format files (PRIVATE).

    This iterator is used to read a gp_information.goa_uniprot
    file which is in the GPI 1.0 format.
    r   r   r   r	   r
      r         r   N)r   r   r   r   r   GPI11FIELDS)r   r   r   r   r   r   _gpi11iterator   s    r   c             C   s~   |  j    } | j   d k r( t |   S| j   d k rD t |   S| j   d k re t d   n t d j |    d S)zRead GPI format files.

    This function should be called to read a
    gp_information.goa_uniprot file. At the moment, there is
    only one format, but this may change, so
    this function is a placeholder a future wrapper.
    z!gpi-version: 1.1z!gpi-version: 1.0z!gpi-version: 2.1z1Sorry, parsing GPI version 2 not implemented yet.zUnknown GPI version {0}
N)readlinestripr   r   NotImplementedError
ValueErrorformat)r   r   r   r   r   gpi_iterator   s    

r#   c             c   s   x |  D] } | d d k r  q | j  d  j d  } t |  d k rM q | d j d  | d <| d j d  | d <| d	 j d  | d	 <| d
 j d  | d
 <t t t |   Vq Wd S)zRead GPA 1.0 format files (PRIVATE).

    This iterator is used to read a gp_association.*
    file which is in the GPA 1.0 format. Do not call directly. Rather,
    use the gpaiterator function.
    r   r   r   r	   r
   r   r         
   N)r   r   r   r   r   GPA10FIELDS)r   r   r   r   r   r   _gpa10iterator   s    r(   c             c   s   x |  D] } | d d k r  q | j  d  j d  } t |  d k rM q | d j d  | d <| d j d  | d <| d	 j d  | d	 <| d
 j d  | d
 <t t t |   Vq Wd S)zRead GPA 1.1 format files (PRIVATE).

    This iterator is used to read a gp_association.goa_uniprot
    file which is in the GPA 1.1 format. Do not call directly. Rather
    use the gpa_iterator function
    r   r   r   r	   r
   r   r   r$   r%   r&   N)r   r   r   r   r   GPA11FIELDS)r   r   r   r   r   r   _gpa11iterator   s    r*   c             C   s]   |  j    } | j   d k r( t |   S| j   d k rD t |   St d j |    d S)zRead GPA format files.

    This function should be called to read a
    gene_association.goa_uniprot file. Reads the first record and
    returns a gpa 1.1 or a gpa 1.0 iterator as needed
    z!gpa-version: 1.1z!gpa-version: 1.0zUnknown GPA version {0}
N)r   r   r*   r(   r!   r"   )r   r   r   r   r   gpa_iterator   s    

r+   c             c   s   x |  D] } | d d k r  q | j  d  j d  } t |  d k rM q | d j d  | d <| d j d  | d <| d	 j d  | d	 <| d
 j d  | d
 <| d j d  | d <t t t |   Vq Wd  S)Nr   r   r   r	   r
   r   r   r   r   r&      )r   r   r   r   r   GAF20FIELDS)r   r   r   r   r   r   _gaf20iterator   s    r.   c             c   s   x |  D] } | d d k r  q | j  d  j d  } t |  d k rM q | d j d  | d <| d j d  | d <| d	 j d  | d	 <| d
 j d  | d
 <| d j d  | d <t t t |   Vq Wd  S)Nr   r   r   r	   r
   r   r   r   r   r&   r,   )r   r   r   r   r   GAF10FIELDS)r   r   r   r   r   r   _gaf10iterator
  s    r0   c             c   s@  d  } g  } x-|  D]%} | d d k r, q | j  d  j d  } t |  d k rY q | d j d  | d <| d j d  | d <| d	 j d  | d	 <| d
 j d  | d
 <| d j d  | d <t t t |   } | d | k r!| r!t j |  } | g } | d } | Vq | d } | j |  q Wd  S)Nr   r   r   r	   r
   r   r   r   r   r&   r,   r   )r   r   r   r   r   r/   copyappend)r   cur_idid_rec_listr   r   cur_recret_listr   r   r   _gaf10byproteiniterator  s*    	

r7   c             c   s@  d  } g  } x-|  D]%} | d d k r, q | j  d  j d  } t |  d k rY q | d j d  | d <| d j d  | d <| d	 j d  | d	 <| d
 j d  | d
 <| d j d  | d <t t t |   } | d | k r!| r!t j |  } | g } | d } | Vq | d } | j |  q Wd  S)Nr   r   r   r	   r
   r   r   r   r   r&   r,   r   )r   r   r   r   r   r-   r1   r2   )r   r3   r4   r   r   r5   r6   r   r   r   _gaf20byproteiniterator2  s*    	

r8   c             C   sy   |  j    } | j   d k r( t |   S| j   d k rD t |   S| j   d k r` t |   St d j |    d S)a  Iterate over records in a gene association file.

    Returns a list of all consecutive records with the same DB_Object_ID
    This function should be called to read a
    gene_association.goa_uniprot file. Reads the first record and
    returns a gaf 2.0 or a gaf 1.0 iterator as needed
    2016-04-09: added GAF 2.1 iterator & fixed bug in iterator assignment
    In the meantime GAF 2.1 uses the GAF 2.0 iterator
    z!gaf-version: 2.0z!gaf-version: 1.0z!gaf-version: 2.1zUnknown GAF version {0}
N)r   r   r8   r7   r!   r"   )r   r   r   r   r   gafbyproteiniteratorK  s    



r9   c             C   sy   |  j    } | j   d k r( t |   S| j   d k rD t |   S| j   d k r` t |   St d j |    d S)a  Iterate over a GAF 1.0 or 2.0 file.

    This function should be called to read a
    gene_association.goa_uniprot file. Reads the first record and
    returns a gaf 2.0 or a gaf 1.0 iterator as needed

    Example: open, read, interat and filter results.

    Original data file has been trimed to ~600 rows.

    Original source ftp://ftp.ebi.ac.uk/pub/databases/GO/goa/YEAST/goa_yeast.gaf.gz

    >>> from Bio.UniProt.GOA import gafiterator, record_has
    >>> Evidence = {'Evidence': set(['ND'])}
    >>> Synonym = {'Synonym': set(['YA19A_YEAST', 'YAL019W-A'])}
    >>> Taxon_ID = {'Taxon_ID': set(['taxon:559292'])}
    >>> with open('UniProt/goa_yeast.gaf', 'r') as handle:
    ...     for rec in gafiterator(handle):
    ...         if record_has(rec, Taxon_ID) and record_has(rec, Evidence) and record_has(rec, Synonym):
    ...             for key in ('DB_Object_Name', 'Evidence', 'Synonym', 'Taxon_ID'):
    ...                 print(rec[key])
    ...
    Putative uncharacterized protein YAL019W-A
    ND
    ['YA19A_YEAST', 'YAL019W-A']
    ['taxon:559292']
    Putative uncharacterized protein YAL019W-A
    ND
    ['YA19A_YEAST', 'YAL019W-A']
    ['taxon:559292']
    Putative uncharacterized protein YAL019W-A
    ND
    ['YA19A_YEAST', 'YAL019W-A']
    ['taxon:559292']

    z!gaf-version: 2.0z!gaf-version: 2.1z!gaf-version: 1.0zUnknown GAF version {0}
N)r   r   r.   r0   r!   r"   )r   r   r   r   r   gafiteratord  s    %


r:   c             C   s   d } xw | d d  D]e } t  |  | t  rj x  |  | D] } | | d 7} q; W| d d	  d } q | |  | d 7} q W| |  | d
 d 7} | j d |  d S)zWrite a single UniProt-GOA record to an output stream.

    Caller should know the  format version. Default: gaf-2.0
    If header has a value, then it is assumed this is the first record,
    a header is written.
     Nr
   r   r	   r   z%sr<   r<   )
isinstancelistwrite)outrecr   fieldsZoutstrfieldZsubfieldr   r   r   writerec  s    rC   c             C   s(   x! |  D] } t  | | d | q Wd S)aO  Write a list of GAF records to an output stream.

    Caller should know the  format version. Default: gaf-2.0
    If header has a value, then it is assumed this is the first record,
    a header is written. Typically the list is the one read by fafbyproteinrec, which
    contains all consecutive lines with the same DB_Object_ID
    rA   N)rC   )Z
outprotrecr   rA   r@   r   r   r   writebyproteinrec  s    rD   c             C   sc   d } xV | D]N } t  |  | t  r6 |  | h } n t |  |  } | | | @r d } Pq W| S)zAccept a record, and a dictionary of field values.

    The format is {'field_name': set([val1, val2])}.
    If any field in the record has  a matching value, the function returns
    True. Otherwise, returns False.
    FT)r=   strset)r   Z	fieldvalsretvalrB   Zset1r   r   r   
record_has  s    rH   __main__)run_doctestverbose)__doc__
__future__r   r1   Z	Bio._py3kr   r-   r/   r'   r)   r   r   r   r   r#   r(   r*   r+   r.   r0   r7   r8   r9   r:   rC   rD   rH   __name__Z
Bio._utilsrJ   r   r   r   r   <module>   s   						4