B
    b\
                 @   sH   d Z ddlmZ ddlmZ ddlmZ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             #   sV   t  | } fdd x:|jD ]0} |||j}tj||j|j|jdV  qW 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   0lib/python3.7/site-packages/Bio/Phylo/NexusIO.py
<listcomp>.   s    z-parse.<locals>.node2clade.<locals>.<listcomp>)Zbranch_lengthnameZcladesZ
confidencecomment)Zsuccr   ZCladedataZbranchlengthZtaxonZsupportr   )r   r   Z	subclades)r
   )r   r   r
   -   s    zparse.<locals>.node2clade)rootrootedr   weightN)	r   treesr   r   r   ZTreer   r   r   )handleZnexr   Znewrootr   )r
   r   parse    s    

r   c             K   s   t | }t|}dd t|jf 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 )   )indexZtree)TREE_TEMPLATE)r   idxZnwkr   r   r   r   G   s   zwrite.<locals>.<listcomp>FT)ZplainZ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)Zget_terminals)r   tr   r   r   	<genexpr>L   s    zwrite.<locals>.<genexpr> 
)countlabelsr   )
listr   Writer	enumerateZ
to_stringsr   NEX_TEMPLATElenjoinwrite)objr   kwargsr   writerZnexus_treesZ
tax_labelstextr   r   r   r)   >   s    

r)   N)__doc__	itertoolsr   Z	Bio.Nexusr   Z	Bio.Phylor   r   r&   r   r   r)   r   r   r   r   <module>   s   