
    {TiT~                     ^   d Z ddlmZ ddlmZ ddiZej                  Zej                         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%y#)&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y)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__     G/home/agent/.local/lib/python3.12/site-packages/Bio/Phylo/PhyloXMLIO.pyr   r   '   s    r   r   c                 4    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files    r   r   r   4   s     $<r   c                 4    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   parser   s    r   r   r   @   s     $<r   Tc                    d t        | t        j                        rnt        | t        j                  j                  t        j                  j
                  f      r |       j                         } n8t        | d      r!t        j                  i fd| D              } nt        d      t        |       j                  |||      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| j	                         S t        | t        j
                  j                        rt        j                  j                  |       S t        | t        j
                  j                        r=t        j                  j                  t        j
                  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    dBLL)KdBHH%$$&&dBKK,,-<<))$//dBKK--.<<))"++*:*:*:*EFFHIIr   __iter__c              3   .   K   | ]  } |        y wNr   ).0tr$   s     r   	<genexpr>zwrite.<locals>.<genexpr>j   s     *FQ:a=*Fs   )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 #r{{#	C"++**BKK,=,=>	?o))+	j	!kk"*F#*FG9
 	
 #;THVDDr   c                 D    | d   dk(  r| | j                  d      dz   d S | S )z;Extract the local tag from a namespaced tag name (PRIVATE).r   {}   N)indextags    r   _localr;   w   s-    
1v}399S>A%'((Jr   c                 T    	 | dd j                  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?   ~   s7    12w}}S!$$ Cys    ''c                     d| d|  S )z5Format an XML tag with the given namespace (PRIVATE).r5   r6   r   )r:   	namespaces     r   _nsrB      s    	{"SE""r   c                 L    | j                  t        |            }| ||      S y)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      s,    
 KKC!E r   c                 z    | j                  t        |            }||j                  r ||j                        S yy)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      s9    
 KKC!EUZZ$$ (r   c                 h    | j                  t        |            D cg c]
  } ||       c}S c c}w )zFind child nodes by tag; pass each through a constructor (PRIVATE).

    Returns an empty list if no matching child is found.
    )findallrB   rE   s       r   _get_children_asrO      s*    
 +1..S*BCIeCCCs   /c                     | j                  t        |            D cg c]!  }|j                  s ||j                        # c}S c c}w )zFind child nodes by tag; pass each node's text through a constructor (PRIVATE).

    Returns an empty list if no matching child is found.
    )rN   rB   rK   rE   s       r   _get_children_textrQ      s4    
 06~~c#h/GVe5::Iejj!VVVs
   AAc                    d|dz  z   }t        |       r| j                  r| j                  j                         s
|dz   | _        | D ]B  }t        ||dz          |j                  r|j                  j                         r9|dz   |_        D j                  r|j                  j                         s||_        yy|r/| j                  r| j                  j                         s|| _        yyy)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   rV   rV      s     	ut|A
4yyy		 1DDI 	"AAuqy!66T	" vvQVV\\^AF , $))499??+<DI ,=5r   c                 J    | dk(  s| dk(  ry| dk(  s| dk(  ryt        d| z         )z$Convert string to boolean (PRIVATE).true1Tfalse0Fz*String could not be converted to boolean: )r"   rK   s    r   	_str2boolrb      s3    v~w$#+
ADH
IIr   c                 `    | j                         }|D ]  }||v st        ||         ||<    |S )zQReturn a new dictionary where string values are replaced with booleans (PRIVATE).)copyrb   )dctkeysoutkeys       r   _dict_str2boolri      s<    
((*C +#: S*CH+ Jr   c                 >    | 	 t        |       S y# t        $ r Y yw xY w)z$Return text as an integer (PRIVATE).N)int	Exceptionra   s    r   _intrm      s0    	t9   		   
 	c                 >    | 	 t        |       S y# t        $ r Y yw xY w)z!Return text as a float (PRIVATE).N)floatrl   ra   s    r   _floatrq      s0    	;   		rn   c                 F    | dj                  | j                               S y)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>   ra   s    r   _collapse_wspaceru      s$     xx

%% r   c                 B    dD ]  }|| v s| j                  |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
    )	rS   rs   )replace)rK   chars     r   _replace_wspacer{      s1     # +4<<<c*D+ Kr   c                   4   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      j                  e	j                               j                  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$y$)%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        |      \  }}|| _        || _        y)zInitialize the class.)startend)eventsN)iterr   	iterparsenextr   context)selfr   r   eventr   s        r   __init__zParser.__init__  s7     {,,T:JKL7mt	r   c           	      ^   t        j                  | j                  j                         D ci c]  \  }}t	        |      | c}}      }d}| j
                  D ]  \  }}t        |j                        \  }}|dk(  rC|t        d   k7  r|dz  }5|dk(  r,| j                  |      }	|j                  j                  |	       |dk(  sl|t        d   k7  sy|dz  }|dk(  s| j                  |||      }
|j                  j                  |
       | j                  j                           |S c c}}w )z<Parse the phyloXML file and create a single Phyloxml object.r   r~   r   r7   	phylogenyr   )r   r.   r   itemsr;   r   r?   r:   
NAMESPACES_parse_phylogenyr+   appendotherclear)r   rh   valphyloxmlother_depthr   rX   rA   localtagr   otrs              r   r   zParser.read  s   ;;ARSXS#sS 0ST<< 	&KE4"2488"<Ix
5 111$K{* $ 5 5d ;I((//	:~)z%/@"@q !#**T9h?CNN))#.IIOO%!	&" '  Ts   D)
c              #      K   t        d      }| j                  D ].  \  }}|dk(  s|j                  |k(  s| j                  |       0 yw)z@Parse the phyloXML file incrementally and return each phylogeny.r   r~   N)rB   r   r:   r   )r   phytagr   rX   s       r   r   zParser.parse1  sK     [!<< 	2KE4DHH$6++D11	2s   #AAAc           
         t        j                  di t        |j                  ddg      }ddddd}| j                  D ]D  \  }}t        |j                        \  }}|dk(  r3|d	k(  r.|j                  t        d
      | j                  |      |_        W|dk(  s]|dk(  r|j                           |S ||v r/t        |||         j                   t        | |      |             |dv rt        || t        | |      |             |dv r!t        ||t        |j                               |t         d   k7  r>|j"                  j                  | j#                  |||             |j                          9t%        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~   cladez)Phylogeny object should only have 1 clader   r   )dateid)namedescriptionr   Misidentified tag: r   )r   r   ri   attribr   r?   r:   r   r"   _parse_clader   getattrr   setattrru   rK   r   r   r   )r   rF   r   
list_typesr   rX   rA   r:   s           r   r   zParser._parse_phylogeny:  s    LL 
V]]X|,DE
	
 ($/!5

  << 	EKE4-dhh7NIsC7N>>-$%PQQ!%!2!24!8	~+%LLN"  *$Iz#7>>?QwtS?QRV?WXN*Is,>GD#,>t,DE33Is,<TYY,GH*U"33OO**4::dIs+KLLLN ((=(CDD5	E6 r   )colorr   binary_charactersr   r   distributions
referencesr   )r   distribution	referencer   )branch_lengthr   node_idwidthc           
         t        j                  di |j                  }|j                  t	        |j                        |_        g }| j
                  D ]  \  }}t        |j                        \  }}|dk(  r|dk(  r+|j                  j                  | j                  |             T|dk(  r+|j                  j                  | j                  |             |dk(  r+|j                  j                  | j                  |             || j                  v r|j                  |       |dk(  s|dk(  r|j!                           |S ||d   k7  r|j#                          || j$                  v r:t'        || j$                  |         j                   t'        | |      |             R|| j(                  v rt+        || t'        | |      |             |dk(  r3|j                  t-        d      t/        |j0                        |_        |d	k(  rt/        |j0                        |_        |d
k(  rt5        |j0                        |_        |dk(  rNt        j8                  |j0                  j;                         |j                  j=                  d            |_        L|t@        d   k7  r>|jB                  j                  | jC                  |||             |j!                          t-        d|z          |S )z;Parse a Clade node and its children, recursively (PRIVATE).r~   r   taxonomysequencer   r   z7Attribute branch_length was already set for this Clade.r   r   r   providerr   r   r   )"r   r   r   r   rp   r   r?   r:   cladesr   r   
taxonomies_parse_taxonomy	sequences_parse_sequence_clade_tracked_tagsr   pop_clade_list_typesr   _clade_complex_typesr   r   rq   rK   r   ru   r   IdrU   getr   r   r   )r   rF   r   	tag_stackr   rX   rA   r:   s           r   r   zParser._parse_cladeu  s   )6==)*"'(;(;"<E	<< /	EKE4-dhh7NIs'>LL''(9(9$(?@*$$$++D,@,@,FG*$OO**4+?+?+EF$222$$S)~'>JJL> = )B-'$000E4#9#9##>?FF*c*40 D555E3(:c(:4(@AO+**6+U  +1*;E'G^"("3EKF]!1$))!<EJI%$&EE		)4;;??:+F%EM *U"33KK&&tzz$	3'GHJJL'(=(CDD_/	E` r   c           
         t        j                  di |j                  }| j                  D ]  \  }}t	        |j
                        \  }}|dk(  s%|dk(  r|j                           |S |dv rt        || t        | |      |             _|dk(  r+|j                  j                  | j                  |             |dk(  rt        |j                        |_        |dv rt        |||j                         |t        d   k7  s|j                   j                  | j!                  |||             |j                           |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   ru   rK   r   r   r   )r   rF   r   r   rX   rA   r:   s          r   r   zParser._parse_sequence  s   ;;//<< 	#KE4-dhh7NIs~*$LLN  PPHc+=74+=d+CDL(((//0EFF]$4TYY$?HM22Hc4995*U"33NN))$**T9c*JKLLN!	#" r   c           
         t        j                  di |j                  }| j                  D ]"  \  }}t	        |j
                        \  }}|dk(  s%|dk(  r|j                           |S |dv rt        || t        | |      |             _|dk(  r/|j                  j                  t        |j                               |dk(  r&|j                  j                  |j                         |dv rt        |||j                         |t        d   k7  s|j                  j                  | j                  |||             |j                          % |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   ru   rK   synonymsr   r   )r   rF   r   r   rX   rA   r:   s          r   r   zParser._parse_taxonomy  s"   ;;//<< 	#KE4-dhh7NIs~*$LLN  -'Hc+=74+=d+CDM)))001A$))1LMI%%%,,TYY7LLHc4995*U"33NN))$**T9c*JKLLN#	#$ r   c                    t        j                  |||j                  |j                  xr |j                  j	                         xs d|D cg c](  } | j
                  |gt        |j                         * c}      S c c}w )z/Create an Other object, a non-phyloXML element.N)valuechildren)r   Otherr   rK   rU   r   r?   r:   )r   rX   rA   r   rH   s        r   r   zParser.other  sn    xxKK))1		 19TMQDI


5?#3EII#>?
 	

s   -Bc                 |    t        j                  |j                  j                         |j	                  d            S )zCreate accession object.source)r   	AccessionrK   rU   r   r   rX   s     r   r   zParser.accession  s'    ||DIIOO-txx/A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   
Annotationru   rL   rI   r   rO   r   r   r   r   s     r   r   zParser.annotation  se    }} 
!/$"?@$T<I'j$--HdE4884	

 kk
 	
r   c                 z   d }t        j                  |j                  d      t        |j                  d            t        |j                  d            t        |j                  d            t        |j                  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)rQ   )rX   s    r   	bc_getterz+Parser.binary_characters.<locals>.bc_getter  s    %dD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   rm   rI   )r   rX   r   s      r   r   zParser.binary_characters  s    	2 ""&!dhh~67DHH\23txx89dhh~67 x;tVY7!$	9= x;
 	
r   c                     t        j                  |j                  d      |j                  d      |j                  d      |j                  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  sU    HHVHHZ HHZ XXj)$T<I
 	
r   c                 P    fddD        \  }}}t        j                  |||      S )zCreate branch color object.c              3   @   K   | ]  }t        |t                y wr'   )rL   rk   )r(   r   rX   s     r   r*   zParser.color.<locals>.<genexpr>  s      
27OD%-
s   redgreenblue)r   BranchColor)r   rX   r   r   r   s    `   r   r   zParser.color  s.    
;S
UD ~~c5$//r   c                 r    t        j                  t        |j                        |j	                  d            S )zCreate confidence object.r   )r   
Confidencerq   rK   r   r   s     r   r   zParser.confidence  s%    }}VDII.0@AAr   c                     t        j                  |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   ru   rL   rp   r   s     r   r   zParser.date   sR    ww&!!/$"?@!$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   Distributionru   rL   rO   r	  r
  r   s     r   r   zParser.distribution*  sB    !/$"?@#D'4::>%dIt||D
 	
r   c           
         t        j                  |j                  j                         t	        |j                  d            dz
  t	        |j                  d            t        |j                  d            |j                  d            S )zCreate protein domain object.fromr7   tor   r   )r   r   )r   ProteinDomainrK   rU   rk   r   rq   r   s     r   domainzParser.domain2  se    IIOO !A%dhh|45xx~
 	
r   c                     t        j                  t        |j                  d            t	        |d| j
                              S )z"Create domain architecture object.lengthr  )r  domains)r   DomainArchitecturerk   r   rO   r  r   s     r   r   zParser.domain_architecture<  s6    $$txx)*$T8T[[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   rk   rI   r   r   s     r   r   zParser.eventsC  sQ    yy v.(~sC'mSA"437$T<I
 	
r   c                     |j                  d      xs |j                  d      }t        j                  |j                  j	                         |      S )zCreate identifier object.r   r   )r   r   r   rK   rU   )r   rX   r   s      r   r   z	Parser.idM  s:    88J';488F+;uuTYY__&11r   c                     |j                  d      }|t        |      }t        j                  |j                  j                         |      S )z!Create molecular sequence object.
is_alignedr  )r   rb   r   MolSeqrK   rU   )r   rX   r  s      r   r   zParser.mol_seqR  s<    XXl+
!":.Jyy*zBBr   c           
          t        j                  |j                  d      t        |dt              t        |dt              t        |dt              |j                  d            S )z,Create point object, coordinates of a point.geodetic_datumlatlongaltalt_unit)r&  r'  )r   Pointr   rL   rp   r   s     r   r	  zParser.pointY  sP    xxHH%&D%/D&%0eU3XXj)
 	
r   c                 X    t        j                  t        |d| j                              S )z&Create polygon object, list of points.r	  )r  )r   PolygonrO   r	  r   s     r   r
  zParser.polygonc  s    zz!1$!LMMr   c           
          t        j                  |j                  j                         |j	                  d      |j	                  d      |j	                  d      |j	                  d      |j	                  d            S )z*Create properties from external resources.ref
applies_todatatyper  id_ref)r  r/  )r   PropertyrK   rU   r   r   s     r   r   zParser.propertyg  s\    {{IIOOHHUOHH\"HHZ &!88H%
 	
r   c                 b    t        j                  |j                  d      t        |d            S )z#Create literature reference object.doir   )r2  r   )r   	Referencer   rL   r   s     r   r   zParser.referencer  s"    ||odF6STTr   c                     t        j                  |j                  d      |j                  d      |j                  d      t        |j                  d            t	        |d| j
                              S )zHCreate sequence relationship object, relationship between two sequences.r   r   r   r   r   r   )r   SequenceRelationr   rq   rI   r   r   s     r   r   zParser.sequence_relationv  sZ    ""HHVHHZ HHZ DHHZ01$T<I
 	
r   c                     t        j                  |j                  j                         t	        |j                  d            |j                  d            S )z(Create uri object, expected to be a url.r   r   r   r   )r   UrirK   rU   ru   r   r   s     r   r   z
Parser.uri  s>    vvIIOO!$((6"23&!
 	
r   N)%r	   r
   r   r   r   r   r   r   r   r   setunionrf   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,\ L#'! 	 	 !	 %%'	(	<	= 7r,.

C

(
0B




2
C
N	
U

r   r   c                     t        | t              rt        |       j                         S t        | t              rt        |       j                         S t        |       S )zEConvert a Python primitive to a phyloXML-compatible string (PRIVATE).)r   rp   strupperboollower)r   s    r   
_serializer@    sF    %5z!!	E4	 5z!!u:r   c                 R    i }|D ]  }t        | |      }|t        |      ||<   ! |S )zNCreate a dictionary from an object's specified, non-None attributes (PRIVATE).)r   r@  )r3   attrsrg   rh   r   s        r   _clean_attribrC    s;    
C 'c3?!#CH' Jr   c                 4      fd}d  d|_         |S )z2Handle to serialize nodes with subnodes (PRIVATE).c           	         t        j                  
t        |            }	D ]  }t        |t              r9t        ||       |j                   t        | |      t        ||                   L|\  }}t        ||      D ]#  }|j                   t        | |      |             %  rt        |j                        |_	        |S )z$Wrap nodes and subnodes as elements.)
r   ElementrC  r   r<  r   r   r@  r   rK   )r   r3   rX   subnmethodpluralitemattribshas_textsubnodesr:   s          r   wrappedz _handle_complex.<locals>.wrapped  s    ""3c7(CD 		=D$$3%1KK 3d 3GC4F GH "&#C0 =DKK 5f 5d ;<=		= "399-DIr   zSerialize a z and its subnodes, in order.r   )r:   rK  rM  rL  rN  s   ```` r   _handle_complexrP    s     " %SE)EFGONr   c                 (      fd}d  d|_         |S )z+Handle to serialize simple nodes (PRIVATE).c                 R    t        j                        }t        |      |_        |S )zWrap node as element.)r   rF  r@  rK   )r   r3   rX   r:   s      r   rN  z_handle_simple.<locals>.wrapped  s#    ""3'sO	r   zSerialize a simple z node.rO  )r:   rN  s   ` r   _handle_simplerS    s     ,C5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<yX)Yr1   z1Methods for serializing a PhyloXML object to XML.c                     t        |t        j                        sJ d       t        j                  | j	                  |            | _        y)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__  s7    (BKK0I2II0 ,,T]]8-DE
r   Tc                     |r#t        | j                  j                                | j                  j                  ||       t	        | j                  j                               S )zWrite PhyloXML to a file.)rV   rV  getrootr2   rT   )r   r   r,   r-   s       r   r2   zWriter.write  sF    DJJ&&()

x(4::%%'((r   c                 
   t        j                  d|j                        }|j                  D ]"  }|j	                  | j                  |             $ |j                  D ]"  }|j	                  | j                  |             $ |S )z"Convert phyloxml to Etree element.r   )r   rF  
attributesr+   r   r   r   )r   r3   rX   r#   r   s        r   r   zWriter.phyloxml  sl    "":s~~>OO 	.DKKt,-	.99 	)CKK

3(	)r   c                    t        j                  t        |j                  |j                        |j
                        }|j                  |_        |j                  D ]"  }|j                  | j                  |             $ |S )zConvert other to Etree element.)r   rF  rB   r:   rA   rZ  r   rK   r   r   r   )r   r3   rX   rH   s       r   r   zWriter.other  s_    ""3sww#>OII	\\ 	+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   r]  r   r   r   )r   r   )r   r   r   r   )r   r   r   )r   r   r^  )r   r   r_  r   )r   r   )rL  r   )r,  r   evidencer   )r   r   r^  r   c                    t        j                  dt        |d            }dD ]Y  }t        j                  |      }t        ||      D ]"  }|j	                  | j                  |             $ |j	                  |       [ |S )z4Serialize a binary_characters node and its subnodes.r   )r   r   r   r   r   )r   r   r   r   )r   rF  rC  r   r   r   )r   r3   rX   rG  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                 p   t        j                  dt        |j                  dz         t        |j                        d      }|j
                  %|j                  dt        |j
                               |j                  |j                  d|j                         t        |j                        |_
        |S )zSerialize a domain node.r  r7   )r  r  r   r   )r   rF  r<  r~   r   r   r9  r@  r   r   rK   )r   r3   rX   s      r   r  zWriter.domain6  s    ""s399q=1SWWF
 >>%HH\:cnn#=>66HHT366"syy)	r   r   )r  ))r  r  r   r  r   )r   r   r   r   r	  )r#  r'  )r$  r%  r&  r
  )re  r   )r,  r  r.  r-  r/  r   )r2  )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   r7  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   rP  r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   r	  r
  r   r   r   r   r   r   rS  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     s   ;F
 $4D )  >	
I" 	
E.  ["tLI -AJ  %FN GR)ABE y"tLJ69.UVD"RL
 *{,D "UF 
}b4	@Bi"tLGiTJG/1GE i-CDG;
	H  XyAI'
	
H  (4 
	
H  %!12
EC
 
C"?3M

C&!DY'GY'G7#E7#E &!D!.1L7#EH%F

C /K 
	B&!D /K&!D /Kj)H&!D&!D$%67OH%FY'G&!Dr   r1   N)r   )F)&r   	xml.etreer   	Bio.Phylor   r   r   register_namespacer   prefixr   rg  rl   r   r   r   r2   r;   r?   rB   rI   r<  rL   rO   rQ   rV   rb   ri   rm   rq   ru   r{   r   r@  rC  rP  rS  r1   r   r   r   <module>rl     s   " $ ./
 33 ##% $KFCvs#$  I 		  /t $EV "%( #
  ,/ %D /2 W8J&	
 
N0
a" a"r   