
    XfT~                     x   d Z ddlmZ ddlmZ ddiZej        Ze                                D ]\  Z	Z
 ee	e
           dZ G d de          Zd	 Zd
 ZedfdZd Zd Zed         fdZd ZefdZd ZefdZd%dZd Zd Zd Zd Zd Zd Z G d d          Z d Z!d Z"d&d Z#d! Z$ G d" d#          Z%d$S )'a  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``)

    )ElementTree)PhyloXMLphyzhttp://www.phyloxml.orgunicodec                       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__     4lib/python3.11/site-packages/Bio/Phylo/PhyloXMLIO.pyr   r   '   s           r   r   c                 D    t          |                                           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files    r   r   r   4   s     $<<r   c                 D    t          |                                           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   parser   s    r   r   r   @   s     $<<r   Tc                    d t          | t          j                  rnt          | t          j        j        t          j        j        f          r |                                           } nBt          | d          r#t          j        i fd| D                       } nt          d          t          |           
                    |||          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                    t          | t          j                  r| S t          | t          j                  r|                                 S t          | t          j        j                  rt          j                            |           S t          | t          j        j                  r=t          j                            t          j                            |                     S t          d          )N)rootz)iterable must contain Tree or Clade types)	
isinstancePX	PhylogenyCladeto_phylogenyBaseTreeTree	from_tree
ValueError)trees    r   
fix_singlezwrite.<locals>.fix_singleY   s    dBL)) 	KdBH%% 	'$$&&&dBK,-- 	0<))$///dBK-.. 	J<))"+*:*:*:*E*EFFFHIIIr   __iter__c              3   .   K   | ]} |          V  d S Nr   ).0tr$   s     r   	<genexpr>zwrite.<locals>.<genexpr>j   s+      *F*FQ::a==*F*F*F*F*F*Fr   )phylogenieszXFirst argument must be a Phyloxml, Phylogeny, Tree, or iterable of Trees or Phylogenies.)encodingindent)r   r   Phyloxmlr   r    r   to_phyloxmlhasattrr"   Writerwrite)objr   r,   r-   r$   s       @r   r2   r2   L   s    
J 
J 
J #r{## 

	C"+*BK,=>	?	? 
joo))++	j	!	! 
k"*F*F*F*F#*F*F*FGGG9
 
 	
 #;;THVDDDr   c                 ^    | d         dk    r | |                      d          dz   d         S | S )z;Extract the local tag from a namespaced tag name (PRIVATE).r   {}   N)indextags    r   _localr;   w   s4    
1v}}399S>>A%''((Jr   c                 h    	 | dd                              dd          S # t          $ r d| fcY S w xY w)z;Split a tag into namespace and local tag strings (PRIVATE).r7   Nr6    )splitr"   r9   s    r   _split_namespacer?   ~   sN    122w}}S!$$$   Cys     11c                     d| d|  S )z5Format an XML tag with the given namespace (PRIVATE).r5   r6   r   )r:   	namespaces     r   _nsrB      s    "	""S"""r   c                 d    |                      t          |                    }| ||          S dS )z|Find a child node by tag, and pass it through a constructor (PRIVATE).

    Returns None if no matching child is found.
    N)findrB   parentr:   	constructchilds       r   _get_child_asrI      s9    
 KKC!!Ey r   c                     |                      t          |                    }||j        r ||j                  S dS dS )z~Find a child node by tag; pass its text through a constructor (PRIVATE).

    Returns None if no matching child is found.
    N)rD   rB   textrE   s       r   _get_child_textrL      sH    
 KKC!!EUZy$$$ r   c                 `    fd|                      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                 &    g | ]} |          S r   r   r(   rH   rG   s     r   
<listcomp>z$_get_children_as.<locals>.<listcomp>   s#    CCCIIeCCCr   findallrB   rF   r:   rG   s     `r   _get_children_asrT      s2    
 DCCC&..S*B*BCCCCr   c                 `    fd|                      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                 >    g | ]}|j         	 |j                   S r   rK   rO   s     r   rP   z&_get_children_text.<locals>.<listcomp>   s,    VVVe5:VIIej!!VVVr   rQ   rS   s     `r   _get_children_textrX      s2    
 WVVVv~~c#hh/G/GVVVVr   c                    d|dz  z   }t          |           r| j        r| j                                        s
|dz   | _        | D ]?}t          ||dz              |j        r|j                                        s
|dz   |_        @|j        r|j                                        s	||_        dS dS |r)| j        r| j                                        s|| _        dS dS 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  r7   N)lenrK   strip_indenttail)elemlevelies       r   r]   r]      s    	ut|A
4yy y 	!	 1 1 	!DDI 	" 	"AAuqy!!!6 " "Tv 	QV\\^^ 	AFFF	 	  	$) 	49??+<+< 	DIII	 	 	 	r   c                 ^    | dk    s| dk    rdS | dk    s| dk    rdS t          d| z             )z$Convert string to boolean (PRIVATE).true1Tfalse0Fz*String could not be converted to boolean: )r"   rW   s    r   	_str2boolrh      sB    v~~tw$#++u
ADH
I
IIr   c                 p    |                                  }|D ]}||v rt          ||                   ||<   |S )zQReturn a new dictionary where string values are replaced with booleans (PRIVATE).)copyrh   )dctkeysoutkeys       r   _dict_str2boolro      sB    
((**C + +#:: S**CHJr   c                 L    | !	 t          |           S # t          $ r Y dS w xY wdS )z$Return text as an integer (PRIVATE).N)int	ExceptionrW   s    r   _intrs      sC    	t99 	 	 	44	     
!!c                 L    | !	 t          |           S # t          $ r Y dS w xY wdS )z!Return text as a float (PRIVATE).N)floatrr   rW   s    r   _floatrw      sC    	;; 	 	 	44	 rt   c                 X    | 'd                     |                                           S d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 )joinr>   rW   s    r   _collapse_wspacer{      s,     xx

%%% r   c                 D    dD ]}|| v r|                      |d          } | 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
    )	rZ   ry   )replace)rK   chars     r   _replace_wspacer      s5     # + +4<<<<c**DKr   c                   T   e Zd ZdZd Zd Zd Zd Zg dZddd	d
dZ	 e
e                              e	                                                              g d          Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$S )%r   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                     t          t          j        |d                    }t          |          \  }}|| _        || _        dS )zInitialize the class.)startend)eventsN)iterr   	iterparsenextr   context)selfr   r   eventr   s        r   __init__zParser.__init__  sC     {,T:JKKKLL7mmt	r   c                 8   t          j        d | j                                        D                       }d}| j        D ]\  }}t          |j                  \  }}|dk    rL|t          d         k    r|dz  }9|dk    r/|                     |          }|j	        
                    |           |dk    rf|t          d         k    rU|dz  }|dk    rJ|                     |||          }|j        
                    |           | j                                         |S )z<Parse the phyloXML file and create a single Phyloxml object.c                 4    i | ]\  }}t          |          |S r   )r;   )r(   rn   vals      r   
<dictcomp>zParser.read.<locals>.<dictcomp>  s$    SSSXS#sSSSSr   r   r   r   r7   	phylogenyr   )r   r.   r   itemsr   r?   r:   
NAMESPACES_parse_phylogenyr+   appendotherclear)	r   phyloxmlother_depthr   r_   rA   localtagr   otrs	            r   r   zParser.read  s'   ;SSARARSSSTT< 	& 	&KE4"248"<"<Ix
5 1111$K{** $ 5 5d ; ;I(//	:::~~)z%/@"@"@q !##**T9h??CN))#...IOO%%%r   c              #      K   t          d          }| j        D ]-\  }}|dk    r"|j        |k    r|                     |          V  .dS )z@Parse the phyloXML file incrementally and return each phylogeny.r   r   N)rB   r   r:   r   )r   phytagr   r_   s       r   r   zParser.parse1  sd      [!!< 	2 	2KE4DH$6$6++D11111	2 	2r   c           
      t   t          j        di t          |j        ddg          }ddddd}| j        D ]\  }}t          |j                  \  }}|dk    r7|d	k    r1|j        t          d          | 	                    |          |_        Z|dk    r |dk    r|
                                  n||v rAt          |||                                        t          | |          |                     |dv r)t          || t          | |          |                     |dv r%t          ||t          |j                             |t           d         k    rE|j                            |                     |||                     |
                                 ot%          d|z             |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confidences
propertiesclade_relationssequence_relations)
confidencepropertyclade_relationsequence_relationr   cladeNz)Phylogeny object should only have 1 clader   r   )dateid)namedescriptionr   Misidentified tag: r   )r   r   ro   attribr   r?   r:   r   r"   _parse_clader   getattrr   setattrr{   rK   r   r   r   )r   rF   r   
list_typesr   r_   rA   r:   s           r   r   zParser._parse_phylogeny:  s    L 
 
V]X|,DEE
 
	
 ($/!5
 

  < 	E 	EKE4-dh77NIsC7NN>-$%PQQQ!%!2!24!8!8	~~+%%LLNNNE*$$Iz#77>>?QwtS?Q?QRV?W?WXXXXN**Is,>GD#,>,>t,D,DEEEE333Is,<TY,G,GHHHH*U"333O**4::dIs+K+KLLLLLNNNN ((=(CDDD' ( r   )colorr   binary_charactersr   r   distributions
referencesr   )r   distribution	referencer   )branch_lengthr   node_idwidthc           
      8   t          j        di |j        }|j        t	          |j                  |_        g }| j        D ]\  }}t          |j                  \  }}|dk    r|dk    r.|j        	                    | 
                    |                     W|dk    r.|j        	                    |                     |                     |dk    r.|j        	                    |                     |                     || j        v r|	                    |           |dk    r|dk    r|                                  n||d         k    r|                                 || j        v rGt'          || j        |                   	                     t'          | |          |                     s|| j        v r*t+          || t'          | |          |                     |dk    r1|j        t-          d	          t/          |j                  |_        |d
k    rt/          |j                  |_        |dk    rt5          |j                  |_        |dk    rKt          j        |j                                        |j                            d                    |_        p|t@          d         k    rE|j!        	                    | !                    |||                     |                                 t-          d|z             |S )z;Parse a Clade node and its children, recursively (PRIVATE).Nr   r   taxonomysequencer   r   z7Attribute branch_length was already set for this Clade.r   r   r   providerr   r   r   )"r   r   r   r   rv   r   r?   r:   cladesr   r   
taxonomies_parse_taxonomy	sequences_parse_sequence_clade_tracked_tagsr   pop_clade_list_typesr   _clade_complex_typesr   r   rw   rK   r   r{   r   Idr\   getr   r   r   )r   rF   r   	tag_stackr   r_   rA   r:   s           r   r   zParser._parse_cladeu  s   ))6=))*"'(;"<"<E	< /	E /	EKE4-dh77NIs'>>L''(9(9$(?(?@@@*$$$++D,@,@,F,FGGG*$$O**4+?+?+E+EFFF$222$$S)))~~'>>JJLLLE)B-''$000E4#9##>??FF*c**400    D555E3(:c(:(:4(@(@AAAAO++*6+U   +1*;*;E''G^^"("3"3EKKF]]!1$)!<!<EJJI%%$&E	))4;??:+F+F% %EMM *U"333K&&tzz$	3'G'GHHHJJLLLL'(=(CDDDC D r   c           
         t          j        di |j        }| j        D ]0\  }}t	          |j                  \  }}|dk    r|dk    r|                                  n|dv r)t          || t          | |          |                     m|dk    r.|j	        
                    |                     |                     |dk    rt          |j                  |_        |dv rt          |||j                   |t          d         k    rC|j        
                    |                     |||                     |                                 2|S )	z%Parse a molecular sequence (PRIVATE).r   r   )	accessionmol_sequridomain_architecture
annotationr   )symbollocationr   r   )r   Sequencer   r   r?   r:   r   r   r   annotationsr   r   r{   rK   r   r   r   )r   rF   r   r   r_   rA   r:   s          r   r   zParser._parse_sequence  sV   ;////< 	# 	#KE4-dh77NIs~~*$$LLNNNEPPPHc+=74+=+=d+C+CDDDDL(((//0E0EFFFFF]]$4TY$?$?HMM222Hc495555*U"333N))$**T9c*J*JKKKLLNNNr   c           
         t          j        di |j        }| j        D ]5\  }}t	          |j                  \  }}|dk    r|dk    r|                                  n|dv r)t          || t          | |          |                     m|dk    r-|j	        
                    t          |j                             |dk    r |j        
                    |j                   |dv rt          |||j                   |t          d         k    rC|j        
                    |                     |||                     |                                 7|S )	z2Parse taxonomic information for a clade (PRIVATE).r   r   )r   r   common_namesynonym)codescientific_name	authorityrankr   r   )r   Taxonomyr   r   r?   r:   r   r   r   common_namesr   r{   rK   synonymsr   r   )r   rF   r   r   r_   rA   r:   s          r   r   zParser._parse_taxonomy  s_   ;////< 	# 	#KE4-dh77NIs~~*$$LLNNNE-''Hc+=74+=+=d+C+CDDDDM)))001A$)1L1LMMMMI%%%,,TY7777LLLHc495555*U"333N))$**T9c*J*JKKKLLNNNr   c                      t          j        |||j        |j        r|j                                        pd fd|D                       S )z/Create an Other object, a non-phyloXML element.Nc                 P    g | ]"} j         |gt          |j                  R  #S r   )r   r?   r:   )r(   rH   r   s     r   rP   z Parser.other.<locals>.<listcomp>  sE       DI

5?#3EI#>#>???  r   )valuechildren)r   Otherr   rK   r\   )r   r_   rA   r   s   `   r   r   zParser.other  sh    xK)1	 1 19T   MQ  
 
 
 	
r   c                     t          j        |j                                        |                    d                    S )zCreate accession object.source)r   	AccessionrK   r\   r   r   r_   s     r   r   zParser.accession  s,    |DIOO--txx/A/ABBBr   c                     t          j        dt          t          |d                    t	          |d| j                  t          |d| j                  t	          |d| j                  d|j	        S )zCreate annotation object.descr   r   r   )r   r   r   r   r   )
r   
Annotationr{   rL   rI   r   rT   r   r   r   r   s     r   r   zParser.annotation  st    } 
!/$"?"?@@$T<II'j$-HHdE4844	
 

 k
 
 	
r   c                    d }t          j        |                    d          t          |                    d                    t          |                    d                    t          |                    d                    t          |                    d                    t	          |d|          t	          |d|          t	          |d	|          t	          |d
|          	  	        S )z Create binary characters object.c                 "    t          | d          S )z$Get binary characters from subnodes.bc)rX   )r_   s    r   	bc_getterz+Parser.binary_characters.<locals>.bc_getter  s    %dD111r   typegained_count
lost_countpresent_countabsent_countgainedlostpresentabsent)	r   r   r   r   r   r   r   r   r   )r   BinaryCharactersr   rs   rI   )r   r_   r   s      r   r   zParser.binary_characters  s    	2 	2 	2 "&!!dhh~6677DHH\2233txx8899dhh~6677 x;;tVY77!$	9== x;;
 
 
 	
r   c                     t          j        |                    d          |                    d          |                    d          |                    d          t          |d| j                            S )z!Create clade relationship object.r   id_ref_0id_ref_1distancer   r   r   )r   CladeRelationr   rI   r   r   s     r   r   zParser.clade_relation  si    HHVHHZ  HHZ  XXj))$T<II
 
 
 	
r   c                 T    fddD             \  }}}t          j        |||          S )zCreate branch color object.c              3   D   K   | ]}t          |t                    V  d S r'   )rL   rq   )r(   r   r_   s     r   r*   zParser.color.<locals>.<genexpr>  s@       
 
27OD%--
 
 
 
 
 
r   redgreenblue)r   BranchColor)r   r_   r  r  r  s    `   r   r   zParser.color  sH    
 
 
 
;S
 
 
UD ~c5$///r   c                 v    t          j        t          |j                  |                    d                    S )zCreate confidence object.r   )r   
Confidencerw   rK   r   r   s     r   r   zParser.confidence  s*    }VDI..0@0@AAAr   c                    t          j        |                    d          t          t	          |d                    t	          |dt
                    t	          |dt
                    t	          |dt
                              S )zCreate date object.unitr   r   minimummaximum)r  r   r   r  r  )r   Dater   r{   rL   rv   r   s     r   r   zParser.date   sl    w&!!!/$"?"?@@!$77#D)U;;#D)U;;
 
 
 	
r   c           	          t          j        t          t          |d                    t	          |d| j                  t	          |d| j                            S )z&Create geographic distribution object.r   pointpolygon)r   pointspolygons)r   Distributionr{   rL   rT   r  r  r   s     r   r   zParser.distribution*  sR    !/$"?"?@@#D'4:>>%dIt|DD
 
 
 	
r   c           
      N   t          j        |j                                        t	          |                    d                    dz
  t	          |                    d                    t          |                    d                    |                    d                    S )zCreate protein domain object.fromr7   tor   r   )r   r   )r   ProteinDomainrK   r\   rq   r   rw   r   s     r   domainzParser.domain2  s    IOO  !!A%dhh|4455xx~~
 
 
 	
r   c                     t          j        t          |                    d                    t	          |d| j                            S )z"Create domain architecture object.lengthr  )r  domains)r   DomainArchitecturerq   r   rT   r  r   s     r   r   zParser.domain_architecture<  sD    $txx))**$T8T[AA
 
 
 	
r   c                     t          j        t          |d          t          |dt                    t          |dt                    t          |dt                    t	          |d| j                            S )zCreate events object.r   duplicationsspeciationslossesr   r   r!  r"  r#  r   )r   EventsrL   rq   rI   r   r   s     r   r   zParser.eventsC  sf    y v..(~sCC'mSAA"4377$T<II
 
 
 	
r   c                     |                     d          p|                     d          }t          j        |j                                        |          S )zCreate identifier object.r   r   )r   r   r   rK   r\   )r   r_   r   s      r   r   z	Parser.idM  sB    88J'';488F+;+;uTY__&&111r   c                     |                     d          }|t          |          }t          j        |j                                        |          S )z!Create molecular sequence object.
is_alignedNr(  )r   rh   r   MolSeqrK   r\   )r   r_   r(  s      r   r   zParser.mol_seqR  sG    XXl++
!":..Jy**zBBBBr   c           
          t          j        |                    d          t          |dt                    t          |dt                    t          |dt                    |                    d                    S )z,Create point object, coordinates of a point.geodetic_datumlatlongaltalt_unit)r/  r0  )r   Pointr   rL   rv   r   s     r   r  zParser.pointY  sg    xHH%&&D%//D&%00eU33XXj))
 
 
 	
r   c                 T    t          j        t          |d| j                            S )z&Create polygon object, list of points.r  )r  )r   PolygonrT   r  r   s     r   r  zParser.polygonc  s$    z!1$!L!LMMMMr   c           
      "   t          j        |j                                        |                    d          |                    d          |                    d          |                    d          |                    d                    S )z*Create properties from external resources.ref
applies_todatatyper  id_ref)r  r8  )r   PropertyrK   r\   r   r   s     r   r   zParser.propertyg  ss    {IOOHHUOOHH\""HHZ  &!!88H%%
 
 
 	
r   c                 p    t          j        |                    d          t          |d                    S )z#Create literature reference object.doir   )r;  r   )r   	Referencer   rL   r   s     r   r   zParser.referencer  s+    |odF6S6STTTTr   c                    t          j        |                    d          |                    d          |                    d          t          |                    d                    t	          |d| j                            S )zHCreate sequence relationship object, relationship between two sequences.r   r   r   r   r   r  )r   SequenceRelationr   rw   rI   r   r   s     r   r   zParser.sequence_relationv  sq    "HHVHHZ  HHZ  DHHZ0011$T<II
 
 
 	
r   c                     t          j        |j                                        t	          |                    d                    |                    d                    S )z(Create uri object, expected to be a url.r   r   r   r   )r   UrirK   r\   r{   r   r   s     r   r   z
Parser.uri  sP    vIOO!$((6"2"233&!!
 
 
 	
r   N)%r	   r
   r   r   r   r   r   r   r   r   setunionrl   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/            .2 2 2, , ,\ LKK#'! 	  	 !!	 %%''	(	(	<<<	=	= 7 7 7r  ,  .

 

 

C C C
 
 

 
 
(
 
 
0 0 0B B B
 
 

 
 

 
 

 
 

 
 
2 2 2
C C C
 
 
N N N	
 	
 	
U U U
 
 

 
 
 
 
r   r   c                     t          | t                    r!t          |                                           S t          | t                    r!t          |                                           S t          |           S )zEConvert a Python primitive to a phyloXML-compatible string (PRIVATE).)r   rv   strupperboollower)r   s    r   
_serializerI    sb    % "5zz!!!	E4	 	  "5zz!!!u::r   c                 \    i }|D ]&}t          | |          }|t          |          ||<   '|S )zNCreate a dictionary from an object's specified, non-None attributes (PRIVATE).)r   rI  )r3   attrsrm   rn   r   s        r   _clean_attribrL    s?    
C ' 'c3?!#CHJr   Fc                 4      fd}d  d|_         |S )z2Handle to serialize nodes with subnodes (PRIVATE).c           	         t          j        
t          |                    }	D ]}t          |t                    rKt          ||          :|                     t          | |          t          ||                               b|\  }}t          ||          D ].}|                     t          | |          |                     /rt          |j                  |_	        |S )z$Wrap nodes and subnodes as elements.)
r   ElementrL  r   rE  r   r   rI  r   rK   )r   r3   r_   subnmethodpluralitemattribshas_textsubnodesr:   s          r   wrappedz _handle_complex.<locals>.wrapped  s    "3c7(C(CDD 		= 		=D$$$ =3%%1KK 3d 3 3GC4F4F G GHHH "&#C00 = =DKK 5f 5 5d ; ;<<<<= 	."39--DIr   zSerialize a z and its subnodes, in order.r   )r:   rT  rV  rU  rW  s   ```` r   _handle_complexrY    sH           " GSFFFGONr   c                 (      fd}d  d|_         |S )z+Handle to serialize simple nodes (PRIVATE).c                 X    t          j                  }t          |          |_        |S )zWrap node as element.)r   rO  rI  rK   )r   r3   r_   r:   s      r   rW  z_handle_simple.<locals>.wrapped  s%    "3''sOO	r   zSerialize a simple z node.rX  )r:   rW  s   ` r   _handle_simpler\    s5         8C777GONr   c                      e Zd ZdZd ZedfdZd Zd Z e	ddd	          Z
 e	d
dd          Z e	dddd          Z e	ddd          Zd Z e	ddd          Z e	ddd          Z e	dddd          Z e	ddd          Z e	ddd           Zd! Z e	d"d#d$          Z e	d%dd&          Z e	d'd(dd          Z e	d)d*dd          Z e	d+d(dd          Z e	d,d-d.          Z e	d/dd0          Z e	d1d2dd          Z e	d3d4d5          Z e	d6d7d8          Z e	d9dd          Z e	d:dd;          Z  e	d<d=dd          Z! e"d>          Z# e"d?          Z$ e"d@          Z% e"dA          Z& e"dB          Z' e"dC          Z( e"dD          Z) e"dE          Z* e"dF          Z+ e"dG          Z, e"dH          Z- e"dI          Z. e"dJ          Z/ e"dK          Z0 e"dL          Z1 e"dM          Z2 e"dN          Z3 e"dO          Z4 e"dP          Z5 e"dQ          Z6 e"dR          Z7 e"dS          Z8 e"dT          Z9 e"dU          Z: e"dV          Z; e"dW          Z<dXS )Yr1   z1Methods for serializing a PhyloXML object to XML.c                     t          |t          j                  s
J d            t          j        |                     |                    | _        dS )z,Build an ElementTree from a PhyloXML object.zNot a Phyloxml objectN)r   r   r.   r   r   _tree)r   r   s     r   r   zWriter.__init__  sE    (BK00II2III0 ,T]]8-D-DEE


r   Tc                     |r&t          | j                                                   | j                            ||           t	          | j                                                  S )zWrite PhyloXML to a file.)r]   r_  getrootr2   r[   )r   r   r,   r-   s       r   r2   zWriter.write  sZ     	*DJ&&(()))
x(((4:%%''(((r   c                    t          j        d|j                  }|j        D ]*}|                    |                     |                     +|j        D ]*}|                    |                     |                     +|S )z"Convert phyloxml to Etree element.r   )r   rO  
attributesr+   r   r   r   )r   r3   r_   r#   r   s        r   r   zWriter.phyloxml  s}    ":s~>>O 	. 	.DKKt,,----9 	) 	)CKK

3((((r   c                     t          j        t          |j        |j                  |j                  }|j        |_        |j        D ]*}|	                    | 
                    |                     +|S )zConvert other to Etree element.)r   rO  rB   r:   rA   rc  r   rK   r   r   r   )r   r3   r_   rH   s       r   r   zWriter.other  sd    "3sw#>#>OOI	\ 	+ 	+EKK

5))****r   r   )r   r   branch_length_unitr   )
r   r   r   r   r   r   r   )r   r   )r   r   r   r   r   r   r   )	id_source)r   r   rf  r   r   r   )r   r   )r   r   r   r   )r   r   r   )r   r   rg  )r   r   rh  r   )r   r   )rU  r   )r5  r   evidencer   )r   r   rg  r   c                    t          j        dt          |d                    }dD ]f}t          j        |          }t          ||          D ]*}|                    |                     |                     +|                    |           g|S )z4Serialize a binary_characters node and its subnodes.r   )r   r   r   r   r   )r   r   r   r   )r   rO  rL  r   r   r   )r   r3   r_   rP  subelemtokens         r   r   zWriter.binary_characters  s    "W 
 
 < 	! 	!D!)$//G d++ / /twwu~~....KK    r   r   )r   r   r   r   )r   r   r  r   )r   r   )r  )r   r   r  r  r   )r   r  r  )r  r  c                 X   t          j        dt          |j        dz             t          |j                  d          }|j        (|                    dt          |j                             |j        |                    d|j                   t          |j	                  |_
        |S )zSerialize a domain node.r  r7   )r  r  Nr   r   )r   rO  rE  r   r   r   rB  rI  r   r   rK   )r   r3   r_   s      r   r  zWriter.domain6  s    "s39q=11SWFF
 
 >%HH\:cn#=#=>>>6HHT36"""sy))	r   r   )r  ))r  r  r   r$  r   )r   r   r)  r   r  )r,  r0  )r-  r.  r/  r  )rn  r   )r5  r  r7  r6  r8  r   )r;  )r   r   )r   r8  ri  )	r   r   r   r   r   r   )r   r   r   rh  r   r   )	r   r   r   r   )r   r   )r   r   r   r   rh  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)=r	   r
   r   r   r   DEFAULT_ENCODINGr2   r   r   rY  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   r   r   r   r   r   r   r   r   r   r   r   r1   r1     sJ       ;;F F F
 $4D ) ) ) )      >	
 I" O	
 E.  ["tLLLI -A J    %_F N OGR)ABBE y"tLLLJ?69.UVVD"?R L
 
 
 */{,D  _"U F 
}b4	@	@	@Boi"tLLLGoiTJJJGO/1G E oi-CDDG;
	  H  XyAAI'
	
 H  (4  
	
 H  /%!12
E
E
EC
 .

C"N?33M
.

C>&!!DnY''GnY''GN7##EN7##E >&!!D!>.11LN7##E^H%%F
.

C .//K 
		B>&!!D .//K>&!!D .//K~j))H>&!!D>&!!D$n%677O^H%%FnY''G>&!!DDDr   r1   N)r   )F)&r   	xml.etreer   	Bio.Phylor   r   r   register_namespacer   prefixr   rp  rr   r   r   r   r2   r;   r?   rB   rI   rE  rL   rT   rX   r]   rh   ro   rs   rw   r{   r   r   rI  rL  rY  r\  r1   r   r   r   <module>ru     s    " ! ! ! ! ! $ $ $ $ $ $ ./
 3 ##%% $ $KFCvs####      I   	 	 		  	  	  /t $E $E $E $EV     "%( # # # #
      ,/ % % % %D D D /2 W W W W   8J J J      & & &	 	 	
 
 
 
 
 
 
 
N       0
 
 
a" a" a" a" a" a" a" a" a" a"r   