o
    Rŀg                     @   s@  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ G dd deZdd ZG dd de	jZG dd deZG dd deZG dd dee	jZG dd dee	jZG dd dee	jZG dd deZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eeZ G d%d& d&eZ!G d'd( d(eZ"G d)d* d*eZ#G d+d, d,eZ$G d-d. d.eZ%G d/d0 d0eZ&G d1d2 d2eZ'G d3d4 d4eZ(G d5d6 d6eZ)G d7d8 d8eZ*G d9d: d:eZ+G d;d< d<eZ,G d=d> d>eZ-G d?d@ d@eZ.G dAdB dBeZ/dS )CzClasses corresponding to phyloXML elements.

See Also
--------
Official specification:
   http://phyloxml.org/
Journal article:
    Han and Zmasek (2009), https://doi.org/10.1186/1471-2105-10-356

    N)BiopythonWarning)	Alignment)MultipleSeqAlignment)BaseTree)Seq)
SeqFeature)SimpleLocation)	SeqRecordc                   @      e Zd ZdZdS )PhyloXMLWarningz;Warning for non-compliance with the phyloXML specification.N__name__
__module____qualname____doc__ r   r   F/var/www/html/myenv/lib/python3.10/site-packages/Bio/Phylo/PhyloXML.pyr           r   c                 C   s4   | dur|| st jd|  dtdd dS dS dS )zFCheck a string using testfunc, and warn if there's no match (PRIVATE).NzString z doesn't match the given regexp   )
stacklevel)warningswarnr   )texttestfuncr   r   r   
_check_str$   s   

r   c                   @   r
   )PhyloElementz$Base class for all PhyloXML objects.Nr   r   r   r   r   r   1   r   r   c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )Phyloxmlav  Root node of the PhyloXML document.

    Contains an arbitrary number of Phylogeny elements, possibly followed by
    elements from other namespaces.

    :Parameters:
        attributes : dict
            (XML namespace definitions)
        phylogenies : list
            The phylogenetic trees
        other : list
            Arbitrary non-phyloXML elements, if any

    Nc                 C   s6   dddd| _ |r| j | |pg | _|pg | _dS )z*Initialize parameters for PhyloXML object.z)http://www.w3.org/2001/XMLSchema-instancezhttp://www.phyloxml.orgzAhttp://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd)z	xmlns:xsixmlnszxsi:schemaLocationN)
attributesupdatephylogeniesother)selfr   r    r!   r   r   r   __init__E   s   
zPhyloxml.__init__c                 C   sb   t |ttfr| j| S t |tstdt| d| jD ]}|j|kr)|  S qtd|)z!Get a phylogeny by index or name.z
can't use z as an indexzno phylogeny found with name )
isinstanceintslicer    strKeyErrortypename)r"   indextreer   r   r   __getitem__R   s   



zPhyloxml.__getitem__c                 C   
   t | jS )z6Iterate through the phylogenetic trees in this object.)iterr    r"   r   r   r   __iter__^      
zPhyloxml.__iter__c                 C   r.   )z7Return the number of phylogenetic trees in this object.)lenr    r0   r   r   r   __len__b   r2   zPhyloxml.__len__c                 C      d| j jdtt| jf S )z)Return name of phylogenies in the object.%s([%s]),
)	__class__r   joinmapr'   r    r0   r   r   r   __str__f   s   zPhyloxml.__str__NN)	r   r   r   r   r#   r-   r1   r4   r;   r   r   r   r   r   5   s    
r   c                   @   "   e Zd ZdZdddZdd ZdS )Othera  Container for non-phyloXML elements in the tree.

    Usually, an Other object will have either a 'value' or a non-empty list
    of 'children', but not both. This is not enforced here, though.

    :Parameters:
        tag : string
            local tag for the XML node
        namespace : string
            XML namespace for the node -- should not be the default phyloXML
            namespace.
        attributes : dict of strings
            attributes on the XML node
        value : string
            text contained directly within this XML node
        children : list
            child nodes, if any (also ``Other`` instances)

    Nc                 C   s*   || _ || _|p	i | _|| _|pg | _dS )z,Initialize values for non-phyloXML elements.N)tag	namespacer   valuechildren)r"   r?   r@   r   rA   rB   r   r   r   r#      s
   
zOther.__init__c                 C   r.   )z5Iterate through the children of this object (if any).)r/   rB   r0   r   r   r   r1      r2   zOther.__iter__)NNNN)r   r   r   r   r#   r1   r   r   r   r   r>   n       
r>   c                   @   s   e Zd ZdZ														dddZedd Zedd	 Zd
d Zdd Z	dd Z
edd Zdd Zdd Zdd ZeeeeZdS )	Phylogenya  A phylogenetic tree.

    :Parameters:
        root : Clade
            the root node/clade of this tree
        rooted : bool
            True if this tree is rooted
        rerootable : bool
            True if this tree is rerootable
        branch_length_unit : string
            unit for branch_length values on clades
        name : string
            identifier for this tree, not required to be unique
        id : Id
            unique identifier for this tree
        description : string
            plain-text description
        date : Date
            date for the root node of this tree
        confidences : list
            Confidence objects for this tree
        clade_relations : list
            CladeRelation objects
        sequence_relations : list
            SequenceRelation objects
        properties : list
            Property objects
        other : list
            non-phyloXML elements (type ``Other``)

    NTc                 C   sz   t |tsJ || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
p%g | _|p*g | _|p/g | _|p4g | _|p9g | _dS )z/Initialize values for phylogenetic tree object.N)r$   boolrootrooted
rerootablebranch_length_unitr)   r*   iddescriptiondateconfidencesclade_relationssequence_relations
propertiesr!   )r"   rF   rG   rH   rI   r)   r*   rJ   rK   rL   rM   rN   rO   rP   r!   r   r   r   r#      s   



zPhylogeny.__init__c                 K   sD   | t |j|j|j|jdurtt|jpdd}|j	| |S )zCreate a new Phylogeny given a Tree (from Newick/Nexus or BaseTree).

        Keyword arguments are the usual ``Phylogeny`` constructor parameters.
        N)rF   rG   r*   rJ   )
Clade
from_claderF   rG   r*   rJ   Idr'   __dict__r   )clsr,   kwargsphyr   r   r   	from_tree   s   
zPhylogeny.from_treec                 K   s   t |jdi |S )zCreate a new Phylogeny given a Newick or BaseTree Clade object.

        Keyword arguments are the usual ``PhyloXML.Clade`` constructor parameters.
        Nr   )rQ   rR   to_phylogeny)rU   claderV   r   r   r   rR      s   zPhylogeny.from_cladec                 C   s   | S )znReturn this tree, a PhyloXML-compatible Phylogeny object.

        Overrides the ``BaseTree`` method.
        r   r0   r   r   r   as_phyloxml   s   zPhylogeny.as_phyloxmlc                 K   s   t || gdS )z<Create a new Phyloxml object containing just this phylogeny.)r    )r   )r"   rV   r   r   r   to_phyloxml_container      zPhylogeny.to_phyloxml_containerc                 C   s,   dd }|  |dd}dd |D }t|S )zIConstruct a MultipleSeqAlignment from the aligned sequences in this tree.c                 S      t | tr| jjrdS dS NTFr$   Sequencemol_seq
is_alignedelemr   r   r   is_aligned_seq      z.Phylogeny.to_alignment.<locals>.is_aligned_seqpreorderTc                 s   s    | ]}|  V  qd S N)to_seqrecord).0seqr   r   r   	<genexpr>   s    z)Phylogeny.to_alignment.<locals>.<genexpr>)_filter_searchr   )r"   rf   seqsrecordsr   r   r   to_alignment   s   zPhylogeny.to_alignmentc           
      C   s   dd }|  |dd}g }g }|D ]}| }|t|j || q|r>t|\}}t||D ]	\}	}t|	|_q3nd}t||S )zFConstruct an Alignment object from the aligned sequences in this tree.c                 S   r^   r_   r`   rd   r   r   r   rf     rg   z+Phylogeny.alignment.<locals>.is_aligned_seqrh   TN)	rn   rj   appendbytesrl   r   parse_printed_alignmentzipr   )
r"   rf   ro   rp   linesrl   record	sequencescoordinatessequencer   r   r   	alignment  s   
zPhylogeny.alignmentc                 C   2   t | jdkr	dS t | jdkrtd| jd S )zEquivalent to self.confidences[0] if there is only 1 value (PRIVATE).

        See Also: ``Clade.confidence``, ``Clade.taxonomy``

        r   N   zAmore than 1 confidence value available; use Phylogeny.confidencesr3   rM   AttributeErrorr0   r   r   r   _get_confidence  s   
zPhylogeny._get_confidencec                 C   s~   |d u r	g | _ d S t|ttfrt|}n	t|tstdt| j dkr-| j | d S t| j dkr;|| j d< d S td)N-value must be a number or Confidence instancer   r}   Kmultiple confidence values already exist; use Phylogeny.confidences insteadrM   r$   floatr%   
Confidence
ValueErrorr3   rr   r"   rA   r   r   r   _set_confidence*  s   

zPhylogeny._set_confidencec                 C   s
   g | _ d S ri   rM   r0   r   r   r   _del_confidence=  s   
zPhylogeny._del_confidence)NTNNNNNNNNNNNN)r   r   r   r   r#   classmethodrX   rR   r[   r\   rq   propertyr{   r   r   r   
confidencer   r   r   r   rD      s<    "
$


rD   c                   @   s   e Zd ZdZ																	dddZedd Zdd Zd	d
 Zdd Z	dd Z
eee	e
Zdd Zdd ZeeeZdS )rQ   a  Describes a branch of the current phylogenetic tree.

    Used recursively, describes the topology of a phylogenetic tree.

    Both ``color`` and ``width`` elements should be interpreted by client code
    as applying to the whole clade, including all descendents, unless
    overwritten in-sub clades. This module doesn't automatically assign these
    attributes to sub-clades to achieve this cascade -- and neither should you.

    :Parameters:
        branch_length
            parent branch length of this clade
        id_source
            link other elements to a clade (on the xml-level)
        name : string
            short label for this clade
        confidences : list of Confidence objects
            used to indicate the support for a clade/parent branch.
        width : float
            branch width for this clade (including branch from parent)
        color : BranchColor
            color used for graphical display of this clade
        node_id
            unique identifier for the root node of this clade
        taxonomies : list
            Taxonomy objects
        sequences : list
            Sequence objects
        events : Events
            describe such events as gene-duplications at the root node/parent
            branch of this clade
        binary_characters : BinaryCharacters
            binary characters
        distributions : list of Distribution objects
            distribution(s) of this clade
        date : Date
            a date for the root node of this clade
        references : list
            Reference objects
        properties : list
            Property objects
        clades : list Clade objects
            Sub-clades
        other : list of Other objects
            non-phyloXML objects

    Nc                 C   s   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
pg | _	|p#g | _
|p(g | _|p-g | _|p2g | _|p7g | _|p<g | _|pAg | _dS )z&Initialize value for the Clade object.N)branch_length	id_sourcer*   widthcolornode_ideventsbinary_charactersrL   rM   
taxonomiesrx   distributions
referencesrP   cladesr!   )r"   r   r   r*   r   r   r   r   r   rL   rM   r   rx   r   r   rP   r   r!   r   r   r   r#   t  s"   






zClade.__init__c                    sh    |j |jd} fdd|D |_|j|_|j|_|jr)t|jj|jj|jj	nd|_|j
| |S )zCreate a new PhyloXML Clade from a Newick or BaseTree Clade object.

        Keyword arguments are the usual PhyloXML Clade constructor parameters.
        )r   r*   c                    s   g | ]}  |qS r   )rR   )rk   crU   r   r   
<listcomp>  s    z$Clade.from_clade.<locals>.<listcomp>N)r   r*   r   r   r   r   BranchColorredgreenbluerT   r   )rU   rZ   rV   	new_clader   r   r   rR     s   zClade.from_cladec                 K   s   t | | jd}|j| |S )z2Create a new phylogeny containing just this clade.)rF   rL   )rD   rL   rT   r   )r"   rV   rW   r   r   r   rY     s   zClade.to_phylogenyc                 C   r|   )z#Return confidence values (PRIVATE).r   Nr}   z=more than 1 confidence value available; use Clade.confidencesr~   r0   r   r   r   r        
zClade._get_confidencec                 C   s~   |du r	g | _ dS t|ttfrt|}n	t|tstdt| j dkr-| j | dS t| j dkr;|| j d< dS td)z#Set the confidence value (PRIVATE).Nr   r   r}   r   r   r   r   r   r   r     s   

zClade._set_confidencec                 C   s
   g | _ dS )z$Delete confidences values (PRIVATE).Nr   r0   r   r   r   r     r2   zClade._del_confidencec                 C   r|   )z*Get taxonomy list for the clade (PRIVATE).r   Nr}   z:more than 1 taxonomy value available; use Clade.taxonomies)r3   r   r   r0   r   r   r   _get_taxonomy  r   zClade._get_taxonomyc                 C   sT   t |ts	tdt| jdkr| j| dS t| jdkr&|| jd< dS td)z'Set a taxonomy for the clade (PRIVATE).z*assigned value must be a Taxonomy instancer   r}   zHmultiple taxonomy values already exist; use Phylogeny.taxonomies insteadN)r$   Taxonomyr   r3   r   rr   r   r   r   r   _set_taxonomy  s   
zClade._set_taxonomy)NNNNNNNNNNNNNNNNN)r   r   r   r   r#   r   rR   rY   r   r   r   r   r   r   r   taxonomyr   r   r   r   rQ   C  s<    3
*


rQ   c                   @   s   e Zd ZdZdd ZdS )r   zManage Tree branch's color.c                 O   s   t jj| g|R i | dS )z1Initialize parameters for the BranchColor object.N)r   r   r#   )r"   argsrV   r   r   r   r#        zBranchColor.__init__Nr   r   r   r   r#   r   r   r   r   r     s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )	AccessionzCaptures the local part in a sequence identifier.

    Example: In ``UniProtKB:P17304``, the Accession instance attribute ``value``
    is 'P17304' and the ``source`` attribute is 'UniProtKB'.
    c                 C      || _ || _dS )z&Initialize value for Accession object.N)rA   source)r"   rA   r   r   r   r   r#        
zAccession.__init__c                 C   s   | j  d| j S )1Show the class name and an identifying attribute.:)r   rA   r0   r   r   r   r;        zAccession.__str__Nr   r   r   r   r#   r;   r   r   r   r   r     s    r   c                   @   s4   e Zd ZdZedZ								dddZdS )
Annotationa  The annotation of a molecular sequence.

    It is recommended to annotate by using the optional 'ref' attribute.

    :Parameters:
        ref : string
            reference string, e.g. 'GO:0008270',
            'KEGG:Tetrachloroethene degradation', 'EC:1.1.1.1'
        source : string
            plain-text source for this annotation
        evidence : str
            describe evidence as free text (e.g. 'experimental')
        desc : string
            free text description
        confidence : Confidence
            state the type and value of support (type Confidence)
        properties : list
            typed and referenced annotations from external resources
        uri : Uri
            link

    ![a-zA-Z0-9_]+:[a-zA-Z0-9_\.\-\s]+Nc	           	      C   sF   t || jj || _|| _|| _|| _|| _|| _|| _	|pg | _
dS )z+Initialize value for the Annotation object.N)r   re_refmatchrefr   evidencer)   descr   urirP   )	r"   r   r   r   r)   r   r   r   rP   r   r   r   r#   0  s   zAnnotation.__init__)NNNNNNNN)r   r   r   r   recompiler   r#   r   r   r   r   r     s    
r   c                   @   s,   e Zd ZdZ									dddZdS )BinaryCharacterszBinary characters at the root of a clade.

    The names and/or counts of binary characters present, gained, and lost
    at the root of a clade.
    Nc
           
      C   sJ   || _ || _|| _|| _|| _|pg | _|pg | _|pg | _|	p!g | _dS )z2Initialize values for the BinaryCharacters object.N)	r)   gained_count
lost_countpresent_countabsent_countgainedlostpresentabsent)
r"   r)   r   r   r   r   r   r   r   r   r   r   r   r#   Q  s   


zBinaryCharacters.__init__)	NNNNNNNNNr   r   r   r   r   r   J  s    	r   c                   @      e Zd ZdZdddZdS )CladeRelationa  Expresses a typed relationship between two clades.

    For example, this could be used to describe multiple parents of a clade.

    :type id_ref_0: str
    :type id_ref_1: str
    :type distance: str
    :type type: str

    :type confidence: Confidence
    Nc                 C   s"   || _ || _|| _|| _|| _dS )z/Initialize values for the CladeRelation object.N)distancer)   id_ref_0id_ref_1r   r"   r)   r   r   r   r   r   r   r   r#   x  
   
zCladeRelation.__init__r<   r   r   r   r   r   r   k  s    r   c                       s.   e Zd ZdZd fdd	Zedd Z  ZS )r   aU  A general purpose confidence element.

    For example, this can be used to express the bootstrap support value of a
    clade (in which case the ``type`` attribute is 'bootstrap').

    :Parameters:
        value : float
            confidence value
        type : string
            label for the type of confidence, e.g. 'bootstrap'

    unknownc                    s   t  | |}||_|S )zHCreate and return a Confidence object with the specified value and type.)super__new__r)   )rU   rA   r)   objr8   r   r   r     s   zConfidence.__new__c                 C   s   t | S )z0Return the float value of the Confidence object.)r   r0   r   r   r   rA     s   zConfidence.value)r   )r   r   r   r   r   r   rA   __classcell__r   r   r   r   r     s
    r   c                   @   r=   )Dateag  A date associated with a clade/node.

    Its value can be numerical by using the 'value' element and/or free text
    with the 'desc' element' (e.g. 'Silurian'). If a numerical value is used, it
    is recommended to employ the 'unit' attribute.

    :Parameters:
        unit : string
            type of numerical value (e.g. 'mya' for 'million years ago')
        value : float
            the date value
        desc : string
            plain-text description of the date
        minimum : float
            lower bound on the date value
        maximum : float
            upper bound on the date value

    Nc                 C   "   || _ || _|| _|| _|| _dS )z%Initialize values of the Date object.N)rA   unitr   minimummaximum)r"   rA   r   r   r   r   r   r   r   r#     r   zDate.__init__c                 C   s:   | j r| jdur| j d| j  S | jdur| jS | jjS )z0Show the class name and the human-readable date.N )r   rA   r   r8   r   r0   r   r   r   r;     s
   
zDate.__str__NNNNNr   r   r   r   r   r     rC   r   c                   @   r   )Distributiona  Geographic distribution of the items of a clade (species, sequences).

    Intended for phylogeographic applications.

    :Parameters:
        desc : string
            free-text description of the location
        points : list of ``Point`` objects
            coordinates (similar to the 'Point' element in Google's KML format)
        polygons : list of ``Polygon`` objects
            coordinate sets defining geographic regions

    Nc                 C   s   || _ |pg | _|pg | _dS )z)Initialize values of Distribution object.N)r   pointspolygons)r"   r   r   r   r   r   r   r#     s   
zDistribution.__init__)NNNr   r   r   r   r   r     s    r   c                   @   r   )DomainArchitecturezDomain architecture of a protein.

    :Parameters:
        length : int
            total length of the protein sequence
        domains : list ProteinDomain objects
            the domains within this protein

    Nc                 C   r   )z3Initialize values of the DomainArchitecture object.Nlengthdomains)r"   r   r   r   r   r   r#     r   zDomainArchitecture.__init__r<   r   r   r   r   r   r     s    
r   c                   @   st   e Zd ZdZh dZ					dddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )Eventsa&  Events at the root node of a clade (e.g. one gene duplication).

    All attributes are set to None by default, but this object can also be
    treated as a dictionary, in which case None values are treated as missing
    keys and deleting a key resets that attribute's value back to None.
    >   mixedr!   fusiontransfer
unassignedspeciation_or_duplicationNc                 C   s0   t || jj || _|| _|| _|| _|| _dS )z'Initialize values of the Events object.N)r   ok_type__contains__r)   duplicationsspeciationslossesr   )r"   r)   r   r   r   r   r   r   r   r#     s   	
zEvents.__init__c                 C      dd | j  D S )zReturn Event's items.c                 S   s    g | ]\}}|d ur||fqS ri   r   rk   kvr   r   r   r     s     z Events.items.<locals>.<listcomp>rT   itemsr0   r   r   r   r   	     zEvents.itemsc                 C   r   )zReturn Event's keys.c                 S   s   g | ]
\}}|d ur|qS ri   r   r   r   r   r   r     s    zEvents.keys.<locals>.<listcomp>r   r0   r   r   r   keys  r   zEvents.keysc                 C   r   )z6Return values from a key-value pair in an Events dict.c                 S   s   g | ]}|d ur|qS ri   r   )rk   r   r   r   r   r     s    z!Events.values.<locals>.<listcomp>)rT   valuesr0   r   r   r   r     r   zEvents.valuesc                 C      t |  S )zReturn number of Events.)r3   r   r0   r   r   r   r4     s   zEvents.__len__c                 C   sB   zt | |}W n ty   t|dw |du rt|d|S )z&Get value of Event with the given key.Nz  has not been set in this object)getattrr   r(   r"   keyvalr   r   r   r-     s   
zEvents.__getitem__c                 C   s   t | || dS )zAdd item to Event dict.Nsetattrr   r   r   r   __setitem__$     zEvents.__setitem__c                 C   s   t | |d dS )zDelete Event with given key.Nr   r"   r   r   r   r   __delitem__(  r   zEvents.__delitem__c                 C   r   )z/Iterate over the keys present in a Events dict.)r/   r   r0   r   r   r   r1   ,  s   zEvents.__iter__c                 C   s&   zt | |duW S  ty   Y dS w )z'Return True if Event dict contains key.NF)r   r   r   r   r   r   r   0  s
   zEvents.__contains__r   )r   r   r   r   r   r#   r   r   r   r4   r-   r   r   r1   r   r   r   r   r   r     s$    

r   c                   @   r=   )rS   zA general-purpose identifier element.

    Allows to indicate the provider (or authority) of an identifier, e.g. NCBI,
    along with the value itself.
    Nc                 C   r   )z,Initialize values for the identifier object.N)rA   provider)r"   rA   r   r   r   r   r#   ?  r   zId.__init__c                 C   s"   | j dur| j  d| j S | jS )zReturn identifier as a string.Nr   )r   rA   r0   r   r   r   r;   D  s   
z
Id.__str__ri   r   r   r   r   r   rS   8  s    
rS   c                   @   s,   e Zd ZdZedZdddZdd ZdS )	MolSeqa  Store a molecular sequence.

    :Parameters:
        value : string
            the sequence itself
        is_aligned : bool
            True if this sequence is aligned with the others (usually meaning
            all aligned seqs are the same length and gaps may be present)

    z[a-zA-Z\.\-\?\*_]+Nc                 C      t || jj || _|| _dS )z,Initialize parameters for the MolSeq object.N)r   re_valuer   rA   rc   )r"   rA   rc   r   r   r   r#   Y     
zMolSeq.__init__c                 C   s   | j S )z2Return the value of the Molecular Sequence object.)rA   r0   r   r   r   r;   _  s   zMolSeq.__str__ri   )	r   r   r   r   r   r   r  r#   r;   r   r   r   r   r   K  s
    

r   c                   @   r   )Pointa  Geographic coordinates of a point, with an optional altitude.

    Used by element 'Distribution'.

    :Parameters:
        geodetic_datum : string, required
            the geodetic datum (also called 'map datum'). For example, Google's
            KML uses 'WGS84'.
        lat : numeric
            latitude
        long : numeric
            longitude
        alt : numeric
            altitude
        alt_unit : string
            unit for the altitude (e.g. 'meter')

    Nc                 C   r   )z&Initialize value for the Point object.N)geodetic_datumlatlongaltalt_unit)r"   r  r  r  r  r  r   r   r   r#   x  r   zPoint.__init__r<   r   r   r   r   r   r  d  s    r  c                   @   r=   )PolygonzA polygon defined by a list of 'Points' (used by element 'Distribution').

    :param points: list of 3 or more points representing vertices.

    Nc                 C   s   |pg | _ dS )z(Initialize value for the Polygon object.N)r   )r"   r   r   r   r   r#     r]   zPolygon.__init__c                 C   r5   )z"Return list of points as a string.r6   r7   )r8   r   r9   r:   r'   r   r0   r   r   r   r;     r   zPolygon.__str__ri   r   r   r   r   r   r	    s    
r	  c                   @   s4   e Zd ZdZedZh dZh dZdddZ	dS )	Propertya  A typed and referenced property from an external resources.

    Can be attached to ``Phylogeny``, ``Clade``, and ``Annotation`` objects.

    :Parameters:
        value : string
            the value of the property
        ref : string
            reference to an external resource, e.g. "NOAA:depth"
        applies_to : string
            indicates the item to which a property applies to (e.g.  'node' for
            the parent node of a clade, 'parent_branch' for the parent branch of
            a clade, or just 'clade').
        datatype : string
            the type of a property; limited to xsd-datatypes
            (e.g. 'xsd:string', 'xsd:boolean', 'xsd:integer', 'xsd:decimal',
            'xsd:float', 'xsd:double', 'xsd:date', 'xsd:anyURI').
        unit : string (optional)
            the unit of the property, e.g. "METRIC:m"
        id_ref : Id (optional)
            allows to attached a property specifically to one element (on the
            xml-level)

    r   >   noderZ   r!   	phylogeny
annotationparent_branch>    xsd:intxsd:bytexsd:datexsd:gDayxsd:longxsd:time	xsd:float	xsd:gYear	xsd:short	xsd:token
xsd:anyURI
xsd:double
xsd:gMonth
xsd:stringxsd:booleanxsd:decimalxsd:integerxsd:dateTimexsd:durationxsd:gMonthDayxsd:hexBinaryxsd:gYearMonthxsd:unsignedIntxsd:base64Binaryxsd:unsignedBytexsd:unsignedLongxsd:unsignedShortxsd:negativeIntegerxsd:positiveIntegerxsd:normalizedStringxsd:nonNegativeIntegerxsd:nonPositiveIntegerNc                 C   s`   t || jj t || jj t || jj t || jj || _|| _|| _|| _	|| _
|| _dS )z)Initialize value for the Property object.N)r   r   r   ok_applies_tor   ok_datatyper   id_refrA   r   
applies_todatatype)r"   rA   r   r2  r3  r   r1  r   r   r   r#     s   
zProperty.__init__r<   )
r   r   r   r   r   r   r   r/  r0  r#   r   r   r   r   r
    s    
#r
  c                   @   s.   e Zd ZdZd	ddZedd Zdd ZdS )
ProteinDomaina  Represents an individual domain in a domain architecture.

    The locations use 0-based indexing, as most Python objects including
    SeqFeature do, rather than the usual biological convention starting at 1.
    This means the start and end attributes can be used directly as slice
    indexes on Seq objects.

    :Parameters:
        start : non-negative integer
            start of the domain on the sequence, using 0-based indexing
        end : non-negative integer
            end of the domain on the sequence
        confidence : float
            can be used to store e.g. E-values
        id : string
            unique identifier/name

    Nc                 C   r   )z,Initialize value for a ProteinDomain object.N)rA   startendr   rJ   )r"   rA   r5  r6  r   rJ   r   r   r   r#     r   zProteinDomain.__init__c                 C   s"   t |j|jj|jj|jddS )z,Create ProteinDomain object from SeqFeature.r   )r   )r4  rJ   locationr5  r6  
qualifiersget)rU   featr   r   r   from_seqfeature  s   
zProteinDomain.from_seqfeaturec                 C   sF   t t| j| j| jd}z| j}W n
 ty   Y |S w ||jd< |S )z2Create a SeqFeature from the ProteinDomain Object.)r7  rJ   r   )r   r   r5  r6  rA   r   r   r8  )r"   r:  r   r   r   r   to_seqfeature  s   

zProteinDomain.to_seqfeaturer<   )r   r   r   r   r#   r   r;  r<  r   r   r   r   r4    s    

	r4  c                   @   s$   e Zd ZdZedZdddZdS )	ReferencezLiterature reference for a clade.

    NB: Whenever possible, use the ``doi`` attribute instead of the free-text
    ``desc`` element.
    z[a-zA-Z0-9_\.]+/[a-zA-Z0-9_\.]+Nc                 C   r   )z2Initialize elements of the Reference class object.N)r   re_doir   doir   )r"   r?  r   r   r   r   r#      r  zReference.__init__r<   )r   r   r   r   r   r   r>  r#   r   r   r   r   r=    s    
r=  c                   @   sZ   e Zd ZdZh dZedZ												dddZe	dddZ
d	d
 ZdS )ra   a  A molecular sequence (Protein, DNA, RNA) associated with a node.

    One intended use for ``id_ref`` is to link a sequence to a taxonomy (via the
    taxonomy's ``id_source``) in case of multiple sequences and taxonomies per
    node.

    :Parameters:
        type : {'dna', 'rna', 'protein'}
            type of molecule this sequence represents
        id_ref : string
            reference to another resource
        id_source : string
            source for the reference
        symbol : string
            short symbol of the sequence, e.g. 'ACTM' (max. 10 chars)
        accession : Accession
            accession code for this sequence.
        name : string
            full name of the sequence, e.g. 'muscle Actin'
        location
            location of a sequence on a genome/chromosome.
        mol_seq : MolSeq
            the molecular sequence itself
        uri : Uri
            link
        annotations : list of Annotation objects
            annotations on this sequence
        domain_architecture : DomainArchitecture
            protein domains on this sequence
        other : list of Other objects
            non-phyloXML elements

    >   dnarnaproteinz\S{1,10}Nc                 C   sp   t || jj t || jj || _|| _|| _|| _|| _	|| _
|| _|| _|	| _|
| _|p/g | _|p4g | _dS )z'Initialize value for a Sequence object.N)r   typesr   	re_symbolr   r)   r1  r   symbol	accessionr*   r7  rb   r   domain_architectureannotationsr!   )r"   r)   r1  r   rE  rF  r*   r7  rb   r   rG  rH  r!   r   r   r   r#   M  s   
zSequence.__init__c                 C   s  |du r	d|j v }t|jd|j|jtt|j |d}|jd}|dur@d|v r/d|d< nd	|v r8d
|d< nd|v r@d|d< dD ]}||jv rP|j| ||< qBt	|jdt
rftdi |jd |d< |jdrg |d< |jd D ]B}i }dD ]}||v r|| ||< q{t	|dtrt|d  |d< t	|dtrdd |d D |d< |d tdi | qu|jrtt|j dd |jD d|d< tdi |S )z7Create a new PhyloXML Sequence from a SeqRecord object.N- )rF  rE  r*   rb   molecule_typeDNAr@  r)   RNArA  rB  )r1  r   r7  r   rH  )r   r   r   r)   r   r   rP   c                 S   s$   g | ]}t |trtd i |qS )r   )r$   dictr
  rk   propr   r   r   r     s    z+Sequence.from_seqrecord.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r4  r;  )rk   r:  r   r   r   r     s    
r   rG  r   )rl   r   rJ   r*   rK   r   r'   rH  r9  r$   rN  Urilistr   rr   r   featuresr   r3   ra   )rU   rw   rc   paramsrK  r   annotann_argsr   r   r   from_seqrecordo  sX   






zSequence.from_seqrecordc              	      s   dd  t t| jjfi  t| j| j| jd}| jr(dd | jj	D |_
| jdkr0d}n| jdkr8d	}n
| jd
kr@d
}nd} | j| j| j| joY | jj| jj| jjd|| jof fdd| jD d|_|S )a  Create a SeqRecord object from this Sequence instance.

        The seqrecord.annotations dictionary is packed like so::

            { # Sequence attributes with no SeqRecord equivalent:
              'id_ref': self.id_ref,
              'id_source': self.id_source,
              'location': self.location,
              'uri': { 'value': self.uri.value,
                              'desc': self.uri.desc,
                              'type': self.uri.type },
              # Sequence.annotations attribute (list of Annotations)
              'annotations': [{'ref': ann.ref,
                               'source': ann.source,
                               'evidence': ann.evidence,
                               'type': ann.type,
                               'confidence': [ann.confidence.value,
                                              ann.confidence.type],
                               'properties': [{'value': prop.value,
                                                'ref': prop.ref,
                                                'applies_to': prop.applies_to,
                                                'datatype': prop.datatype,
                                                'unit': prop.unit,
                                                'id_ref': prop.id_ref}
                                               for prop in ann.properties],
                              } for ann in self.annotations],
            }

        c                 S   s   dd |   D S )z+Remove None-valued items from a dictionary.c                 S   s   i | ]\}}|d ur||qS ri   r   )rk   r   r   r   r   r   
<dictcomp>  s    z=Sequence.to_seqrecord.<locals>.clean_dict.<locals>.<dictcomp>)r   )dctr   r   r   
clean_dict  r   z)Sequence.to_seqrecord.<locals>.clean_dict)rJ   r*   rK   c                 S   s   g | ]}|  qS r   )r<  )rk   domr   r   r   r     s    z)Sequence.to_seqrecord.<locals>.<listcomp>r@  rL  rA  rM  rB  NrA   r   r)   c                    sL   g | ]"} |j |j|j|j|jo|jj|jjg fd d|jD dqS )c              
      s.   g | ]} |j |j|j|j|j|jd qS )rA   r   r2  r3  r   r1  r]  rO  rZ  r   r   r     s    z4Sequence.to_seqrecord.<locals>.<listcomp>.<listcomp>)r   r   r   r)   r   rP   )r   r   r   r)   r   rA   rP   )rk   annr^  r   r   r     s    
)r1  r   r7  r   rK  rH  )r	   r   rb   rA   r'   rF  rE  r*   rG  r   rS  r)   r1  r   r7  r   r   rH  )r"   seqrecrK  r   r^  r   rj     sR   




+zSequence.to_seqrecord)NNNNNNNNNNNNri   )r   r   r   r   rC  r   r   rD  r#   r   rW  rj   r   r   r   r   ra   '  s(    "

"6ra   c                   @   s"   e Zd ZdZh dZdddZdS )SequenceRelationa1  Express a typed relationship between two sequences.

    For example, this could be used to describe an orthology (in which case
    attribute 'type' is 'orthology').

    :Parameters:
        id_ref_0 : Id
            first sequence reference identifier
        id_ref_1 : Id
            second sequence reference identifier
        distance : float
            distance between the two sequences
        type : restricted string
            describe the type of relationship
        confidence : Confidence
            confidence value for this relation

    >   r!   r   paralogyxenology	orthologyultra_paralogysuper_orthologyone_to_one_orthologyNc                 C   s0   t || jj || _|| _|| _|| _|| _dS zInitialize the class.N)r   r   r   r   r)   r   r   r   r   r   r   r   r#   .  s   
zSequenceRelation.__init__r<   )r   r   r   r   r   r#   r   r   r   r   ra    s    ra  c                   @   sH   e Zd ZdZedZh dZ										d	ddZdd Z	dS )
r   a:  Describe taxonomic information for a clade.

    :Parameters:
        id_source : Id
            link other elements to a taxonomy (on the XML level)
        id : Id
            unique identifier of a taxon, e.g. Id('6500',
            provider='ncbi_taxonomy') for the California sea hare
        code : restricted string
            store UniProt/Swiss-Prot style organism codes, e.g. 'APLCA' for the
            California sea hare 'Aplysia californica'
        scientific_name : string
            the standard scientific name for this organism, e.g. 'Aplysia
            californica' for the California sea hare
        authority : string
            keep the authority, such as 'J. G. Cooper, 1863', associated with
            the 'scientific_name'
        common_names : list of strings
            common names for this organism
        synonyms : list of strings
            synonyms for this taxon?
        rank : restricted string
            taxonomic rank
        uri : Uri
            link
        other : list of Other objects
            non-phyloXML elements

    z[a-zA-Z0-9_]{2,10}>0   formclassgenusorderr!   tribebranchcohortdomainfamilylegionphylumkingdomspeciessubformr   varietycultivardivisionsubclasssubgenussubordersubtribe	subcohort	subfamily	sublegion	subphylum
infraclass
infratribe
subkingdom
subspecies
subvariety
superclass
superorder
supertribeinfracohortinfralegioninfraphylummicrophylumsubdivisionsupercohortsuperfamilysuperlegionsuperphyluminfrakingdomsuperspeciesinfradivisionsuperdivisionNc                 C   sh   t || jj t || jj || _|| _|| _|| _|| _	|| _
|| _|p&g | _|	p+g | _|
p0g | _dS rh  )r   re_coder   ok_rankr   r   rJ   codescientific_name	authorityrankr   common_namessynonymsr!   )r"   r   rJ   r  r  r  r  r   r  r  r!   r   r   r   r#     s   

zTaxonomy.__init__c                 C   sL   | j dur| j S | jdur| jS | jdur| jS | jdur"t| jS | jjS )r   N)r  r  r  rJ   r'   r8   r   r0   r   r   r   r;     s   




zTaxonomy.__str__)
NNNNNNNNNN)
r   r   r   r   r   r   r  r  r#   r;   r   r   r   r   r   8  s     
6
r   c                   @   r=   )rQ  zA uniform resource identifier.

    In general, this is expected to be an URL (for example, to link to an image
    on a website, in which case the ``type`` attribute might be 'image' and
    ``desc`` might be 'image of a California sea hare').
    Nc                 C   s   || _ || _|| _dS rh  r\  )r"   rA   r   r)   r   r   r   r#     s   
zUri.__init__c                 C   s   | j r| j S t| S )z$Return string representation of Uri.)rA   reprr0   r   r   r   r;     s   zUri.__str__r<   r   r   r   r   r   rQ    s    
rQ  )0r   r   r   Bior   	Bio.Alignr   r   	Bio.Phylor   Bio.Seqr   Bio.SeqFeaturer   r   Bio.SeqRecordr	   r   r   TreeElementr   r   r>   TreerD   rQ   r   r   r   r   r   r   r   r   r   r   r   rS   r   r  r	  r
  r4  r=  ra   ra  r   rQ  r   r   r   r   <module>   sT   9" 4 84!&PT2 i)~