
    瞤d;              
       V   d Z ddlm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mZ dd	lmZmZmZmZ 	 dd
lmZ dZn# e$ r dZY nw xY w G d d          Zd Zej         d                    d  e e ej                              d d          D                       z  e_         dS )zw
this module defines the evolutionary Model that can be linked
to phylogeny, and computed by one of codeml, gerp, slr.
    )absolute_importN)range)sub)warn   )PARAMSAVAIL)
parse_paml	parse_rstget_ancestor	parse_slr)SequencePlotFaceTFc                   T    e Zd ZdZddZd Zd Zd Zd Z	 	 ddZ	ddZ
ddZd ZdS )ModelaD  Evolutionary model.
    "omega" stands for starting value of omega, in the computation. As
    Zihen Yang says, it is good to try with different starting values...
    model linked to tree by _tree variable
    results of calculation are stored in dictionaries:
     * branches: w dN dS bL by mean of their node_id
     * sites   : values at each site.
     * classes : classes of sites and proportions
     * stats   : lnL number of parameters kappa value and codon frequencies stored here.

    available models are:
        =========== ============================= ==================
        Model name  Description                   Model kind
        =========== ============================= ==================
%s
        =========== ============================= ==================


    :argument model_name: string with model name. Add a dot followed by anything at the end of the string in order to extend the name of the model and avoid overwriting.
    :argument None tree: a Tree object
    :argument None path: path to outfile, were model computation output can be found.

    Nc                 l   || _         t          |          \  | _        }d | _        d | _        d | _        i | _        i | _        i | _        t          |
                                          D ]\  }}|| j        |<   t          t          t          j
                                        }|                     |           t          |
                                          D ])\  }	}
|	|vrt          d|	z             |	dk    r|
 }
|
||	<   *|                                  |r|                     |           d S d S )Nz0WARNING: unknown param %s, can cause problems...gappy)_tree
check_namenamesitesclasses	n_classesbranchesstats
propertieslistitemsdictr   _change_paramsr   _Model__check_marks_load)self
model_nametreepathkwargsargsabparamskeyargs              /lib/python3.11/site-packages/ete3/evol/model.py__init__zModel.__init__Q   s@   
$Z00	4

&& 	$ 	$DAq"#DOQd6<>>**++F###V\\^^,, 	 	HC&  G#NOOOg~~gF3KK 	JJt	 	    c                    d}d}t          | j                                                  D ]\  }}|                                r/||| j        |j                 d         d|j        |j        pdfz  z  }H||| j        |j                 d         | j        |j                                     dd          |j        |j        pdfz  z  }d}d}| j        rbd	 t          | j                  D             D ]C||d

                    fdt          | j                           D                       fz  z  }Dd| j        dd| j        v r| j        nddd| j        v r| j        ndd| j        rFd
                    t          t          | j                                                                      ndd| j        r|ndd| j        r|nddS )z$
        to print nice info
         z:
        mark:%-5s, omega: %-10s, node_ids: %-4s, name: %smarkNoneROOTwEDGEz
        %-12s: %s c                     g | ]}|S  r8   ).0ts     r-   
<listcomp>z!Model.__str__.<locals>.<listcomp>{   s    666Aa666r/    c                 6    g | ]\  }}d          |d|dS )r   =9r8   )r9   jir:   s      r-   r;   z!Model.__str__.<locals>.<listcomp>|   sJ     5Q 5Q 5Q9=A EFaDD!!!QQQ5O 5Q 5Q 5Qr/   z Evolutionary Model z!:
        log likelihood       : lnLz 
        number of parameters : npz 
        sites inference      : z, z 
        sites classes        : z 
        branches             : z	
        )	enumerater   traverseis_rootr   node_idr   getr   sortedjoinr   rB   rC   r   r   keys)r"   str_markstr_linerA   nodestr_siter:   s         @r-   __str__zModel.__str__h   s8    P !4!4!6!677 		K 		KGAt||~~ KHdl(CF(K(.(,di6I6(K K K Hdl(CF(K(,dl(C(G(GHN)P )P(,di6I6(K K K )< 	366!5!5666 3 3H388 5Q 5Q 5Q 5Q5>t|A5O5O5Q 5Q 5Q ,2 ,2 (3 3 3 yyy DJ..txxF::DJ..twwF::>BjTtyyTZ__%6%6 7 788999fTT<3xxV33=4xxf44 	r/   c                    t          d | j                                        D                       }t          | j                                                  D ]o\  }}|r&| j        d         rd|j        pdi| j        |j        <   -d| j        d         v r"ddt          |          z   i| j        |j        <   ^ddi| j        |j        <   pd	S )
zn
        checks if tree is marked and if model allows marks.
        fill up branches dict with marks
        c                     g | ]	}|j         
S r8   )r2   )r9   ns     r-   r;   z'Model.__check_marks.<locals>.<listcomp>   s    FFF1FFFr/   
allow_markr2   z #0branchtypz #r1   N)
anyr   iter_descendantsrD   rE   r   r2   r   rG   str)r"   has_markrA   rN   s       r-   __check_markszModel.__check_marks   s    
 FF
(C(C(E(EFFFGG !4!4!6!677 	; 	;GAt ;DOL9 ;/5ty7IE.Jdl++T_U333/5tCFF{.Cdl++/5rldl++	; 	;r/   c                 j   | j         d         dk    rt          ||            d| j         d         v r8t          j        t	          |                    D ]\  }}t          | ||           d| j         d         v rt          ||            | j        d         t          |           d<   | j        d         t          |           d<   d
S | j         d         dk    r^t          j        t          |                    D ]\  }}t          | ||           d	t          |           d<   d	t          |           d<   d
S d
S )z9
        parse outfiles and load in model object
        execcodemlsiterV   ancestorrB   rC   Slrr   N)
r   r
   six	iteritemsr   setattrr   r   varsr   )r"   r%   r+   vals       r-   r!   zModel._load   s@    ?6"h..tT"""/// #ioo > > , ,HCD#s++++T_U333T4(((!%U!3DJJ!%T!2DJJ_V$--M)T*:*:;; ) )SsC((((!"DJJ!"DJJ	 .-r/   c                 J    | j         d         D ]
\  }}|||<   || j         d<   dS )z.
        change model specific values
        changesr*   N)r   )r"   r*   r+   changes       r-   r   zModel._change_params   s;      ?I6 	! 	!KC F3KK%+"""r/   Tg      ?g333333?barFc           	         | j         t          d| j        z             dS d|vrd| j        z  |d<   d| j         v rd}nd| j         v rd}nd}|                     ||          }d	|vrd
|d	<   |r!| j         |                             dd          }t
          ri	 t          | j         |         d         f|||d|d|}n# t          $ r t          d          w xY w|rt          |dd           nt          |dd           nd}|| j
        d<   dS )a-  
        To add histogram face for a given site mdl (M1, M2, M7, M8)
        can choose to put it up or down the tree.
        2 types are available:
           * stick: to draw histogram.
           * curve: to draw plot.
        You can define color scheme by passing a diccionary, default is:
            col = {'NS' : 'grey'  ,
                   'RX' : 'green' ,
                   'RX+': 'green' ,
                   'CN' : 'cyan'  ,
                   'CN+': 'blue'  ,
                   'PS' : 'orange',
                   'PS+': 'red'    }
        NzWARNING: model %s not computed.headerz$Omega value for sites under %s modelBEBNEBSLR)colylim)r   r   ser5   u
   Omega (ω))hlinescolorserrorsylabelkindzEERROR: no sites to display, only available histfaces for site models
upTFhistface)r   r   r   colorize_rstrH   TREEVIEWr   KeyError	Exceptionrd   r   )	r"   ry   rt   rx   rv   ru   r&   rf   hists	            r-   set_histfacezModel.set_histface   s   " :2di@AAA46!!E $	 +F8DJCCdj  CCC""3F"33#F6N 	5Z_((t44F 	?'
3(< 2V/5f/@t2 2 +12 2  ? ? ? !> ? ? ??  +dD))))dE****D&*
###s   $B< <Cc                 :   d}d| j         v r| j         d         }nd}dD ]-}||d|t          | j         d         |                   dz  }.|dz  }t          t          | j         d                                                   d 	          D ]f}|dv rt          | j         d         |                                       d
          r;||d|t          | j         d         |                   dz  }g||S t          |d                              |           dS )z
        generate ctrl string to write to a file, if file is given,
        write it, otherwise returns the string

        :argument None outfile: if a path is given here, write control string into it.

        :returns: the control string

        r1   sepz = )seqfiletreefileoutfilez>15r*   
c                 H    t          dd|                                           S )Nfix_r1   )r   lowerxs    r-   <lambda>z'Model.get_ctrl_string.<locals>.<lambda>  s    fb!''))44 r/   )r+   *Nr5   )r   rY   rI   r   rK   
startswithopenwrite)r"   r   stringr   prms        r-   get_ctrl_stringzModel.get_ctrl_string   sm    DO##/5)CCC5 	K 	KCccc33&)$/(*CC*H&I&I&I&IK KFF$$t9>>@@AA H5 H5 6 6 6 
	P 
	PC8884?H-c233>>sCC P ###ss*-dox.H.M*N*N*N*NP P?M#$$V,,,,,r/   c                     p	ddddddddd| j         d	         vrt          d
          |                     |          }fd|D             S )al  
        Colorize function, that take in argument a list of values
        corresponding to a list of classes and returns a list of
        colors to paint histogram.

        :param val: type of estimation, can be BEB or NEB (only
           positive-selection models have BEB)
        :param None col: a dictionary of colors that by default is:
           {"NS" : "grey",
            "RX" : "green",
            "RX+": "green",
            "CN" : "cyan",
            "CN+": "blue",
            "PS" : "orange",
            "PS+": "red"}
        
        :returns: a list of colors dependending categories of sites that are among:
          - CN+ > 0.99 probabylity of beloging to conserved class of site
          - CN  > 0.95 probabylity of beloging to conserved class of site
          - NS  not significant
          - RX+ > 0.99 probabylity of beloging to relaxed class of site
          - RX  > 0.95 probabylity of beloging to relaxed class of site
          - PS+ > 0.99 probabylity of beloging to positively-selected class of site
          - PS  > 0.95 probabylity of beloging to positively-selected class of site
        greygreencyanblueorangered)NSRXRX+CNCN+PSPS+r_   rV   z:ERROR: histogram are only for site and branch-site models.c                      g | ]
}|         S r8   r8   )r9   catrq   s     r-   r;   z&Model.colorize_rst.<locals>.<listcomp>7  s    ///SC///r/   )r   r~   significance_by_site)r"   rf   rq   
categoriess     ` r-   r{   zModel.colorize_rst  s    4  $V$$##%"$ $ /// 2 3 3 3..s33
////J////r/   c                 H   d| j         d         vrt          d          d| j         d         v }g }t          | j        |         d         | j        |         d                   D ]>\  }}|dk     r|                    d	           "|| j        |         k    r4|s2|d
k     r|                    d           Q|                    d           g|dk    r2|d
k     r|                    d           |                    d           || j        |         k    r4|r2|d
k     r|                    d           |                    d           || j        |         k    r4|d
k     r|                    d           |                    d           )|                    d	           @|S )a  
        Summarize significance of site models.

        :param val: type of estimation, can be BEB or NEB (only
           positive-selection models have BEB)
        
        :returns: a list of categories among:
          - CN+ > 0.99 probabylity of beloging to conserved class of site
          - CN  > 0.95 probabylity of beloging to conserved class of site
          - NS  not significant
          - RX+ > 0.99 probabylity of beloging to relaxed class of site
          - RX  > 0.95 probabylity of beloging to relaxed class of site
          - PS+ > 0.99 probabylity of beloging to positively-selected class of site
          - PS  > 0.95 probabylity of beloging to positively-selected class of site
        r_   rV   z,ERROR: only for site and branch-site models.positiveevolpvclassgffffff?r   gGz?r   r      r   r   r   r   )r   r~   zipr   appendr   )r"   rf   ps_modelr   pval
curr_classs         r-   r   zModel.significance_by_site9  s     /// 2 3 3 3!88
 #DJsOD$9$(JsOG$<!> !> 	( 	(D*d{{!!$''''t~c22282$;;%%d++++%%e,,,,q$;;%%d++++%%e,,,,t~c222x2$;;%%d++++%%e,,,,t~c222$;;%%d++++%%e,,,,!!$''''r/   )NN)Trj   rk   FN)N)__name__
__module____qualname____doc__r.   rP   r    r!   r   r   r   r{   r   r8   r/   r-   r   r   ;   s         *   .$ $ $N; ; ;# # #*, , , =B+/1+ 1+ 1+ 1+h!- !- !- !-F%0 %0 %0 %0N/ / / / /r/   r   c                 r    t          dd|           t          v r| t          t          dd|                    fS dS )zC
    check that model name corresponds to one of the available
    z\..*r1   N)r   r	   )models    r-   r   r   k  s?     62u&&eSU33444 '&r/   r   c           	      v    g | ]6}d d|z  ddt           |         d         ddt           |         d         dd7S )	z
          z%s8z   r   27rV   15z  r	   )r9   r   s     r-   r;   r;   u  s^     / / /!" #ajjjj%(6*:*:*:*:E!HUOOOOM / / /r/   c                 (    t           |          d         S )NrV   r   r   s    r-   r   r   w  s     %a r/   )r+   reverse)r   
__future__r   rb   	six.movesr   rer   warningsr   evol.controlr   r	   evol.parserr
   r   r   r   treeview.facesr   r|   ImportErrorr   r   rJ   rI   rK   r8   r/   r-   <module>r      s  P  ' & & & & & 



                   ( ( ( ( ( ( ( ( I I I I I I I I I I I I111111 HH    HHH
m m m m m m m m`	5 5 5  / /&,fVVJEJLL-A-A G0 G0(,'. '. './ / / 0 01s   = AA