
:>"^5                 @   s  d  Z  d d l Z d d l m Z d d l m Z d d l m Z y\ d d f e j d d  k ou d d f 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 d i Z y e
 j Z WnJ e k
 r(e e
 d  rd d	 l	 m
 Z e j e
 _ d d   Z Yn Xx' e j   D] \ Z Z e e e  q6We j d d k rld n d Z Gd d   d e  Z d d   Z d d   Z e d d d  Z d d   Z d d   Z e d d d   Z d! d"   Z e d# d$  Z  d% d&   Z! e d' d(  Z" d d) d*  Z# d+ d,   Z$ d- d.   Z% d/ d0   Z& d1 d2   Z' d3 d4   Z( d5 d6   Z) Gd7 d8   d8 e*  Z+ d9 d:   Z, d; d<   Z- d= d> d?  Z. d@ dA   Z/ GdB dC   dC e*  Z0 d S)Da  PhyloXML reader/parser, writer, and associated functions.

Instantiates tree elements from a parsed PhyloXML file, and constructs an XML
file from a ``Bio.Phylo.PhyloXML`` object.

About capitalization:
 - phyloXML means the file format specification
 - PhyloXML means the Biopython module ``Bio.Phylo.PhyloXML`` and its classes
 - Phyloxml means the top-level class used by ``PhyloXMLIO.read`` (but not
   ``Bio.Phylo.read``!), containing a list of Phylogenies (objects derived from
   ``BaseTree.Tree``)

    N)
basestring)unicode)PhyloXML         )ElementTree)cElementTreephyzhttp://www.phyloxml.org_namespace_mapc             C   s   |  t  j | <d S)z"Set the namespace for ElementTree.N)r   r   )prefixuri r   9/tmp/pip-build-ww9dw3qa/biopython/Bio/Phylo/PhyloXMLIO.pyregister_namespace4   s    r   r   zutf-8c               @   s   e  Z d  Z d Z d S)PhyloXMLErrorzException raised when PhyloXML object construction cannot continue.

    XML syntax errors will be found and raised by the underlying ElementTree
    module; this exception is for valid XML that breaks the phyloXML
    specification.
    N)__name__
__module____qualname____doc__r   r   r   r   r   @   s   r   c             C   s   t  |   j   S)zParse a phyloXML file or stream and build a tree of Biopython objects.

    The children of the root node are phylogenies and possibly other arbitrary
    (non-phyloXML) objects.

    :returns: a single ``Bio.Phylo.PhyloXML.Phyloxml`` object.

    )Parserread)filer   r   r   r   O   s    	r   c             C   s   t  |   j   S)a  Iterate over the phylogenetic trees in a phyloXML file.

    This ignores any additional data stored at the top level, but may be more
    memory-efficient than the ``read`` function.

    :returns: a generator of ``Bio.Phylo.PhyloXML.Phylogeny`` objects.

    )r   parse)r   r   r   r   r   [   s    	r   Tc                s   d d     t  |  t j  r! n| t  |  t j j t j j f  rW   |   j   }  nF t |  d  r t j i  d   f d d   |  D }  n t d   t	 |   j
 | d | d	 | S)
aR  Write a phyloXML file.

    :Parameters:
        obj
            an instance of ``Phyloxml``, ``Phylogeny`` or ``BaseTree.Tree``,
            or an iterable of either of the latter two. The object will be
            converted to a Phyloxml object before serialization.
        file
            either an open handle or a file name.

    c             S   s   t  |  t j  r |  St  |  t j  r2 |  j   St  |  t j j  rW t j j |   St  |  t j j  r t j j t j j d |    St d   d  S)Nrootz)iterable must contain Tree or Clade types)	
isinstancePX	PhylogenyCladeZto_phylogenyBaseTreeTreeZ	from_tree
ValueError)treer   r   r   
fix_singlet   s    
zwrite.<locals>.fix_single__iter__phylogeniesc             3   s   |  ] }   |  Vq d  S)Nr   ).0t)r#   r   r   	<genexpr>   s    zwrite.<locals>.<genexpr>zXFirst argument must be a Phyloxml, Phylogeny, Tree, or iterable of Trees or Phylogenies.encodingindent)r   r   Phyloxmlr   r    r   Zto_phyloxmlhasattrr!   Writerwrite)objr   r)   r*   r   )r#   r   r.   g   s    !+	r.   c             C   s/   |  d d k r+ |  |  j  d  d d  S|  S)z;Extract the local tag from a namespaced tag name (PRIVATE).r   {}r   N)index)tagr   r   r   _local   s    r4   c             C   sA   y |  d d  j  d d  SWn t k
 r< d |  f SYn Xd S)z;Split a tag into namespace and local tag strings (PRIVATE).r   Nr1    )splitr!   )r3   r   r   r   _split_namespace   s    r7   c             C   s   d | |  f S)z5Format an XML tag with the given namespace (PRIVATE).z{%s}%sr   )r3   	namespacer   r   r   _ns   s    r9   c             C   s/   |  j  t |   } | d k	 r+ | |  Sd S)z|Find a child node by tag, and pass it through a constructor (PRIVATE).

    Returns None if no matching child is found.
    N)findr9   )parentr3   	constructchildr   r   r   _get_child_as   s    r>   c             C   s;   |  j  t |   } | d k	 r7 | j r7 | | j  Sd S)z~Find a child node by tag; pass its text through a constructor (PRIVATE).

    Returns None if no matching child is found.
    N)r:   r9   text)r;   r3   r<   r=   r   r   r   _get_child_text   s    r@   c                s&     f d d   |  j  t |   D S)zFind child nodes by tag; pass each through a constructor (PRIVATE).

    Returns an empty list if no matching child is found.
    c                s   g  |  ] }   |   q Sr   r   )r&   r=   )r<   r   r   
<listcomp>   s   	 z$_get_children_as.<locals>.<listcomp>)findallr9   )r;   r3   r<   r   )r<   r   _get_children_as   s    rC   c                s&     f d d   |  j  t |   D S)zFind child nodes by tag; pass each node's text through a constructor (PRIVATE).

    Returns an empty list if no matching child is found.
    c                s(   g  |  ] } | j  r   | j    q Sr   )r?   )r&   r=   )r<   r   r   rA      s   	 z&_get_children_text.<locals>.<listcomp>)rB   r9   )r;   r3   r<   r   )r<   r   _get_children_text   s    rD   c             C   s   d | d } t  |   r |  j s4 |  j j   rA | d |  _ xF |  D]> } t | | d  | j sy | j j   rH | d | _ qH W| j s | j j   r | | _ n) | r |  j s |  j j   r | |  _ d S)zAdd line breaks and indentation to ElementTree in-place (PRIVATE).

    Sources:
     - http://effbot.org/zone/element-lib.htm#prettyprint
     - http://infix.se/2007/02/06/gentlemen-indent-your-xml

    
z  r   N)lenr?   strip_indenttail)elemlevelier   r   r   rH      s     rH   c             C   sL   |  d k s |  d k r d S|  d k s4 |  d k r8 d St  d |    d S)	z$Convert string to boolean (PRIVATE).true1Tfalse0Fz*String could not be converted to boolean: N)r!   )r?   r   r   r   	_str2bool   s
    rR   c             C   sA   |  j    } x. | D]& } | | k r t | |  | | <q W| S)zQReturn a new dictionary where string values are replaced with booleans (PRIVATE).)copyrR   )dctkeysoutkeyr   r   r   _dict_str2bool   s
    rX   c             C   s7   |  d k	 r3 y t  |   SWn t k
 r2 d SYn Xd S)z$Return text as an integer (PRIVATE).N)int	Exception)r?   r   r   r   _int   s
    r[   c             C   s7   |  d k	 r3 y t  |   SWn t k
 r2 d SYn Xd S)z!Return text as a float (PRIVATE).N)floatrZ   )r?   r   r   r   _float  s
    r]   c             C   s#   |  d k	 r d j  |  j    Sd S)a  Replace all spans of whitespace with a single space character (PRIVATE).

    Also remove leading and trailing whitespace. See "Collapse Whitespace
    Policy" in the phyloXML spec glossary:
    http://phyloxml.org/documentation/version_100/phyloxml.xsd.html#Glossary
    N )joinr6   )r?   r   r   r   _collapse_wspace
  s    r`   c             C   s3   x, d D]$ } | |  k r |  j  | d  }  q W|  S)zReplace tab, LF and CR characters with spaces, but don't collapse (PRIVATE).

    See "Replace Whitespace Policy" in the phyloXML spec glossary:
    http://phyloxml.org/documentation/version_100/phyloxml.xsd.html#Glossary
    	rE   r^   )ra   rE   rb   )replace)r?   charr   r   r   _replace_wspace  s    re   c               @   s  e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d d d g Z d d d d d d d d i Z	 e
 e  j e	 j    j d d d d g  Z d d   Z d d   Z d d   Z d  d!   Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z d. d/   Z d0 d1   Z d2 d3   Z d4 d5   Z d6 d7   Z d8 d9   Z d: d;   Z d< d=   Z d> d?   Z  d@ dA   Z! dB dC   Z" dD dE   Z# dF dG   Z$ dH S)Ir   a  Methods for parsing all phyloXML nodes from an XML stream.

    To minimize memory use, the tree of ElementTree parsing events is cleared
    after completing each phylogeny, clade, and top-level 'other' element.
    Elements below the clade level are kept in memory until parsing of the
    current clade is finished -- this shouldn't be a problem because clade is
    the only recursive element, and non-clade nodes below this level are of
    bounded size.
    c             C   sC   t  t j | d d  } t |  \ } } | |  _ | |  _ d S)zInitialize the class.eventsstartendN)zstartzend)iterr   	iterparsenextr   context)selfr   rl   eventr   r   r   r   __init__-  s    	zParser.__init__c       	      C   s  t  j d d   |  j j   D  } d } x |  j D] \ } } t | j  \ } } | d k r | t d k r | d 7} q5 | d k r |  j |  } | j	 j
 |  | d k r5 | t d k r5 | d 8} | d k r5 |  j | | |  } | j j
 |  |  j j   q5 W| S)	z<Parse the phyloXML file and create a single Phyloxml object.c             S   s%   i  |  ] \ } } | t  |   q Sr   )r4   )r&   rW   valr   r   r   
<dictcomp>7  s   	 zParser.read.<locals>.<dictcomp>r   rg   r
   r   	phylogenyrh   )r   r+   r   itemsrl   r7   r3   
NAMESPACES_parse_phylogenyr%   appendotherclear)	rm   phyloxmlZother_depthrn   rJ   r8   localtagrr   otrr   r   r   r   5  s$    %

zParser.readc             c   sS   t  d  } x@ |  j D]5 \ } } | d k r | j | k r |  j |  Vq Wd S)z@Parse the phyloXML file incrementally and return each phylogeny.rr   rg   N)r9   rl   r3   ru   )rm   Zphytagrn   rJ   r   r   r   r   L  s    zParser.parsec             C   s  t  j t | j d d g    } d d d d d d d	 d
 i } xq|  j D]f\ } } t | j  \ } } | d k r | d k r | j d k	 r t d   |  j	 |  | _ qI | d k rI | d k r | j
   P| | k rt | | |  j t |  |  |   qI | d k r;t | | t |  |  |   qI | d k rct | | t | j   qI | t d k r| j j |  j | | |   | j
   qI t d |   qI W| S)a  Parse a single phylogeny within the phyloXML tree (PRIVATE).

        Recursively builds a phylogenetic tree with help from parse_clade, then
        clears the XML event history for the phylogeny element and returns
        control to the top-level parsing function.
        rooted
rerootable
confidenceconfidencesproperty
propertiesclade_relationclade_relationssequence_relationsequence_relationsrg   cladeNz)Phylogeny object should only have 1 claderh   rr   dateidnamedescriptionr
   zMisidentified tag: )zdatezid)znamezdescription)r   r   rX   attribrl   r7   r3   r   r!   _parse_claderx   getattrrv   setattrr`   r?   rt   rw   r   )rm   r;   rr   Z
list_typesrn   rJ   r8   r3   r   r   r   ru   U  s8    
,"zParser._parse_phylogenycolorrf   binary_charactersr   r~   r   distributiondistributions	reference
referencesr   r   branch_lengthr   node_idwidthc             C   s  t  j | j   } | j d k	 r3 t | j  | _ g  } x~|  j D]s\ } } t | j  \ } } | d k r| d k r | j j	 |  j
 |   qC | d k r | j j	 |  j |   qC | d k r | j j	 |  j |   qC | |  j k r| j	 |  | d k rC | d k r'| j   P| | d k r:qC | j   | |  j k rt | |  j |  j	 t |  |  |   qC | |  j k rt | | t |  |  |   qC | d k r| j d k	 rt d	   t | j  | _ qC | d
 k rt | j  | _ qC | d k r1t | j  | _ qC | d k rjt  j | j j   | j j d   | _ qC | t  d k r| j! j	 |  j! | | |   | j   qC t d |   qC W| S)z;Parse a Clade node and its children, recursively (PRIVATE).Nrg   r   taxonomysequencerh   r   r   z7Attribute branch_length was already set for this Clade.r   r   r   providerr
   zMisidentified tag: )"r   r   r   r   r\   rl   r7   r3   cladesrv   r   
taxonomies_parse_taxonomy	sequences_parse_sequence_clade_tracked_tagsrx   pop_clade_list_typesr   _clade_complex_typesr   r   r]   r?   r   r`   r   IdrG   getr   rt   rw   )rm   r;   r   Z	tag_stackrn   rJ   r8   r3   r   r   r   r     s^    

"	'zParser._parse_cladec             C   s:  t  j | j   } x!|  j D]\ } } t | j  \ } } | d k r | d k r` | j   P| d k r t | | t |  |  |   q | d k r | j	 j
 |  j |   q | d k r t | j  | _ q | d k r t | | | j  q | t d k r | j j
 |  j | | |   | j   q W| S)z%Parse a molecular sequence (PRIVATE).rh   r   	accessionmol_seqr   domain_architecture
annotationr   symbollocationr
   )z	accessionzmol_seqzurizdomain_architecture)zsymbolzlocation)r   Sequencer   rl   r7   r3   rx   r   r   annotationsrv   r   r`   r?   r   rt   rw   )rm   r;   r   rn   rJ   r8   r3   r   r   r   r     s&    
"zParser._parse_sequencec             C   s;  t  j | j   } x"|  j D]\ } } t | j  \ } } | d k r | d k r` | j   P| d k r t | | t |  |  |   q | d k r | j	 j
 t | j   q | d k r | j j
 | j  q | d k r t | | | j  q | t d k r | j j
 |  j | | |   | j   q W| S)z2Parse taxonomic information for a clade (PRIVATE).rh   r   r   r   common_namesynonymcodescientific_name	authorityrankr
   )zidzuri)zcodezscientific_namez	authorityzrank)r   ZTaxonomyr   rl   r7   r3   rx   r   r   common_namesrv   r`   r?   synonymsrt   rw   )rm   r;   r   rn   rJ   r8   r3   r   r   r   r     s&    
"zParser._parse_taxonomyc          
      sM   t  j | | | j d | j r- | j j   p0 d d   f d d   | D S)z/Create an Other object, a non-phyloXML element.valueNchildrenc                s+   g  |  ]! }   j  | t | j    q Sr   )rw   r7   r3   )r&   r=   )rm   r   r   rA     s   	 z Parser.other.<locals>.<listcomp>)r   ZOtherr   r?   rG   )rm   rJ   r8   rz   r   )rm   r   rw     s    	zParser.otherc             C   s"   t  j | j j   | j d   S)zCreate accession object.source)r   Z	Accessionr?   rG   r   )rm   rJ   r   r   r   r     s    zParser.accessionc             C   sd   t  j d t t | d   d t | d |  j  d t | d |  j  d t | d |  j  | j	  S)zCreate annotation object.descr~   r   r   r   )
r   Z
Annotationr`   r@   r>   r~   rC   r   r   r   )rm   rJ   r   r   r   r     s    	zParser.annotationc             C   s   d d   } t  j d | j d  d t | j d   d t | j d   d t | j d   d t | j d   d t | d |  d	 t | d	 |  d
 t | d
 |  d t | d |   	S)z Create binary characters object.c             S   s   t  |  d  S)z$Get binary characters from subnodes.bc)rD   )rJ   r   r   r   	bc_getter  s    z+Parser.binary_characters.<locals>.bc_gettertypegained_count
lost_countpresent_countabsent_countgainedlostpresentabsent)r   ZBinaryCharactersr   r[   r>   )rm   rJ   r   r   r   r   r     s    	zParser.binary_charactersc             C   sR   t  j | j d  | j d  | j d  d | j d  d t | d |  j  S)z!Create clade relationship object.r   id_ref_0id_ref_1distancer~   )r   ZCladeRelationr   r>   r~   )rm   rJ   r   r   r   r   &  s    zParser.clade_relationc                s5     f d d   d D \ } } } t  j | | |  S)zCreate branch color object.c             3   s!   |  ] } t    | t  Vq d  S)N)r@   rY   )r&   r   )rJ   r   r   r(   3  s    zParser.color.<locals>.<genexpr>redgreenblue)zredzgreenzblue)r   ZBranchColor)rm   rJ   r   r   r   r   )rJ   r   r   0  s    "zParser.colorc             C   s"   t  j t | j  | j d   S)zCreate confidence object.r   )r   Z
Confidencer]   r?   r   )rm   rJ   r   r   r   r~   7  s    zParser.confidencec             C   sd   t  j d | j d  d t t | d   d t | d t  d t | d t  d t | d t   S)zCreate date object.unitr   r   minimummaximum)r   Dater   r`   r@   r\   )rm   rJ   r   r   r   r   ;  s    	zParser.datec          
   C   sI   t  j d t t | d   d t | d |  j  d t | d |  j   S)z&Create geographic distribution object.r   pointspointpolygonspolygon)r   Distributionr`   r@   rC   r   r   )rm   rJ   r   r   r   r   E  s    	zParser.distributionc          	   C   sb   t  j | j j   t | j d   d t | j d   d t | j d   d | j d  S)zCreate protein domain object.fromr   tor~   r   )r   ZProteinDomainr?   rG   rY   r   r]   )rm   rJ   r   r   r   domainM  s    zParser.domainc             C   s4   t  j d t | j d   d t | d |  j   S)z"Create domain architecture object.lengthdomainsr   )r   ZDomainArchitecturerY   r   rC   r   )rm   rJ   r   r   r   r   W  s    	zParser.domain_architecturec             C   sd   t  j d t | d  d t | d t  d t | d t  d t | d t  d t | d |  j   S)zCreate events object.r   duplicationsspeciationslossesr~   )r   ZEventsr@   rY   r>   r~   )rm   rJ   r   r   r   rf   ^  s    	zParser.eventsc             C   s7   | j  d  p | j  d  } t j | j j   |  S)zCreate identifier object.r   r   )r   r   r   r?   rG   )rm   rJ   r   r   r   r   r   h  s    z	Parser.idc             C   sC   | j  d  } | d k	 r' t |  } t j | j j   d | S)z!Create molecular sequence object.
is_alignedN)r   rR   r   ZMolSeqr?   rG   )rm   rJ   r   r   r   r   r   m  s    zParser.mol_seqc          	   C   sU   t  j | j d  t | d t  t | d t  d t | d t  d | j d  S)z,Create point object, coordinates of a point.geodetic_datumlatlongaltalt_unit)r   ZPointr   r@   r\   )rm   rJ   r   r   r   r   t  s    zParser.pointc             C   s   t  j d t | d |  j   S)z&Create polygon object, list of points.r   r   )r   ZPolygonrC   r   )rm   rJ   r   r   r   r   ~  s    zParser.polygonc          
   C   sX   t  j | j j   | j d  | j d  | j d  d | j d  d | j d  S)z*Create properties from external resources.ref
applies_todatatyper   id_ref)r   ZPropertyr?   rG   r   )rm   rJ   r   r   r   r     s    zParser.propertyc             C   s(   t  j d | j d  d t | d   S)z#Create literature reference object.doir   )r   Z	Referencer   r@   )rm   rJ   r   r   r   r     s    zParser.referencec             C   sX   t  j | j d  | j d  | j d  d t | j d   d t | d |  j  S)zHCreate sequence relationship object, relationship between two sequences.r   r   r   r   r~   )r   ZSequenceRelationr   r]   r>   r~   )rm   rJ   r   r   r   r     s    zParser.sequence_relationc             C   s:   t  j | j j   d t | j d   d | j d  S)z(Create uri object, expected to be a url.r   r   )r   ZUrir?   rG   r`   r   )rm   rJ   r   r   r   r     s    z
Parser.uriN)%r   r   r   r   ro   r   r   ru   r   r   setunionrU   r   r   r   r   rw   r   r   r   r   r   r~   r   r   r   r   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   "  sH   		.9






r   c             C   sH   t  |  t  r t |   j   St  |  t  r> t |   j   St |   S)zMConvert a Python primitive to a phyloXML-compatible Unicode string (PRIVATE).)r   r\   r   upperboollower)r   r   r   r   
_serialize  s
    r   c             C   sF   i  } x9 | D]1 } t  |  |  } | d k	 r t |  | | <q W| S)zNCreate a dictionary from an object's specified, non-None attributes (PRIVATE).N)r   r   )r/   attrsrV   rW   rp   r   r   r   _clean_attrib  s    r   Fc                s,        f d d   } d  | _  | S)z2Handle to serialize nodes with subnodes (PRIVATE).c                s   t  j  t |     } x  D] } t | t  rt t | |  d k	 r | j t |  |  t | |    q" | \ } } x3 t | |  D]" } | j t |  |  |   q Wq" W r t | j  | _	 | S)z$Wrap nodes and subnodes as elements.N)
r   Elementr   r   r   r   rv   r   r   r?   )rm   r/   rJ   subnmethodpluralitem)attribshas_textsubnodesr3   r   r   wrapped  s    ($z _handle_complex.<locals>.wrappedz*Serialize a %s and its subnodes, in order.)r   )r3   r   r   r   r   r   )r   r   r   r3   r   _handle_complex  s    r   c                s#     f d d   } d   | _  | S)z+Handle to serialize simple nodes (PRIVATE).c                s"   t  j    } t |  | _ | S)zWrap node as element.)r   r   r   r?   )rm   r/   rJ   )r3   r   r   r     s    z_handle_simple.<locals>.wrappedzSerialize a simple %s node.)r   )r3   r   r   )r3   r   _handle_simple  s    r   c               @   sR  e  Z d  Z d Z d d   Z e d d d  Z d d   Z d	 d
   Z e	 d dh dn  Z
 e	 d do dx  Z e	 d. dy f  d0 d Z e	 d1 dz d|  Z d6 d7   Z e	 d d} d~  Z e	 d! f  d  Z e	 d d f  d0 d Z e	 d d d  Z e	 d) f  d  Z dF dG   Z e	 dH d d  Z e	 d' f  d  Z e	 d d f  d0 d Z e	 dP d f  d0 d Z e	 d" d f  d0 d Z e	 dB d d  Z e	 dD f  d  Z e	 d d f  d0 d Z e	 d+ d d  Z e	 d% d d  Z e	 d d d  Z e	 d# d d  Z  e	 d5 d f  d0 d Z! e" dV  Z# e" d  Z$ e" dT  Z% e" dU  Z& e" dA  Z' e" d@  Z( e" d?  Z) e" d   Z* e" d=  Z+ e" dL  Z, e" d<  Z- e" dN  Z. e" d;  Z/ e" dM  Z0 e" df  Z1 e" d^  Z2 e" da  Z3 e" d4  Z4 e" d  Z5 e" d\  Z6 e" d  Z7 e" de  Z8 e" d_  Z9 e" d[  Z: e" dc  Z; e" d  Z< dg S)r-   z1Methods for serializing a PhyloXML object to XML.c             C   s=   t  | t j  s t d   t j |  j |   |  _ d S)z,Build an ElementTree from a PhyloXML object.zNot a Phyloxml objectN)r   r   r+   AssertionErrorr   ry   _tree)rm   ry   r   r   r   ro     s    zWriter.__init__Tc             C   s?   | r t  |  j j    |  j j | |  t |  j j    S)zWrite PhyloXML to a file.)rH   r   getrootr.   rF   )rm   r   r)   r*   r   r   r   r.     s    zWriter.writec             C   sm   t  j d | j  } x' | j D] } | j |  j |   q Wx' | j D] } | j |  j |   qI W| S)z"Convert phyloxml to Etree element.ry   )r   r   
attributesr%   rv   rr   rw   )rm   r/   rJ   r"   r{   r   r   r   ry     s    zWriter.phyloxmlc             C   s^   t  j t | j | j  | j  } | j | _ x' | j D] } | j	 |  j
 |   q: W| S)zConvert other to Etree element.)r   r   r9   r3   r8   r   r   r?   r   rv   rw   )rm   r/   rJ   r=   r   r   r   rw     s
    $zWriter.otherrr   r|   r}   branch_length_unitr   r   r   r   r   r~   r   r   r   r   r   r   r   r   rw   	id_sourcer   r   r   r   r   r   r   r   rf   r   r   r   r   r   r   r   r   r   r   r   evidencer   r   c          	   C   s|   t  j d t | d   } xZ d D]R } t  j |  } x- t | |  D] } | j |  j |   qG W| j |  q" W| S)z4Serialize a binary_characters node and its subnodes.r   r   r   r   r   r   r   r   r   r   )ztypezgained_countz
lost_countzpresent_countzabsent_count)zgainedzlostzpresentzabsent)r   r   r   r   rv   r   )rm   r/   rJ   r   Zsubelemtokenr   r   r   r   3  s    zWriter.binary_charactersr   r   r   r   r   r   r   r   r   r   r   r   r   r   c             C   s   t  j d d t | j d  d t | j  i  } | j d k	 r\ | j d t | j   | j d k	 r~ | j d | j  t | j	  | _
 | S)zSerialize a domain node.r   r   r   r   Nr~   r   )r   r   strrg   rh   r~   r   r   r   r   r?   )rm   r/   rJ   r   r   r   r   Q  s    .zWriter.domainr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   N)zrootedz
rerootabler   type
confidenceconfidenceszclade_relationzclade_relationszsequence_relationzsequence_relationsproperty
propertiesotherr  )
nameidzdescriptiondater  clader  r	  r
  r  )r   r  r  ztaxonomyz
taxonomieszsequencez	sequenceszdistributionzdistributionsz	referencez
referencesr  r  r  zcladesr  r  )r  zbranch_lengthr  zwidthzcolorznode_idr  r  zeventszbinary_charactersr  r  r  r  r  r  )source)refr  r  r  r  r  )descr  r  uri)id_ref_0id_ref_1distancer  )r  )zredzgreenzblue)r  )unit)r  zvaluezminimumzmaximumpointpointszpolygonzpolygons)r  r$  r'  )zlengthzdomainzdomains)r(  )r  zduplicationszspeciationszlossesr  )provider)z
is_aligned)r)  )zgeodetic_datumzalt_unit)zlatzlongzaltr%  r&  )r*  )r  r#  zdatatypez
applies_toid_ref)zdoi)r  )r  r+  r   z
annotationzannotationsr  r  )	zsymbolz	accessionr  zlocationzmol_seqr  r,  zdomain_architecturer-  )r   r!  r"  r  )r  )r   zcommon_namezcommon_nameszsynonymzsynonymsr  r  )	r  zcodezscientific_namez	authorityr.  r/  zrankr  r0  )r  r  )=r   r   r   r   ro   DEFAULT_ENCODINGr.   ry   rw   r   rr   r   r   r   r   r   r   r~   r   r   r   r   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-     s   		         	               			        		        	r-   )1r   sysZ	Bio._py3kr   r   Z	Bio.Phylor   r   version_infoZ	xml.etreer   r	   ImportErrorrt   r   AttributeErrorr,   ZET_pyr   rs   r   r   r1  rZ   r   r   r   r.   r4   r7   r9   r>   r@   rC   rD   rH   rR   rX   r[   r]   r`   re   objectr   r   r   r   r   r-   r   r   r   r   <module>   s\   5+

				 	
