ó
ÊïLc           @   sû   d  d l  m Z d  d l m Z m Z d  d l m Z m Z d  d l Z d  d l	 m
 Z
 d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d d „ Z d d „ Z d „  Z d „  Z d d d „ Z d S(   iÿÿÿÿ(   t   sqrt(   t   argvt   exit(   t   sept   mkdirN(   t   builtin_tpl_dirc         C   sŠ   |  } t  j d | t  j ƒ } xe | r… t t | j ƒ  d ƒ ƒ } | j d | j ƒ  d d | d ƒ } t  j d | t  j ƒ } q! W| S(   Ns   !!!(.*?)!!!i    s   !!!i   (   t   ret   searcht	   MULTILINEt   strt   evalt   groupst   replace(   t   templatet   executed_templatet   matcht   exec_result(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   exec_template   s    	c         C   s1  | g  k rÀ |  } | } xJ | D]B } | \ } } | d t  | ƒ 7} | j d | t  | ƒ ƒ } q Wt | d d ƒ }	 t | ƒ }
 |
 j d d ƒ j d d ƒ } |	 j | ƒ |	 j ƒ  | g S| d \ } } g  } xP | D]H } | | f g } | j | ƒ t |  | | d	 | ƒ } | j | ƒ qÝ W| Sd  S(
   Nt   _t   ?s   .part   ws   
s   
s   

i    i   (   R	   R   t   openR   t   writet   closet   extendt   process_para(   t	   in_stringt   out_file_prefixt	   para_listt   curr_valuesR   t   f_namet   tupt   namet   valt   fR   t   clean_templatet   rngt   fnamest
   new_valuest
   more_names(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyR      s*     
c         C   s6   d } x) t  d | d ƒ D] } | |  d 7} q W| S(   Nt    i   s   
(   t   range(   t   motift   timest   ret_strt   i(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   dupe9   s    c         C   s&   | d |  } | d |  } | | f S(   Ni   (    (   t   x_maxt   y_maxt   post   yt   x(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   get_xy_from_matrix?   s    c   	      C   sf   t  |  | | ƒ \ } } t  |  | | ƒ \ } } | | d | | d d k r^ t | ƒ d Sd Sd  S(   Ni   i   t    s   0 (   R4   R	   (	   R/   R0   R3   R2   t   migt   my_xt   my_yt   other_xt   other_y(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   get_step_2dD   s
     c      	   C   st   d } xg t  d |  | d ƒ D]N } x; t  d |  | d ƒ D]" } | t |  | | | | ƒ 7} q< W| d 7} q W| S(   NR(   i   s   
(   R)   R;   (   R/   R0   R6   t   mig_matR3   R2   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   generate_ssm2d_matM   s     c         C   s   d } xt t  d |  d ƒ D]_ } xL t  d |  d ƒ D]7 } | | k rS | d 7} q4 | d t | ƒ d 7} q4 W| d 7} q W| S(   NR(   i   s   0 s   !!!s   !!! s   
(   R)   R	   (   t
   total_sizeR6   R<   R3   R2   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   generate_island_matU   s    c         C   sZ   d } xM t  d |  d ƒ D]8 } x% t  d |  d ƒ D] } | d 7} q4 W| d 7} q W| S(   NR(   i   s   0 s   
(   R)   (   R>   t   null_matR3   R2   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   generate_null_mat`   s    c         C   sŠ   d } x= t  d | d ƒ D]( } | t |  ƒ d t | ƒ d 7} q W| t |  ƒ d t | d ƒ d t d | | | ƒ d 7} | S(   NR(   i   R5   s    0 1 1 0 1
s    0 1 g      ð?s    0 1
(   R)   R	   (   t   tR>   t	   join_sizet	   orig_sizet   eventsR-   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   generate_join_eventsh   s
    &@c         C   s   |  S(   N(    (   R   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   no_processoro   s    c         C   s   | |  ƒ } t  | | | g  ƒ S(   N(   R   (   R   R   R   R   t   specific_processort   text(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   process_textr   s    t   .c         C   s7   |  j  ƒ  } t j | | g ƒ } t | | | g  | ƒ S(   N(   t   readR   t   joinRJ   (   t
   par_streamt
   out_prefixt   paramsRH   t   out_dirRI   R   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   generate_modelƒ   s    c         C   s™   | d k r1 t t j t | d g ƒ d ƒ } n" t t j | | d g ƒ d ƒ } | j ƒ  } x) | d k rŠ |  j | ƒ | j ƒ  } qb W| j ƒ  d S(   s  
        Gets a demograpy template.
 
        Most probably this model needs to be sent to GenCases.
 
        stream - Writable stream.
        param  - Template file.
        tp_dir - Directory where to find the template, if None
                 use an internal template
    s   .part   rR(   N(   t   NoneR   R   RM   R   t   readlineR   R   (   t   streamt   modelt   tp_dirR"   t   l(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   get_demography_template‹   s    %"c      	   C   s‡   |  j  d ƒ |  j  t t | ƒ ƒ d ƒ |  j  d ƒ xI | D]A } |  j  d j | d g t d „  t | d ƒ ƒ ƒ d ƒ q> Wd  S(   Ns4   //Number of contiguous linkage blocks in chromosome
s   
sj   //Per Block: Data type, No. of loci, Recombination rate to the right-side locus, plus optional parameters
R5   i    c         S   s
   t  |  ƒ S(   N(   R	   (   R3   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   <lambda>¨   s    i   (   R   R	   t   lenRM   t   mapt   list(   RV   t   locit   locus(    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt	   _gen_loci¢   s    c         C   sà   t  d „  | d ƒ } |  j d ƒ t | ƒ d k s@ | d k rZ |  j t | ƒ d ƒ n |  j t | ƒ d ƒ xh | D]` } | d } | d } t | ƒ d k r´ t |  | ƒ qx x! t | ƒ D] } t |  | ƒ qÁ Wqx Wd S(   s³  
        Writes a Simcoal2 loci template part.

        stream - Writable stream.
        chr    - Chromosome list.

        Current loci list:
          [(chr_repeats,[(marker, (params))])]
          chr_repeats --> Number of chromosome repeats
          marker  --> 'SNP', 'DNA', 'RFLP', 'MICROSAT'
          params  --> Simcoal2 parameters for markers (list of floats
            or ints - if to be processed by generate_model)
    c         S   s   |  | d S(   Ni    (    (   R3   R2   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyR[   ¹   s    i    sº   //Number of independent (unlinked) chromosomes, and "chromosome structure" flag:  0 for identical structure across chromosomes, and  1 for different structures on different chromosomes.
i   s    1
s    0
N(   t   reduceR   R\   R	   Ra   R)   (   RV   t   chrst   num_chrst   chrt   repeatsR_   R-   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   get_chr_template«   s    

c         C   sy   t  | t d d ƒ } t | |  | ƒ t | | ƒ | j ƒ  t  | t d d ƒ } t | |  | d | ƒ| j ƒ  d S(   sT  
       Writes a complete SimCoal2 template file.

       This joins together get_demography_template and get_chr_template,
       which are feed into generate_model
       Please check the three functions for parameters (model from
         get_demography_template, chrs from get_chr_template and
         params from generate_model).
    s   tmp.parR   RS   RQ   N(   R   R   RZ   Rg   R   RR   (   RW   Rc   RP   RQ   RX   RV   RN   (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   generate_simcoal_from_templateÈ   s    

(   t   mathR    t   sysR   R   t   osR   R   R   t   Bio.PopGen.SimCoalR   R   R   R.   R4   R;   R=   R?   RA   RF   RG   RJ   RR   RT   RZ   Ra   Rg   Rh   (    (    (    s<   C:\Python27\Lib\site-packages\Bio\PopGen\SimCoal\Template.pyt   <module>   s(   															