
    Xf"                     h    d Z ddlZddlmZ ddlmZ  G d de          Z G d d	e          Z	d
 Z
dS )zaClasses for the support of CODEML.

Maximum likelihood analysis using codon substitution models.
    N   )_parse_codeml)Pamlc                       e Zd ZdZdS )CodemlErrorzDCODEML failed. Run with verbose=True to view CODEML's error message.N)__name__
__module____qualname____doc__     5lib/python3.11/site-packages/Bio/Phylo/PAML/codeml.pyr   r      s        NNNNr   r   c                   :    e Zd ZdZddZd Zd Zd Zd ZddZ	dS )Codemlz1An interface to CODEML, part of the PAML package.Nc                 B   t          j        | |||           |.t          j                            |          st          d          || _        d| _        i ddddddddddd	dd
dddddddddddddddddddddddddddddddddd| _        dS )at  Initialize the codeml instance.

        The user may optionally pass in strings specifying the locations
        of the input alignment and tree files, the working directory and
        the final output file. Other options found in the CODEML control
        have typical settings by default to run site class models 0, 1 and
        2 on a nucleotide alignment.
        N'The specified tree file does not exist.z
codeml.ctlnoisyverboserunmodeseqtype	CodonFreqndataclockaaDist
aaRatefilemodelNSsitesicodeMgene	fix_kappakappa	fix_omegaomega)	fix_alphaalphaMalphancatGgetSERateAncestor
Small_Diff	cleandatafix_blengthmethodrhofix_rho)	r   __init__ospathexistsFileNotFoundErrortreectl_file_options)self	alignmentr5   working_dirout_files        r   r0   zCodeml.__init__   s^    	dI{H===7>>$'' S'(QRRR	$
T
t
 t
 t	

 
 T
 T
 d
 $
 T
 t
 T
 T
 
 T
  !
" T#
$  ;
 
 
r   c                 ~   |                                   t          | j        d          5 }|                    d| j         d           |                    d| j         d           |                    d| j         d           | j                                        D ]}|d         |d         d	k    rGd
	                    d |d         D                       }|                    |d          d| d           ^|                    |d          d|d          d           	 ddd           dS # 1 swxY w Y   dS )zDynamically build a CODEML control file from the options.

        The control file is written to the location specified by the
        ctl_file property of the codeml class.
        wz
seqfile = 
z
outfile = ztreefile = r   Nr   r    c              3   4   K   | ]}t          |          V  d S Nstr.0sites     r   	<genexpr>z(Codeml.write_ctl_file.<locals>.<genexpr>_   s(      &G&GTs4yy&G&G&G&G&G&Gr    = )
_set_rel_pathsopenr6   write_rel_alignment_rel_out_file	_rel_treer7   itemsjoin)r8   
ctl_handleoptionr   s       r   write_ctl_filezCodeml.write_ctl_fileI   s    	$-%% 	EA$*=AAABBB@$*<@@@AAA=4>===>>>---// E E!9$ !9	)) "hh&G&GVAY&G&G&GGGG$$q	%A%Ag%A%A%ABBBB$$q	%C%CfQi%C%C%CDDDDE		E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es   C:D22D69D6c           	      b   i }t           j                            |          st          d|          t	          |          5 }|D ]}|                                }|                    dd          d         }|dk    rld|vrt          d|          |                    dd          \  }}|                                }|                                }|dk    r|| _        |d	k    r|| _	        |d
k    r|| _
        |dk    r{|                    d          }t          t          |                    D ]B}		 t          ||	                   ||	<   # t          $ r t          d||	                    dw xY w||d<   <|| j        vrt#          d|           d|v r#	 t%          |          }
n5# t          $ r |}
Y n'w xY w	 t          |          }
n# t          $ r |}
Y nw xY w|
||<   	 ddd           n# 1 swxY w Y   | j        D ]!}||v r||         | j        |<   d| j        |<   "dS )zCParse a control file and load the options into the Codeml instance.zFile not found: *r   r    =z Malformed line in control file:
seqfiletreefileoutfiler   r?   zInvalid site class: NzInvalid option: .)r1   r2   isfiler4   rJ   stripsplitAttributeErrorr9   r5   r;   rangelenint
ValueError	TypeErrorr7   KeyErrorfloat)r8   r6   temp_optionsrQ   lineuncommentedrR   valuesite_classesnconverted_values              r   read_ctl_filezCodeml.read_ctl_filed   s   w~~h'' ,	C#$Cx$C$CDDDh )C:& (C (CD::<<D"&**S!"4"4Q"7K"b((k11"0 LD L L# #  +6*;*;C*C*C!' %!Y..-2DNN#z11(-DII#y00,1DMM#y00+0;;s+;+;L%*3|+<+<%=%= 0 0!069,q/6J6JLOO'1 !0 !0 !0*3(P|A(P(P+& +&+/%0!0 7CL33#4=88"*+Ff+F+F"G"GG"e||!<6;EllOO'1 !< !< !<6;OOO!<!<69%jjOO'1 !< !< !<6;OOO!<3BL0Q(C)C )C )C )C )C )C )C )C )C )C )C )C )C )C )CT m 	- 	-F%%(4V(<f%%(,f%%		- 	-ss   C7G;;EG;$E8	8)G;"F21G;2G>G; GG;GG;G$!G;#G$$
G;;G?G?c                 &   | j                                         D ]v}|d         dk    rH|d         @d                    d |d         D                       }t          |d          d|            Vt          |d          d|d                     wdS )z8Print out all of the options and their current settings.r   r   r   Nr?   c              3   4   K   | ]}t          |          V  d S rA   rB   rD   s     r   rG   z'Codeml.print_options.<locals>.<genexpr>   s(      "C"C3t99"C"C"C"C"C"Cr   rH   )r7   rO   rP   print)r8   rR   r   s      r   print_optionszCodeml.print_options   s    m))++ 	4 	4FayI%%&)*? (("C"C"C"C"CCC00w00111122vay223333	4 	4r   c                     t          j        |            | j        1t          j                            | j        | j                  | _        dS dS )zMake all file/directory paths relative to the PWD (PRIVATE).

        CODEML requires that all paths specified in the control file be
        relative to the directory from which it is called rather than
        absolute paths.
        N)r   rI   r5   r1   r2   relpathr:   rN   )r8   s    r   rI   zCodeml._set_rel_paths   sE     	D!!!9 W__TY8HIIDNNN ! r   FcodemlTc                     | j         t          d          t          j                            | j                   st          d          t          j        | |||           |rt          | j	                  S dS )a  Run codeml using the current configuration.

        If parse is True then read and return the results, otherwise
        return None.

        The arguments may be passed as either absolute or relative
        paths, despite the fact that CODEML requires relative paths.
        NzTree file not specified.r   )
r5   rc   r1   r2   r3   r4   r   runreadr;   )r8   r6   r   commandparses        r   rw   z
Codeml.run   sv     97888w~~di(( 	O#$MNNNx'222 	'&&&tr   )NNNN)NFru   T)
r   r	   r
   r   r0   rS   rn   rr   rI   rw   r   r   r   r   r      s        ;;-
 -
 -
 -
^E E E64- 4- 4-l
4 
4 
4	J 	J 	J     r   r   c                    i }t           j                            |           st          d          t	          |           5 }|                                }ddd           n# 1 swxY w Y   |st          d          t          j        ||          \  }}}t          j	        ||||          }t          j
        ||          }t          j        ||          }|st          d          |S )zParse a CODEML results file.zResults file does not exist.Nz[Empty results file.  Did CODEML exit successfully?  Run 'Codeml.run()' with 'verbose=True'.zInvalid results file)r1   r2   r3   r4   rJ   	readlinesrc   r   parse_basicsparse_nssitesparse_pairwiseparse_distances)results_fileresultshandlelinesmulti_modelsmulti_geness         r   rx   rx      s.   G7>>,'' @ >???	l		 #v  ""# # # # # # # # # # # # # # # 
6
 
 	
 ,9+EeW+U+U(WlK)%,TTG*5'::G+E7;;G 1/000Ns    A!!A%(A%)r   os.pathr1   rV   r   _pamlr   EnvironmentErrorr   r   rx   r   r   r   <module>r      s    
             O O O O O" O O Oj j j j jT j j jZ    r   