
    {Tip
                     L    d Z ddlmZ ddlmZ ddlmZ ddlmZ dZdZ	d Z
d	 Zy
)z.I/O function wrappers for ``Bio.Nexus`` trees.    )chain)Nexus)Newick)NewickIOzh#NEXUS
Begin Taxa;
 Dimensions NTax=%(count)d;
 TaxLabels %(labels)s;
End;
Begin Trees;
 %(trees)s
End;
zTree tree%(index)d=%(tree)sc              #     K   t        j                   |       }fd|j                  D ]]  } ||j                  |j                              }t	        j
                  ||j                  |j                  |j                         _ yw)aU  Parse the trees in a Nexus file.

    Uses the old Nexus.Trees parser to extract the trees, converts them back to
    plain Newick trees, and feeds those strings through the new Newick parser.
    This way we don't have to modify the Nexus module yet. (Perhaps we'll
    eventually change Nexus to use the new NewickIO parser directly.)
    c           	      @   |j                   D cg c]  } | | j                  |             }}t        j                  |j                  j
                  |j                  j                  ||j                  j                  |j                  j                        S c c}w )N)branch_lengthnameclades
confidencecomment)	succnoder   Cladedatabranchlengthtaxonsupportr   )nxtreer   n	subclades
node2clades       D/home/agent/.local/lib/python3.12/site-packages/Bio/Phylo/NexusIO.pyr   zparse.<locals>.node2clade-   su    AEKAZA7K	K||))00yy((II%%
 	
 Ls   B)rootrootedr
   weightN)	r   treesr   r   r   Treer   r
   r   )handlenexr   newrootr   s       @r   parser"       sp      ++f
C

 )) 
VV[[%=>kkV[[
 	

s   BBc                    t        |       }t        j                  |      }t         |j                  d
ddd|      D cg c]  \  }}t
        |dz   |dz   }}}t        d |D         D cg c]  }t        |j                         }	}t        t        |	      dj                  |	      dj                  |      d	z  }
|j                  |
       t        |      S c c}}w c c}w )zWrite a new Nexus file containing the given trees.

    Uses a simple Nexus template and the NewickIO writer to serialize just the
    trees and minimal supporting info needed for a valid Nexus file.
    FT)plainplain_newick   )indextreec              3   <   K   | ]  }|j                           y w)N)get_terminals).0ts     r   	<genexpr>zwrite.<locals>.<genexpr>L   s     .PQq/@.Ps    
)countlabelsr    )listr   Writer	enumerate
to_stringsTREE_TEMPLATEr   strr
   NEX_TEMPLATElenjoinwrite)objr   kwargsr   writeridxnwknexus_treesx
tax_labelstexts              r   r<   r<   >   s     IE__U#F "FGEGG
C 	#'377K  (-.P%.P'QR!#aff+RJRZ((:&;' D
 LL{ Ss   C/C#N)__doc__	itertoolsr   	Bio.Nexusr   	Bio.Phylor   r   r9   r7   r"   r<   r2       r   <module>rK      s.    5    	 .
<rJ   