o
    Rŀgd                     @   sP   d Z ddlZddlmZ ddlmZ G dd deZG dd	 d	eZ	d
d Z
dS )zClasses for the support of yn00.

Yang and Nielsen 2000,  estimating synonymous and nonsynonymous substitution
rates in pairwise comparison of protein-coding DNA sequences.
    N   )_parse_yn00)Pamlc                   @   s   e Zd ZdZdS )	Yn00Errorz@yn00 failed. Run with verbose=True to view yn00's error message.N)__name__
__module____qualname____doc__ r
   r
   G/var/www/html/myenv/lib/python3.10/site-packages/Bio/Phylo/PAML/yn00.pyr      s    r   c                   @   s4   e Zd ZdZdddZdd Zdd ZdddZdS )Yn00z/An interface to yn00, part of the PAML package.Nc                 C   s,   t | ||| d| _dddddd| _dS )zInitialize the Yn00 instance.

        The user may optionally pass in strings specifying the locations
        of the input alignment, the working directory and
        the final output file.
        zyn00.ctlN)verboseicode	weighting
commonf3x4ndata)r   __init__ctl_file_options)self	alignmentworking_dirout_filer
   r
   r   r      s   zYn00.__init__c                 C   s   |    t| jd;}|d| j d |d| j d | j D ]}|d du r-q$||d  d|d  d q$W d   dS 1 sHw   Y  dS )	zDynamically build a yn00 control file from the options.

        The control file is written to the location specified by the
        ctl_file property of the yn00 class.
        wz
seqfile = 
z
outfile = r   Nr   z = )_set_rel_pathsopenr   write_rel_alignment_rel_out_filer   items)r   
ctl_handleoptionr
   r
   r   write_ctl_file,   s    "zYn00.write_ctl_filec           	   
   C   sn  i }t j|std|t|}|D ]x}| }|ddd }|dkrd|vr3td||d\}}| }| }|dkrJ|| _q|d	krR|| _	q|| j
vr^td
| d|v sfd|v rxzt|}W n tyw   |}Y nw zt|}W n ty   |}Y nw |||< qW d   n1 sw   Y  | j
D ]}||v r|| | j
|< qd| j
|< qdS )zAParse a control file and load the options into the yn00 instance.zFile not found: *r   r    =z Malformed line in control file:
seqfileoutfilezInvalid option: .ze-N)ospathisfileFileNotFoundErrorr   stripsplitAttributeErrorr   r   r   KeyErrorfloat
ValueErrorint)	r   r   temp_optionsr!   lineuncommentedr"   valueconverted_valuer
   r
   r   read_ctl_file?   sR   


zYn00.read_ctl_fileFyn00Tc                 C   s"   t | ||| |rt| jS dS )zRun yn00 using the current configuration.

        If parse is True then read and return the result, otherwise
        return None.
        N)r   runreadr   )r   r   r   commandparser
   r
   r   r<   i   s   
zYn00.run)NNN)NFr;   T)r   r   r   r	   r   r#   r:   r<   r
   r
   r
   r   r      s    
*r   c           	      C   s   i }t j| stdt| }| }W d   n1 sw   Y  |s*tdt|D ]>\}}d|v r;|d }q.d|v rPt	||| |\}}|d }q.d|v rlt
||| ||}t||d d ||}q.|sstd|S )	zParse a yn00 results file.zResults file does not exist.NzWEmpty results file.  Did YN00 exit successfully?  Run 'Yn00.run()' with 'verbose=True'.z(A) Nei-Gojobori (1986) methodr   z (B) Yang & Nielsen (2000) methodz(C) LWL85, LPB93 & LWLm methodszInvalid results file.)r*   r+   existsr-   r   	readlinesr3   	enumerater   
parse_ng86
parse_yn00parse_others)	results_fileresultshandlelinesline_numr6   
ng86_start	sequences
yn00_startr
   r
   r   r=   u   s:   



r=   )r	   os.pathr*   r%   r   _pamlr   EnvironmentErrorr   r   r=   r
   r
   r
   r   <module>   s   ]