Evolife
Evolife has been developed to study Genetic algorithms, Natural evolution and behavioural ecology.
Genome.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2""" @brief Definition of genes as DNA segments having semantics. """
3
4#============================================================================#
5# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles #
6# Telecom Paris 2022-04-11 www.dessalles.fr #
7# -------------------------------------------------------------------------- #
8# License: Creative Commons BY-NC-SA #
9#============================================================================#
10# Documentation: https://evolife.telecom-paris.fr/Classes #
11#============================================================================#
12
13
14
17
18
19
20import sys
21if __name__ == '__main__': sys.path.append('../..') # for tests
22
23
24
25from Evolife.Genetics.DNA import DNA
26
27class Gene:
28 """ Actual gene (semantic segment on DNA) with intensity
29 A Gene also knows its locus (position in the list of genes, as defined in genetic map)
30 """
31
32 def __init__(self, gene_locus, intensity = 0):
33 """ A gene knows its intensity and its locus (position in the list of genes, as defined in genetic map)
34 """
35 self.locus = gene_locus
36 self.intensity = intensity
37
38 def __str__(self):
39 # return ' L' + str(self.locus) + ': ' + MyScenario.get_gene_name(self.locus) + ' ('+ str(self.intensity) + ')'
40 return ' L' + str(self.locus) + ': ' + ' ('+ str(self.intensity) + ')'
41
42class Genome(DNA):
43 """ class Genome: list of genes carried by individuals
44 """
45
46 def __init__(self, Scenario):
47 self.ScenarioScenario = Scenario
48 self.genome = []
49 for g in self.ScenarioScenario.GeneMap:
50 self.genome.append(Gene(g.locus))
51 DNA.__init__(self, self.ScenarioScenario, self.ScenarioScenario.geneMap_length())
52
53 def update(self):
54 """ gene values are read from DNA and stored in Genes
55 """
56 for locus in range(0,len(self.ScenarioScenario.GeneMap)):
57 (b1,b2) = self.ScenarioScenario.get_gene_boundaries(locus)
58 coding = self.ScenarioScenario.get_coding(locus)
59 self.genome[locus].intensity = self.read_DNA(b1, b2, coding=coding)
60
61 def gene_value(self, name):
62 """ absolute intensity addressed trough name
63 """
64 return self.genome[self.ScenarioScenario.get_locus(name)].intensity
65
66 def gene_relative_value(self, name):
67 """ relative intensity (between 0 and 100) addressed through name
68 """
69 return self.locus_relative_value(self.ScenarioScenario.get_locus(name))
70
71 def locus_value(self, locus):
72 """ absolute intensity addressed trough locus
73 """
74 return self.genome[locus].intensity
75
76 def locus_relative_value(self, locus):
77 """ relative intensity (between 0 and 100) addressed through locus
78 """
79 return 100 * float(self.genome[locus].intensity) / self.ScenarioScenario.locus_range(locus)
80
81 def signature(self):
82 """ returns all gene relative values - useful for statistics and display
83 """
84 return [self.locus_relative_value(locus[0]) for locus in enumerate(self.ScenarioScenario.GeneMap)]
85
86 def __str__(self):
87 return ' || '.join([g.__str__() for g in self.genome])
88
89
90if __name__ == "__main__":
91 print(__doc__)
92 print(Gene.__doc__)
93 print(Genome.__doc__ + '\n')
94 GG = Genome(Blank=False)
95 print(GG)
96 print(DNA.__str__(GG))
97 GG.update()
98 print(GG)
99 raw_input('[Return]')
100
101
102__author__ = 'Dessalles'
class DNA: individuals' 'DNA' defined as a string of bits
Definition: DNA.py:35
Actual gene (semantic segment on DNA) with intensity A Gene also knows its locus (position in the lis...
Definition: Genome.py:27
class Genome: list of genes carried by individuals
Definition: Genome.py:42
def update(self)
gene values are read from DNA and stored in Genes
Definition: Genome.py:53
def locus_relative_value(self, locus)
relative intensity (between 0 and 100) addressed through locus
Definition: Genome.py:76
def signature(self)
returns all gene relative values - useful for statistics and display
Definition: Genome.py:81
def gene_value(self, name)
absolute intensity addressed trough name
Definition: Genome.py:61
def locus_value(self, locus)
absolute intensity addressed trough locus
Definition: Genome.py:71
def gene_relative_value(self, name)
relative intensity (between 0 and 100) addressed through name
Definition: Genome.py:66
Genomes in EVOLIFE are defined as a binary strings.
Definition: DNA.py:1