
:>"^H-                 @   s  d  Z  d d l m Z d d l m Z d d l m Z d d l Z d d l m	 Z	 m
 Z
 m Z yP d* e j d d	  k o d+ k n r d d
 l m Z n d d l m Z Wn" e k
 r d d
 l m Z Yn Xd d d d d d d d i Z e j e
  e d Z d Z d Z y e j Z WnI e k
 rle e d  s\d d
 l m Z e j e _ d d   Z Yn Xx' e j   D] \ Z Z e e e  qzWd d   Z d d   Z d d   Z  Gd d    d  e!  Z" d! d"   Z# d# d$ d%  Z$ Gd& d'   d' e%  Z& Gd( d)   d) e%  Z' d S),zLI/O function wrappers for the NeXML file format.

See: http://www.nexml.org
    )StringIO)NeXML)minidomN   )cdao_elementscdao_namespacesresolve_uri      )ElementTree)cElementTreexsiz)http://www.w3.org/2001/XMLSchema-instancexmlz$http://www.w3.org/XML/1998/namespaceZnexzhttp://www.nexml.org/2009Zxsdz!http://www.w3.org/2001/XMLSchema#z0.9z-http://www.nexml.org/2009/nexml/xsd/nexml.xsd_namespace_mapc             C   s   |  t  j | <d S)zSet NameSpace map.N)r   r   )prefixuri r   6/tmp/pip-build-ww9dw3qa/biopython/Bio/Phylo/NeXMLIO.pyregister_namespace7   s    r   c             C   s   t  |  d t d d S)z*Given a prefixed URI, return the full URI.
namespacesZ	xml_styleT)r   
NAMESPACES)sr   r   r   qUri@   s    r   c             C   s   d t  |  t d  d  S)zAOptionally converts a CDAO-prefixed URI into an OBO-prefixed URI.zobo:%szcdao:N)r   len)r   r   r   r   cdao_to_oboE   s    r   c             C   s*   |  j  d  r |  t |   f S|  f Sd S)z2Check for matches in both CDAO and OBO namespaces.zcdao:N)
startswithr   )r   r   r   r   matchesJ   s    r   c               @   s   e  Z d  Z d Z d S)
NeXMLErrorz@Exception raised when NeXML object construction cannot continue.N)__name__
__module____qualname____doc__r   r   r   r   r   R   s   r   c             K   s   t  |   j |   S)znIterate over the trees in a NeXML file handle.

    :returns: generator of Bio.Phylo.NeXML.Tree objects.

    )Parserparse)handlekwargsr   r   r   r#   \   s    r#   Fc             K   s   t  |   j | d | | S)zeWrite a trees in NeXML format to the given file handle.

    :returns: number of trees written.

    plain)Writerwrite)treesr$   r&   r%   r   r   r   r(   e   s    r(   c               @   sd   e  Z d  Z d Z d d   Z e d d    Z d d   Z d d d	 d
  Z e d d    Z	 d S)r"   z]Parse a NeXML tree given a file handle.

    Based on the parser in ``Bio.Nexus.Trees``.
    c             C   s   | |  _  d S)z,Initialize parameters for NeXML file parser.N)r$   )selfr$   r   r   r   __init__x   s    zParser.__init__c             C   s   t  |  } |  |  S)z'Convert file handle to StringIO object.)r   )clsZtreetextr$   r   r   r   from_string|   s    zParser.from_stringc             C   s^   d | j  k r | j  d } n d } | t d  k rM t | j  | d <n | j | | <d S)z%Add annotations for the NeXML parser.propertymetazcdao:has_Support_Value
confidenceN)attribr   floattext)r*   	node_dictZ	meta_nodepropr   r   r   add_annotation   s    zParser.add_annotationFc             #   s   t  j |  j d d } x| D]\ } } | j t d  k r i  } i  } d } | j   }	 g  }
 g  } xR |	 D]J } | j t d  k r |
 j |  | j t d  k rq | j |  qq Wx |
 D] } | j d } i  } | | <d | j k r| j d r| j d | d	 <d
 | j k r<| j d
 d k r<| } x= | j   D]/ } | j t d  k rI|  j | | |  qIWq Wt	     t	    x| D]} | j d | j d } }   j
 |   j
 |  | | k rt	   | | <| | j
 |  d | j k r(t | j d  | | d <d | j k rk| j d t d  k rkt | j d  | | d <x= | j   D]/ } | j t d  k rx|  j | | |  qxWqW| d k rd }    f d d   |
 D } t |  } n d } t j d
 |  j | | |  d |  Vq Wd S)z7Parse the text stream this object was initialized with.eventsendznex:treeNznex:nodeznex:edgeidotunameroottrueznex:metasourcetargetlengthbranch_lengthr.   zcdao:has_Support_Valuecontentr0   Fc             3   sB   |  ]8 } | j  d    k r | j  d   k r | j  d  Vq d S)r9   N)r1   ).0node)srcstarsr   r   	<genexpr>   s   zParser.parse.<locals>.<genexpr>Trooted)zend)r   	iterparser$   tagr   getchildrenappendr1   r6   setaddr2   r   nextr   ZTree
_make_tree)r*   Zvalues_are_confidencerH   Z	nexml_doceventrD   r4   Znode_childrenr<   Z
child_tagsZnodesedgeschildnode_id	this_nodeedgesrctarZpossible_rootsr   )rE   rF   r   r#      sb    "		
	zParser.parsec                sO    | } t  j |   } |   k rK     f d d     | D | _ | S)zTraverse the tree creating a nested clade structure (PRIVATE).

        Return a NeXML.Clade, and calls itself recursively for each child,
        traversing the  entire tree and creating a nested structure of NeXML.Clade
        objects.
        c                s%   g  |  ] }  j  |      q Sr   )rP   )rC   rS   )childrenr,   r4   r   r   
<listcomp>   s   	 z%Parser._make_tree.<locals>.<listcomp>)r   ZCladeclades)r,   rD   r4   rY   rU   clader   )rY   r,   r4   r   rP      s
    
&zParser._make_treeN)
r   r   r    r!   r+   classmethodr-   r6   r#   rP   r   r   r   r   r"   r   s   Er"   c               @   sO   e  Z d  Z d Z d d   Z d d   Z d d d  Z d	 d
 d d  Z d	 S)r'   z8Based on the writer in Bio.Nexus.Trees (str, to_string).c             C   s(   | |  _  d |  _ d |  _ d |  _ d S)z'Initialize parameters for NeXML writer.r   N)r)   Znode_counterZedge_counterZtree_counter)r*   r)   r   r   r   r+      s    			zWriter.__init__c             C   s>   d | } t  |  | t |  |  d  d | t |  |  f S)z'Create new labels for the NeXML writer.z
%s_counterr   z%s%s)setattrgetattr)r*   Zobj_typecounterr   r   r   	new_label   s    
zWriter.new_labelTc             K   s  | |  _  t j d  } | j d t  | j d t  | j d t  x. t j   D]  \ } } | j d | |  qU Wt j	 | d d d d	 d
 i  } t j	 | d d d d	 d d d i  } d }	 t   }
 xj |  j
 D]_ } t j	 | d d |  j d  i  } | j } |
 j |  j | | d | j  |	 d 7}	 q Wx) |
 D]! } t j	 | d d | i  } qDWt j | d  } t j |  } y | j | j d d   Wn4 t k
 r| j t | j d d  d   Yn X|	 S)z-Write this instance's trees to a file handle.z	nex:nexmlversionxmlnszxsi:schemaLocationzxmlns:%sotusr9   ZtaxlabelZRootTaxaBlockr)   ZTreesZTreesBlockFromXMLr   treerH   r   r:   zutf-8indentz  utf8)r   r   ElementrM   VERSIONDEFAULT_NAMESPACESCHEMAr   items
SubElementr)   ra   r\   update_write_treerH   tostringr   parseStringr(   Ztoprettyxml	TypeErrorbytes)r*   r$   r   r%   Z	root_noder   r   rd   r)   counttusrf   Z	this_treeZfirst_cladeZtur:   Zrough_stringZreparsedr   r   r   r(      s<    			"	'zWriter.writeNFc             C   s  t    } |  j r t n	 d d   } |  j d  } | | _ d | d | i } | o] | d k }	 |	 rp d | d <| j r | j | j  | j | d	 <t j | d |  }
 | d k	 r[|  j d
  } d | d | j d | d t | j	  d | d  i } t
 | d  rF| j d k	 rF| j d | d  d d d d | j i  t j | d
 |  }
 | j   sx0 | j D]% } | j |  j | | d |  qqW| ` | S)zRecursively process tree, adding nodes and edges to Tree object (PRIVATE).

        Returns a set of all OTUs encountered.
        c             S   s   |  S)Nr   )r   r   r   r   <lambda>:  s    z$Writer._write_tree.<locals>.<lambda>rD   r9   re   Nr=   r<   r:   rV   r>   r?   r@   Ztypeofz	cdao:Edger0   r.   zcdao:has_Support_Valuedatatypez	xsd:floatrB   z%1.2fparent)rM   r   ra   rT   r;   rN   r   rn   strrA   hasattrr0   ro   Zis_terminalr[   rp   )r*   r\   rf   ry   rH   rv   Zconvert_urirT   r1   r<   rD   Zedge_idZ	new_clader   r   r   rp   3  s<    		
		#zWriter._write_tree)r   r   r    r!   r+   ra   r(   rp   r   r   r   r   r'      s
   8r'   )r	   r   )r	   r   )(r!   Z	Bio._py3kr   Z	Bio.Phylor   xml.domr   sysZ	_cdao_owlr   r   r   version_infoZ	xml.etreer   r   ImportErrorr   ro   rk   rj   rl   r   AttributeErrorr{   ZET_pyr   rm   r   r   r   r   r   	Exceptionr   r#   r(   objectr"   r'   r   r   r   r   <module>   sH   )

	x