ó
"ˆIc           @   s=  d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z d d l
 m Z d	 e f d
 „  ƒ  YZ d „  Z d d d „  ƒ  YZ d e j e f d „  ƒ  YZ d e j e f d „  ƒ  YZ d d d „  ƒ  YZ d e j f d „  ƒ  YZ e d k r9e j d d ƒ Z e j d e ƒ n  d S(   s4   Tests for Genetic Algorithm mutation functionality.
iÿÿÿÿN(   t
   MutableSeq(   t   SingleLetterAlphabet(   t   Organism(   t   SafeFitnessMutation(   t   ConversionMutation(   t   SinglePositionMutationt   TestAlphabetc           B   s   e  Z d  Z d d d g Z RS(   s   Simple test alphabet.
    t   1t   2t   3(   t   __name__t
   __module__t   __doc__t   letters(    (    (    s   test_GAMutation.pyR      s   c         C   s   |  j  ƒ  } t | j ƒ S(   s,   Simple class for calculating fitnesses.
    (   t   toseqt   intt   data(   t   genomet
   seq_genome(    (    s   test_GAMutation.pyt   test_fitness   s    t   MutationHelperc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   sG   Mixin class which provides useful functions for testing mutations.
    iô  c         C   sƒ   d } xE t  |  j ƒ D]4 } | j |  j ƒ } | |  j k r | d 7} q q Wt | ƒ t |  j ƒ } | | k s t d ƒ ‚ d S(   sF  Test the ability of a mutator to always mutate.

        Arguments:

        o mutator - The mutation class we're testing.

        o expected_percent - The minimum percent of mutations we expect
        to see under 'always mutate.' This will depend on how many letters
        are in the alphabet and other factors.
        i    i   s2   Did not recieve an acceptable number of mutations.N(   t   ranget
   num_trialst   mutatet   organismt   floatt   AssertionError(   t   selft   mutatort   expected_percentt   num_mutationst   trialt   new_orgt   percent_mutants(    (    s   test_GAMutation.pyt   _always_mutate"   s    c         C   sK   xD t  |  j ƒ D]3 } | j |  j ƒ } | |  j k s t d ƒ ‚ q Wd S(   sA   Test that a mutator does not cause unexpected mutations.
        s   Unexpected mutation foundN(   R   R   R   R   R   (   R   R   R   R    (    (    s   test_GAMutation.pyt   _never_mutate9   s    (   R
   R   R   R   R"   R#   (    (    (    s   test_GAMutation.pyR      s   	t   ConversionTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sB   Test mutation which just converts one gene in the chromosome.
    c         C   s(   t  d t ƒ  ƒ } t | t ƒ |  _ d  S(   Nt   1111(   R    R   R   R   R   (   R   R   (    (    s   test_GAMutation.pyt   setUpC   s    c         C   s)   t  d d ƒ } d } |  j | | ƒ d S(   s)   Test ability to cause mutations.
        t   mutation_rateg      ð?gÍÌÌÌÌÌì?N(   R   R"   (   R   R   R   (    (    s   test_GAMutation.pyt   test_always_mutateG   s    c         C   s    t  d d ƒ } |  j | ƒ d S(   s8   Make sure we do not mutate at unexpected times.
        R'   g        N(   R   R#   (   R   R   (    (    s   test_GAMutation.pyt   test_never_mutateV   s    (   R
   R   R   R&   R(   R)   (    (    (    s   test_GAMutation.pyR$   @   s   		t   SinglePositionTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s5   Test mutations at a single position in a genome.
    c         C   s(   t  d t ƒ  ƒ } t | t ƒ |  _ d  S(   NR%   (   R    R   R   R   R   (   R   R   (    (    s   test_GAMutation.pyR&   _   s    c         C   s)   t  d d ƒ } d } |  j | | ƒ d S(   s)   Test ability to cause mutations.
        R'   g      ð?g333333ã?N(   R   R"   (   R   R   R   (    (    s   test_GAMutation.pyR(   c   s    c         C   s    t  d d ƒ } |  j | ƒ d S(   s8   Make sure we do not mutate at unexpected times.
        R'   g        N(   R   R#   (   R   R   (    (    s   test_GAMutation.pyR)   o   s    (   R
   R   R   R&   R(   R)   (    (    (    s   test_GAMutation.pyR*   \   s   		t   TestMutatorc           B   s    e  Z d  Z d „  Z d „  Z RS(   s#   Provide basic mutator ability.
    c         C   s   d |  _  d  S(   Nt   lower(   t   type(   R   (    (    s   test_GAMutation.pyt   __init__x   s    c         C   s¾   | j  j ƒ  } | j } | j ƒ  } |  j d k r7 | S|  j d k ro t t t | ƒ d ƒ | j ƒ | _  | S|  j d k r§ t t t | ƒ d ƒ | j ƒ | _  | St	 d |  j ƒ ‚ d  S(   Nt   sameR,   i   t   highers   Got type %s(
   R   R   R   t   copyR-   R    t   strR   t   alphabett
   ValueError(   R   t   orgt   org_genome_seqt   old_org_genomeR    (    (    s   test_GAMutation.pyR   {   s    	(   R
   R   R   R.   R   (    (    (    s   test_GAMutation.pyR+   u   s   	t   SafeFitnessTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s=   Test mutation which does not allow decreases in fitness.
    c         C   s@   t  ƒ  |  _ t d |  j ƒ } t | t ƒ |  _ t ƒ  |  _ d  S(   NR   (   R   R3   R    R   R   R5   R+   t   test_mutator(   R   R   (    (    s   test_GAMutation.pyR&   ‘   s    c         C   sÄ   t  |  j ƒ } d |  j _ | j |  j ƒ } | |  j k sH t d ƒ ‚ d |  j _ | j |  j ƒ } | |  j k s t d ƒ ‚ d |  j _ | j |  j ƒ } | j |  j j k sÀ t d ƒ ‚ d S(   s5   Make sure we always keep the higher fitness.
        R/   s)   Did not retain organism for same fitness.R,   s9   Did not retain organism when crossover had lower fitness.R0   s4   Did not get new organism when it had higher fitness.N(   R   R9   R-   R   R5   R   t   fitness(   R   R   R    (    (    s   test_GAMutation.pyt   test_keep_higher˜   s    		c         C   sÍ   t  |  j d ƒ } d |  j _ | j |  j ƒ } | |  j k sK t d ƒ ‚ d |  j _ | j |  j ƒ } | j |  j j k  sŠ t d ƒ ‚ d |  j _ | j |  j ƒ } | j |  j j k sÉ t d ƒ ‚ d S(	   sB   Make sure we always keep the new organism when specified.
        g      ð?R/   s)   Did not retain organism for same fitness.R,   s3   Did not get new organism when it had lower fitness.R0   s9   Did not get new organism under higher fitness conditions.N(   R   R9   R-   R   R5   R   R:   (   R   R   R    (    (    s   test_GAMutation.pyt   test_keep_new¬   s    		(   R
   R   R   R&   R;   R<   (    (    (    s   test_GAMutation.pyR8   Ž   s   		t   __main__t	   verbosityi   t
   testRunner(    (    (   R   t   unittestt   Bio.SeqR    t   Bio.AlphabetR   t   Bio.GA.OrganismR   t   Bio.GA.Mutation.GeneralR   t   Bio.GA.Mutation.SimpleR   R   R   R   R   t   TestCaseR$   R*   R+   R8   R
   t   TextTestRunnert   runnert   main(    (    (    s   test_GAMutation.pyt   <module>   s"   	#2