o
    Rŀgp
                     @   sP   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d	 Z
d
d ZdS )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                 #   sT    t  | } fdd |jD ]} |||j}tj||j|j|jdV  qdS )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                    s:    fdd|j D }tj|jj|jj||jj|jjdS )Nc                    s   g | ]
}  |qS  )node).0n)
node2cladenxtreer   E/var/www/html/myenv/lib/python3.10/site-packages/Bio/Phylo/NexusIO.py
<listcomp>.   s    z-parse.<locals>.node2clade.<locals>.<listcomp>)branch_lengthnameclades
confidencecomment)succr   Cladedatabranchlengthtaxonsupportr   )r   r   	subcladesr
   )r   r   r
   -   s   zparse.<locals>.node2clade)rootrootedr   weightN)	r   treesr   r   r   Treer   r   r   )handlenexr   newrootr   r   r   parse    s   



r#   c                 K   s   t | }t|}dd t|jdddd|D }dd tdd |D  D }tt|d	|d
|d }|	| t|S )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.
    c                 S   s"   g | ]\}}t |d  |d qS )   )indextree)TREE_TEMPLATE)r   idxnwkr   r   r   r   F   s    zwrite.<locals>.<listcomp>FT)plainplain_newickc                 S   s   g | ]}t |jqS r   )strr   )r   xr   r   r   r   L   s    c                 s   s    | ]}|  V  qd S )N)get_terminals)r   tr   r   r   	<genexpr>L   s    zwrite.<locals>.<genexpr> 
)countlabelsr   Nr   )
listr   Writer	enumerate
to_stringsr   NEX_TEMPLATElenjoinwrite)objr    kwargsr   writernexus_trees
tax_labelstextr   r   r   r<   >   s   

r<   N)__doc__	itertoolsr   	Bio.Nexusr   	Bio.Phylor   r   r9   r'   r#   r<   r   r   r   r   <module>   s   