
    Xfn                     <    d Z ddlmZ ddlmZ  G d d          ZdS )zsControl GenePop through an easier interface.

This interface is less efficient than the standard GenePopControler

    )GenePop   )GenePopControllerc                       e Zd ZdZddZd ZddZ	 	 	 	 	 d dZ	 d!dZd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd"dZdS )#EasyControllerz@Define a class for an easier interface with the GenePop program.Nc                 X    || _         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_dirs      Alib/python3.11/site-packages/Bio/PopGen/GenePop/EasyController.py__init__zEasyController.__init__   s1     ,[99 ""$    c                     t          | j                  5 }t          j        |          }ddd           n# 1 swxY 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recs      r   get_basic_infozEasyController.get_basic_info"   s    $+ 	"!,q//C	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"|S]**s   6::probabilityc                 H   |dk    r | j                             | j                  }nJ|dk    r | j                             | j                  }n$| j                             | j        d          \  }}}t          |dz
            D ]}t          |           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is          r   test_hw_popzEasyController.test_hw_pop)   s    $$%<<T[IIFF(""%88EEFF,0,<,M,MT- -)Hfk w{## 	 	ALLLLF||r   r   T'       c                     |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alls	            r   test_hw_globalzEasyController.test_hw_global8   s     $$$($4$N$NY% %!GWcc %)$4$J$JY% %!GWc G}}d7mmS00r   c                     | j                             | j        |||          d         }|D ]&}|\  \  }}	}
||k    r|	|k    s||k    r
|	|k    r|
c S 'dS )zITest for linkage disequilibrium for each pair of loci in each population.r   N)r
   test_ldr	   )r   locus1locus2r3   r4   r5   all_ldld_casel1l2results              r   test_ld_all_pairzEasyController.test_ld_all_pairL   s     !))K*
 

  	 	G&HRffv2<<B&LL	 	r   c                 @    | j                             | j                  S )z"Estimate Nm. Just a simple bridge.)r
   estimate_nmr	   r   s    r   rE   zEasyController.estimate_nmX   s    ++DK888r   c                     | 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	   r1   r   r$   
locus_name
geno_freqspop_iterloc_iterpopss          r   get_heterozygosity_infoz&EasyController.get_heterozygosity_info\   sJ     %@@MM
'(H~~G}Q
+A..r   c                     | j                             | j                  }|\  }}t          |          }||         d         |         d         S )z>Return the genotype counts for a certain population and locus.r   r   rH   r   r$   rK   rL   rM   rN   s         r   get_genotype_countz!EasyController.get_genotype_countg   sI    %@@MM
'(>> #J/22r   c                     | 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      NrH   rJ   s          r   get_fiszEasyController.get_fisn   sN     %@@MM
'(H~~G}Q
+ABB//r   c                     | j                             | j                  }|\  }}t          |          }t          ||         d         |         d                                                   S )6Return the alleles for a certain population and locus.r   rU   )r
   rI   r	   r1   keysrR   s         r   get_alleleszEasyController.get_alleles   s]    %@@MM
'(>>HW%a(4Q7<<>>???r   c                     | j                             | j                  }|\  }}|D ]}|d         |k    r
|d         c S dS )rX   r   r   N)r
   rI   r	   )r   rK   rL   rM   rN   
locus_infos         r   get_alleles_all_popsz#EasyController.get_alleles_all_pops   sa    %@@MM
'(" 	% 	%J!}
**!!}$$$ +	% 	%r   c                    t          | j                  dk    rZ| j                            | j                  }|\  }}|D ]3}|d         d| j        |d         <   |dd         | j        |d         <   4| j        |         }|d         |         \  }}	}
i }|d         }t          |          D ]\  }}|	|         ||<   |
|fS )zCCalculate the allele frequency for a certain locus on a population.r   N)NNr   )lenr   r
   rI   r	   	enumerate)r   r$   rK   rL   rM   rN   r\   infopop_namefreqstotalallele_freqallelesr)   alleles                  r   get_allele_frequencyz#EasyController.get_allele_frequency   s    t&''1,,)DDT[QQJ!+Hh& L L
a=(=GD+JqM::=G^D+JqM::&z2!%a!1%q'"7++ 	+ 	+IAv"'(Kk!!r   c                 L    | j                             | j                  d         S )zeReturn the multilocus F stats.

        Explain averaging.
        Returns Fis(CW), Fst, Fit
        r   r
   calc_fst_allr	   rF   s    r   get_multilocus_f_statsz%EasyController.get_multilocus_f_stats   s"     ,,T[99!<<r   c                     | j                             | j                  d         }|D ]\  }}}}}}||k    r	|||||fc S dS )zWReturn F stats for a locus.

        Returns Fis(CW), Fst, Fit, Qintra, Qinter
        r   Nrj   )	r   rK   	loci_iternamefisfstfitqintraqinters	            r   get_f_statszEasyController.get_f_stats   sn    
 $11$+>>qA	3< 	5 	5/D#sCz!!Cff4444 "	5 	5r   c                 L    | j                             | j                  d         S )z$Calculate identity-base average Fis.r   )r
   "calc_diversities_fis_with_identityr	   rF   s    r   get_avg_fiszEasyController.get_avg_fis   s!    BB4;OOPQRRr   c                 L    | j                             | j                  d         S )z@Calculate Allele size-base average Fis for all population pairs.r   )r
   calc_fst_pairr	   rF   s    r   get_avg_fst_pairzEasyController.get_avg_fst_pair   s     --dk::1==r   c                     t          | j                  dk    r@| j                            | j                  d         }|D ]}|d         | j        |d         <   | j        |         S )zSCalculate Allele size-base average Fis for all population pairs of the given locus.r   r   )r_   r   r
   rz   r	   )r   locusiterr\   s       r   get_avg_fst_pair_locusz%EasyController.get_avg_fst_pair_locus   sl    t$%%**#11$+>>qAD" E E
7A!}%jm44$U++r   aLogh㈵>c                     |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_dists        r   calc_ibdzEasyController.calc_ibd   sI     	W#224;eXVVV#224;eXVVVr   )N)r   )r   Tr+   r,   r-   )r+   r,   r-   )Tr   r   r   )__name__
__module____qualname____doc__r   r   r*   r9   rC   rE   rP   rS   rV   rZ   r]   rh   rl   ru   rx   r{   r   r    r   r   r   r      se       JJ
% 
% 
% 
%+ + +   " 1 1 1 1* LP
 
 
 
9 9 9	/ 	/ 	/3 3 30 0 0"@ @ @% % %" " "$= = =5 5 5S S S> > >, , ,W W W W W Wr   r   N)r   
Bio.PopGenr   
Controllerr   r   r   r   r   <module>r      s{           ) ) ) ) ) )tW tW tW tW tW tW tW tW tW tWr   