o
    Rŀgn                     @   s.   d Z ddlmZ ddlmZ G dd dZdS )zsControl GenePop through an easier interface.

This interface is less efficient than the standard GenePopControler

    )GenePop   )GenePopControllerc                   @   s   e Zd ZdZd0ddZdd Zd1dd	Z	
				d2ddZ	d3d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#d$ Zd%d& Zd'd( Zd)d* Zd4d.d/ZdS )5EasyControllerz@Define a class for an easier interface with the GenePop program.Nc                 C   s    || _ t|| _i | _i | _dS )zInitialize the controller.

        genepop_dir is the directory where GenePop is.

        The binary should be called Genepop (capital G)
        N)_fnamer   _controller_EasyController__fst_pair_locus!_EasyController__allele_frequency)selffnamegenepop_dir r   U/var/www/html/myenv/lib/python3.10/site-packages/Bio/PopGen/GenePop/EasyController.py__init__   s   

zEasyController.__init__c                 C   s@   t | j}t|}W d   n1 sw   Y  |j|jfS )z7Obtain the population list and loci list from the file.N)openr   r   readpop_list	loci_list)r
   frecr   r   r   get_basic_info"   s   zEasyController.get_basic_infoprobabilityc                 C   sh   |dkr| j | j}n|dkr| j | j}n| j | jd\}}}t|d D ]}t| q)t|S )z2Perform Hardy-Weinberg test on the given position.
deficiencyexcessz.Pr   )r   test_pop_hz_deficiencyr   test_pop_hz_excesstest_pop_hz_probrangenext)r
   pop_pos	test_typehw_resloci_resfisher_fullir   r   r   test_hw_pop)   s   

zEasyController.test_hw_popr   T'       c           	      C   sT   |dkr| j | j||||\}}}n| j | j||||\}}}t|t||fS )z0Perform Hardy-Weinberg global Heterozygote test.r   )r   test_global_hz_deficiencyr   test_global_hz_excesslist)	r
   r    	enum_testdememorizationbatches
iterationspop_resloc_resallr   r   r   test_hw_global8   s   	
zEasyController.test_hw_globalc                 C   sZ   | j | j|||d }|D ]}|\\}}	}
||kr|	|ks&||kr*|	|kr*|
  S qdS )zITest for linkage disequilibrium for each pair of loci in each population.r   N)r   test_ldr   )r
   locus1locus2r-   r.   r/   all_ldld_casel1l2resultr   r   r   test_ld_all_pairL   s   
 zEasyController.test_ld_all_pairc                 C   s   | j | jS )z"Estimate Nm. Just a simple bridge.)r   estimate_nmr   r
   r   r   r   r=   X   s   zEasyController.estimate_nmc                 C   s2   | j | j}|\}}t|}|| d | d S )zReturn the heterozygosity info for a certain locus on a population.

        Returns (Expected homozygotes, observed homozygotes,
        Expected heterozygotes, observed heterozygotes)
        r   r   calc_allele_genotype_freqsr   r+   r
   r   
locus_name
geno_freqspop_iterloc_iterpopsr   r   r   get_heterozygosity_info\   s   z&EasyController.get_heterozygosity_infoc                 C   s2   | j | j}|\}}t|}|| d | d S )z>Return the genotype counts for a certain population and locus.r   r   r?   r
   r   rB   rC   rD   rE   r   r   r   get_genotype_countg   s   z!EasyController.get_genotype_countc                 C   s6   | j | j}|\}}t|}|| d | dd S )aT  Return the Fis for a certain population and locus.

        Below CW means Cockerham and Weir and RH means Robertson and Hill.

        Returns a pair:

        - dictionary [allele] = (repetition count, frequency, Fis CW )
          with information for each allele
        - a triple with total number of alleles, Fis CW, Fis RH

        r      Nr?   rA   r   r   r   get_fisn   s   zEasyController.get_fisc                 C   s:   | j | j}|\}}t|}t|| d | d  S )6Return the alleles for a certain population and locus.r   rJ   )r   r@   r   r+   keysrH   r   r   r   get_alleles   s   zEasyController.get_allelesc                 C   s<   | j | j}|\}}|D ]}|d |kr|d   S qdS )rL   r   r   N)r   r@   r   )r
   rB   rC   rD   rE   
locus_infor   r   r   get_alleles_all_pops   s   z#EasyController.get_alleles_all_popsc                 C   s   t | jdkr0| j| j}|\}}|D ]}|d du r$d| j|d < q|dd | j|d < q| j| }|d | \}}	}
i }|d }t|D ]
\}}|	| ||< qH|
|fS )zCCalculate the allele frequency for a certain locus on a population.r   N)NNr   )lenr	   r   r@   r   	enumerate)r
   r   rB   rC   rD   rE   rO   infopop_namefreqstotalallele_freqallelesr$   alleler   r   r   get_allele_frequency   s   
z#EasyController.get_allele_frequencyc                 C      | j | jd S )zeReturn the multilocus F stats.

        Explain averaging.
        Returns Fis(CW), Fst, Fit
        r   r   calc_fst_allr   r>   r   r   r   get_multilocus_f_stats   s   z%EasyController.get_multilocus_f_statsc           	      C   sF   | j | jd }|D ]\}}}}}}||kr |||||f  S qdS )zWReturn F stats for a locus.

        Returns Fis(CW), Fst, Fit, Qintra, Qinter
        r   Nr\   )	r
   rB   	loci_iternamefisfstfitqintraqinterr   r   r   get_f_stats   s   zEasyController.get_f_statsc                 C   r[   )z$Calculate identity-base average Fis.r   )r   "calc_diversities_fis_with_identityr   r>   r   r   r   get_avg_fis      zEasyController.get_avg_fisc                 C   r[   )z@Calculate Allele size-base average Fis for all population pairs.r   )r   calc_fst_pairr   r>   r   r   r   get_avg_fst_pair   ri   zEasyController.get_avg_fst_pairc                 C   sF   t | jdkr| j| jd }|D ]}|d | j|d < q| j| S )zSCalculate Allele size-base average Fis for all population pairs of the given locus.r   r   )rQ   r   r   rj   r   )r
   locusiterrO   r   r   r   get_avg_fst_pair_locus   s
   
z%EasyController.get_avg_fst_pair_locusaLogh㈵>c                 C   s,   |r| j | j|||S | j | j|||S )zBCalculate isolation by distance statistics for Diploid or Haploid.)r   calc_ibd_diplor   calc_ibd_haplo)r
   is_diplostatscalemin_distr   r   r   calc_ibd   s   zEasyController.calc_ibd)N)r   )r   Tr&   r'   r(   )r&   r'   r(   )Tro   rp   rq   )__name__
__module____qualname____doc__r   r   r%   r3   r<   r=   rG   rI   rK   rN   rP   rZ   r^   rf   rh   rk   rn   rx   r   r   r   r   r      s4    




r   N)r|   
Bio.PopGenr   
Controllerr   r   r   r   r   r   <module>   s   