
:>"^i                 @   s"  d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z yP d e j d d
  k o d k n r d d l m Z n d d l m Z Wn" e k
 r d d l m Z Yn Xd Z d Z e j d d d  Z Gd d   d e  Z d S)zBio.SeqIO support for the "uniprot-xml" file format.

See Also:
http://www.uniprot.org

The UniProt XML format essentially replaces the old plain text file format
originally introduced by SwissProt ("swiss" format in Bio.SeqIO).

    N)Seq)
SeqFeature)Alphabet)	SeqRecord)	as_handle)StringIO         )ElementTree)cElementTreez{http://uniprot.org/uniprot}z4%(name)s %(volume)s:%(first)s-%(last)s(%(pub_date)s)Fc             c   s  t  |  d  w}  |  j   d k r0 t d   t | t j  rR t d |   t | t j  r t | j t j  r t d |   t |  d  s t |  t	  r d d l
 } d d l m } | j d	 |  t |   }  n t d
   t d k rd d l m } | d   xi t j |  d d D]R \ } } | d k r,| j t d k r,t | d | d | j   V| j   q,WWd QRXd S)a  Iterate over UniProt XML as SeqRecord objects.

    parses an XML entry at a time from any UniProt XML file
    returns a SeqRecord for each iteration

    This generator can be used in Bio.SeqIO

    return_raw_comments = True --> comment fields are returned as complete XML to allow further processing
    skip_parsing_errors = True --> if parsing errors are found, skip to next entry
    rU zEmpty file.zWrong alphabet %rreadr   N)BiopythonDeprecationWarningz/Passing an XML-containing handle is recommendedzMRequires an XML-containing handle (or XML as a string, but that's deprecated))MissingExternalDependencyErrorzmNo ElementTree module was found. Use Python 2.5+, lxml or elementtree if you want to use Bio.SeqIO.UniprotIO.eventsstartendentryalphabetreturn_raw_comments)zstartzend)r   readline
ValueError
isinstancer   ZNucleotideAlphabetZGappedr   hasattrstrwarningsBior   warnr   	TypeErrorr   r   	iterparsetagNSParserparseclear)handler   r   r   r   r   eventelem r*   8/tmp/pip-build-ww9dw3qa/biopython/Bio/SeqIO/UniprotIO.pyUniprotIterator,   s6    		"r,   c               @   s7   e  Z d  Z d Z e j d d d  Z d d   Z d S)r$   zParse a UniProt XML entry to a SeqRecord.

    return_raw_comments=True to get back the complete comment field in XML format
    alphabet=Alphabet.ProteinAlphabet()    can be modified if needed, default is protein alphabet.
    Fc             C   s   | |  _  | |  _ | |  _ d S)zInitialize the class.N)r   r   r   )selfr)   r   r   r*   r*   r+   __init__m   s    		zParser.__init__c                s'   j  j t d k s t   f d d     f d d   }   f d d   }   f d d	   }   f d
 d   }  f d d   }   f d d   }  f d d   }  f d d   }   f d d   }	  f d d   }
   f d d   } d d d       f d d   }  f d d    }  f d! d"   }  f d# d$   } t d% d& d%  _  j  j j d' d(   _ xU  j  j j	   D]A \ } } | d) k rt
 |   j j | <q|  j j | <qWx j  D]} | j t d* k r| |  q| j t d+ k r8| |  q| j t d, k rX| |  q| j t d- k rx| |  q| j t d. k r| |  q| j t d/ k r| |  q| j t d0 k r| |  q| j t d1 k r| |  q| j t d2 k r|	 |  q| j t d3 k r8|
 |  q| j t d4 k rX| |  q| j t d5 k rx| |  q| j t d6 k r| |  q| j t d7 k r| |  q| j t d8 k r| |  qqWt t  j j    j _  j j s  j j d9 d  j _  j S):zParse the input.r   c                sS   |    j  j k r" g    j  j |  <|   j  j |  k rO   j  j |  j |  d  S)N)ParsedSeqRecordannotationsappend)keyvalue)r-   r*   r+   append_to_annotationsy   s    z+Parser.parse.<locals>.append_to_annotationsc                s4   |  j    j _   j j j   j d |  j   d  S)N:)textr/   namedbxrefsr1   dbname)element)r-   r*   r+   _parse_name   s    z!Parser.parse.<locals>._parse_namec                s5     d |  j    j j j  j d |  j   d  S)N
accessionsr5   )r6   r/   r8   r1   r9   )r:   )r4   r-   r*   r+   _parse_accession   s    z&Parser.parse.<locals>._parse_accessionc                s   d } x |  D] } | j  t d t d t d g k r x | D]s } d | j  j t d  | j  j t d  f }   | | j  | j  t d k r> | r> | j  j _ d } q> Wq | j  t d	 k r q | j  t d
 k r q Wd S)zParse protein names (PRIVATE).FZrecommendedNameZsubmittedNameZalternativeNamez%s_%sr   ZfullNameT	componentdomainN)r"   r#   replacer6   r/   description)r:   Z	descr_setZprotein_elementZrec_nameann_key)r4   r-   r*   r+   _parse_protein   s$    z$Parser.parse.<locals>._parse_proteinc                s   x| |  D]t } d | j  k r d | j j t d  | j  d f } | j  d d k rk | j  j j | <q   | | j  q Wd  S)Ntypez
gene_%s_%sr   Zprimary)attribr"   r@   r#   r6   r/   r0   )r:   Zgenename_elementrB   )r4   r-   r*   r+   _parse_gene   s    z!Parser.parse.<locals>._parse_genec                s     d |  j  d  d  S)NgeneLocationrD   )rE   )r:   )r4   r*   r+   _parse_geneLocation   s    z)Parser.parse.<locals>._parse_geneLocationc                s`  d } } } x|  D] } | j  t d k r | j r| j d d k rV | j } q| j d d k ru | j } q  d | j  q | j  t d k r  j j j | j d d | j d	  q | j  t d
 k r x1 | D]) } | j  t d k r   d | j  q Wq W| r1| r1d | | f } n | r@| } n | rL| } |  j j d <d  S)Nr   r7   rD   Z
scientificcommonorganism_namedbReferencer5   idZlineageZtaxonZtaxonomyz%s (%s)organism)r"   r#   r6   rE   r/   r8   r1   r0   )r:   rJ   Zcom_nameZsci_nameorganism_elementZtaxon_element)r4   r-   r*   r+   _parse_organism   s0    	 	z%Parser.parse.<locals>._parse_organismc                s8   x1 |  D]) } | j  t d k r   d | j  q Wd  S)Nr7   Zorganism_host)r"   r#   r6   )r:   rN   )r4   r*   r+   _parse_organismHost   s    z)Parser.parse.<locals>._parse_organismHostc                s     d |  j   d  S)Nkeywords)r6   )r:   )r4   r*   r+   _parse_keyword   s    z$Parser.parse.<locals>._parse_keywordc                s  d d d d d d d d d	 d
 d d d d d d d d d d d d d g } |  j  d | k r d |  j  d j d d  } x|  j t d  D] } | j r   | | j  q Wn|  j  d d k rEx|  j t d  D]b } xY | D]Q } | j r d |  j  d j d d  | j j t d  f }   | | j  q Wq Wn|  j  d d  k rx|  j t d!  D]+ } d" |  j  d }   | | j  d#  qlWn|  j  d d$ k rx|  j t d%  D]Q } d& |  j  d j d d  } x+ | j t d'  D] }   | | j  qWqWn/|  j  d d( k rd |  j  d j d d  } d) }	 }
 x |  j t d*  D] } t | j t d+   } y | rt | d) j  d+  }
 |
 d, }	 n^ t t | j t d-   d) j  d+  }	 |	 d, 8}	 t t | j t d.   d) j  d+  }
 Wqkt t	 f k
 r0YqkXqkW|  j  d/ } |  j  d0 } |	 |
 k ofd) k n r  | d1 | | f  qL  | d2 |	 |
 | | f  n |  j  d d3 k rn |  j  d d4 k rLx{ |  j t d5  D]f } d |  j  d j d d  } x@ | j t d5  D]+ }   | d6 |  j  d7 | j  d8 f  qWqW j
 rd9 |  j  d j d d  }   | t j |    d: S);ac  Parse comments (PRIVATE).

            Comment fields are very heterogeneus. each type has his own (frequently mutated) schema.
            To store all the contained data, more complex data structures are needed, such as
            annotated dictionaries. This is left to end user, by optionally setting:

            return_raw_comments=True

            The original XML is returned in the annotation fields.

            Available comment types at december 2009:
             - "allergen"
             - "alternative products"
             - "biotechnology"
             - "biophysicochemical properties"
             - "catalytic activity"
             - "caution"
             - "cofactor"
             - "developmental stage"
             - "disease"
             - "domain"
             - "disruption phenotype"
             - "enzyme regulation"
             - "function"
             - "induction"
             - "miscellaneous"
             - "pathway"
             - "pharmaceutical"
             - "polymorphism"
             - "PTM"
             - "RNA editing"
             - "similarity"
             - "subcellular location"
             - "sequence caution"
             - "subunit"
             - "tissue specificity"
             - "toxic dose"
             - "online information"
             - "mass spectrometry"
             - "interaction"

            ZallergenZbiotechnologyzbiophysicochemical propertieszcatalytic activityZcautionZcofactorzdevelopmental stageZdiseaser?   zdisruption phenotypezenzyme regulationfunctionZ	inductionZmiscellaneousZpathwayZpharmaceuticalZpolymorphismZPTMzRNA editingZ
similarityZsubunitztissue specificityz
toxic doserD   z
comment_%s r   r6   zsubcellular locationZsubcellularLocationzcomment_%s_%sZinteractionZinteractantzcomment_%s_intactIdZintactIdzalternative productsZisoformzcomment_%s_isoformrL   zmass spectrometryr   locationpositionr
   beginr   massmethodzundefined:%s|%sz%s..%s:%s|%szsequence cautionzonline informationlinkz%s@%sr7   urizcomment_%s_xmlN)rE   r@   getiteratorr#   r6   r"   listintr   KeyErrorr   r   tostring)r:   Zsimple_commentsrB   Ztext_elementZsubloc_elementelZinteract_elementZalt_elementZ
id_elementr   r   Zpos_elsrX   rY   Zlink_element)r4   r-   r*   r+   _parse_comment   s    ,			
'
+	$	z$Parser.parse.<locals>._parse_commentc                s    j  j j |  j d d |  j d  d |  j k r|  j d d k rd } d } xo|  D]g} | j t d k r^ | j d } | d k r | j d } | d	 k r | j d } | d
 k r^ | j d j d  } x | D] } | j   j d  } | d d k r t j   } |  j d | _	 |  j d | j
 d <| | j
 d <| | j
 d	 <| d j d  | j
 d
 <t | d j d  d  d }	 t | d j d  d  }
 t j |	 |
  | _ q Wq^ Wx! |  D] } | j t d k rqWd  S)NrD   r5   rL   ZPDBr   propertyrY   r3   
resolutionZchains,=r
   -r7   r   /)r/   r8   r1   rE   r"   r#   splitstripr   rD   
qualifiersr^   FeatureLocationrU   )r:   rY   rd   ref_elementZdat_typepairsr)   pairfeaturer   r   )r-   r*   r+   _parse_dbReferencef  sB    
!z(Parser.parse.<locals>._parse_dbReferencec                s8  t  j   } g  } g  } g  } d } d } d } x"|  D]} | j t d k r| j d } | d k r~ | d | j d 7} d | j k r | j d } | j j d d  } | j j d	 d  }	 | j j d
 d  }
 | j j d d  } x`| D] } | j t d k r| j | _ q | j t d k rXx | D] } | j | j d  q7Wq | j t d k r  j	 j
 j | j d d | j d  | j d d k r| j d | _ q | j d d k r | j d | _ q Wq7 | j t d k r
| j | j  q7 | j t d k r7 x1 | D]) } | j t d k r$| j | j  q$Wq7 W| rqd d j |  } n d } | rd d j |  } n d } g  | _ d j |  | _ | r	| r |	 r |
 r | r t d | d	 |	 d
 |
 d | d | i | _ n	 | | _ d j | | | | f  | _   d |  d  S)Nr   ZcitationrD   Z
submissionz to the dbr7   datevolumefirstlasttitleZ
authorListrK   r5   rL   ZPubMedZMEDLINEZscopesourceZtissuezScope: z, zTissue: pub_datez | Z
references)r   Z	Referencer"   r#   rE   getr6   rw   r1   r/   r8   Z	pubmed_idZ
medline_idjoinrU   authorsREFERENCE_JOURNALZjournalcomment)r:   	referencer|   ZscopesZtissuesZjournal_nameZpub_typery   rm   Zj_volumeZj_firstZj_lastZcit_elementZperson_elementZsource_elementZ
scopes_strZtissues_str)r4   r-   r*   r+   _parse_reference  sr    		z&Parser.parse.<locals>._parse_referencer   c             S   s   y t  |  j d  | } Wn+ t k
 rH } z d  } WYd  d  } ~ Xn X|  j j d d  } | d k r | d  k s| t  t j   S| s t j |  S| d k r t j |  S| d k r t j	 |  S| d k r t j
 |  St d |   d  S)	NrV   statusr   unknownzgreater thanz	less thanZ	uncertainzPosition status %r)r^   rE   r_   rz   AssertionErrorr   ZUnknownPositionZExactPositionZAfterPositionZBeforePositionZUncertainPositionNotImplementedError)r:   offsetrV   errr   r*   r*   r+   _parse_position  s"    
z%Parser.parse.<locals>._parse_positionc                s  t  j    } x* |  j j   D] \ } } | | j | <q W|  j j d d  | _ d |  j k rp |  j d | _ x |  D] } | j t d k r2| j	 t d  } | r | d }    |  d
  }   |   } nI | j	 t d  d }    |  d  } | j	 t d	  d }    |   } t  j
 | |  | _ qw y# | j | j | j j t d  <Wqw t k
 riYqw Xqw W j j j |  d  S)NrD   r   rL   rU   rV   r   r
   rW   r   r   )r   rE   itemsrk   rz   rD   rL   r"   r#   findallrl   rU   r6   r@   	Exceptionr/   featuresr1   )r:   rp   kvZfeature_elementZposition_elementsZstart_positionZend_position)r   r-   r*   r+   _parse_feature  s0    
#	z$Parser.parse.<locals>._parse_featurec                s     d |  j  d  d  S)NproteinExistencerD   )rE   )r:   )r4   r*   r+   _parse_proteinExistence  s    z-Parser.parse.<locals>._parse_proteinExistencec                s7   x0 |  j  j   D] \ } } | }   | |  q Wd  S)N)rE   r   )r:   r   r   rB   )r4   r*   r+   _parse_evidence  s    z%Parser.parse.<locals>._parse_evidencec                s   xZ |  j  j   D]I \ } } | d k rE t |    j j d | <q |   j j d | <q Wd j |  j j    } t j |   j	    j _
 d  S)NlengthrX   versionzsequence_%sr   )zlengthzmasszversion)rE   r   r^   r/   r0   r{   r6   ri   r   r   seq)r:   r   r   r   )r-   r*   r+   _parse_sequence  s    z%Parser.parse.<locals>._parse_sequencer   rL   ZdatasetZUnknownDatasetr   r7   Z	accessionZproteinZgenerG   rM   ZorganismHostkeywordr~   rK   r   rp   r   Zevidencesequencer<   )r   r"   r#   r   r   r/   rE   rz   r9   r   r^   r0   sortedsetr8   rL   )r-   r;   r=   rC   rF   rH   rO   rP   rR   rb   rq   r   r   r   r   r   r   r   r:   r*   )r   r4   r-   r+   r%   u   sx     0GzParser.parseN)__name__
__module____qualname____doc__r   generic_proteinr.   r%   r*   r*   r*   r+   r$   f   s   r$   )r   r   )r   r
   )r   sysr   r   r   r   ZBio.SeqRecordr   ZBio.Filer   Z	Bio._py3kr   version_infoZ	xml.etreer   r   ImportErrorr#   r}   r   r,   objectr$   r*   r*   r*   r+   <module>   s"   )9