ó
$<ïHc           @   s‡   d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z d d l m	 Z	 m Z d d l
 m Z m Z d d	 d „  ƒ  YZ d S(
   s¡   
This module allows to control fdist.

This will allow to call fdist and associated program (cplot, datacal, pv).

http://www.rubic.rdg.ac.uk/~mab/software.html
iÿÿÿÿN(   t   platformt   maxint(   t   copyfile(   t   randintt   random(   t   strftimet   clockt   FDistControllerc           B   s†   e  Z d  d d „ Z d „  Z d „  Z d d „ Z d „  Z d d d d	 „ Z d d d d
 d d „ Z	 d d d „ Z
 d d d „ Z RS(   t    c         C   ss   d |  _  | |  _ t j |  _ |  j d k s9 t d k rB d } n d } | d k r` | |  _ n	 | |  _ d } d S(   s¾   Initializes the controller.
        
        fdist_dir is the directory where fdist2 is.
        ext is the extension of binaries (.exe on windows, 
          none on Unix)
        
        i    t   ntt   cygwins   .exeR   N(   t   tmp_idxt	   fdist_dirt   ost   namet   os_nameR    t   Nonet   ext(   t   selfR   R   t   py_extt   exec_counts(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   __init__   s    				c         C   s>   |  j  d k r | |  j St j j |  j  | g ƒ |  j Sd S(   s€   Returns the path to an fdist application.

           Includes Path where fdist can be found plus executable extension.
        R   N(   R   R   R   t   sept   join(   R   t   app(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt	   _get_path.   s    c         C   sP   |  j  d 7_  t d ƒ t t t ƒ  d ƒ ƒ t t d d ƒ ƒ t |  j  ƒ S(   s¥   Gets a temporary file name.

           Returns a temporary file name, if executing inside jython
           tries to replace unexisting tempfile.mkstemp().
        i   s   %H%M%Sid   i    iè  (   R   R   t   strt   intR   R   (   R   (    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   _get_temp_file8   s    t   .c   
      C   s8  |  j  ƒ  } |  j  ƒ  } t | t j | d ƒ } | j d ƒ | j ƒ  t j ƒ  } t j d | d |  j d ƒ d | d | ƒ t | t j | ƒ } | j	 ƒ  j
 ƒ  j d ƒ } t | d	 ƒ } | j	 ƒ  j
 ƒ  j d ƒ } t | d
 ƒ }	 | j ƒ  t j | t j | ƒ t j | t j | ƒ | |	 f S(   sR   Executes datacal.
        
           data_dir - Where the data is found.
        t   ws   a
s   cd s    && t   datacals    < s    > t    i   i	   (   R   t   openR   R   t   writet   closet   getcwdt   systemR   t   readlinet   rstript   splitt   floatR   t   remove(
   R   t   data_dirt   in_namet   out_namet   ft   curr_dirt   fst_linet   fstt   sample_linet   sample(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   run_datacalA   s"    
,
c         C   su   t  | t j d d ƒ } x= t d ƒ D]/ } | j t t t d t d ƒ ƒ d ƒ q' W| j d ƒ | j ƒ  d S(   sZ   Generates an INTFILE.

           Parameter:
           data_dir - data directory
        t   INTFILER   ib   i   s   
s   8
N(	   R!   R   R   t   rangeR"   R   R   R   R#   (   R   R+   t   inft   i(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   _generate_intfileX   s
    -i    i N  c         C   s÷  | d k r d } n  | d k r* d } n  d } d }	 t  | t j | d ƒ }
 |
 j d ƒ |
 j ƒ  t  | t j d	 d ƒ }
 |
 j t | ƒ d
 ƒ |
 j t | ƒ d
 ƒ |
 j t | ƒ d
 ƒ |
 j t | ƒ d
 ƒ |
 j t | ƒ d
 ƒ |
 j t | ƒ d
 ƒ |
 j ƒ  |  j | ƒ t j d | d |  j d ƒ d | d |	 ƒ t  | t j |	 ƒ }
 |
 j	 ƒ  } |
 j ƒ  x? | D]7 } | j
 d ƒ rˆt | j ƒ  j d ƒ d ƒ } qˆqˆWt j | t j | ƒ t j | t j |	 ƒ | S(   sÉ  Executes fdist.
        
        Parameters:
        npops - Number of populations
        nsamples - Number of populations sampled
        fst - expected Fst
        sample_size - Sample size per population
        mut - 1=Stepwise, 0=Infinite allele
        num_sims - number of simulations
        data_dir - Where the data is found

        Returns:
        fst - Average Fst
        
        Important Note: This can take quite a while to run!
        gÍÌÌÌÌÌì?gøSã¥›Äì?g        gü©ñÒMbP?s   input.fds	   output.fdR   s   y

s   fdist_params2.dats   
s   cd s    && t   fdist2s    < s    > s   average FstR    iÿÿÿÿ(   R!   R   R   R"   R#   R   R9   R%   R   t	   readlinest
   startswithR)   R'   R(   R*   (   R   t   npopst   nsamplesR1   t   sample_sizet   mutt   num_simsR+   R,   R-   R.   t   linest   line(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt	   run_fdistd   s:    		

,
&iˆ  gü©ñÒMbP?c
         C   s  d }
 d } | } | } xû t  r|  j | | | | | | | ƒ } t | | ƒ |	 k  rw |  j | | | | | | | ƒ S| } | | k rÏ | }
 | | |	 k  r¾ |  j | | | | | | | ƒ S| | d } q | } | |
 |	 k r|  j | | | | | | | ƒ S|
 | d } q Wd S(   sý   Exectues fdist trying to force Fst.
        
        Parameters:
        try_runs - Number of simulations on the part trying to get
                   Fst correct
        limit - Interval limit
        Other parameters can be seen on run_fdist.
        i   i    i   N(   t   TrueRD   t   abs(   R   R=   R>   R1   R?   R@   RA   R+   t   try_runst   limitt   max_run_fstt   min_run_fstt   current_run_fstt   old_fstt   real_fst(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   run_fdist_force_fst˜   s,    
	gffffffî?c   	      C   sy  |  j  ƒ  } |  j  ƒ  } t | t j | d ƒ } | j d t | ƒ d ƒ | j ƒ  t j ƒ  } |  j | ƒ t j	 d | d |  j
 d ƒ d | d | ƒ t j | t j | ƒ t j | t j | ƒ t | t j d	 ƒ } g  } | j ƒ  } yQ xJ | d
 k rJ| j t t d „  | j ƒ  j d ƒ ƒ ƒ ƒ | j ƒ  } qWWn t k
 rj| j ƒ  g  SX| j ƒ  | S(   sg   Executes cplot.

        ci - Confidence interval.
        data_dir - Where the data is found.
        R   s   out.dat out.cpl
s   
s   cd s    && t   cplots    < s    > s   out.cplR   c         S   s
   t  |  ƒ S(   N(   R)   (   t   x(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   <lambda>Ø   s    R    (   R   R!   R   R   R"   R   R#   R$   R9   R%   R   R*   R&   t   appendt   tuplet   mapR'   R(   t
   ValueError(	   R   t   ciR+   R,   R-   R.   R/   t
   conf_linest   l(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt	   run_cplotÁ   s0    
,(

s	   probs.datc         C   s   |  j  ƒ  } |  j  ƒ  } t | t j | d ƒ } | j d | d ƒ | j ƒ  |  j | ƒ t j d | d |  j d ƒ d | d | ƒ t | t j | d	 ƒ } t	 d
 „  | j
 ƒ  ƒ } | j ƒ  t j | t j | ƒ t j | t j | ƒ | S(   sj   Executes pv.

        out_file - Name of output file.
        data_dir - Where the data is found.
        R   s   data_fst_outfile s	    out.dat
s   cd s    && t   pvs    < s    > t   rc         S   s%   t  t d „  |  j ƒ  j d ƒ ƒ ƒ S(   Nc         S   s
   t  |  ƒ S(   N(   R)   (   t   y(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyRQ   ð   s    R    (   RS   RT   R'   R(   (   RP   (    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyRQ   ð   s    (   R   R!   R   R   R"   R#   R9   R%   R   RT   R;   R*   (   R   t   out_fileR+   R,   R-   R.   t   pvft   result(    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   run_pvá   s    
,	
N(   t   __name__t
   __module__R   R   R   R   R4   R9   RD   RN   RY   R`   (    (    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyR      s   	
			4( (    (   t   __doc__R   t   tempfilet   sysR    R   t   shutilR   R   R   t   timeR   R   R   (    (    (    sŽ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PopGen/FDist/Controller.pyt   <module>   s   