o
    Rŀg                     @   s6   d Z ddlZddlZG dd deZG dd dZdS )zPBase class for the support of PAML, Phylogenetic Analysis by Maximum Likelihood.    Nc                   @   s   e Zd ZdZdS )	PamlErrorzKpaml has failed.

    Run with verbose=True to view the error message.
    N)__name__
__module____qualname____doc__ r   r   H/var/www/html/myenv/lib/python3.10/site-packages/Bio/Phylo/PAML/_paml.pyr      s    r   c                   @   sZ   e Zd Z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S )Pamlz&Base class for wrapping PAML commands.Nc                 C   sL   |du r
t  | _n|| _|durt j|std|| _|| _i | _dS )zInitialize the class.N,The specified alignment file does not exist.)	osgetcwdworking_dirpathexistsFileNotFoundError	alignmentout_file_options)selfr   r   r   r   r   r   __init__   s   
zPaml.__init__c                 C      dS )zWrite control file.Nr   r   r   r   r   write_ctl_file%       zPaml.write_ctl_filec                 C   r   )zRead control file.Nr   r   r   r   r   read_ctl_file(   r   zPaml.read_ctl_filec                 C   s.   | j  D ]}t|d  d|d   qdS )z8Print out all of the options and their current settings.r   z =    N)r   itemsprintr   optionr   r   r   print_options+   s   zPaml.print_optionsc                 K   s6   |  D ]\}}|| jvrtd| || j|< qdS )zSet the value of an option.

        This function abstracts the options dict to prevent the user from
        adding options that do not exist or misspelling options.
        Invalid option: N)r   r   KeyError)r   kwargsr   valuer   r   r   set_options0   s
   
zPaml.set_optionsc                 C   s"   || j vrtd| | j |S )zReturn the value of an option.r!   )r   r"   getr   r   r   r   
get_option<   s   
zPaml.get_optionc                 C   s   t | j S )z%Return the values of all the options.)listr   r   r   r   r   r   get_all_optionsC   s   zPaml.get_all_optionsc                 C   s^   | j durtj| j | _| jdurtj| j| j | _| jdur-tj| j| j | _dS dS )a  Convert all file/directory locations to paths relative to the current working directory (PRIVATE).

        paml 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   r   relpath_rel_working_dirr   _rel_alignmentr   _rel_out_filer   r   r   r   _set_rel_pathsG   s   


zPaml._set_rel_pathsc                 C   s6  | j du r	tdtj| j std| jdu rtd| jdu r&tdt }tj| js7t	| j t
| j |du rI|   | j}n
tj|sStd|r]t||g}nttj}tj||g||d}W d   n1 sww   Y  t
| |dkrtd	||f |dk rtd
||f dS )a~  Run a paml program using the current configuration.

        Check that the class attributes exist and raise an error
        if not. Then run the command and check if it succeeds with
        a return code of 0, otherwise raise an error.

        The arguments may be passed as either absolute or relative
        paths, despite the fact that paml requires relative paths.
        NzAlignment file not specified.r
   zOutput file not specified.z Working directory not specified.z*The specified control file does not exist.)stdoutstderrr   zM%s has failed (return code %i). Run with verbose = True to view error messagez+The %s process was killed (return code %i).)r   
ValueErrorr   r   r   r   r   r   r   mkdirchdirr   ctl_file
subprocesscallopendevnullr   OSError)r   r4   verbosecommandcwdresult_codednr   r   r   runU   sD   





zPaml.run)NNN)r   r   r   r   r   r   r   r    r%   r'   r)   r.   r?   r   r   r   r   r	      s    
r	   )r   r   r5   EnvironmentErrorr   r	   r   r   r   r   <module>   s
   