Exemple #1
0
def parse_sampler_params(header):
    """
    Parse parameters that were used to produce a set of samples.  Build a Gene instance
    out of the Gene it was used on and return that as well.
    """
    if header[0] == '#':
        # strip header start
        header = header[1:]
    fields = header.split('\t')
    isoforms = []
    params = {}
    exon_lens = {}
    for field in fields:
        key, value = field.split('=')
        if key == 'isoforms':
            isoform_desc = eval(value)
        elif key == 'exon_lens':
            exon_lens = dict(eval(value))
        else:
            params[key] = value
    exons = []
    for e, exon_len in exon_lens.iteritems():
        exons.append(Gene.Exon(0, 0 + exon_len - 1, label=e))
    gene = Gene.Gene(isoform_desc, exons)
    return (params, gene)
Exemple #2
0
 def crossover(self, x, y):
     """Do Gene Crossover and use real type"""
     ratio = (random.random() - 0.5) * 2 * self.rat_CS
     gx = Gene.Gene()
     gy = Gene.Gene()
     for i in range(len(x.DNA)):
         gx.DNA[i] = x.DNA[i] + ratio * (x.DNA[i] - y.DNA[i])
         gy.DNA[i] = y.DNA[i] - ratio * (x.DNA[i] - y.DNA[i])
     return gx, gy
Exemple #3
0
    def addNode(self, innovationHistory):
        #pick a random connection to create a node between
        if len(self.genes) == 0:
            self.addConnection(innovationHistory)
            return
        #Bias is node 0, so we don't want to lose it
        if len(self.genes) > 1:
            randomConnection = math.floor(
                random.randint(1,
                               len(self.genes) - 1))
        else:
            randomConnection = 0

        self.genes[randomConnection].enabled = False  #disable it

        newNodeNo = self.nextNode
        self.nodes.append(Node(newNodeNo))
        self.nextNode += 1

        #add a new connection to the new node with a weight of 1
        connectionInnovationNumber = self.getInnovationNumber(
            innovationHistory, self.genes[randomConnection].fromNode,
            self.getNode(newNodeNo))
        self.genes.append(
            Gene(self.genes[randomConnection].fromNode,
                 self.getNode(newNodeNo), 1, connectionInnovationNumber))

        connectionInnovationNumber = self.getInnovationNumber(
            innovationHistory, self.getNode(newNodeNo),
            self.genes[randomConnection].toNode)  #fix this
        #add a new connection from the new node with a weight the same as the disabled connection
        self.genes.append(
            Gene(self.getNode(newNodeNo), self.genes[randomConnection].toNode,
                 self.genes[randomConnection].weight,
                 connectionInnovationNumber))
        self.getNode(
            newNodeNo).layer = self.genes[randomConnection].fromNode.layer + 1

        connectionInnovationNumber = self.getInnovationNumber(
            innovationHistory, self.nodes[self.biasNode],
            self.getNode(newNodeNo))
        #connect the bias to the new node with a weight of 0
        self.genes.append(
            Gene(self.nodes[self.biasNode], self.getNode(newNodeNo), 0,
                 connectionInnovationNumber))

        #if the layer of the new node is equal to the layer of the output node of the old connection then a new layer needs to be created
        #more accurately the layer numbers of all layers equal to or greater than this new node need to be incrimented
        if self.getNode(
                newNodeNo).layer == self.genes[randomConnection].toNode.layer:
            for i in self.nodes:  #dont include this newest node
                if i.layer >= self.getNode(newNodeNo).layer:
                    i.layer += 1
            self.layers += 1
        self.connectNodes()
Exemple #4
0
    def crossover(self, permutation, param=Parameters):
        #obtain the index list of individuals who are selected for crossover
        index_pool = self.selection(permutation, param)
        #offsprings list
        offspring = []
        #loop over the pool in order to perform the crossovers
        for i in range(0, int(len(index_pool)), 2):
            #sample two indices of the list to be the cut points (1, len-2)
            cut_point = random.sample(range(1, param.jobs - 1), 2)
            #sort the cut point
            cut_point = sorted(cut_point)

            #crossover then reparation, it's faster to repair than to treat before repairing
            h1 = permutation[index_pool[i]].genotype[:cut_point[0]]
            h2 = permutation[index_pool[i + 1]].genotype[:cut_point[0]]
            t1 = permutation[index_pool[i]].genotype[cut_point[1] + 1:]
            t2 = permutation[index_pool[i + 1]].genotype[cut_point[1] + 1:]

            #look up the elements that are missing in both junctions of subsets
            diff1 = set(list(range(0, param.jobs))) - set(h1 + t2)
            diff2 = set(list(range(0, param.jobs))) - set(h2 + t1)

            #initialize offsprings as new clean individuals
            offspring.append(Gene.Gene(param, type=1))
            offspring.append(Gene.Gene(param, type=1))

            #append h1+ shuffle(diff1) + t2
            offspring[i].genotype = h1 + random.sample(diff1, len(diff1)) + t2
            #append h2+ shuffle(diff2) + t1
            offspring[i +
                      1].genotype = h2 + random.sample(diff2, len(diff2)) + t1

            #print(offspring[i].genotype)
            #print(offspring[i+1].genotype)

            #do any reparation necessary
            offspring[i].genotype = list(unique_everseen(
                offspring[i].genotype))
            offspring[i + 1].genotype = list(
                unique_everseen(offspring[i + 1].genotype))

            #print(offspring[i].genotype)
            #print(offspring[i+1].genotype)
            #print("\n")
        #print("POPULATION")
        #for i in range(0, int(len(permutation))):
        #print(permutation[i].genotype)
        #loop over the pool in order to evaluate everything
        #print("OFFSPRING")
        for i in range(0, int(len(offspring))):
            offspring[i].fitness = self.makespam(offspring[i].genotype, param)
            #print(offspring[i].genotype, offspring[i].fitness )

        return offspring
Exemple #5
0
 def _mutate_node(self):
     gene = random.choice(list(filter(lambda g: g.enabled, self.genes)))
     node = Node(self.functions,
                 self.nodes[gene.in_node].after + [gene.in_node])
     i = len(self.nodes)
     self.nodes[gene.out_node].after.append(i)
     self.nodes.append(node)
     self.layers[-2].append(node)
     gene1 = Gene(gene.in_node, i, gene.weight,
                  self.new_innovation(gene.in_node, i))
     gene2 = Gene(i, gene.out_node, 1,
                  self.new_innovation(i, gene.out_node))
     self._add_gene(gene2)
     self._add_gene(gene1)
     gene.disable()
Exemple #6
0
def index_gff(gff_filename, output_dir):
    """
    Index the given GFF and placed the indexed representation
    in the output directory.
    """
    print "Indexing GFF..."

    # First check that the GFF is not already indexed
    indexed_files = glob.glob(os.path.join(output_dir, "chr*"))
    if len(indexed_files) >= 1:
        print "%s appears to already be indexed. Aborting." % (gff_filename)
        return

    print "  - GFF: %s" % (gff_filename)
    print "  - Outputting to: %s" % (output_dir)
    overall_t1 = time.time()
    t1 = time.time()
    gff_genes = gene_utils.load_genes_from_gff(gff_filename)
    t2 = time.time()
    print "  - Loading of genes from GFF took %.2f seconds" % (t2 - t1)

    t1 = time.time()
    #    pickle_filename = os.path.join(output_dir,
    #                                   "%s.pickle" %(os.path.basename(os.path.abspath(gff_filename))))
    #    pickle_utils.write_pickled_file(gff_genes, pickle_filename)
    serialize_genes(gff_genes, output_dir)
    t2 = time.time()
    print "  - Serialization of genes from GFF took %.2f seconds" % (t2 - t1)
    overall_t2 = time.time()
    print "Indexing of GFF took %.2f seconds." % (overall_t2 - overall_t1)
Exemple #7
0
def index_gff(gff_filename, output_dir):
    """
    Index the given GFF and placed the indexed representation
    in the output directory.
    """
    print "Indexing GFF..."

    # First check that the GFF is not already indexed
    indexed_files = glob.glob(os.path.join(output_dir, "chr*"))
    if len(indexed_files) >= 1:
        print "%s appears to already be indexed. Aborting." % (gff_filename)
        return

    print "  - GFF: %s" % (gff_filename)
    print "  - Outputting to: %s" % (output_dir)
    overall_t1 = time.time()
    t1 = time.time()
    gff_genes = gene_utils.load_genes_from_gff(gff_filename)
    t2 = time.time()
    print "  - Loading of genes from GFF took %.2f seconds" % (t2 - t1)

    t1 = time.time()
    #    pickle_filename = os.path.join(output_dir,
    #                                   "%s.pickle" %(os.path.basename(os.path.abspath(gff_filename))))
    #    pickle_utils.write_pickled_file(gff_genes, pickle_filename)
    serialize_genes(gff_genes, output_dir)
    t2 = time.time()
    print "  - Serialization of genes from GFF took %.2f seconds" % (t2 - t1)
    overall_t2 = time.time()
    print "Indexing of GFF took %.2f seconds." % (overall_t2 - overall_t1)
Exemple #8
0
    def __init__(self,  param = Parameters):
        
        self.population = []
        self.tnPool = []
        self.param = param

        #list with every best element
        self.bests = []

        #list with all the children 
        self.children = []

        #number of restriction
        self.restrictions = param.restrictions

        #define the pool size
        self.pool_size = (math.floor(self.param.popNum * self.param.cRate))
        #ensuring an even value
        if self.pool_size % 2 != 0:
            self.pool_size = self.pool_size - 1

        for _ in range(0, self.param.popNum):
            self.population.append(Gene.Gene(self.param.lowBound, self.param.uppBound, self.param.dim))

        #as a placeholder, set the superindividual as the first element of the population
        self.super = self.population[0]
Exemple #9
0
    def makeCopy(self, id):
        genome = Genome(id)

        genome.neurons = []
        neuronMap = {}
        if self.neurons is not None:
            for neuron in self.neurons:
                copy = Neuron(neuron.id,
                              neuron.type,
                              active=neuron.active,
                              trait=neuron.trait)
                genome.neurons.append(copy)
                neuronMap[copy.id] = copy

        # XXX: not using traits
        genome.genes = []
        if self.genes is not None:
            for gene in self.genes:
                if gene.input.id in neuronMap and gene.output.id in neuronMap:
                    input = neuronMap[gene.input.id]
                    output = neuronMap[gene.output.id]
                    genome.genes.append(
                        Gene(input, output, gene.synapse.weight,
                             gene.synapse.recurrent, gene.synapse.trait,
                             gene.enabled, gene.mutation, gene.innovation))

        # XXX: copying reference
        genome.traits = self.traits

        return genome
 def translate_using_sequence(self, contig):
     """
     Translate the SNPs at this position and give the proportion.
     :param contig: A Contig object with associated Gene(s)
     :return:A line to stdout
     """
     if type(contig).__name__ != "Contig":
         Error.error("Genomic_environment: translate_using_sequence: The object passed is not of type Contig")
     #For each gene in Contig... we search for the one that contain the position
     gene = Gene.__init__()
     for orf in contig.genes:
         if orf.start <= self.position <= orf.end:
             gene = orf
     if gene.sequence == "":
         Error.error("Genomic_environment: translate_using_sequence: There is no sequence in the selected gene")
     orf_seq = Seq(gene.sequence, IUPAC.unambiguous_dna)
     amino_acid_sequence = str(orf_seq.translate(table=11))
     orf_seq = orf_seq.tomutable()
     nucleotides_sequences = self.group_environment()
     total = len(self.reads)
     header = "Contig\tSNP position(contig)\tFrame\tAmino acid\tProportion\n"
     #We put - in the frame number column.
     print(header),
     for entry in nucleotides_sequences:
         mutation = entry[2]
         mutated_seq = copy.copy(orf_seq)
         mutated_seq[self.position-gene.start] = mutation
         mutated_prot_seq = str(mutated_seq.translate(table=11))
         changed_amino_acid_position = math.ceil(float(self.position-gene.start+1)/3)
         changed_amino_acid = mutated_prot_seq[changed_amino_acid_position-1]    #-1 cuz its a python string.
         line = (self.contig+"\t"+self.position+"\t-\t"+changed_amino_acid+"\t" +
                 str(float(nucleotides_sequences[entry])/total))
         print(line)
Exemple #11
0
    def addConnection(self, innovationHistory):
        #cannot add a connection to a fully connected network
        if self.fullyConnected():
            print("connection failed")
            return

        #get random nodes
        randomNode1 = math.floor(random.randint(1, len(self.nodes) - 1))
        randomNode2 = math.floor(random.randint(1, len(self.nodes) - 1))
        while self.randomConnectionNodesAreBad(randomNode1, randomNode2):
            #while the random nodes are no good, get new ones
            randomNode1 = math.floor(random.randint(1, len(self.nodes) - 1))
            randomNode2 = math.floor(random.randint(1, len(self.nodes) - 1))

        temp = 0

        if self.nodes[randomNode1].layer > self.nodes[randomNode2].layer:
            #if the first random node is after the second then switch
            temp = randomNode2
            randomNode2 = randomNode1
            randomNode1 = temp

        #get the innovation number of the connection
        #this will be a new number if no identical genome has mutated in the same way
        connectionInnovationNumber = self.getInnovationNumber(
            innovationHistory, self.nodes[randomNode1],
            self.nodes[randomNode2])

        #add the connection with a random array
        self.genes.append(
            Gene(self.nodes[randomNode1], self.nodes[randomNode2],
                 random.uniform(-1, 1), connectionInnovationNumber))
        self.connectNodes()
Exemple #12
0
    def loaded_events_to_genes(self,
                               single_event_name=None,
                               read_len=None,
                               overhang_len=None):
        """
	Parse the loaded set of events into gene structures.  Map events to genes.
	"""
        if len(self.events) == 0:
            raise Exception, "Must load events first before they can be converted to genes."
        events_to_genes = {}

        t1 = time.time()
        if single_event_name:
            # If given an event name, only parse that event
            event_names = [single_event_name]
        else:
            event_names = self.events.keys()
        for event_name in event_names:
            event = self.events[event_name]

            if self.event_type == 'SE' or self.event_type == 'RI':
                gene = Gene.se_event_to_gene(event.up_part_len,
                                             event.len,
                                             event.dn_part_len,
                                             event.chrom,
                                             label=event.label)
            elif self.event_type == 'TandemUTR':
                gene = Gene.tandem_utr_event_to_gene(event.core_len,
                                                     event.ext_len,
                                                     event.chrom,
                                                     label=event.label)
            elif (self.event_type == 'AFE' or self.event_type == 'ALE'):
                gene = Gene.afe_ale_event_to_gene(event.proximal_exons,
                                                  event.distal_exons,
                                                  self.event_type,
                                                  event.chrom,
                                                  label=event.label,
                                                  read_len=read_len,
                                                  overhang_len=overhang_len)
            else:
                raise Exception, "Unsupported event type: %s" % (
                    self.event_type)
            events_to_genes[event_name] = gene
        t2 = time.time()
        print "Parsing of events to genes took %.2f seconds." % (t2 - t1)
        return events_to_genes
Exemple #13
0
 def reproduct(self, genelist, pickedlist):
     """Do Gene Reproduction and use real type"""
     gene = Gene.Gene()
     gene.f = 1e9
     for i in pickedlist:
         if genelist[i].f < gene.f:
             gene = genelist[i].clone()
     return gene
Exemple #14
0
 def complete_connect(
         self):  # call after creation to make completely connected
     for node_in in self.layers[0]:
         for node_out in self.layers[1]:
             gene = Gene(
                 self.nodes.index(node_in), self.nodes.index(node_out),
                 random.uniform(0, 1),
                 self.new_innovation(self.nodes.index(node_in),
                                     self.nodes.index(node_out)))
             self._add_gene(gene)
Exemple #15
0
 def init(self, bestGeneList):
     """initialize"""
     if not bestGeneList:
         for _ in range(self.bestgenesize):
             gene = Gene.Gene()
             gene.f = 1e9
             self.bestgenelist.append(gene)
         for _ in range(self.poolsize):
             gene = Gene.Gene()
             gene.generate()
             self.genelist.append(gene)
     else:
         self.bestgenelist = copy.deepcopy(bestGeneList)
         percent25 = int(self.poolsize / 4)
         for _ in range(percent25):
             x = random.sample(range(len(self.bestgenelist)), 1)[0]
             gene = self.bestgenelist[x].clone()
             self.genelist.append(gene)
         for _ in range(percent25, self.poolsize):
             gene = Gene.Gene()
             gene.generate()
             self.genelist.append(gene)
Exemple #16
0
    def __init__(self, outname, param=Parameters):

        self.population = []
        self.param = param

        #output file name
        self.outname = outname

        #list with every best element
        self.bests = []

        for _ in range(0, self.param.popNum):
            self.population.append(Gene.Gene(self.param))
Exemple #17
0
    def loaded_events_to_genes(self, single_event_name=None,
                               read_len=None, overhang_len=None):
	"""
	Parse the loaded set of events into gene structures.  Map events to genes.
	"""
	if len(self.events) == 0:
	    raise Exception, "Must load events first before they can be converted to genes."
	events_to_genes = {}

	t1 = time.time()
	if single_event_name:
	    # If given an event name, only parse that event
	    event_names = [single_event_name]
	else:
	    event_names = self.events.keys()
	for event_name in event_names:
	    event = self.events[event_name]

	    if self.event_type == 'SE' or self.event_type == 'RI':
		gene = Gene.se_event_to_gene(event.up_part_len, event.len, event.dn_part_len,
                                             event.chrom,
                                             label=event.label)
	    elif self.event_type == 'TandemUTR':
		gene = Gene.tandem_utr_event_to_gene(event.core_len, event.ext_len,
                                                     event.chrom,
                                                     label=event.label)
            elif (self.event_type == 'AFE' or self.event_type == 'ALE'):
                gene = Gene.afe_ale_event_to_gene(event.proximal_exons, event.distal_exons,
                                                  self.event_type, event.chrom,
                                                  label=event.label, read_len=read_len,
                                                  overhang_len=overhang_len)
            else:
                raise Exception, "Unsupported event type: %s" %(self.event_type)
	    events_to_genes[event_name] = gene
	t2 = time.time()
	print "Parsing of events to genes took %.2f seconds." %(t2 - t1)
	return events_to_genes
Exemple #18
0
def initialize(numStrains: int = 1):

    seq = rRNA.rRNA(length=5)
    print(seq.seq())
    gene = Gene.Gene("A", 1)
    strain = Strain.Strain(name="asdasd", genome={gene.name(): gene}, ssu=seq)
    community = Community.Community({strain.name(): strain})
    print(community.richness())
    community.speciate(strainName=strain.name(),
                       newStrainName="B",
                       changeSSU=True)
    print(community.richness())
    print(community.getStrain(strain.name()).ssu().seq())

    return None
Exemple #19
0
 def factory(db,xml):
     if db=='pcassay':
         import PCAssay
         return PCAssay.SummaryList(xml)
     if db=='gene':
         import Gene
         return Gene.SummaryList(xml)
     if db=='omim':
         import Omim
         return Omim.SummaryList(xml)
     if db=='pubmed':
         import PubMed
         return PubMed.SummaryList(xml)
     if db=='mesh':
         import MeSH
         return MeSH.SummaryList(xml)
Exemple #20
0
    def __init__(self, outname, param=Parameters):

        self.population = []
        self.param = param

        #output file name
        self.outname = outname

        #list with every best element
        self.bests = []

        #list holding information on the amount of each behavior in the population
        self.behavior_ind = []

        for _ in range(0, self.param.popNum):
            self.population.append(Gene.Gene(self.param))
 def translate_using_sequence(self, contig):
     """
     Translate the SNPs at this position and give the proportion.
     :param contig: A Contig object with associated Gene(s)
     :return:A line to stdout
     """
     if type(contig).__name__ != "Contig":
         Error.error(
             "Genomic_environment: translate_using_sequence: The object passed is not of type Contig"
         )
     #For each gene in Contig... we search for the one that contain the position
     gene = Gene.__init__()
     for orf in contig.genes:
         if orf.start <= self.position <= orf.end:
             gene = orf
     if gene.sequence == "":
         Error.error(
             "Genomic_environment: translate_using_sequence: There is no sequence in the selected gene"
         )
     orf_seq = Seq(gene.sequence, IUPAC.unambiguous_dna)
     amino_acid_sequence = str(orf_seq.translate(table=11))
     orf_seq = orf_seq.tomutable()
     nucleotides_sequences = self.group_environment()
     total = len(self.reads)
     header = "Contig\tSNP position(contig)\tFrame\tAmino acid\tProportion\n"
     #We put - in the frame number column.
     print(header),
     for entry in nucleotides_sequences:
         mutation = entry[2]
         mutated_seq = copy.copy(orf_seq)
         mutated_seq[self.position - gene.start] = mutation
         mutated_prot_seq = str(mutated_seq.translate(table=11))
         changed_amino_acid_position = math.ceil(
             float(self.position - gene.start + 1) / 3)
         changed_amino_acid = mutated_prot_seq[
             changed_amino_acid_position - 1]  #-1 cuz its a python string.
         line = (self.contig + "\t" + self.position + "\t-\t" +
                 changed_amino_acid + "\t" +
                 str(float(nucleotides_sequences[entry]) / total))
         print(line)
Exemple #22
0
 def _mutate_gene(self):
     in_node = random.choice(list(sum(self.layers[:-1], [])))
     connections = [
         self.nodes[gene.out_node]
         if self.nodes[gene.in_node] == in_node else None
         for gene in self.genes
     ]  # TODO make better
     ls = filter(
         lambda x: True if (self.nodes.index(x) not in in_node.after) and
         (x not in connections) and (x != in_node) and
         (x not in self._get_input() + [self._get_bias()]) else False,
         self.nodes)
     ls = list(ls)
     if len(ls) == 0:
         # print("no possible connections")
         return None
     out_node = self.nodes.index(random.choice(ls))
     in_node = self.nodes.index(in_node)
     weight = random.uniform(-1, 1)
     innovation = self.new_innovation(in_node, out_node)
     gene = Gene(in_node, out_node, weight, innovation)
     self._add_gene(gene)
Exemple #23
0
def main():
    """Start Gene Machine to find best parameters of RBF Network"""
    ret = setinfo()
    path = "./data/no_pos/"
    inputt = []
    outputt = []

    for dirPath, fileNames in os.walk(path):
        for filee in fileNames:
            filee = os.path.join(dirPath, filee)
            f = open(filee, "r")
            for line in f.readlines():
                tp = line.split()
                listt = []
                if isinstance(eval(tp[0]), float):
                    listt.append(eval(tp[0]))
                    listt.append(eval(tp[1]))
                    listt.append(eval(tp[2]))
                    inputt.append(listt)
                    outputt.append(eval(tp[3]))
                else:
                    print tp
    print "N = " + str(len(inputt))
    print "input list = " + str(inputt)
    print "output list = " + str(outputt)

    # Normalize
    for i in range(len(outputt)):
        outputt[i] = (outputt[i] + 40.0) / 80.0

    fError_ori = 1e9
    fError_now = 1e9
    bestGeneSize = int(ret.get('poolsize') / 10)

    while fError_now > 5:
        if os.path.isfile("./bestGA.txt"):
            readfile = open("./bestGA.txt", "r")
            strlist = []
            for i in range(bestGeneSize):
                strlist.append(readfile.readline())
            readfile.close()

            genelist = []
            for i in range(bestGeneSize):
                gene = Gene.Gene()
                gene.setGene(strlist[i])
                gene.calculateFitness(inputt, outputt)
                genelist.append(gene)
            fError_ori = genelist[0].f
            print("before min function error = ", genelist[0].f)
            genepool = GenePool.GenePool(ret.get("poolsize"),
                                         ret.get('itertimes'),
                                         ret.get("pro_CS"), ret.get("rat_CS"),
                                         ret.get("pro_MU"), ret.get("rat_MU"),
                                         genelist)
        else:
            genepool = GenePool.GenePool(ret.get("poolsize"),
                                         ret.get('itertimes'),
                                         ret.get("pro_CS"), ret.get("rat_CS"),
                                         ret.get("pro_MU"), ret.get("rat_MU"),
                                         [])

        bestGeneList = genepool.geneIteration(inputt, outputt)

        print("after min function error", bestGeneList[0].f)

        fError_now = bestGeneList[0].f

        if fError_now < fError_ori:
            fw = open("./bestGA.txt", 'w')
            for i in range(genepool.bestgenesize):
                DNAList = bestGeneList[i].getDNAList()
                s = " ".join(str(ele) for ele in DNAList) + "\n"
                fw.write(s)
            fw.close()
            print "Training Complete!"
Exemple #24
0
 def get_gene(self, g):
     # Get the definition file
     gene_definition = get_definition_file(g)
     gene = Gene.Gene(gene_definition, build=self.build, debug=self.debug)
     return gene
Exemple #25
0
 def generate(self, size):
     for i in range(0, size):
         self.genes.append(gn.Gene(scdule.generate_schedule(self.events)))
     return self
Exemple #26
0
 def create_gene(self, _geneid, _points, _life_len, _genelist):
     g = Gene(_geneid, _genelist, self, self.trade_mode)
     g.set_params(_points, _life_len)
     return g
    #print((rows))

    # Limiting to 500 Rows
    if rows.__len__() != 0 and count < 500:
        #This one extract desription
        soup = bs.BeautifulSoup(str(rows.pop()), 'lxml')

        m.Description = de.getDescriptionDetailsfromSoup(soup.text).replace(
            ',', ';')

        ####################################################################################################

        #This one extract gene
        # AMke sure that this  Gene does not generate an error Exception
        soup = bs.BeautifulSoup(unicode(rows.pop()), 'lxml')
        m.Gene = gen.getGeneDetailsfromSoupFullName(soup.text)
        type = gen.getGeneDetailsType(soup.text)
        if type == 1:
            m.GeneParameter1 = gen.getGeneDetailsfromSoup(soup.text, 1)
            m.GeneParameter2 = gen.getGeneDetailsfromSoup(soup.text, 4)
        elif type == 2:
            m.GeneParameter1 = gen.getGeneDetailsfromSoup(soup.text, 1)
            m.GeneParameter2 = ""
        else:
            m.GeneParameter1 = ""
            m.GeneParameter2 = ""
        #print(gene)

########################################################################################################################

#This one extract annotation
Exemple #28
0
    def mutateAddNeuron(self, population):
        splitGene = None

        if len(self.genes) < 15:
            checking = False
            for gene in self.genes:
                if checking:
                    if randfloat(
                    ) > 0.3 and gene.synapse.input.type != Neuron.BIAS:
                        splitGene = gene
                        break
                elif gene.enabled and gene.synapse.input.type != Neuron.BIAS:
                    checking = True
        else:
            tryCount = 0
            while tryCount < 20 and splitGene is None:
                index = random.randint(0, len(self.genes) - 1)
                if self.genes[index].enabled and self.genes[
                        index].synapse.input.type != Neuron.BIAS:
                    splitGene = self.genes[index]
                    break
                else:
                    tryCount += 1

        if splitGene is not None:
            splitGene.enabled = False

            synapse = splitGene.synapse
            oldWeight = synapse.weight
            input = synapse.input
            output = synapse.output

            newGene1 = None
            newGene2 = None
            newNeuron = None

            found = False
            for innovation in population.innovations:
                if innovation.type == Innovation.NEURON and \
                   innovation.inId == input.id and \
                   innovation.outId == output.id and \
                   innovation.oldId == splitGene.innovation:
                    newNeuron = Neuron(innovation.newNeuronId)

                    if len(self.traits) > 0:
                        newNeuron.trait = self.traits[0]

                    newGene1 = Gene(input,
                                    newNeuron,
                                    1.0,
                                    synapse.recurrent,
                                    synapse.trait,
                                    innovation=innovation.idA)
                    newGene2 = Gene(newNeuron,
                                    output,
                                    oldWeight,
                                    False,
                                    synapse.trait,
                                    innovation=innovation.idB)

                    found = True
                    break

            if not found:
                id = population.getNextNeuronId()
                newNeuron = Neuron(id)

                if len(self.traits) > 0:
                    newNeuron.trait = self.traits[0]

                innovationId = population.getNextInnovationId(2)

                newGene1 = Gene(input,
                                newNeuron,
                                1.0,
                                synapse.recurrent,
                                synapse.trait,
                                innovation=innovationId)
                newGene2 = Gene(newNeuron,
                                output,
                                oldWeight,
                                False,
                                synapse.trait,
                                innovation=innovationId + 1)

                population.innovations.append(
                    Innovation(input.id,
                               output.id,
                               idA=innovationId,
                               idB=innovationId + 1,
                               oldId=splitGene.innovation,
                               type=Innovation.NEURON,
                               newNeuronId=newNeuron.id))

            if newNeuron is not None:
                self.genes.append(newGene1)
                self.genes.append(newGene2)
                self.neurons.append(newNeuron)
Exemple #29
0
    def mutateAddSynapse(self, population, tries):
        recurrent = False
        if randfloat() < Configuration.recurrentProbability:
            recurrent = True

        startIndex = 0
        for neuron in self.neurons:
            if neuron.type != Neuron.INPUT:
                break
            else:
                startIndex += 1

        numNeurons = len(self.neurons)

        threshold = numNeurons * numNeurons

        found = False
        count = 0

        input = None
        output = None

        if recurrent:
            while count < tries:
                inputIndex = 0
                outputIndex = 0

                # random recurrency
                if randfloat() > 0.5:
                    inputIndex = random.randint(startIndex, numNeurons - 1)
                    outputIndex = inputIndex
                else:
                    inputIndex = random.randint(0, numNeurons - 1)
                    outputIndex = random.randint(startIndex, numNeurons - 1)

                input = self.neurons[inputIndex]
                output = self.neurons[outputIndex]

                if output.type != Neuron.INPUT and self.phenotype.isRecurrent(
                        input.analogue, output.analogue, 0, threshold):
                    for gene in self.genes:
                        if gene.synapse.input == input and gene.synapse.output == output and gene.synapse.recurrent:
                            found = True
                            break

                    if found:
                        break

                count += 1
        else:
            while count < tries:
                inputIndex = random.randint(0, numNeurons - 1)
                outputIndex = random.randint(startIndex, numNeurons - 1)

                input = self.neurons[inputIndex]
                output = self.neurons[outputIndex]

                if output.type != Neuron.INPUT and not self.phenotype.isRecurrent(
                        input.analogue, output.analogue, 0, threshold):
                    for gene in self.genes:
                        if gene.synapse.input == input and gene.synapse.output == output and not gene.synapse.recurrent:
                            found = True
                            break

                    if found:
                        break

                count += 1

        if found:
            found = False

            newGene = None

            for innovation in population.innovations:
                if innovation.type == Innovation.SYNAPSE and \
                   innovation.inId == input.id and \
                   innovation.outId == output.id and \
                   innovation.recurrent == recurrent:
                    newGene = Gene(input,
                                   output,
                                   innovation.weight,
                                   recurrent,
                                   self.traits[innovation.traitId],
                                   innovation=innovation.idA)
                    found = True
                    break

            if not found:
                innovationId = population.getNextInnovationId()
                traitIndex = random.randint(0, len(self.traits) - 1)
                newWeight = randposneg() * randfloat() * 10.0
                newGene = Gene(input,
                               output,
                               newWeight,
                               recurrent,
                               innovation=innovationId,
                               mutation=newWeight)
                population.innovations.append(
                    Innovation(inId=input.id,
                               outId=output.id,
                               weight=newWeight,
                               idA=innovationId,
                               traitId=traitIndex,
                               type=Innovation.SYNAPSE,
                               recurrent=recurrent))

            self.genes.append(newGene)
Exemple #30
0
    def crossover(self, type, dad, id, momFitness=0, dadFitness=0):
        babyTraits = []
        for i in range(len(self.traits)):
            babyTraits.append(Trait(t1=self.traits[i], t2=dad.traits[i]))

        babyGenes = []
        babyNeurons = []

        blxPos = randfloat()

        momBetter = True
        if momFitness < dadFitness or (momFitness == dadFitness
                                       and len(dad.genes) < len(self.genes)):
            momBetter = False

        momIndex, dadIndex = 0, 0

        momStopIndex, dadStopIndex = len(self.genes), len(dad.genes)

        geneCounter = 0
        crossoverPoint = 0

        momGenes = self.genes
        dadGenes = dad.genes

        if type == Crossover.SINGLEPOINT:
            if momStopIndex < dadStopIndex:
                crossoverPoint = random.randint(0, momStopIndex)
            else:
                crossoverPoint = random.randint(0, dadStopIndex)
                momGenes = dad.genes
                momStopIndex = dadStopIndex
                dadGenes = self.genes
                dadStopIndex = len(dadGenes)

        while momIndex < momStopIndex or dadIndex < dadStopIndex:
            skip = False
            chosenGene = None
            disabled = False

            if momIndex == momStopIndex:
                chosenGene = dadGenes[dadIndex]
                dadIndex += 1
                if type != Crossover.SINGLEPOINT and momBetter:
                    skip = True
            elif dadIndex == dadStopIndex:
                chosenGene = momGenes[momIndex]
                momIndex += 1
                if type != Crossover.SINGLEPOINT and not momBetter:
                    skip = True
            else:
                momGene = momGenes[momIndex]
                dadGene = dadGenes[dadIndex]

                momInnovation = momGene.innovation
                dadInnovation = dadGene.innovation

                if momInnovation == dadInnovation:
                    if type == Crossover.MULTIPOINT:
                        if randfloat() < 0.5:
                            chosenGene = momGene
                        else:
                            chosenGene = dadGene

                        if not momGene.enabled or not dadGene.enabled:
                            if randfloat() < 0.75:
                                disabled = True
                    else:
                        useAverage = True

                        if type == Crossover.SINGLEPOINT:
                            useAverage = False
                            if geneCounter < crossoverPoint:
                                chosenGene = momGene
                            elif geneCounter > crossoverPoint:
                                chosenGene = dadGene
                            else:
                                useAverage = True

                        if useAverage:
                            enabled = True
                            trait = None
                            weight = None
                            input = None
                            output = None
                            recurrent = None

                            if randfloat() > 0.5:
                                trait = momGene.synapse.trait
                            else:
                                trait = dadGene.synapse.trait

                            if type == Crossover.BLX:
                                blxAlpha = -0.4
                                blxMax = 0.0
                                blxMin = 0.0

                                w1 = momGene.synapse.weight
                                w2 = dadGene.synapse.weight

                                if w1 > w2:
                                    blxMax = w1
                                    blxMin = w2
                                else:
                                    blxMax = w2
                                    blxMin = w1

                                blxRange = blxMax - blxMin
                                blxExplore = blxAlpha * blxRange

                                blxMin -= blxExplore
                                blxMax += blxExplore

                                blxRange = blxMax - blxMin

                                weight = blxMin + blxPos * blxRange
                            else:
                                weight = (momGene.synapse.weight +
                                          dadGene.synapse.weight) / 2.0

                            if randfloat() > 0.5:
                                input = momGene.synapse.input
                            else:
                                input = dadGene.synapse.input

                            if randfloat() > 0.5:
                                output = momGene.synapse.output
                            else:
                                output = dadGene.synapse.output

                            if randfloat() > 0.5:
                                recurrent = momGene.synapse.recurrent
                            else:
                                recurrent = dadGene.synapse.recurrent

                            innovation = momGene.innovation
                            mutation = (momGene.mutation +
                                        dadGene.mutation) / 2.0

                            if not momGene.enabled or not dadGene.enabled:
                                if randfloat() < 0.75:
                                    enabled = False

                            chosenGene = Gene(input, output, weight, recurrent,
                                              trait, enabled, mutation,
                                              innovation)

                    momIndex += 1
                    dadIndex += 1
                    geneCounter += 1

                elif momInnovation < dadInnovation:
                    if type == Crossover.SINGLEPOINT:
                        if geneCounter < crossoverPoint:
                            chosenGene = momGene
                            momIndex += 1
                            geneCounter += 1
                        else:
                            chosenGene = dadGene
                            dadIndex += 1
                    else:
                        chosenGene = momGene
                        momIndex += 1
                        if not momBetter:
                            skip = True
                elif dadInnovation < momInnovation:
                    if type == Crossover.SINGLEPOINT:
                        dadIndex += 1
                        skip = True
                    else:
                        chosenGene = dadGene
                        dadIndex += 1
                        if momBetter:
                            skip = True

            for gene in babyGenes:
                if gene.synapse.input.id == chosenGene.synapse.input.id   and \
                   gene.synapse.output.id == chosenGene.synapse.output.id and \
                   (gene.synapse.recurrent == chosenGene.synapse.recurrent or \
                    gene.synapse.recurrent or chosenGene.synapse.recurrent):
                    skip = True
                    break

            if not skip:
                traitId = 0
                newInput = None
                newOutput = None

                if chosenGene.synapse.trait is None:
                    traitId = self.traits[0].id
                else:
                    traitId = chosenGene.synapse.trait.id - self.traits[0].id

                input = chosenGene.synapse.input
                output = chosenGene.synapse.output

                if input.id < output.id:
                    newInput = self.addNeuron(input, babyNeurons, babyTraits)
                    newOutput = self.addNeuron(output, babyNeurons, babyTraits)
                else:
                    newOutput = self.addNeuron(output, babyNeurons, babyTraits)
                    newInput = self.addNeuron(input, babyNeurons, babyTraits)

                newGene = Gene(newInput, newOutput, chosenGene.synapse.weight, \
                               chosenGene.synapse.recurrent, babyTraits[traitId], \
                               enabled=chosenGene.enabled, \
                               mutation=chosenGene.mutation, innovation=chosenGene.innovation)

                if disabled:
                    newGene.enabled = False
                    disabled = False

                babyGenes.append(newGene)

        # TODO:
        # make sure all possible input, output, and bias neurons are replicated to children
        # this allows disconnected nodes to survive crossover
        # see the NEAT FAQ for more info
        # TODO: should also add mutateAddInput above if this is used
        #for neuron in self.neurons:
        #   if neuron.type == Neuron.INPUT or neuron.type == Neuron.BIAS or neuron.type == Neuron.OUTPUT:
        #      self.addNeuron(neuron, babyNeurons, babyTraits)

        # TODO: should validate the network here to make sure there is at least
        # one path from at least one input to at least one output
        # otherwise mutate the network until this occurs or throw the network away

        return Genome(id, babyNeurons, babyGenes, babyTraits)
Exemple #31
0
    def load(self, file):
        traitMap = {}
        neuronMap = {}

        for line in file.readlines():
            if not line.startswith('/*'):
                pieces = line.split()
                type = pieces.pop(0)
                if type == 'genomestart':
                    self.id = int(pieces[0])
                elif type == 'genomeend':
                    if self.id != int(pieces[0]):
                        print "ERROR: id mismatch in genome specification"
                        break
                elif type == 'trait':
                    trait = Trait(config=pieces)
                    self.traits.append(trait)
                    traitMap[trait.id] = trait
                elif type == 'node':
                    id = int(pieces[0])
                    traitId = int(pieces[1])
                    type = int(pieces[2])  # wtf?
                    type = int(pieces[3])

                    if traitId in traitMap:
                        neuron = Neuron(id, type, trait=traitMap[traitId])
                    else:
                        neuron = Neuron(id, type)

                    self.neurons.append(neuron)
                    neuronMap[id] = neuron
                elif type == 'gene':
                    traitId = int(pieces[0])
                    inputId = int(pieces[1])
                    outputId = int(pieces[2])
                    weight = float(pieces[3])
                    recurrent = int(pieces[4])
                    innovation = int(pieces[5])
                    mutation = int(pieces[6])
                    enabled = int(pieces[7])

                    if recurrent == 1:
                        recurrent = True
                    else:
                        recurrent = False

                    if enabled == 1:
                        enabled = True
                    else:
                        enabled = False

                    trait = None
                    input = None
                    output = None

                    if traitId in traitMap:
                        trait = traitMap[traitId]

                    if inputId in neuronMap:
                        input = neuronMap[inputId]

                    if outputId in neuronMap:
                        output = neuronMap[outputId]

                    if trait is not None and input is not None and output is not None:
                        self.genes.append(
                            Gene(input, output, weight, recurrent, trait,
                                 enabled, mutation, innovation))
Exemple #32
0
def Kcross(lowerBound,
           upperBound,
           dir,
           parent1=Gene,
           parent2=Gene,
           param=Parameters):

    offspring1 = parent1.genotype
    offspring2 = parent2.genotype

    resultoffspring1 = list(range(param.dim))
    resultoffspring2 = list(range(param.dim))

    for i in range(0, param.dim):
        gene1 = offspring1[i]
        gene2 = offspring2[i]

        if (random.uniform(0, 1) < param.getcRate()):

            result = getGeneResult(gene1, gene2, param.getZeta())

            if dir[i] > 0:
                resultoffspring1[i] = gene1 + result
                resultoffspring2[i] = gene2 + result

                #bounding the problem
                if resultoffspring1[i] > upperBound[i]:
                    resultoffspring1[1] = upperBound[i]

                if resultoffspring2[i] > upperBound[i]:
                    resultoffspring2[1] = upperBound[i]

            else:
                resultoffspring1[i] = gene1 - result
                resultoffspring2[i] = gene2 - result

                #bounding the problem
                if resultoffspring1[i] < lowerBound[i]:
                    resultoffspring1[1] = lowerBound[i]

                if resultoffspring2[i] < lowerBound[i]:
                    resultoffspring2[1] = lowerBound[i]

            if resultoffspring1[i] < 0:
                resultoffspring1[i] = 0

            if resultoffspring2[i] < 0:
                resultoffspring2[i] = 0

            if random.uniform(0, 1) < param.getdirCoeff():

                if dir[i] > 0:
                    resultoffspring1[i] = gene2 - result
                    resultoffspring2[i] = gene1 - result

                    #bounding the problem
                    if resultoffspring1[i] < lowerBound[i]:
                        resultoffspring1[1] = lowerBound[i]

                    if resultoffspring2[i] < lowerBound[i]:
                        resultoffspring2[1] = lowerBound[i]

                else:

                    resultoffspring1[i] = gene2 + result
                    resultoffspring2[i] = gene1 + result

                    #bounding the problem
                    if resultoffspring1[i] > upperBound[i]:
                        resultoffspring1[1] = upperBound[i]

                    if resultoffspring2[i] > upperBound[i]:
                        resultoffspring2[1] = upperBound[i]

            else:

                if dir[i] > 0:

                    resultoffspring1[i] = gene2 + result
                    resultoffspring2[i] = gene1 + result

                    #bounding the problem
                    if resultoffspring1[i] > upperBound[i]:
                        resultoffspring1[1] = upperBound[i]

                    if resultoffspring2[i] > upperBound[i]:
                        resultoffspring2[1] = upperBound[i]

                else:

                    resultoffspring1[i] = gene2 - result
                    resultoffspring2[i] = gene1 - result

                    #bounding the problem
                    if resultoffspring1[i] < lowerBound[i]:
                        resultoffspring1[1] = lowerBound[i]

                    if resultoffspring2[i] < lowerBound[i]:
                        resultoffspring2[1] = lowerBound[i]
        else:
            resultoffspring1[i] = gene1
            resultoffspring2[i] = gene2

    child1 = Gene.Gene(param.lowBound, param.uppBound, param.dim)

    child2 = Gene.Gene(param.lowBound, param.uppBound, param.dim)

    child1.genotype = list(resultoffspring1)
    child2.genotype = list(resultoffspring2)

    return child1, child2
Exemple #33
0
def mutation(permutation, p=Parameters):
    #getting a random index
    shift = random.sample(range(0, p.jobs), 1)
    #saving the value of the random index
    tmp = permutation[shift[0]]
    #getting how many units the index will shift (it will either shift the length of permutation
    #vector - 1 to the left or to the right)
    move = random.randint(-(p.jobs - 2), (p.jobs - 2))
    #remove the element from the list
    permutation.remove(tmp)
    #add the removed element in the list mod(len(list)) units to right or left
    permutation.insert((shift[0] + move) % (len(permutation) + 1), tmp)
    #print("VALUE:",tmp, " MOVE:", move)
    return permutation


p = Parameters.Params("test")
#print(p.jobs,p.machines, p.popNum, p.tn_size, p.generations)
#print(p.joblist)

perm = []
for i in range(0, p.popNum):
    perm.append(Gene.Gene(p))
#print(perm[0].genotype)
#print(mutation(perm[0].genotype,p))
for i in range(0, len(perm)):
    perm[i].fitness = makespam(perm[i].genotype, p)

#selection(perm,p)
update(perm, crossover(perm, p), p)
Exemple #34
0
            num_untrait_genes += 1

    return num_trait_genes / (num_untrait_genes + num_trait_genes)


def organize_gene_info(all_tissue_list):
    all_tissue_list[0].gene_num = 90

    temp_tissue = all_tissue_list[0]

    all_tissue_list[0] = all_tissue_list[1]

    all_tissue_list[1] = temp_tissue


all_tissue_list = Gene.return_tissue_info()
organize_gene_info(all_tissue_list)

# list of genes which lable on the main gene pie chart
gene_label = list()

# number of different kind of cells in tissue
gene_values = list()

# Appending gean number information of each tissue
for i in range(len(all_tissue_list)):
    gene_values.append(all_tissue_list[i].gene_num)

# Set window size for displaying the graph
plt.figure(figsize=(12, 7))
Exemple #35
0
def compute_insert_len(bam_filename, gff_filename, output_dir,
                       min_exon_size):
    """
    Compute insert length distribution and output it to the given
    directory.
    """
    print "Computing insert length distribution of %s" %(bam_filename)
    print "  - Using gene models from: %s" %(gff_filename)
    print "  - Outputting to: %s" %(output_dir)
    print "  - Minimum exon size used: %d" %(min_exon_size)

    if not os.path.isdir(output_dir):
        print "Making directory: %s" %(output_dir)
        os.makedirs(output_dir)

    output_filename = os.path.join(output_dir,
                                   "%s.insert_len" %(os.path.basename(bam_filename)))

    # Load BAM file with reads
    bamfile = sam_utils.load_bam_reads(bam_filename)
    
    # Load the genes from the GFF
    print "Loading genes from GFF..."
    t1 = time.time()
    gff_genes = gene_utils.load_genes_from_gff(gff_filename)
    t2 = time.time()
    print "  - Loading genes from GFF took %.2f seconds" %(t2 - t1)

    insert_lengths = []

    t1 = time.time()

    relevant_region = 0
    
    for gene_id, gene_info in gff_genes.iteritems():
        gene_obj = gene_info["gene_object"]

        # Get all the constitutive parts
        const_parts = gene_obj.get_const_parts()

        chrom = gene_obj.chrom

        # Consider only the large constitutive parts
        for part in const_parts:
            if part.len >= min_exon_size:
                # Get all the reads that land in the coordinates of the exon
                try:
                    exon_reads = bamfile.fetch(chrom, part.start, part.end)
                except ValueError:
                    print "Could not fetch from region: ", chrom, part.start, part.end
                    continue

                # Pair all the paired-end reads that land there
                paired_reads = sam_utils.pair_sam_reads(exon_reads)
                num_paired_reads = len(paired_reads)

                if num_paired_reads == 0:
                    continue

                print "Found %d region" %(relevant_region)
                relevant_region += 1

                # Compute the insert length of each read
                for read_pair_id, read_pair in paired_reads.iteritems():
                    if len(read_pair) != 2:
                        # Skip non-paired reads
                        continue
                    
                    left_read, right_read = read_pair
                    insert_len = right_read.pos - left_read.pos + 1

                    if insert_len > 0:
                        insert_lengths.append(insert_len)
                    else:
                        print "Negative or zero insert length ignored..."

    # Output results to file
    output_file = open(output_filename, 'w')
    insert_length_str = "\n".join(map(str, insert_lengths))
    output_file.write(insert_length_str)
    output_file.close()
                    
    t2 = time.time()
    print "Insert length computation took %.2f seconds." %(t2 - t1)