Evolife
Evolife has been developed to study Genetic algorithms, Natural evolution and behavioural ecology.
Genetic_map.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2""" @brief Definition of genes as DNA segment 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
24from Evolife.Tools.Tools import error
25
27 """ definition of semantic segments on DNA.
28 A gene is a geographical entity: start_position, end_position, length...
29 """
30
31 def __init__(self, gene_name, gene_length, locus, position, coding):
32 """ A gene knows its name, its length,
33 its locus (order in the list of genes) and its start and end position
34 on the DNA
35 """
36 self.name = gene_name
37 self.length = gene_length
38 if self.length == 0: error('Gene definition','Zero length with zero Default length')
39 self.locus = locus # rank in the list of genes
40 self.start = position # start location on DNA
41 self.end = position + self.length # end location on DNA
42 self.coding = coding
43 if self.coding in range(-1,3):
44 # old numeric designation of coding
45 self.coding = ['Nocoding', 'Weighted', 'Unweighted', 'Gray'][self.coding+1]
46
47
48 def __str__(self):
49 return 'L' + str(self.locus) + ': ' + self.name + ' ' + str(self.start) + '->' + str(self.end) + ' '
50
52 """ a Genetic_map is a series of genes, located one after the other
53 """
54 def __init__(self, GeneMap):
55 """ just calls init_genes
56 """
57 self.init_genes(GeneMap)
58
59 def init_genes(self,gene_list):
60 """ creates genes and puts them into 'GeneMap'
61 Accepted syntax:
62 ['genename1', 'genename2',...]: lengths and coding are retrieved from configuration.
63 [('genename1', 8), ('genename2', 4),...]: numbers give lengths in bits; coding is retrieved from configuration.
64 [('genename1', 8, 'Weighted'), ('genename2', 4, 'Unweighted'),...]: coding can be 'Weighted', 'Unweighted', 'Gray', 'NoCoding'.
65 Note that 'Unweighted' is unsuitable to explore large space.
66 """
67 self.GeneMap = []
68 locus = 0
69 current_pos = 0
70 # for (g_name,g_length) in gene_list:
71 for GeneDefinition in gene_list:
72 # genes are assigned locus and future position on DNA
73 g_length = self.Parameter('GeneLength', Default=0) # default value
74 g_coding = self.Parameter('GeneCoding') # default value
75 if isinstance(GeneDefinition, tuple):
76 if len(GeneDefinition) == 2:
77 (g_name,g_length) = GeneDefinition
78 elif len(GeneDefinition) == 3:
79 (g_name,g_length, g_coding) = GeneDefinition
80 else: error("Bad definition of gene map")
81 else: g_name = GeneDefinition
82 if g_length == 0: g_length = self.Parameter('GeneLength') # compatibility
83 NewGene = Gene_def(g_name, g_length, locus, current_pos, g_coding)
84 self.GeneMap.append(NewGene)
85 locus += 1
86 current_pos = NewGene.end
87
88 def get_gene(self, locus):
89 """ returns GeneMap[locus]
90 """
91 try:
92 return self.GeneMap[locus]
93 except IndexError:
94 error("Gene_def: incorrect locus")
95
96 def get_locus(self, gene_name):
97 """ returns the gene's locus
98 """
99 for g in self.GeneMap:
100 if g.name == gene_name:
101 return g.locus
102 error("Genetic_map: unknown gene name: " + str(gene_name))
103 return None
104
105 def get_gene_name(self, locus):
106 """ finds the name of the gene at locus
107 """
108 return self.get_gene(locus).name
109
110 def get_gene_names(self):
111 """ returns genes' names as a list
112 """
113 return [g.name for g in self.GeneMap]
114
115 def get_gene_boundaries(self,locus):
116 """ finds the gene's boundaries on the DNA
117 """
118 return (self.get_gene(locus).start, self.get_gene(locus).end)
119
120 def get_coding(self, locus):
121 """ returns the gene's coding type (weighted, unweighted, ...)
122 """
123 return self.get_gene(locus).coding
124
125 def gene_boundaries(self, gene_name):
126 """ finds the gene's boundaries on the DNA
127 """
128 return get_boundaries(self, get_locus(self, gene_name))
129
130 def geneMap_length(self):
131 """ location of the end of the last gene on Genemap
132 """
133 return self.get_gene(len(self.GeneMap)-1).end
134
135 def locus_range(self, Locus):
136 """ returns the maximal amplitude of the gene at Locus
137 """
138 coding = self.get_gene(Locus).coding.lower()
139 if coding in ['weighted', 'gray']:
140 # Usual integer coding
141 return (1 << self.get_gene(Locus).length ) - 1
142 elif coding == 'unweighted':
143 # Genes are coded as the number of 1s on the DNA section
144 return self.get_gene(Locus).length
145 elif coding == 'nocoding':
146 return 1
147 else:
148 error("Genetic Map", 'unknown binary coding mode: %s' % str(coding))
149
150 def gene_range(self, gene_name):
151 """ returns the maximal amplitude of the gene
152 """
153 return self.locus_range(self.get_locus(gene_name))
154
155 def gene_pattern(self):
156 """ returns a binary mask showing gene alternation on DNA
157 """
158 G = 0
159 pattern = []
160 for g in self.GeneMap:
161 pattern += [G] * g.length
162 G = 1 - G
163 return tuple(pattern)
164
165 def __str__(self):
166 return "Genetic map:\n\t" + '\n\t'.join([g.__str__() for g in self.GeneMap])
167
168
169
170if __name__ == "__main__":
171 print(__doc__)
172 print(Gene_def.__doc__)
173 print(Genetic_map.__doc__ + '\n')
174
175
176
177
178
181
182if __name__ == "__main__":
183 from Evolife.Scenarii.Parameters import Parameters
185 def __init__(self):
186 Parameters.__init__(self,'../Evolife.evo')
187 Genetic_map.__init__(self,[('sex',1), ('prems',4),('deuze',7),('terce',2)])
188 GeneMap = GMTest()
189 print(GeneMap)
190 print('pattern: '),
191 print(GeneMap.gene_pattern())
192 raw_input('[Return]')
193
194
195__author__ = 'Dessalles'
def __init__(self)
just calls init_genes
Definition: Genetic_map.py:185
definition of semantic segments on DNA.
Definition: Genetic_map.py:26
a Genetic_map is a series of genes, located one after the other
Definition: Genetic_map.py:51
def get_gene_boundaries(self, locus)
finds the gene's boundaries on the DNA
Definition: Genetic_map.py:115
def get_gene_names(self)
returns genes' names as a list
Definition: Genetic_map.py:110
def get_gene(self, locus)
returns GeneMap[locus]
Definition: Genetic_map.py:88
def geneMap_length(self)
location of the end of the last gene on Genemap
Definition: Genetic_map.py:130
def init_genes(self, gene_list)
creates genes and puts them into 'GeneMap' Accepted syntax: ['genename1', 'genename2',...
Definition: Genetic_map.py:59
def __init__(self, GeneMap)
just calls init_genes
Definition: Genetic_map.py:54
def get_coding(self, locus)
returns the gene's coding type (weighted, unweighted, ...)
Definition: Genetic_map.py:120
def gene_range(self, gene_name)
returns the maximal amplitude of the gene
Definition: Genetic_map.py:150
def get_locus(self, gene_name)
returns the gene's locus
Definition: Genetic_map.py:96
def get_gene_name(self, locus)
finds the name of the gene at locus
Definition: Genetic_map.py:105
def gene_pattern(self)
returns a binary mask showing gene alternation on DNA
Definition: Genetic_map.py:155
def locus_range(self, Locus)
returns the maximal amplitude of the gene at Locus
Definition: Genetic_map.py:135
def gene_boundaries(self, gene_name)
finds the gene's boundaries on the DNA
Definition: Genetic_map.py:125
Various functions.
Definition: Tools.py:1
def error(ErrMsg, Explanation='')
Definition: Tools.py:182