ó
X<c           @   sW   d  Z  d d l Z d d l Z d d l m Z d „  Z d „  Z d d d „  ƒ  YZ d S(	   s9   Deal with an Organism in a Genetic Algorithm population.
iÿÿÿÿN(   t
   MutableSeqc         C   s@   g  } x3 t  | ƒ D]% } |  ƒ  } | j t | | ƒ ƒ q W| S(   s×  Generate a population given a function to create genomes

    Arguments:

    o new_genome - A function or callable object that will return
    a genome that can be used for a new organism. This new genome
    should be a MutableSeq object with a specified alphabet.

    o num_organisms - The number of individuals we want in the population.

    o fitness_calculator -- A funtion that will calculate the fitness
    of the organism when given the organisms genome.
    (   t   ranget   appendt   Organism(   t
   new_genomet   num_organismst   fitness_calculatort   all_orgst   org_numt
   cur_genome(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Organism.pyt   function_population
   s
    	c         C   s  g  } t  j ƒ  } t |  j d ƒ t d ƒ k r: d } nc t |  j d ƒ t d ƒ k rb d } n; t |  j d ƒ t d ƒ k rŠ d } n t d t j ƒ ‚ xx t | ƒ D]j } t t j | ƒ |  ƒ } x3 t | ƒ D]% }	 | j	 |  j ƒ }
 | j
 |
 ƒ qÕ W| j
 t | | ƒ ƒ qª W| S(	   sç  Generate a population of individuals with randomly set genomes.

    Arguments:

    o genome_alphabet -- An Alphabet object describing all of the
    possible letters that could potentially be in the genome of an
    organism.

    o genome_size -- The size of each organisms genome.

    o num_organism -- The number of organisms we want in the population.

    o fitness_calculator -- A funtion that will calculate the fitness
    of the organism when given the organisms genome.
    i    t   At   ci   t   ig      ð?t   ds    Alphabet type is unsupported: %s(   t   randomt   Randomt   typet   letterst
   ValueErrort   alphabetR   R    t   arrayt   choiceR   R   (   t   genome_alphabett   genome_sizeR   R   R   t   letter_randt   alphabet_typeR   R   t   gene_numt   new_gene(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Organism.pyt   random_population    s     			R   c           B   s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sC  Represent a single individual in a population.

    Attributes:

    o genome -- The genome of the organism. This is a Bio.MutableSeq
    object that has the sequence of the genome, and the alphabet
    describing all elements that can be a part of the genome.

    o fitness -- The calculate fitness of the organism. This fitness is
    based on the last time it was calculated using the fitness_calculator.
    So... the fitness could potentially be out of date with the real genome
    if you are not careful to recalculate it after changes with
    recalculate_fitness()
    c         C   s^   t  | t ƒ s t d ƒ ‚ | |  _ | |  _ | d k rQ |  j |  j ƒ |  _ n	 | |  _ d S(   sÀ  Initialize an organism

        Arguments:

        o genome -- A MutableSeq object representing the sequence of the
        genome.

        o fitness_calculator -- A funtion that will calculate the fitness
        of the organism when given the organisms genome.

        o start_fitness - the starting fitness corresponding with the
        given genome. If not supplied, the fitness will be calculated
        using fitness_calculator.
        s   Genome must be a MutableSeqN(   t
   isinstanceR    t   AssertionErrort   genomet   _fitness_calct   Nonet   fitness(   t   selfR    R   t   start_fitness(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Organism.pyt   __init__\   s    		c         C   s   d |  j  j |  j f S(   s/   Provide a string output for debugging.
        s   Genome: %s; Fitness %s(   R    t   dataR#   (   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/Organism.pyt   __str__v   s    c         C   s   t  |  j | j ƒ S(   sW   Define comparisons for organisms.

        Compare organisms by their genomes.
        (   t   cmpR    (   R$   t   other(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Organism.pyt   __cmp__{   s    c         C   s    |  j  } t | |  j |  j ƒ S(   sp   Return a copy of the organism.

        This makes it easy to duplicate an organism before changing it.
        (   R    R   R!   R#   (   R$   t   copy_genome(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Organism.pyt   copy‚   s    
c         C   s   |  j  |  j ƒ |  _ d S(   sÆ   Calculate and reset the fitness of the current genome

        This should be called after the genome is updated to ensure that
        fitness always stays in sync with the current genome.
        N(   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/Organism.pyt   recalculate_fitnessŠ   s    N(	   t   __name__t
   __module__t   __doc__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/Organism.pyR   M   s   			(    (   R1   R   R   t   Bio.SeqR    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/Organism.pyt   <module>   s   		-