
    Xfd                     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 )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                       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__     3lib/python3.11/site-packages/Bio/Phylo/PAML/yn00.pyr   r      s        JJJJr   r   c                   .    e Zd ZdZd
dZd Zd Zdd	ZdS )Yn00z/An interface to yn00, part of the PAML package.Nc                 \    t          j        | |||           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_files       r   r   zYn00.__init__   sB     	dI{H==="
 
r   c                    |                                   t          | j        d          5 }|                    d| j         d           |                    d| j         d           | j                                        D ]2}|d         |                    |d          d|d          d           3	 ddd           dS # 1 swxY w 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options      r   write_ctl_filezYn00.write_ctl_file,   sF    	$-%% 		AA$*=AAABBB@$*<@@@AAA---// A A!9$   F1I!?!?&)!?!?!?@@@@A		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		A 		As   B	CCCc                 H   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|| _	        || j
        vrt          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 )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_values	            r   read_ctl_filezYn00.read_ctl_file?   sd   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#y00,1DMM#4=88"*+Ff+F+F"G"GG"e||tu}}!<6;EllOO'1 !< !< !<6;OOO!<!<69%jjOO'1 !< !< !<6;OOO!<3BL09CC C C C C C C C C C C C C C C< m 	- 	-F%%(4V(<f%%(,f%%		- 	-s[   CE.D%$E.%D41E.3D44E.8EE.EE.E
E..E25E2Fyn00Tc                 `    t          j        | |||           |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parses        r   rD   zYn00.runi   s7     	x'222 	'&&&tr   )NNN)NFrB   T)r   r	   r
   r   r   r)   rA   rD   r   r   r   r   r      sd        99
 
 
 
"A A A&(- (- (-T	 	 	 	 	 	r   r   c                 8   i }t           j                            |           st          d          t	          |           5 }|                                }ddd           n# 1 swxY w Y   |st          d          t          |          D ]|\  }}d|v r|dz   }d|v r&t          j	        |||         |          \  }}|dz   }9d|v r?t          j
        |||         ||          }t          j        ||dz   d         ||          }}|st          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.)r1   r2   existsr4   r"   	readlinesr:   	enumerater   
parse_ng86
parse_yn00parse_others)	results_fileresultshandlelinesline_numr=   
ng86_start	sequences
yn00_starts	            r   rE   rE   u   s   G7>>,'' @ >???	l		 #v  ""# # # # # # # # # # # # # # # 
4
 
 	
 $E**  $+t33!AJJ/477#.#9j)*G$ $ Wi "AJJ.$66!,j)*GY G ".hlnn%w	 G  20111Ns    A!!A%(A%)r   os.pathr1   r,   r   _pamlr   EnvironmentErrorr   r   rE   r   r   r   <module>rZ      s                 K K K K K  K K KZ Z Z Z Z4 Z Z Zz    r   