
    XfQ                      p    d Z ddl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 )zYClasses for the support of baseml.

Maximum likelihood analysis of nucleotide sequences.
    N   )_parse_baseml)Pamlc                       e Zd ZdZdS )BasemlErrorzDBASEML failed. Run with verbose=True to view BASEML's error message.N)__name__
__module____qualname____doc__     5lib/python3.11/site-packages/Bio/Phylo/PAML/baseml.pyr   r      s        NNNNr   r   c                   4    e Zd ZdZddZd Zd Zd Zdd
ZdS )Basemlz1An interface to BASEML, part of the PAML package.Nc           
      :   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S )zInitialize the Baseml 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.
        N'The specified tree file does not exist.z
baseml.ctlnoisyverboserunmodemodelmodel_optionsMgenendataclock	fix_kappakappa	fix_alphaalphaMalphancatGfix_rhorhonparK)nhomogetSERateAncestor
Small_Diff	cleandataicodefix_blengthmethod)	r   __init__ospathexistsFileNotFoundErrortreectl_file_options)self	alignmentr1   working_dirout_files        r   r,   zBaseml.__init__   sR    	dI{H===7>>$'' S'(QRRR	$
T
t
 t
 T	

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

        The control file is written to the location specified by the
        ctl_file property of the baseml class.
        wz
seqfile = 
z
outfile = ztreefile = r   Nr   r   r   )	   
   zmodel = z  z = )	_set_rel_pathsopenr2   write_rel_alignment_rel_out_file	_rel_treer3   items)r4   
ctl_handleoptions      r   write_ctl_filezBaseml.write_ctl_fileD   s    	$-%% 	AA$*=AAABBB@$*<@@@AAA=4>===>>>---// A A!9$ !9// !9''F1I,@,@}_5A"((TvayTTDM/4RTT   !  F1I!?!?&)!?!?!?@@@@)A		A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	As   DD<<E E c                    i }t           j                            |          st          d|          t	          |          5 }|D ]}|                                }|                    dd          d         }|dk    rd|vrt          d|          |                    d          \  }}|                                }|                                }|dk    r|| _        |d	k    r|| _	        |d
k    r|| _
        || j        vrt          d|           |dk    rt          |          dk    r-|                                rt          |          |d<   d|d<   |                    d          d         }|                    d          d                                         }	t          |          |d<   |	|d<   |d|v s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 Baseml instance.zFile not found: *r   r    =z Malformed line in control file:
seqfiletreefileoutfilezInvalid option: r      Nr    .ze-)r-   r.   isfiler0   r>   stripsplitAttributeErrorr5   r1   r7   r3   KeyErrorlenisdigitint	partitionfloat
ValueError)r4   r2   temp_optionsrD   lineuncommentedrE   value	model_num	model_optconverted_values              r   read_ctl_filezBaseml.read_ctl_filef   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*@*@!' %!Y..-2DNN#z11(-DII#y00,1DMM#4=88"*+Ff+F+F"G"GG#w.."5zzQ5==??8;E

W 5@D_ = =,1OOC,@,@,C	,1OOC,@,@,C,I,I,K,K	8;IW 5@I_ = ="e||tu}}!<6;EllOO'1 !< !< !<6;OOO!<!<69%jjOO'1 !< !< !<6;OOO!<3BL0O'C(C (C (C (C (C (C (C (C (C (C (C (C (C (C (CR m 	- 	-F%%(4V(<f%%(,f%%		- 	-s[   FH$GH$G*'H$)G**H$.G>=H$>H
H$H
H$$H(+H(c                     t          j        |            | j        1t          j                            | j        | j                  | _        dS dS )zMake file/directory paths relative to the PWD (PRIVATE).

        BASEML 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   r=   r1   r-   r.   relpathr6   rB   )r4   s    r   r=   zBaseml._set_rel_paths   sE     	D!!!9 W__TY8HIIDNNN ! r   FbasemlTc                     | j         t          d          t          j                            | j                   st          d          t          j        | |||           |rt          | j	                  S dS )ak  Run baseml using the current configuration.

        Check that the tree attribute is specified and exists, and then
        run baseml. If parse is True then read and return the result,
        otherwise return none.

        The arguments may be passed as either absolute or relative paths,
        despite the fact that BASEML requires relative paths.
        NzTree file not specified.r   )
r1   r[   r-   r.   r/   r0   r   runreadr7   )r4   r2   r   commandparses        r   rh   z
Baseml.run   sv     97888w~~di(( 	O#$MNNNx'222 	'&&&tr   )NNNN)NFrf   T)	r   r	   r
   r   r,   rF   rc   r=   rh   r   r   r   r   r      sx        ;;'
 '
 '
 '
R A  A  AD3- 3- 3-j	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	        |||          }|
                    d          t          d          |S )zParse a BASEML results file.zResults file does not exist.Nz[Empty results file.  Did BASEML exit successfully?  Run 'Baseml.run()' with 'verbose=True'.versionzInvalid results file)r-   r.   r/   r0   r>   	readlinesr[   r   parse_basicsparse_parametersget)results_fileresultshandlelines
num_paramss        r   ri   ri      s   G7>>,'' @ >???	l		 #v  ""# # # # # # # # # # # # # # # 
6
 
 	
 *6ugFFWj,UGZHHG{{9%/000Ns    A!!A%(A%)r   r-   os.pathrI   r   _pamlr   EnvironmentErrorr   r   ri   r   r   r   <module>rz      s    
 
			             O O O O O" O O O_ _ _ _ _T _ _ _D    r   