def reinitialize(self, requirements): inputCount = requirements['inputs'] outputCount = requirements['outputs'] self.genes = [] self.findGenes() # for all the inputs add an input gene. Inputs regulate but don't get regulated. We want all the inputs to have the same impact and for the concentrations to play the main role. We don't care about the promoter, enhancer or inhibitor regions. So, the only thing we want is the same protein sequence for the inputs which means that they effect eachother as low as possible and effect the other genes equally as much. for i in range(inputCount): self.genes.append( G.Gene("11111111", [0 for b in range(self.enhSize)], [0 for b in range(self.inhSize)], [0 for b in range(self.protSize)], "I")) # for all the outputs add an output gene. Outputs don't regulate but get regulated. We want all the genes to have different impacts on the outputs. So, we want them to be as different as possible. for i in range(outputCount): pattern = [] comp_pattern = [] for j in range(self.enhSize): if j >= i * (self.protSize / outputCount) and j < (i + 1) * ( self.protSize / outputCount): pattern.append(1) comp_pattern.append(0) else: pattern.append(0) comp_pattern.append(1) self.genes.append( G.Gene("00000000", pattern, comp_pattern, [0 for b in range(self.protSize)], "O")) self.resetConcentrations()
def get_first_generation(self, vectors): for k in range(0, Evolotion.first_population): temp_gene = gn.Gene(vectors[k]) Evolotion.lf.log_gene(temp_gene.vector) logger.debug('get_first_generation - k:%s, gene:%s', k, temp_gene.vector) self.genes.append(temp_gene)
def findGenes(self): self.genes = [] for i in range( len(self.DNA) - len(self.promoter) - self.protSize * self.protSeqMult - self.enhSize - self.inhSize - 1): #here if ''.join(str(e) for e in self.DNA[i:i + len(self.promoter)]) == self.promoter: enhancer = self.DNA[i + len(self.promoter) + 1:i + len(self.promoter) + 1 + self.enhSize] inhibitor = self.DNA[i + len(self.promoter) + 1 + self.enhSize:i + len(self.promoter) + 1 + self.enhSize + self.inhSize] i += len(self.promoter) + self.enhSize + self.inhSize # here. gotta use the majority rule to come up with the protein sequence protein = [] for j in range(self.protSize): ones = 0 zeroes = 0 for k in range(self.protSeqMult): if self.DNA[i + j + k * self.protSize] == 0: ones += 1 else: zeroes += 1 if ones >= zeroes: # favors 1s over 0s protein.append(1) else: protein.append(0) # print ("enhancer: {} inhibitor: {} protein: {}".format(enhancer, inhibitor, protein)) i += (self.protSeqMult * self.protSize) self.genes.append( G.Gene(self.promoter, enhancer, inhibitor, protein, "TF"))
def recombine(self): for k in range(0, Evolotion.childs_population): p1, p2 = Evolotion.parents[k] if random.random() < Evolotion.pc: Evolotion.childs.append( gn.Gene(Evolotion.genes[p1].recombine( Evolotion.genes[p2]))) else: Evolotion.childs.append( gn.Gene(Evolotion.genes[p1].recombine( Evolotion.genes[p1]))) Evolotion.lf.log_gene( str(p1) + "," + str(p2) + " : " + str(Evolotion.childs[k].vector)) logger.debug('recombine - k:%s, F1:%s , F2:%s', k, p1, p2)
def make_first_generation(self): for k in range(0, Evolotion.first_population): temp_gene = gn.Gene() temp_gene.random_gene() Evolotion.lf.log_gene(temp_gene.vector) logger.debug('make_first_generation - k:%s, gene:%s', k, temp_gene.vector) self.genes.append(temp_gene)
def generate(self): self.genes = list() self.genotype = list() for i in range(Parameter.num_of_genes): gene = g.Gene() self.genes.append(gene) self.genotype.append(''.join(gene.genotype)) # if i is not self.num_of_genes - 1: # self.genotype.append(self.connection) pass
def mutation(self, mutate_gene: gene.Gene): """Insert random small gene into given gene.""" mutation_size = int(np.random.exponential(self.mutation_scale)) if mutation_size == 0: return mutation_gene = gene.Gene( np.random.randint(0, 2, (mutation_size, mutation_size))) mutatepoint_x = np.random.randint(0, self.genesize_x) mutatepoint_y = np.random.randint(0, self.genesize_y) mutate_gene.insert_subgene(mutation_gene, mutatepoint_x, mutatepoint_y) return
def initialize(self, requirements): inputCount = requirements['inputs'] outputCount = requirements['outputs'] DNAlength = self.internalGenesCount * self.geneLength # enhancer inhibitor prot seq => DNA self.DNA = [random.randint(0, 1) for b in range(0, DNAlength)] self.findGenes() # for all the inputs add an input gene. Inputs regulate but don't get regulated. We want all the inputs to have the same impact and for the concentrations to play the main role. We don't care about the promoter, enhancer or inhibitor regions. So, the only thing we want is the same protein sequence for the inputs which means that they effect eachother as low as possible and effect the other genes equally as much. configParser = configparser.ConfigParser() configParser.read("./inputs.ini") counter = 0 for i in range(inputCount): enhancer_tmp = configParser['INP'][repr(counter)] inhibitor_tmp = configParser['INP'][repr(counter + 1)] protein_tmp = configParser['INP'][repr(counter + 2)] enhancer = [int(a) for a in enhancer_tmp] inhibitor = [int(a) for a in inhibitor_tmp] protein = [int(a) for a in protein_tmp] counter += 3 self.genes.append( G.Gene("11111111", enhancer, inhibitor, protein, "I")) # for all the outputs add an output gene. Outputs don't regulate but get regulated. We want all the genes to have different impacts on the outputs. So, we want them to be as different as possible. for i in range(outputCount): enhancer_tmp = configParser['INP'][repr(counter)] inhibitor_tmp = configParser['INP'][repr(counter + 1)] protein_tmp = configParser['INP'][repr(counter + 2)] enhancer = [int(a) for a in enhancer_tmp] inhibitor = [int(a) for a in inhibitor_tmp] protein = [int(a) for a in protein_tmp] counter += 3 self.genes.append( G.Gene("00000000", enhancer, inhibitor, protein, "O")) self.resetConcentrations() # for gene in self.genes: # gene.print() pass
def __init__(self, genotype: list = None): if genotype is None: self.generate() else: self.genotype = list() self.genes = list() for i in range(Parameter.num_of_genes): gene = g.Gene( genotype[i * Parameter.gene_length:i * Parameter.gene_length + Parameter.gene_length]) self.genes.append(gene) self.genotype.append(''.join(gene.genotype)) pass
def __init__(self, selector: Callable[[np.ndarray], int], genesize_x: int = 5, genesize_y: int = 5, worldsize: int = 100, mutation_scale: float = 0.5): self.selector = selector self.genesize_x = genesize_x self.genesize_y = genesize_y self.generation_count = 0 self.worldsize = worldsize if worldsize % 2 == 0 else worldsize + 1 # worldsize must be even. self.world = [0 for i in range(self.worldsize)] self.mutation_scale = mutation_scale for i in range(self.worldsize): self.world[i] = gene.Gene( np.random.randint(0, 2, (genesize_y, genesize_x))) return
def amp_less_5_QC(self, Drug_action, Range): ## the amplicons of each gene should be organize as a list low_ex_gene = {} for amp in self.low_count_ex_amplicon.keys(): values = low_ex_gene.get(amp, []) low_ex_gene[self.low_count_ex_amplicon[amp]] = values + [ amp ] ## key: gene, values: amplicons if len(self.low_count_ex_amplicon) > 7: QC_status = False ## too many critial amplicons have extrme low coverage failed_amp_notice = '%s critical amplicons have coverage less than 6, sample has completely failed!' % len( self.low_count_ex_amplicon) failed_gene = "NA" return failed_gene, failed_amp_notice, QC_status else: QC_status = True ## temp assign a value to be a placeholder amplicon_check = { low_gene[0]: G.Gene(self.ID, self.ICD, low_gene[0], Drug_action, low_gene[1], Range) for low_gene in low_ex_gene.items() } failed_gene = [ gene[0] for gene in amplicon_check.items() if gene[1].ICD_relevant ] ## Does the low coverage amp associate to the ICD codes? potential_failed_gene = [ gene for gene in failed_gene if gene not in self.AMPLICONS_SCORED_LIST ] if QC_status: QC_status = not bool( potential_failed_gene) #True: passed; False: failed failed_amp_notice = "Nothing!" else: failed_amp_notice = 'Sample %s has failed on critical amplicons: %s' % ( self.ID, potential_failed_gene) return failed_gene, failed_amp_notice, QC_status
def findGenes(self): tmp_genes = [] for index, gene in enumerate(self.genes): if gene.type != "TF": tmp_genes.append(gene) self.genes = tmp_genes for i in range(self.internalGenesCount): index = i * self.geneLength enhancer = self.DNA[index:index + self.enhSize] index += self.enhSize inhibitor = self.DNA[index:index + self.inhSize] index += self.inhSize protein = [] for j in range(self.protSize): tempArray = [] for k in range(self.protSeqMult): try: tempArray.append(self.DNA[index + k * self.protSize]) except: print( "DNA size: {} gene length: {} protsize: {} index: {} k: {}" .format(len(self.DNA), self.geneLength, self.protSize, index, k)) exit() zeroes = 0 for l in tempArray: if l == 0: zeroes += 1 if zeroes < len(tempArray) / 2: protein.append(1) else: protein.append(0) # print ("enhancer: {} inhibitor: {} protein: {}".format(enhancer, inhibitor, protein)) self.genes.append( G.Gene("10101010", enhancer, inhibitor, protein, "TF"))
def _mutAcrescentar(self): pos = random.randint(0, len(self.genes)) g = gene.Gene(rand = True) self.genes.insert(pos, g)
def __init__(self,numberOfGenes,origin, geneInit = geneFunction): self.numberOfGenes = numberOfGenes self.genes = [gene.Gene(geneInit(),origin) for x in xrange(numberOfGenes)]
baseh = [10]*30 h = [1.7]*100 fc = 2900 Tx = 80.2 #TX = 38.2 ... G = 16.34 htb = 10 hre = 1.7 Noise = -110 rsrpthre = -88 sinrthre = -3 coverthre = 0.7 obaseallx = [] obaseally = [] obaseallx.extend(random.sample(basex,10)) obaseally.extend(random.sample(basey,10)) ncost = 300 ocost = 100 parameter1 = [basex,basey,baseh,x,y,h,fc,Tx,G,htb,hre,Noise,rsrpthre,sinrthre,coverthre,obaseallx,obaseally,ncost,ocost] baseprenum = 30 parameter = parameter1 popsize = 8 baseusingnum = 4 choosenum =6 crosspro = 0.9 mutepro = 0.1 iterationtime = 10 #print("a's basex is %r"%parameter[0]) a = gene.Gene(baseprenum,parameter,popsize,baseusingnum,choosenum,crosspro,mutepro,iterationtime) maxfit, bestindividual, cost = a.Gene_main()
def mutate(self,chromo): if random.uniform(0,1) < self.mutationRate: choice = random.randint(0,chromo.numberOfGenes-1) chromo.genes[choice] = gene.Gene(chromosome.geneFunction(),chromo.genes[choice].origin)
def inicializarAleatorio(self): self.genes = [gene.Gene(rand = True) for x in xrange(self.initialSize)]
def addGene(self, name, description, purpose, sequence): self.genes.append( gene.Gene(name, description, sequence, gene.GeneType.METABOLISM, purpose)) self.allOneHot = np.vstack( (self.allOneHot, self.genes[-1].numericSequence))
def _mutAlterar(self): pos = random.randint(0, len(self.genes)-1) g = gene.Gene(rand = True) self.genes[pos] = g