/usr/share/pyshared/pyevolve/GAllele.py is in python-pyevolve 0.6~rc1+svn398+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | """
:mod:`GAllele` -- the genome alleles module
===========================================================
In this module, there are the :class:`GAllele.GAlleles` class (which is the
class that holds the allele types) and all the
allele types to use with the supported chromosomes.
"""
import random
import Consts
import Util
class GAlleles:
""" GAlleles Class - The set of alleles
Example:
>>> alleles = GAlleles()
>>> choices = [1,2,3,4]
>>> lst = GAlleleList(choices)
>>> alleles.add(lst)
>>> alleles[0].getRandomAllele() in lst
True
:param allele_list: the list of alleles
:param homogeneous: if is True, all the alleles will be use only the first added
"""
def __init__(self, allele_list = None, homogeneous=False):
""" The constructor of GAlleles class """
self.allele_list = []
if allele_list is not None:
self.allele_list.extend(allele_list)
self.homogeneous = homogeneous
def __iadd__(self, allele):
""" To add more alleles using the += operator
.. versionadded:: 0.6
The __iadd__ method.
"""
self.add(allele)
return self
def add(self, allele):
""" Appends one allele to the alleles list
:param allele: allele to be added
"""
self.allele_list.append(allele)
def __getslice__(self, a, b):
""" Returns the slice part of alleles list """
return self.allele_list[a:b]
def __getitem__(self, index):
""" Returns the index allele of the alleles list """
if self.homogeneous: return self.allele_list[0]
try:
val = self.allele_list[index]
except IndexError:
Util.raiseException(
"""An error was occurred while finding allele for the %d position of chromosome.
You may consider use the 'homogeneous' parameter of the GAlleles class.
""" % (index,))
return val
def __setitem__(self, index, value):
""" Sets the index allele of the alleles list """
if self.homogeneous: self.allele_list[0] = value
self.allele_list[index] = value
def __iter__(self):
""" Return the list iterator """
if self.homogeneous:
oneList = [self.allele_list[0]]
return iter(oneList)
return iter(self.allele_list)
def __len__(self):
""" Returns the lenght of the alleles list """
if self.homogeneous: return 1
return len(self.allele_list)
def __repr__(self):
""" Return a string representation of the allele """
ret = "- GAlleles\n"
ret += "\tHomogeneous:\t %s\n" % (self.homogeneous,)
ret += "\tList size:\t %s\n" % (len(self),)
ret += "\tAlleles:\n\n"
if self.homogeneous:
ret += "Allele for 0 position:\n"
ret += self.allele_list[0].__repr__()
else:
for i in xrange(len(self)):
ret += "Allele for %d position:\n" % (i,)
ret += self.allele_list[i].__repr__()
return ret
class GAlleleList:
""" GAlleleList Class - The list allele type
Example:
>>> alleles = GAlleles()
>>> choices = [1,2,3,4]
>>> lst = GAlleleList(choices)
>>> alleles.add(lst)
>>> alleles[0].getRandomAllele() in lst
True
"""
def __init__(self, options=None):
""" The constructor of GAlleleList class """
self.options = []
if options is not None:
self.options.extend(options)
def clear(self):
""" Removes all the allele options from the list """
del self.options[:]
def getRandomAllele(self):
""" Returns one random choice from the options list """
return random.choice(self.options)
def add(self, option):
""" Appends one option to the options list
:param option: option to be added in the list
"""
self.options.append(option)
def __getslice__(self, a, b):
""" Returns the slice part of options """
return self.options[a:b]
def __getitem__(self, index):
""" Returns the index option from the options list """
return self.options[index]
def __setitem__(self, index, value):
""" Sets the index option of the list """
self.options[index] = value
def __iter__(self):
""" Return the list iterator """
return iter(self.options)
def __len__(self):
""" Returns the lenght of the options list """
return len(self.options)
def remove(self, option):
""" Removes the option from list
:param option: remove the option from the list
"""
self.options.remove(option)
def __repr__(self):
""" Return a string representation of the allele """
ret = "- GAlleleList\n"
ret += "\tList size:\t %s\n" % (len(self),)
ret += "\tAllele Options:\t %s\n\n" % (self.options,)
return ret
class GAlleleRange:
""" GAlleleRange Class - The range allele type
Example:
>>> ranges = GAlleleRange(0,100)
>>> ranges.getRandomAllele() >= 0 and ranges.getRandomAllele() <= 100
True
:param begin: the begin of the range
:param end: the end of the range
:param real: if True, the range will be of real values
"""
def __init__(self, begin=Consts.CDefRangeMin,
end=Consts.CDefRangeMax, real=False):
""" The constructor of GAlleleRange class """
self.beginEnd = [(begin, end)]
self.real = real
self.minimum = None
self.maximum = None
self.__processMinMax()
def __processMinMax(self):
""" Process the mininum and maximum of the Allele """
self.minimum = min([x for x,y in self.beginEnd])
self.maximum = max([y for x,y in self.beginEnd])
def add(self, begin, end):
""" Add a new range
:param begin: the begin of range
:param end: the end of the range
"""
if begin > end:
Util.raiseException('Wrong value, the end of the range (%s) is greater than the begin (%s) !' % (end, begin), ValueError)
self.beginEnd.append((begin, end))
self.__processMinMax()
def __getitem__(self, index):
return self.beginEnd[index]
def __setitem__(self, index, value):
if value[0] > value[1]:
Util.raiseException('Wrong value, the end of the range is greater than the begin ! %s' % value, ValueError)
self.beginEnd[index] = value
self.__processMinMax()
def __iter__(self):
return iter(self.beginEnd)
def getMaximum(self):
""" Return the maximum of all the ranges
:rtype: the maximum value
"""
return self.maximum
def getMinimum(self):
""" Return the minimum of all the ranges
:rtype: the minimum value
"""
return self.minimum
def clear(self):
""" Removes all ranges """
del self.beginEnd[:]
self.minimum = None
self.maximum = None
def getRandomAllele(self):
""" Returns one random choice between the range """
rand_func = random.uniform if self.real else random.randint
if len(self.beginEnd) <= 1: choice = 0
else: choice = random.randint(0, len(self.beginEnd)-1)
return rand_func(self.beginEnd[choice][0], self.beginEnd[choice][1])
def setReal(self, flag=True):
""" Sets True if the range is real or False if is integer
:param flag: True or False
"""
self.real = flag
def getReal(self):
""" Returns True if the range is real or False if it is integer """
return self.real
def __len__(self):
""" Returns the ranges in the allele """
return len(self.beginEnd)
def __repr__(self):
""" Return a string representation of the allele """
ret = "- GAlleleRange\n"
ret += "\tReal:\t\t %s\n" % (self.real,)
ret += "\tRanges Count:\t %s\n" % (len(self),)
ret += "\tRange List:\n"
for beg, end in self.beginEnd:
ret += "\t\t\t Range from [%s] to [%s]\n" % (beg, end)
ret += "\n"
return ret
|