
    Xfi                         d Z ddlZddlZddlZddlZddlmZ ddlmZ d Zd Z	d Z
d Zd	 Zd
 ZddZ G d d          Z G d de          Z G d d          ZdS )zModule to control GenePop.    N)	_Argument)AbstractCommandlinec                 `    	 t          |           S # t          $ r t          |           cY S w xY w)zDGet a float from a token, if it fails, returns the string (PRIVATE).)float
ValueErrorstrtoks    =lib/python3.11/site-packages/Bio/PopGen/GenePop/Controller.py	_gp_floatr      s=    Szz   3xx    --c                 `    	 t          |           S # t          $ r t          |           cY S w xY w)zBGet a int from a token, if it fails, returns the string (PRIVATE).)intr   r   r	   s    r   _gp_intr      s<    3xx   3xxr   c                 (   |                                  }d|vr+|dk    rt          d|v rdS |                                  }d|v+d |                                                                                      d          D             }d |D             }|                                                                  }g }|dk    rd |                    d          D             }	 |                    |d	         d
 |dd         D             t          |d                   f           n?# t          $ r2 |                    |d	         d gt          |          z  d	f           Y nw xY w|                                                                  }|dk    ||fS )Nz -- No data)NNc                     g | ]
}|d k    |S r    .0xs     r   
<listcomp>z+_read_allele_freq_table.<locals>.<listcomp>+   s    FFFQa2ggqggg     c                 ,    g | ]}t          |          S r   )r   r   s     r   r   z+_read_allele_freq_table.<locals>.<listcomp>,   s    +++awqzz+++r   c                     g | ]
}|d k    |S r   r   r   s     r   r   z+_read_allele_freq_table.<locals>.<listcomp>0   s    777qqBwwwwwr   r   c                 ,    g | ]}t          |          S r   r   r   s     r   r   z+_read_allele_freq_table.<locals>.<listcomp>3   s    >>>QIaLL>>>r      )readlineStopIterationrstripsplitappendr   r   len)flineallelestablepartss        r   _read_allele_freq_tabler.   #   s   ::<<D
t

2:::zz|| t

 GF!**,,--//55c::FFFG++7+++G::<<  DE
"**77DJJsOO777	?LLq>>%"+>>>b	@R@RS     	? 	? 	?LL%(TFS\\$91=>>>>>	?zz||""$$ "** E>s   !AD% %9E! E!c                    g }|                                                                  }d|vr*|                                                                  }d|v*|                                                                  }d|vrd|vr|dk    rd |                    d          D             }g }t          |          D ]O\  }}	 |                     ||         |                     +# t
          $ r |                    |           Y Lw xY w|                    t          |                     |                                                                  }d|vr
d|vr|dk    |S )N---===r   c                     g | ]
}|d k    |S r   r   r   s     r   r   z_read_table.<locals>.<listcomp>B   s    666aa2gggggr   r   )r#   r%   r&   	enumerater'   r   tuple)r)   funsr,   r*   toksr-   ir
   s           r   _read_tabler8   ;   sr   E::<<  D
t

zz||""$$ t

::<<  D
t

T 1 1dbjj664::c??666oo 	" 	"FAs"WT!WS\\**** " " "S!!!!!"U5\\"""zz||""$$ t

T 1 1dbjj Ls   $C&&DDc                 2   g }|                                                                  }|dk    rh|                    d d |                    d          D             D                        |                                                                  }|dk    h|S )Nr   c                 ,    g | ]}t          |          S r   r    r   s     r   r   z)_read_triangle_matrix.<locals>.<listcomp>R   s    UUUy||UUUr   c                     g | ]
}|d k    |S r   r   r   ys     r   r   z)_read_triangle_matrix.<locals>.<listcomp>R   s    -T-T-TAAQSGGaGGGr   r   )r#   r%   r'   r&   )r)   matrixr*   s      r   _read_triangle_matrixr?   N   s    F::<<  D
"**UU-T-TC-T-T-TUUUVVVzz||""$$ "** Mr   c                    i }|                                                                  }d|v sd|v r&|                                                                  }t          d |                    d          D                       dz
  }t	          |          D ]}|                                                                  }d |                    d          dd          D             }g }|D ]J}	 |                    t          |                     &# t          $ r |                    d            Y Gw xY wt          |          D ]\  }	}
|
||dz   |	f<   |S )Nr0   r1   c                     g | ]
}|d k    |S r   r   r   s     r   r   z0_read_headed_triangle_matrix.<locals>.<listcomp>\   s    :::!r''!'''r   r   r!   c                     g | ]
}|d k    |S r   r   r   s     r   r   z0_read_headed_triangle_matrix.<locals>.<listcomp>_   s    :::a!r'''''r   )	r#   r%   r(   r&   ranger'   r   r   r3   )r)   r>   headernlinesline_popr*   vals
clean_valsvalcol_pop	clean_vals              r   _read_headed_triangle_matrixrL   W   sr   FZZ\\  ""F%6//$$&&::V\\#..:::;;a?F&MM 
8 
8zz||""$$::4::c??122.:::
 	( 	(C(!!)C..1111 ( ( (!!$'''''("+J"7"7 	8 	8GY.7FHqL'*++	8Ms   +"DD0/D0Fc           
      Z   |                                  }|rd}nd}|dk    r|                                                    |          r|                                   |                                   |                                   t          | t          t
          t
          t
          t
          t          t          g          }i }|D ]D}t          |          dk     rd ||d         <   !|d d         \  }}	}
}}}|
dk    rd }
|	|
|||f||<   E|S |                                  }|dk    t          )NzLocus zPop : r      r   r"   -)	r#   lstrip
startswithr8   r   r   r   r(   r$   )streamis_locus
has_fisherr*   hookr,   locientrylocuspsefis_wcfis_rhstepss                 r   _hw_funcr^   k   s9   ??D 
"**;;==##D)) 	OOOOOOiIy'SVW E D ? ?u::>>%)DqNN:?*7E1b&&%Syy!"#R">DKKK  ' "*** r   c                   ,    e Zd ZdZddZd Zd Zd ZdS )_FileIteratorzReturn an iterator which crawls over a stream of lines with a function (PRIVATE).

    The generator function is expected to yield a tuple, while
    consuming input
    Nc                 l    || _         |t          |          | _        n|| _        || _        d| _        d S NF)funcopenrR   fnamedone)selfrc   re   handles       r   __init__z_FileIterator.__init__   s8    	>u++DKK !DK
			r   c                 0    | j         rd| _         t          | S NT)rf   r$   rg   s    r   __iter__z_FileIterator.__iter__   s    9 	 DIr   c                 ,    |                      |           S N)rc   rl   s    r   __next__z_FileIterator.__next__   s    yyr   c                 j    | j                                          t          j        | j                   d S ro   )rR   closeosremovere   rl   s    r   __del__z_FileIterator.__del__   s.    
	$*r   ro   )__name__
__module____qualname____doc__ri   rm   rp   ru   r   r   r   r`   r`      s_         	 	 	 	        r   r`   c                   &    e Zd ZdZddZd Zd ZdS )_GenePopCommandlinez4Return a Command Line Wrapper for GenePop (PRIVATE).NGenepopc                    t          dgdd          t          dgdd          t          dgdd          t          d	gd
          t          dgd          t          dgd          t          dgd          t          dgd          t          dgd          t          dgd          g
| _        t          j        | |fi | |                     dd           d S )NcommandzGenePop option to be calledT)is_requiredmodezShould always be batchinputz
Input fileDememorizationzDememorization stepBatchNumberzNumber of MCMC batchesBatchLengthzLength of MCMC chainsHWtestszEnumeration or MCMCIsolBDstatisticzIBD statistic (a or e)MinimalDistancezMinimal IBD distanceGeographicScalezLog or Linearz
Mode=Batch)r   
parametersr   ri   set_parameter)rg   genepop_dircmdkwargss       r   ri   z_GenePopCommandline.__init__   s    yk#@dSSSvh 8dKKKwi4@@@'(*?@@}o'?@@}o'>??yk#899()+CDD()+ABB()?;;
 	$T399&9996<00000r   c                 r    |                      ddd                    d |D                       z              dS )z`Set the menu option.

        Example set_menu([6,1]) = get all F statistics (menu 6.1)
        r~   zMenuOptions=.c              3   4   K   | ]}t          |          V  d S ro   )r   r   s     r   	<genexpr>z/_GenePopCommandline.set_menu.<locals>.<genexpr>   s(      0M0MAQ0M0M0M0M0M0Mr   N)r   join)rg   option_lists     r   set_menuz_GenePopCommandline.set_menu   sK    
 	~0M0M0M0M0M(M(MM	
 	
 	
 	
 	
r   c                 8    |                      dd|z              dS )zSet the input file name.r   z
InputFile=N)r   rg   re   s     r   	set_inputz_GenePopCommandline.set_input   s#    7L5$899999r   )Nr|   )rv   rw   rx   ry   ri   r   r   r   r   r   r{   r{      sL        >>1 1 1 1 
 
 
: : : : :r   r{   c                      e Zd Z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 Z	 d*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+d$Zd+d%Zd+d&ZdS ),GenePopControllerz5Define a class to interface with the GenePop program.Nc                 .    t          |          | _        dS )zInitialize the controller.

        genepop_dir is the directory where GenePop is.

        The binary should be called Genepop (capital G)
        N)r{   
controller)rg   r   s     r   ri   zGenePopController.__init__   s     .k::r   c                 J    i }||d<   ||d<   ||d<   ||du rd|d<   nd|d<   |S )Nr   r   r   TEnumerationr   MCMCr   )rg   dememorizationbatches
iterations	enum_testoptss         r   	_get_optszGenePopController._get_opts   sP    !/%](] D  "/Y"(Yr   c           	      h   |i }t          j                    }t          j                    }t          j        |           | j                            |           t           j                            |          r| j        	                    |           n*| j        	                    |t           j
        z   |z              |D ]6}| j                            ||dz   t          ||                   z              7|                                  t          j        |           t          j        |           d S )N=)rs   getcwdtempfilemkdtempchdirr   r   pathisabsr   sepr   r   shutilrmtree)rg   
extensionsoptionre   r   cwdtemp_diropts           r   _run_genepopzGenePopController._run_genepop   s   <Dikk#%%
  (((7== 	<O%%e,,,,O%%cBFlU&:;;; 	K 	KCO))#sSy3tCy>>/IJJJJ
hr   T'       c                     |                      ||||          }|                     |gd|g||           d }	t          |	||z             S )a;  Use Hardy-Weinberg test for heterozygote deficiency/excess (PRIVATE).

        Returns a population iterator containing a dictionary where
        dictionary[locus]=(P-val, SE, Fis-WC, Fis-RH, steps).

        Some loci have a None if the info is not available.
        SE might be none (for enumerations).
        r!   c                 ,    t          | j        d          S rb   r^   rR   rl   s    r   hw_funcz4GenePopController._test_pop_hz_both.<locals>.hw_func  s    DK///r   )r   r   r`   )
rg   re   typeextr   r   r   r   r   r   s
             r   _test_pop_hz_bothz#GenePopController._test_pop_hz_both   s`    $ ~~ngz9MM3%!TE4888	0 	0 	0 Weck222r   c                    |                      ||||          }|                     |gd|g||           d }	t          ||z             5 }
|
                                }d|vr|
                                }d|vt	          |
t
          t          t          t          g          }ddd           n# 1 swxY w Y   t          ||z             5 }|                                }d|vr|                                }d|vt	          |t
          t          t          t          g          }ddd           n# 1 swxY w Y   t          ||z             5 }|                                }d|vr|                                }d|v|                                 |                                 |                                 |                                 |                                                                }t          d d |	                    d	          D             D                       \  }}}ddd           n# 1 swxY w Y   |||||ffS )
a  Use Global Hardy-Weinberg test for heterozygote deficiency/excess (PRIVATE).

        Returns a triple with:
         - A list per population containing (pop_name, P-val, SE, switches).
           Some pops have a None if the info is not available.
           SE might be none (for enumerations).
         - A list per loci containing (locus_name, P-val, SE, switches).
           Some loci have a None if the info is not available.
           SE might be none (for enumerations).
         - Overall results (P-val, SE, switches).

        r!   c                 \    t          | j        t          t          t          t          g          S ro   )r8   rR   r   r   rl   s    r   hw_pop_funcz;GenePopController._test_global_hz_both.<locals>.hw_pop_func&  s    t{S)Y	,RSSSr   zby populationNzby locusz	all locusc              3   4   K   | ]}t          |          V  d S ro   r    r   s     r   r   z9GenePopController._test_global_hz_both.<locals>.<genexpr><  s9       $ $!"	!$ $ $ $ $ $r   c                     g | ]
}|d k    |S r   r   r<   s     r   r   z:GenePopController._test_global_hz_both.<locals>.<listcomp>=  s    &M&M&MQQ"WWqWWWr   r   )
r   r   rd   r#   r8   r   r   r%   r4   r&   )rg   re   r   r   r   r   r   r   r   r   f1r*   pop_pf2loc_pr)   rY   rZ   switchess                      r   _test_global_hz_bothz&GenePopController._test_global_hz_both  s   , ~~ngz9MM3%!TE4888	T 	T 	T %#+ 	L";;==D!--{{}} "--S)Y	$JKKE		L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L
 %#+ 	L";;==DD(({{}} D((S)Y	$JKKE		L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L
 %#+ 	!::<<DT))zz|| T))JJLLLJJLLLJJLLLJJLLL::<<&&((D# $ $&M&M$**S//&M&M&M$ $ $  OAr8	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 eaX...s9   	AB..B25B2AD00D47D4C!H::H>H>c           	      8    |                      |dd||||          S )*  Use Hardy-Weinberg test for heterozygote deficiency.

        Returns a population iterator containing a dictionary where
        dictionary[locus]=(P-val, SE, Fis-WC, Fis-RH, steps).

        Some loci have a None if the info is not available.
        SE might be none (for enumerations).
        r!   z.Dr   rg   re   r   r   r   r   s         r   test_pop_hz_deficiencyz(GenePopController.test_pop_hz_deficiencyB  +     %%1dI~w

 
 	
r   c           	      8    |                      |dd||||          S )r      z.Er   r   s         r   test_pop_hz_excessz$GenePopController.test_pop_hz_excessR  r   r   Fc                     |                      ||||          }|                     |gddg||           d }d }	t          j        |dz   |dz              t	          ||dz             t	          |	|dz             fS )a  Use Hardy-Weinberg test based on probability.

        Returns 2 iterators and a final tuple:

         1. Returns a loci iterator containing:
             - A dictionary[pop_pos]=(P-val, SE, Fis-WC, Fis-RH, steps).
               Some pops have a None if the info is not available.
               SE might be none (for enumerations).
             - Result of Fisher's test (Chi2, deg freedom, prob).
         2. Returns a population iterator containing:
             - A dictionary[locus]=(P-val, SE, Fis-WC, Fis-RH, steps).
               Some loci have a None if the info is not available.
               SE might be none (for enumerations).
             - Result of Fisher's test (Chi2, deg freedom, prob).
         3. Final tuple (Chi2, deg freedom, prob).

        r!      c                 .    t          | j        dd          S rk   r   rl   s    r   hw_prob_loci_funcz=GenePopController.test_pop_hz_prob.<locals>.hw_prob_loci_func  s    DKt444r   c                 .    t          | j        dd          S )NFTr   rl   s    r   hw_prob_pop_funcz<GenePopController.test_pop_hz_prob.<locals>.hw_prob_pop_func  s    DK555r   z.Pz.P2)r   r   r   copyfiler`   )
rg   re   r   r   r   r   r   r   r   r   s
             r   test_pop_hz_probz"GenePopController.test_pop_hz_probb  s    4 ~~ngz9MM3%!Q555	5 	5 	5	6 	6 	6 	eem444 +UT\::*EEM::
 	
r   c           	      8    |                      |dd||||          S )a  Use Global Hardy-Weinberg test for heterozygote deficiency.

        Returns a triple with:
         - An list per population containing (pop_name, P-val, SE, switches).
           Some pops have a None if the info is not available.
           SE might be none (for enumerations).
         - An list per loci containing (locus_name, P-val, SE, switches).
           Some loci have a None if the info is not available.
           SE might be none (for enumerations).
         - Overall results (P-val, SE, switches).

        rN   z.DGr   r   s         r   test_global_hz_deficiencyz+GenePopController.test_global_hz_deficiency  +     ((1eY
 
 	
r   c           	      8    |                      |dd||||          S )a  Use Global Hardy-Weinberg test for heterozygote excess.

        Returns a triple with:
         - A list per population containing (pop_name, P-val, SE, switches).
           Some pops have a None if the info is not available.
           SE might be none (for enumerations).
         - A list per loci containing (locus_name, P-val, SE, switches).
           Some loci have a None if the info is not available.
           SE might be none (for enumerations).
         - Overall results (P-val, SE, switches)

           z.EGr   r   s         r   test_global_hz_excessz'GenePopController.test_global_hz_excess  r   r   c                 8   |                      |||          }|                     dgddg||           d }d }t          |dz             }|                                }	d|	vr|                                }	d|	vt	          j        |dz   |dz              t          |dz             }
|
                                }	d|	vr|
                                }	d|	vd|	vr|
                                }	d|	vt          ||dz   |          t          ||dz   |
          fS )	zHTest for linkage disequilibrium on each pair of loci in each population.z.DISr   r!   c                    d }| j                                                                         }|dk    rd| _        t          d |                    d          D             }|d         |d         |d         }}}t          | d          s||}}d	}||k    r||k    r|dz  }|d
         dk    r||||fd fS t          |d
                   t          |d                   t          |d                   }}
}	||||f|	|
|ffS )Nr   Tc                     g | ]
}|d k    |S r   r   r   s     r   r   zBGenePopController.test_ld.<locals>.ld_pop_func.<locals>.<listcomp>      :::!!r''A'''r   r   r   r!   r   start_locus1r"   r   NorN   r   )	rR   r#   r%   rf   r$   r&   hasattrr   r   )rg   current_popr*   r6   poplocus1locus2r   start_locus2rY   rZ   r   s               r   ld_pop_funcz.GenePopController.test_ld.<locals>.ld_pop_func  s   K;''))0022Drzz 	##::tzz#:::D"&q'47DGC400 !-3Vl %%&L*@*@q Aw$"C&&)94??'Q00)DG2D2DgdSTgFVFV8rAff%52x7HHHr   c                    | j                                                                         }|dk    rd| _        t          d |                    d          D             }|d         |d         }}	 t          |d                   t          |d                   t          |d	                   }}}n# t          $ r	 ||fd fcY S w xY w||f|||ffS )
Nr   Tc                     g | ]
}|d k    |S r   r   r   s     r   r   z>GenePopController.test_ld.<locals>.ld_func.<locals>.<listcomp>  r   r   r   r   r   r   rN   r   )	rR   r#   r%   rf   r$   r&   r   r   r   )rg   r*   r6   r   r   chi2dfrY   s           r   ld_funcz*GenePopController.test_ld.<locals>.ld_func  s    ;''))0022Drzz 	##::tzz#:::D!!Wd1gFF.'Q00'$q'2B2BIdSTgDVDV!b . . .'----.F#dB]22s   0?B0 0CC----z.DI2z
Locus pair)r   r   rd   r#   r   r   r`   )rg   re   r   r   r   r   r   r   r   r*   r   s              r   test_ldzGenePopController.test_ld  sQ   ~~ngzBB6(QFE4888	I 	I 	I$	3 	3 	3 %&.!!{{}}D  ;;==D D  777%&.!!{{}}$&&;;==D $&&D  ;;==D D   +uv~r::'56>266
 	
r   c                     t           )z4Provision for creating Genotypic contingency tables.NotImplementedErrorr   s     r   create_contingency_tablesz+GenePopController.create_contingency_tables      !!r   c                     t           )z8Provision for Genic differentiation for all populations.r   rg   re   r   r   r   s        r   test_genic_diff_allz%GenePopController.test_genic_diff_all  
     "!r   c                     t           )z=Provision for Genic differentiation for all population pairs.r   r   s        r   test_genic_diff_pairz&GenePopController.test_genic_diff_pair  r   r   c                     t           )z<Provision for Genotypic differentiation for all populations.r   r   s        r   test_genotypic_diff_allz)GenePopController.test_genotypic_diff_all  r   r   c                     t           )zAProvision for Genotypic differentiation for all population pairs.r   r   s        r   test_genotypic_diff_pairz*GenePopController.test_genotypic_diff_pair  r   r   c                    |                      dgdg|           t          |dz             5 }|                                }ddd           n# 1 swxY w Y   |D ]Y}t          j        d|          }|"t          |                    d                    }t          j        d|          }|"t          |                    d                    }t          j        d|          }|"t          |                    d                    }t          j        d	|          }|"t          |                    d                    }	t          j        d
|          }|"t          |                    d                    }
t          j        d|          }|"t          |                    d                    }[t          j        |dz              ||||	|
|fS )ao  Estimate the Number of Migrants.

        Parameters:
         - fname - file name

        Returns
         - Mean sample size
         - Mean frequency of private alleles
         - Number of migrants for Ne=10
         - Number of migrants for Ne=25
         - Number of migrants for Ne=50
         - Number of migrants after correcting for expected size

        PRIrN   z.PRINzMean sample size: ([.0-9]+)r!   z3Mean frequency of private alleles p\(1\)= ([.0-9]+)zN=10: ([.0-9]+)zN=25: ([.0-9]+)zN=50: ([.0-9]+)zfor size= ([.0-9]+))	r   rd   	readlinesresearchr   grouprs   rt   )rg   re   r)   linesr*   mmean_sample_sizemean_priv_allelesmig10mig25mig50mig_correcteds               r   estimate_nmzGenePopController.estimate_nm  s    	5'A3...%&.!! 	"QKKMME	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	6 	6D	7>>A}#,QWWQZZ#8#8 	PRVWWA}$-aggajj$9$9!	+T22A}!!''!**--	+T22A}!!''!**--	+T22A}!!''!**--	/66A} )!''!** 5 5
	%&.!!!!2E5%VVs   AAAc                     |                      dgddg|           d }d }t          j        |dz   |dz              t          ||dz             }t          ||dz             }||fS )a  Calculate allele and genotype frequencies per locus and per sample.

        Parameters:
         - fname - file name

        Returns tuple with 2 elements:
         - Population iterator with

           - population name
           - Locus dictionary with key = locus name and content tuple as
             Genotype List with
             (Allele1, Allele2, observed, expected)
             (expected homozygotes, observed hm,
             expected heterozygotes, observed ht)
             Allele frequency/Fis dictionary with allele as key and
             (count, frequency, Fis Weir & Cockerham)
           - Totals as a pair
           - count
           - Fis Weir & Cockerham,
           - Fis Robertson & Hill

         - Locus iterator with

           - Locus name
           - allele list
           - Population list with a triple

             - population name
             - list of allele frequencies in the same order as allele list above
             - number of genes

        Will create a file called fname.INF

        INFr   r!   c           
         t          | d          r
| j        }| `n| j                                        }i }|dk    r4|                                }d|v r	| j        |fS t          j        d|          }||                    d                                          }|                    d          }t          | d          s|| _	        t          | d          r!| j	        |k    r| j        }|| _        | `	| `||fS || _        n| j                                        }g }| j                                        }d	|v rd
|vr| j                                        }d
|v|dk    rt          j        d|          }||
                    t          |                    d                    t          |                    d                    t          |                    d                    t          |                    d                    f           n| j                                        }| j                                        }|dk    d|vr| j                                        }d|vt          |dd                    }	| j                                        }t          |dd                    }
| j                                        }t          |dd                    }| j                                        }t          |dd                    }| j                                        }d|vr| j                                        }d|v| j                                        }i }d }d|vrd |                                                    d          D             }|d         dk    r?t          |d                   t          |d                   t          |d                   f}nTt          |d                   t          |d                   t          |d                   f|t          |d                   <   | j                                        }d|v||	|
||f||f||<   |dk    4d| _        t          )Nold_liner   z,Tables of allelic frequencies for each locusz.*Pop: (.+) Locus: (.+)r!   r   first_locuscurr_popr   zGenotypes  Obs.
z% +([0-9]+) , ([0-9]+) *([0-9]+) *(.+)r   rN   zExpected number of ho&   zSample countr   c                     g | ]
}|d k    |S r   r   r   s     r   r   zTGenePopController.calc_allele_genotype_freqs.<locals>.pop_parser.<locals>.<listcomp>      KKK!177A777r   r   r   TotT)r   r  rR   r#   r%   r  r	  matchr  r  r'   r   r   r&   rf   r$   )rg   r*   loci_contentr   r   rX   old_pop	geno_listm2expHoobsHoexpHeobsHefreq_fisoverall_fisrG   s                   r   
pop_parserz@GenePopController.calc_allele_genotype_freqs.<locals>.pop_parserf  s|   tZ(( .}MM{++--L"**{{}}ATII=,66!:DAA$++a..//11C!KKNNE"477 1+0(tZ00 9+u44&*mG,0DM $ 0 $#*L#88$'DMM;//11D	{++--$$'t33;//11D (t33 dll"I4PPB~!(( ' 4 4 ' 4 4 ' 4 4 )"((1++ 6 6	     ${3355 ;//11D dll  .T99;//11D .T99!$rss),,{++--RSS	**{++--!$rss),,{++--RSS	**{++--$D00;//11D %D00{++--"D((KKt{{}}':':3'?'?KKKDAw%''#DG,,%d1g..%d1g..' $DG,,%d1g..%d1g..6a!1!12
  ;//11D D((  E5%0	'U#[ "**f DIr   c                 R   | j                                         }|dk    r{|                                }t          j        d|          }|1|                    d          }t          | j                   \  }}|||fS | j                                         }|dk    {d| _        t          Nr   z Locus: (.+)r!   T)	rR   r#   r%   r	  r   r  r.   rf   r$   )rg   r*   r   rX   r+   r,   s         r   locus_parserzBGenePopController.calc_allele_genotype_freqs.<locals>.locus_parser  s    ;''))D"**{{}}66$!KKNNE%<T[%I%INGU '500{++-- "** DIr   z.INFz.IN2)r   r   r   r`   )rg   re   r+  r.  pop_iter
locus_iters         r   calc_allele_genotype_freqsz,GenePopController.calc_allele_genotype_freqs4  s    F 	5'Aq65111[	  [	  [	 z	  	  	  	777 UV^<<"<@@
*%%r   c                    |                      |gddg|           t          ||z             5 }|                                }|dk    r|                                }|                    d          rDt          |t          t          t          t          g          }t          |t          t          g          }|                                }|dk    d d d            n# 1 swxY w Y   d }t          |||z             ||fS )Nr   r   r   zGStatistics per sample over all loci with at least two individuals typedc                    | j                                         }|dk    rN|                                }t          j        d|          }||                    d          }| j                                          d| j                                         v r|d fS | j                                          t          | j         t          t          t          t          g          }| j                                          t          d d | j                                         
                    d          D             D                       \  }}||||fS | j                                         }|dk    Nd| _        t          )	Nr   zLocus: (.+)r!   zNo completec              3   4   K   | ]}t          |          V  d S ro   r    r   s     r   r   zLGenePopController._calc_diversities_fis.<locals>.fis_func.<locals>.<genexpr>  s<       0 0 "!0 0 0 0 0 0r   c                     g | ]
}|d k    |S r   r   r<   s     r   r   zMGenePopController._calc_diversities_fis.<locals>.fis_func.<locals>.<listcomp>  s%     " " ""#AQSGGAGGGr   r   T)rR   r#   r%   r	  r
  r  r8   r   r   r4   r&   rf   r$   )rg   r*   r  rX   	fis_table
avg_qinteravg_fiss          r   fis_funcz9GenePopController._calc_diversities_fis.<locals>.fis_func  ss   ;''))D"**{{}}ImT22=GGAJJEK((***$(<(<(>(>>>$d{*K((*** +c9i%K! !I K((****/ 0 0" "'+{';';'='='C'CC'H'H" " "0 0 0 + +'J !)Z@@{++--) "*** DIr   )	r   rd   r#   r%   rQ   r8   r   r   r`   )rg   re   r   r)   r*   r8  
avg_Qintrar9  s           r   _calc_diversities_fisz'GenePopController._calc_diversities_fis  s3   3%!Q///%#+ 		$!::<<D"**{{}}??]  B *!c9i-STTG!,Qi0@!A!AJzz|| "**		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$ 		$	  	  	 4 Xus{33WjHHs   B"CC"Cc                 .    |                      |d          S )z/Compute identity-base Gene diversities and Fis.z.DIV)r;  r   s     r   "calc_diversities_fis_with_identityz4GenePopController.calc_diversities_fis_with_identity  s    ))%888r   c                     t           )zAProvision to Computer Allele size-based Gene diversities and Fis.r   r   s     r   calc_diversities_fis_with_sizez0GenePopController.calc_diversities_fis_with_size  r   r   c                    |                      dgddg|           t          |dz             5 }|                                }|dk    r|                    d          rd |                                                    d          D             }	 t          |d                   }n# t          $ r d}Y nw xY w	 t          |d	                   }n# t          $ r d}Y nw xY w	 t          |d
                   }n# t          $ r d}Y nw xY w|                                }|dk    ddd           n# 1 swxY w Y   d }|||ft          ||dz             fS )a  Execute GenePop and gets Fst/Fis/Fit (all populations).

        Parameters:
         - fname - file name

        Returns:
         - (multiLocusFis, multiLocusFst, multiLocus Fit),
         - Iterator of tuples
           (Locus name, Fis, Fst, Fit, Qintra, Qinter)

        Will create a file called ``fname.FST``.

        This does not return the genotype frequencies.

        z.FST   r!   r   z           All:c                     g | ]
}|d k    |S r   r   r   s     r   r   z2GenePopController.calc_fst_all.<locals>.<listcomp>  r  r   r   Nr   r   c                 |   t          | d          r
| j        }| `n| j                                        }d }d }d }d }d }d }|dk    r|                                }|                    d          r@||| _        ||||||fS |                    d          d                                         }n=|                    d          r)t          |                    d          d                   }n|                    d          r)t          |                    d          d                   }n|                    d	          r)t          |                    d          d                   }n|                    d
          r)t          |                    d          d                   }nE|                    d          r0t          |                    d          d                   }||||||fS | j                                        }|dk    |||||||fS | j        	                                 d| _
        t          )N	last_liner   z  Locus::r!   zFis^=r   zFst^=zFit^=z
1-Qintra^=z
1-Qinter^=T)r   rD  rR   r#   r%   rQ   r&   rP   r   rr   rf   r$   )rg   r*   rX   fisfstfitqintraqinters           r   procz,GenePopController.calc_fst_all.<locals>.proc-  s*   t[)) .~NN{++--ECCCFF"**{{}}??:.. @()-$c3VVCC $

3 2 9 9 ; ;__W-- 
@#DJJsOOA$677CC__W-- @#DJJsOOA$677CC__W-- @#DJJsOOA$677CC__\22 @&tzz#q'9::FF__\22 @&tzz#q'9::F #sC??{++--' "**(  c3VV;;KDIr   )	r   rd   r#   rQ   r%   r&   r   r   r`   )	rg   re   r)   r*   r6   allFisallFstallFitrK  s	            r   calc_fst_allzGenePopController.calc_fst_all	  s     	6(QFE222%&.!! 	$Q::<<D"**??#455 &KKt{{}}':':3'?'?KKKD&!*47!3!3% & & &!%&&!*47!3!3% & & &!%&&!*47!3!3% & & &!%&zz|| "**	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$&$	  $	  $	 L 'tUV^)L)LLLs~   A!D/B%$D/%B41D/3B44D/8CD/CD/CD/!C76D/7DD/DD//D36D3c                    |                      ddgddg|           t          |dz             5 }|                                }|dk    rR|                                }|                    d          rt          |          }|                                }|dk    Rddd           n# 1 swxY w Y   d }t          j        |dz              t          ||dz             |fS )	zIEstimate spatial structure from Allele identity for all population pairs.z.ST2.MIGrA  r   r   zEstimates for all lociNc                 J   | j                                         }|dk    rw|                                }t          j        d|          }|-|                    d          }t          | j                   }||fS | j                                         }|dk    wd| _        t          r-  )	rR   r#   r%   r	  r
  r  rL   rf   r$   )rg   r*   r  rX   r>   s        r   	loci_funcz2GenePopController.calc_fst_pair.<locals>.loci_funca  s    ;''))D"**{{}}Ind33=GGAJJE9$+FFF &=({++-- "** DIr   )	r   rd   r#   r%   rQ   rL   rs   rt   r`   )rg   re   r)   r*   avg_fstrS  s         r   calc_fst_pairzGenePopController.calc_fst_pairV  s"   66*QFE:::%&.!! 	$Q::<<D"**{{}}??#;<< >:1==Gzz||	 "**	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$	  	  	  		%&.!!!Y77@@s   A-B''B+.B+c                     t           )zPProvision for estimating spatial structure from Allele size for all populations.r   r   s     r   calc_rho_allzGenePopController.calc_rho_allr  r   r   c                     t           )zUProvision for estimating spatial structure from Allele size for all population pairs.r   r   s     r   calc_rho_pairzGenePopController.calc_rho_pairw  r   r   aLogh㈵>c           	         |                      g dd|g||||d           t          |dz             5 }|                                 |                                 |                                 |                                 t          |          }|                                 |                                 t          |          }|                                 t	          j        d|                                                                          }	t          |	                    d                    }
t          |	                    d                    }|                                 |                                 t	          j        d	|                                                                          }	t          |	                    d                    }t	          j        d
|                                                                          }	t          |	                    d                    }t          |	                    d                    }ddd           n# 1 swxY w Y   t          j
        |dz              t          j
        |dz              t          j
        |dz              |||
|f|||ffS )z5Calculate isolation by distance statistics (PRIVATE).).GRArQ  .ISOrA  )r   r   r   )r   r_  za = (.+), b = (.+)r!   r   z b=(.+)z.*\[(.+)  ;  (.+)\]NrQ  r^  )r   rd   r#   r?   r	  r   r%   r   r  rs   rt   )rg   re   substatscalemin_distr)   estimatedistancer   rZ  bbbbblowbbhighs                  r   	_calc_ibdzGenePopController._calc_ibd{  sv   $$$H#+#(#' 	 	 		
 		
 		
 %&.!! 	/QJJLLLJJLLLJJLLLJJLLL,Q//HJJLLLJJLLL,Q//HJJLLLH11::<<3F3F3H3HIIE%++a..))A%++a..))AJJLLLJJLLLHY

(;(;(=(=>>E5;;q>>**BH3QZZ\\5H5H5J5JKKEekk!nn--Eu{{1~~..F'	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/( 		%&.!!!
	%&.!!!
	%&.!!!Aq6Bv+>>>s   H(I((I,/I,c                 4    |                      |d|||          S )zCalculate isolation by distance statistics for diploid data.

        See _calc_ibd for parameter details.

        Note that each pop can only have a single individual and
        the individual name has to be the sample coordinates.
        r   rj  rg   re   ra  rb  rc  s        r   calc_ibd_diploz GenePopController.calc_ibd_diplo       ~~eQeX>>>r   c                 4    |                      |d|||          S )zCalculate isolation by distance statistics for haploid data.

        See _calc_ibd for parameter details.

        Note that each pop can only have a single individual and
        the individual name has to be the sample coordinates.
        rA  rl  rm  s        r   calc_ibd_haploz GenePopController.calc_ibd_haplo  ro  r   ro   )Tr   r   r   )Fr   r   r   )r   r   r   )rZ  r[  r\  ) rv   rw   rx   ry   ri   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r1  r;  r=  r?  rO  rU  rW  rY  rj  rn  rq  r   r   r   r   r      s       ??; ; ; ;
 
 
 
       , 3 3 3 3> 2/ 2/ 2/ 2/l SW
 
 
 
" SW
 
 
 
( (
 (
 (
 (
X SW
 
 
 
* SW
 
 
 
(2
 2
 2
 2
j" " " CG" " " " CG" " " " CG" " " " CG" " " "&W &W &WR_& _& _&B'I 'I 'IT9 9 9
" " "
JM JM JMZA A A8" " "
" " "#? #? #? #?L? ? ? ?? ? ? ? ? ?r   r   )F)ry   rs   r	  r   r   Bio.Applicationr   r   r   r   r.   r8   r?   rL   r^   r`   r{   r   r   r   r   <module>rs     sT   !   				 				   % % % % % % / / / / / /      0  &    (   <       @: : : : :- : : :Bj? j? j? j? j? j? j? j? j? j?r   