ó
BªVCc           @   sU   d  Z  d d l Z d d	 d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(
   s  
Generalized N-Point Crossover.

For even values of N, perform N point crossover 
  (select N/2 points each in the two genomes, and alternate)
For odd values of N, perform symmetric N+1 point crossover
  (select N/2 points for both genomes)
  
N-Point introduction (my notation):
    genome 1:    A-----B-----C-----D-----E-----F-----G
    genome 2:    a=====b=====c=====d=====e=====f=====g
    
    2-point, symmetric (points=1):
	         A-----B-----C- 1 -D-----E-----F-----G
		 a=====b=====c= 2 =d=====e=====f=====g
    returns: (ABCdefg, abcDEFG)

    2-point, asymmetric (points=2):
	         A-----B- 1 -C-----D-----E-----F-----G
		 a=====b=====c=====d=====e= 2 =f=====g
    returns: (ABfg, abcdeCDEFG)

and for the drastic (n can be arbitrary to the length of the genome!):
    12-point, symmetric (points=11):
	         A- 1 -B- 2 -C- 3 -D- 4 -E- 5 -F- 6 -G
		 a= 7 =b= 8 =c= 9 =d= 10 e= 11 f= 12 g
    returns: (AbCdEfG, aBcDeFg)
    (note that points=12 will yield the same result, but 11
     may be somewhat faster)
	
iÿÿÿÿNt   GeneralPointCrossoverc           B   s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s  Perform n-point crossover between genomes at some defined rates.

       Ideas on how to use this class:
	   - Call it directly ( construct, do_crossover )
	   - Use one of the provided subclasses
	   - Inherit from it:
	       * replace _generate_locs with a more domain 
	         specific technique
	       * replace _crossover with a more efficient 
	         technique for your point-count
    gš™™™™™¹?c         C   s.   | |  _  | d |  _ | |  j d |  _ d S(   sB   Initialize to do crossovers at the specified probability.
        i   N(   t   _crossover_probt   _symt   _npoints(   t   selft   pointst   crossover_prob(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Crossover/GeneralPoint.pyt   __init__/   s    	c         C   sT  | j  ƒ  | j  ƒ  f } t j ƒ  } | |  j k rPt | d j ƒ t | d j ƒ f } t | ƒ } |  j d k s‡ |  j | d k r— | d |  _ n  g  } |  j | ƒ } |  j d k rÄ | } n? | | d k rê |  j | d ƒ } n | } |  j | d ƒ }	 |  j	 d | | | f ƒ }
 |  j	 d | | | f ƒ | d _ |
 | d _ n  | S(   s7   Potentially do a crossover between the two organisms.
	i    i   i   (
   t   copyt   randomR   t   lent   genomet   minR   t   _generate_locsR   t
   _crossover(   R   t   org_1t   org_2t   new_orgt   crossover_chancet   boundt   mboundt   y_locst   x_locst   xlocst   tmp(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Crossover/GeneralPoint.pyt   do_crossover7   s&    &"	"c         C   sˆ   g  } xc t  |  j ƒ D]R } t j d | d ƒ } x& | | k rZ t j d | d ƒ } q5 W| j | ƒ q W| j ƒ  d g | | g S(   sÏ   Generalized Location Generator:
	    
	   arguments:
	       bound (int)   - upper bound 
	    
	   returns: [0]+x_0...x_n+[bound]
             where n=self._npoints-1
	       and 0 < x_0 < x_1 ... < bound
	i   i    (   t   rangeR   R	   t   randintt   appendt   sort(   R   R   t   resultst	   incrementt   x(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Crossover/GeneralPoint.pyR   ^   s    

c         C   s‰   | | j  | | d  } xi t d |  j ƒ D]U } | | d } | | j  | | | | | | d !} | r{ | | } q, | } q, W| S(   so  Generalized Crossover Function:
	    
	   arguments: 
	       x (int)        - genome number [0|1]
	       no (organism,organism)
	                      - new organisms
	       locs (int list, int list)
	                      - lists of locations, 
			        [0, +n points+, bound]
				for each genome (sync'd with x)

	    return type: sequence (to replace no[x])
	i   i   (   R   R   R   (   R   R    t   not   locst   st   nt   modet   t(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Crossover/GeneralPoint.pyR   q   s    ( 
(   t   __name__t
   __module__t   __doc__R   R   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/GA/Crossover/GeneralPoint.pyR    #   s
   	'	t   TwoCrossoverc           B   s    e  Z d  Z d „  Z d „  Z RS(   s”   Helper class for Two Point crossovers
    
    Offers more efficient replacements to the GeneralPoint framework
    for single pivot crossovers
    c         C   s   d t  j d | d ƒ | g S(   sY   Replacement generation
	 
	   see GeneralPoint._generate_locs documentation for details
	i    i   (   R	   R   (   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/GA/Crossover/GeneralPoint.pyR   ‘   s    c         C   s<   | d d } | | j  | | d  | | j  | | d S(   sT   Replacement crossover
	 
	   see GeneralPoint._crossover documentation for details
	i   i   (   R   (   R   R    R!   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/GA/Crossover/GeneralPoint.pyR   ™   s    (   R'   R(   R)   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/GA/Crossover/GeneralPoint.pyR*   ‹   s   	t   InterleaveCrossoverc           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   s\   Demonstration class for Interleaving crossover
    
    Interleaving:  AbCdEfG, aBcDeFg
    gš™™™™™¹?c         C   s   t  j |  d | ƒ d  S(   Ni    (   R    R   (   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/GA/Crossover/GeneralPoint.pyR   ¦   s    c         C   s   t  d | d ƒ S(   Niÿÿÿÿi   (   R   (   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/GA/Crossover/GeneralPoint.pyR   ©   s    c         C   sy   | | j  d d !} xH t d |  j d ƒ D]0 } | | d } | | | j  | | d !} q+ W| | | j  |  j d S(   Ni    i   i   i   (   R   R   R   (   R   R    R!   R"   R#   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/GA/Crossover/GeneralPoint.pyR   ¬   s
     (   R'   R(   R)   R   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/GA/Crossover/GeneralPoint.pyR,   ¡   s   	(    (   R)   R	   R    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/GA/Crossover/GeneralPoint.pyt   <module>   s   h