コード例 #1
0
void GeneticAlgorithm::mutate(Genotype& genotype) {

    // Generate a dummy genotype with which to test mutations with.
    Genotype dummyGenotype = genotype;
    do { // Whilst the mutated genome are acceptable to the user...

        // Generate a dummy genotype with which to test mutations with.
        dummyGenotype = genotype;

        // Apply mutations to each gene in the dummy genome.
        for (int gene = 0; gene < dummyGenotype.myGenes.size(); gene++) {

            // Generate prototype mutations until one fits into [0, 1].
            double newValue = 0;
            do {
                newValue = dummyGenotype.getGene(gene) + myRand.randNorm(0.0,
                           myMutationVariance);
            } while ((newValue < 0) || (newValue > 1));

            // Apply the mutation.
            dummyGenotype.setGene(gene, newValue);
        }
    } while(myGenomeChecker.checkGenomeIsValid(dummyGenotype) == false);
    // The genome is valid, so overwrite the original genotype.
    genotype = dummyGenotype;
}
コード例 #2
0
ファイル: genotypes.cpp プロジェクト: jrabbit/Critterding
Genotype* Genotypes::loadGenotype(string& content)
{
// 	cerr << "loading genotype start" << endl;
	Genotype* gt = new Genotype();

	string passToBody;
	string passToBrain;
	string line = Parser::Instance()->returnUntillStrip( "\n", content );
	while ( !line.empty() )
	{
		// adamdist=690;
			if ( Parser::Instance()->beginMatchesStrip( "adamdist=", line ) )
			{
				string AD = Parser::Instance()->returnUntillStrip( ";", line );
				//cerr << "AD: " << AD << endl;
				if(EOF == sscanf(AD.c_str(), "%d", &gt->adamdist)) cerr << "ERROR INSERTING CRITTER" << endl;
			}

		// the rest goes to the body or brain
			else
			{
				if ( 
					Parser::Instance()->beginMatches( "color=", line ) ||
					Parser::Instance()->beginMatches( "retinasize=", line ) ||
					Parser::Instance()->beginMatches( "b ", line ) ||
					Parser::Instance()->beginMatches( "m ", line ) ||
					Parser::Instance()->beginMatches( "c ", line ) ||
					Parser::Instance()->beginMatches( "cm ", line )
				)
				{
					passToBody.append(line);
					passToBody.append("\n");
				}
				else
				{
					passToBrain.append(line);
					passToBrain.append("\n");
				}
			}

		line = Parser::Instance()->returnUntillStrip( "\n", content );
	}

	gt->bodyArch->setArch(&passToBody);
	
	gt->registerBrainInputOutputs();
	gt->brainzArch->setArch(&passToBrain);

	gt->count=1;
	gt->speciescolor = colorH.randomColorRGB().getNormalized();
	list.push_back(gt);
	
// 	genotype = genotypes->newg(passToBody, passToBrain, retinasize); // FIXME, with a speciesID

// 	genotype->bodyArch->setArch(&passToBody);
// 	genotype->brainzArch->setArch(&passToBrain);
// 	cerr << "loading genotype end" << endl;
	return gt;
}
コード例 #3
0
ファイル: Genotype.cpp プロジェクト: jenczuen/SztuczneZycie
//mutates current with 2 given
Genotype* Genotype::mutateWith(float f, Genotype* x1, Genotype* x2){
	Genotype *substract = x1->substract(x2);
	Genotype *mutlipiled = substract->multiply(f);
	Genotype *result = this->add(mutlipiled);
	delete substract;
	delete mutlipiled;
	return result;
}
コード例 #4
0
Genotype* Genotypes::copy(Genotype* gt, bool brainmutant, unsigned int brruns, bool bodymutant, unsigned int boruns)
{
// cerr << " gentotype copy start" << endl;
	if ( bodymutant || brainmutant )
	{
		Genotype* newgt = new Genotype();

		newgt->bodyArch->copyFrom(gt->bodyArch);

		if ( bodymutant )
		{
			unsigned int runs = BeRand::Instance()->get(1, boruns);
			newgt->bodyArch->mutate( runs );
		}

		newgt->registerBrainInputOutputs();
		newgt->brainzArch->copyFrom(gt->brainzArch);

		if ( bodymutant )
			newgt->brainzArch->removeObsoleteMotorsAndSensors();

		if ( brainmutant )
		{
			unsigned int runs = BeRand::Instance()->get(1, brruns);
			newgt->brainzArch->mutate( runs );
		}

		newgt->count=1;
		newgt->adamdist = gt->adamdist+1;
		if ( BeRand::Instance()->get( 1 , 1000) <= Settings::Instance()->getCVar("critter_mutate_carnivore") && Settings::Instance()->getCVar("critter_enablecarnivores") == 1 ) 
		{
			if (gt->crittertype == 1 )
				newgt->crittertype = 0;
			else 	newgt->crittertype = 1;
		}
		else
			newgt->crittertype = gt->crittertype;
		
		
		newgt->speciescolor = colorH.randomBeColorRGB().getNormalized();

		list.push_back(newgt);
// cerr << " gentotype copy end" << endl;
		return newgt;
	}
	else
	{
		add(gt);
// cerr << " gentotype copy end" << endl;
		return gt;
	}

	
// 	cerr << " copied genotype " << list.size() << endl;
}
コード例 #5
0
/*Want to access P-value, I statistic, and name of test.
 *
 * void gene_accumulate_assoc( VariantGroup & vars , void *p ){
	Py_geneassocGroup geneassocgroup;

	std::vector<Py_test> tests;
	for( int i = 0; i < )


}*/
void group_accumulate_func( VariantGroup &v, void *p ){
  //Variant Group
	Py_variantGroup vargroup;
	vargroup.NV = v.n_variants();
	vargroup.SIZE = v.size();
	vargroup.COORD = v.coordinate();
	vargroup.SPAN = v.span();
	vargroup.MIDPOS = v.midposition();
	vargroup.NAME = v.name();
	vargroup.NIND = v.n_individuals();
	std::vector<Py_variant> vars;
	for(int i = 0; i < v.n_variants(); i++){
		// Add new variant to the vector of variant groups.
		Variant vn = v.var(i);
		Py_variant var;
		var.CHR = vn.chromosome();
		var.BP1 = vn.position();
		var.BP2 = vn.stop();
		var.ID = vn.name();
		var.NS = vn.n_samples();

		// Sample variant
		SampleVariant &sv = vn.sample(-1);
		Py_sample_variant svar;
		svar.FSET = sv.fileset();
		svar.REF = sv.reference();
		svar.ALT = sv.alternate();
		svar.QUAL = sv.quality();

		// Genotype
		Py_genotype py_geno;
		std::vector<int> gt;
		int nind = vn.size(-1);
		for (int i = 0; i < nind; i++) {
			Genotype* geno = vn.genotype(-1, i);
		    if (geno->more() || geno->null()) {
		    	gt.push_back(-1); // Code NA as -1
		    } else {
		    	gt.push_back(geno->allele_count());
		    }
		  }
		 py_geno.GT = gt;
		 svar.GENO = py_geno;
		 var.CON = svar;

		vars.push_back(var);

	}
	vargroup.VARS = vars;
	std::vector<Py_variantGroup>* d = (std::vector<Py_variantGroup>*)p;
	d->push_back(vargroup);
}
コード例 #6
0
void GeneticAlgorithm::mutate ( )
{
	int transfer;
	int first;
	double x;
	vector<int>::iterator geneIterator;


	for (int i = 0; i < population.size(); i++ )
	{
		transfer = 0;
		Genotype g;
		g.setGene(*population[i].getGene());
		for (geneIterator = g.getGene()->begin(); geneIterator != g.getGene()->end(); ++geneIterator)
		{

			x = rand ( ) % 1000 / 1000.0;
			if ( x < PMUTATION )
			{
				++transfer;

				if ( transfer % 2 == 1 )
					first = distance(g.getGene()->begin(),geneIterator);

				else
				{
					if(g.getGene()->at(first) < *geneIterator)
					{
						++g.getGene()->at(first);
						--*geneIterator;
					}
					else if(g.getGene()->at(first) > *geneIterator)
					{
						--g.getGene()->at(first);
						++*geneIterator;
					}
					else
					{
						++g.getGene()->at(first);
						++*geneIterator;
					}
				}
			}
		}

		verifyAndPush(g);

	}

	return;
}
コード例 #7
0
ファイル: genotypes.cpp プロジェクト: jrabbit/Critterding
Genotype* Genotypes::copy(Genotype* gt, bool brainmutant, unsigned int brruns, bool bodymutant, unsigned int boruns)
{
// cerr << " gentotype copy start" << endl;
	if ( bodymutant || brainmutant )
	{
		Genotype* newgt = new Genotype();

		newgt->bodyArch->copyFrom(gt->bodyArch);

		if ( bodymutant )
		{
			unsigned int runs = RandGen::Instance()->get(1, boruns);
			newgt->bodyArch->mutate( runs );
		}

		newgt->registerBrainInputOutputs();
		newgt->brainzArch->copyFrom(gt->brainzArch);

		if ( bodymutant )
			newgt->brainzArch->removeObsoleteMotorsAndSensors();

		if ( brainmutant )
		{
			unsigned int runs = RandGen::Instance()->get(1, brruns);
			newgt->brainzArch->mutate( runs );
		}

		newgt->count=1;
		newgt->adamdist = gt->adamdist+1;
		newgt->speciescolor = colorH.randomColorRGB().getNormalized();

		list.push_back(newgt);
// cerr << " gentotype copy end" << endl;
		return newgt;
	}
	else
	{
		add(gt);
// cerr << " gentotype copy end" << endl;
		return gt;
	}

	
// 	cerr << " copied genotype " << list.size() << endl;
}
コード例 #8
0
void Environment::createIntermediatePopulation(){
	list<Worm*>::iterator currentWorm;
	int index1, index2, index3;

	for(currentWorm = population.begin(); currentWorm != population.end(); ++currentWorm){
		Genotype* g1;
		Genotype* g2;
		Genotype* g3;
		Genotype* gBest;
		Genotype* mutated;
		Genotype* current;

		index1 = rand() % population.size();
		index2 = rand() % population.size();
		while(index1 == index2) index2 = rand() % population.size();
		index3 = rand() % population.size();
		while(index1 == index3 || index2 == index3) index3 = rand() % population.size();

		g1 = getWromFromPopulationAt(index1)->getBrain()->getGenotype();
		g2 = getWromFromPopulationAt(index2)->getBrain()->getGenotype();
		g3 = getWromFromPopulationAt(index3)->getBrain()->getGenotype();
		gBest = bestWorm->getBrain()->getGenotype();
		//mutated = g1->mutateWith(f, g2, g3);
		mutated = g1->mutateCurrentToBest(f, gBest, g2, g3);
		current = (*currentWorm)->getBrain()->getGenotype();

		intermediatePopulation.push_back(new Worm(
			type,
			(*currentWorm)->getInitPoint()->x,
			(*currentWorm)->getInitPoint()->y,
			(*currentWorm)->getEndPoint()->x,
			(*currentWorm)->getEndPoint()->y,
			current->crossWith(cr, mutated),
			movementSteps
		));

		delete g1;
		delete g2;
		delete g3;
		delete gBest;
		delete mutated;
		delete current;
	}

}
コード例 #9
0
void GeneticAlgorithm::elitist ( Genotype oldBest )
{

	if(newPopulation[newPopulation.size() - 1].getFitness() < oldBest.getFitness()) 
		newPopulation[0] = oldBest;

	return;

}
コード例 #10
0
void GeneticAlgorithm::verifyAndPush( Genotype g )
{
	vector<int>::iterator geneIterator;
	int points = 0;

	for(geneIterator = g.getGene()->begin(); geneIterator != g.getGene()->end(); ++geneIterator)
		{

			points += *geneIterator;

		}

	if(points <= pointLimit)
		population.push_back(g);



}
コード例 #11
0
ファイル: genotypekit.cpp プロジェクト: edawson/vg
double FixedGenotypePriorCalculator::calculate_log_prior(const Genotype& genotype) {
    // Are all the alleles the same?
    bool all_same = true;
    // What is the common allele number (-1 for unset)
    int allele_value = -1;
    for(size_t i = 0; i < genotype.allele_size(); i++) {
        // For each allele in the genotype
        if(allele_value == -1) {
            // On the first one, grab it. Everyone else will have to match.
            allele_value = genotype.allele(i);
        }
        
        if(allele_value != genotype.allele(i)) {
            // There are two distinct allele values in here
            all_same = false;
            break;
        }
    }
    
    // Return the appropriate prior depending on whether the alleles are all the
    // same (homozygous) or not (heterozygous).
    return all_same ? homozygous_prior_ln : heterozygous_prior_ln;
}
コード例 #12
0
void accumulate_func(Variant &v, void *p) {
  // Variant
  Py_variant var;
  var.CHR = v.chromosome();
  var.BP1 = v.position();
  var.BP2 = v.stop();
  var.ID = v.name();
  var.NS = v.n_samples();

  // Sample variant
  SampleVariant &sv = v.sample(-1);
  Py_sample_variant svar;
  svar.FSET = sv.fileset();
  svar.REF = sv.reference();
  svar.ALT = sv.alternate();
  svar.QUAL = sv.quality();

  // Genotype
  Py_genotype py_geno;
  std::vector<int> gt;
  int nind = v.size(-1);
  for (int i = 0; i < nind; i++) {
    Genotype* geno = v.genotype(-1, i);
    if (geno->more() || geno->null()) {
      gt.push_back(-1); // Code NA as -1
    } else {
      gt.push_back(geno->allele_count());
    }
  }
  py_geno.GT = gt;
  svar.GENO = py_geno;
  var.CON = svar;

  // Add new variant to the vector of variants
  std::vector<Py_variant>* d = (std::vector<Py_variant>*)p;
  d->push_back(var);
}
コード例 #13
0
ファイル: ResultData.cpp プロジェクト: outpaddling/freebayes
vcflib::Variant& Results::vcf(
    vcflib::Variant& var, // variant to update
    BigFloat pHom,
    long double bestComboOddsRatio,
    //long double alleleSamplingProb,
    Samples& samples,
    string refbase,
    vector<Allele>& altAllelesIncludingNulls,
    map<string, int> repeats,
	int genotypingIterations,
    vector<string>& sampleNames,
    int coverage,
    GenotypeCombo& genotypeCombo,
    map<string, vector<Allele*> >& alleleGroups,
    map<string, vector<Allele*> >& partialObservationGroups,
    map<Allele*, set<Allele*> >& partialObservationSupport,
    map<int, vector<Genotype> >& genotypesByPloidy,
    vector<string>& sequencingTechnologies,
    AlleleParser* parser) {

    Parameters& parameters = parser->parameters;

    GenotypeComboMap comboMap;
    genotypeCombo2Map(genotypeCombo, comboMap);

    // set up the reported reference allele
    long int referencePosition = (long int) parser->currentPosition; // 0-based

    // remove NULL alt alleles
    vector<Allele> altAlleles;
    for (vector<Allele>::iterator aa = altAllelesIncludingNulls.begin(); aa != altAllelesIncludingNulls.end(); ++aa) {
        if (!aa->isNull()) {
            altAlleles.push_back(*aa);
        }
    }

    map<string, string> adjustedCigar;
    vector<Allele>& adjustedAltAlleles = altAlleles; // just an alias
    for (vector<Allele>::iterator aa = altAlleles.begin(); aa != altAlleles.end(); ++aa) {
        adjustedCigar[aa->base()] = aa->cigar;
        var.alt.push_back(aa->alternateSequence);
    }

    var.ref = refbase;
    assert(!var.ref.empty());

    // get the required size of the reference sequence
    // strip identical bases from start and/or end of alleles
    // if bases have been stripped from the beginning, 

    // set up VCF record-wide variables

    var.sequenceName = parser->currentSequenceName;
    var.position = referencePosition + 1;
    var.id = ".";
    var.filter = ".";

    // note that we set QUAL to 0 at loci with no data
    var.quality = max((long double) 0, nan2zero(big2phred(pHom)));
    if (coverage == 0) {
        var.quality = 0;
    }


    // set up format string

    var.format.clear();
    var.format.push_back("GT");
    if (parameters.calculateMarginals) var.format.push_back("GQ");
    // XXX
    var.format.push_back("DP");
    var.format.push_back("RO");
    var.format.push_back("QR");
    var.format.push_back("AO");
    var.format.push_back("QA");
    // add GL/GLE later, when we know if we need to use one or the other

    unsigned int refBasesLeft = 0;
    unsigned int refBasesRight = 0;
    unsigned int refReadsLeft = 0;
    unsigned int refReadsRight = 0;
    unsigned int refEndLeft = 0;
    unsigned int refEndRight = 0;
    unsigned int refmqsum = 0;
    unsigned int refProperPairs = 0;
    long double refReadMismatchSum = 0;
    long double refReadSNPSum = 0;
    long double refReadIndelSum = 0;
    long double refReadSoftClipSum = 0;
    unsigned int refObsCount = 0;
    map<string, int> refObsBySequencingTechnology;

    map<string, vector<Allele*> >::iterator f = alleleGroups.find(refbase);
    if (f != alleleGroups.end()) {
        vector<Allele*>& referenceAlleles = alleleGroups.at(refbase);
        refObsCount = referenceAlleles.size();
        for (vector<Allele*>::iterator app = referenceAlleles.begin(); app != referenceAlleles.end(); ++app) {
            Allele& allele = **app;
            refReadMismatchSum += allele.readMismatchRate;
            refReadSNPSum += allele.readSNPRate;
            refReadIndelSum += allele.readIndelRate;
            if (allele.isProperPair) {
                ++refProperPairs;
            }
            if (!allele.sequencingTechnology.empty()) {
                ++refObsBySequencingTechnology[allele.sequencingTechnology];
            }
            refBasesLeft += allele.basesLeft;
            refBasesRight += allele.basesRight;
            if (allele.basesLeft >= allele.basesRight) {
                refReadsLeft += 1;
                if (allele.strand == STRAND_FORWARD) {
                    refEndLeft += 1;
                } else {
                    refEndRight += 1;
                }
            } else {
                refReadsRight += 1;
                if (allele.strand == STRAND_FORWARD) {
                    refEndRight += 1;
                } else {
                    refEndLeft += 1;
                }
            }
            refmqsum += allele.mapQuality;
        }
    }

    long double refReadMismatchRate = (refObsCount == 0 ? 0 : refReadMismatchSum / (long double) refObsCount);
    long double refReadSNPRate = (refObsCount == 0 ? 0 : refReadSNPSum / (long double) refObsCount);
    long double refReadIndelRate = (refObsCount == 0 ? 0 : refReadIndelSum / (long double) refObsCount);

    //var.info["XRM"].push_back(convert(refReadMismatchRate));
    //var.info["XRS"].push_back(convert(refReadSNPRate));
    //var.info["XRI"].push_back(convert(refReadIndelRate));

    var.info["MQMR"].push_back(convert((refObsCount == 0) ? 0 : (double) refmqsum / (double) refObsCount));
    var.info["RPPR"].push_back(convert((refObsCount == 0) ? 0 : nan2zero(ln2phred(hoeffdingln(refReadsLeft, refReadsRight + refReadsLeft, 0.5)))));
    var.info["EPPR"].push_back(convert((refBasesLeft + refBasesRight == 0) ? 0 : nan2zero(ln2phred(hoeffdingln(refEndLeft, refEndLeft + refEndRight, 0.5)))));
    var.info["PAIREDR"].push_back(convert((refObsCount == 0) ? 0 : (double) refProperPairs / (double) refObsCount));

    //var.info["HWE"].push_back(convert(nan2zero(ln2phred(genotypeCombo.hweComboProb()))));
    var.info["GTI"].push_back(convert(genotypingIterations));

    // loop over all alternate alleles
    for (vector<Allele>::iterator aa = altAlleles.begin(); aa != altAlleles.end(); ++aa) {

        Allele& altAllele = *aa;
        string altbase = altAllele.base();

        // count alternate alleles in the best genotyping
        unsigned int alternateCount = 0;
        unsigned int alleleCount = 0;
        double alternateQualitySum = 0;
        double partialObservationCount = 0;
        double partialObservationQualitySum;
        // reference / alternate base counts by strand
        //map<string, unsigned int> altCountBySample;
        //map<string, unsigned int> altQualBySample;
        // het counts
        unsigned int hetReferenceObsCount = 0;
        unsigned int hetOtherObsCount = 0;
        unsigned int hetAlternateObsCount = 0;
        unsigned int hetAltSamples = 0;
        unsigned int homAltSamples = 0;
        unsigned int homRefSamples = 0;
        unsigned int refSampleObsCount = 0; // depth in hom-ref samples
        unsigned int altSampleObsCount = 0; // depth in samples with called alternates
        // unique alternate alleles / all alternate alleles in alt-associated samples
        unsigned int uniqueAllelesInAltSamples = 0;
        //unsigned int hetAllObsCount = hetOtherObsCount + hetAlternateObsCount + hetReferenceObsCount;
        unsigned int hetAllObsCount = 0;

        StrandBaseCounts baseCountsTotal;
        map<string, StrandBaseCounts> baseCountsBySample;
        for (vector<string>::iterator sampleName = sampleNames.begin(); sampleName != sampleNames.end(); ++sampleName) {
            GenotypeComboMap::iterator gc = comboMap.find(*sampleName);
            //cerr << "alternate count for " << altbase << " and " << *genotype << " is " << genotype->alleleCount(altbase) << endl;
            if (gc != comboMap.end()) {
                Genotype* genotype = gc->second->genotype;

                Sample& sample = *gc->second->sample;

                // check that we actually have observations for this sample
                unsigned int observationCount = sample.observationCount();
                if (observationCount == 0) {
                    continue;
                }

                alternateCount += genotype->alleleCount(altbase);
                alleleCount += genotype->ploidy;

                unsigned int altCount = sample.observationCount(altbase);
                unsigned int refCount = sample.observationCount(refbase);

                if (!genotype->homozygous) {
                    // het case
                    if (altCount > 0) {
                        ++hetAltSamples;
                        hetAllObsCount += observationCount;
                        hetReferenceObsCount += refCount;
                        hetOtherObsCount += observationCount - altCount;
                        hetAlternateObsCount += altCount;
                        altSampleObsCount += observationCount;
                        uniqueAllelesInAltSamples += sample.size();
                        if (refCount > 0) {
                            --uniqueAllelesInAltSamples; // ignore reference allele
                        }
                    }
                } else {
                    if (altCount > 0) {
                        ++homAltSamples;
                        altSampleObsCount += observationCount;
                        uniqueAllelesInAltSamples += sample.size();
                        if (refCount > 0) {
                            --uniqueAllelesInAltSamples; // ignore reference allele
                        }
                    } else {
                        ++homRefSamples;
                        refSampleObsCount += observationCount;
                    }
                }
                //altCountBySample[*sampleName] = altCount;

                //altQualBySample[*sampleName] = sample.qualSum(altbase);

                StrandBaseCounts baseCounts = sample.strandBaseCount(refbase, altbase);
                baseCountsBySample[*sampleName] = baseCounts;
                baseCountsTotal.forwardRef += baseCounts.forwardRef;
                baseCountsTotal.forwardAlt += baseCounts.forwardAlt;
                baseCountsTotal.reverseRef += baseCounts.reverseRef;
                baseCountsTotal.reverseAlt += baseCounts.reverseAlt;
            }
        }

        unsigned int altBasesLeft = 0;
        unsigned int altBasesRight = 0;
        unsigned int altReadsLeft = 0;
        unsigned int altReadsRight = 0;
        unsigned int altEndLeft = 0;
        unsigned int altEndRight = 0;
        unsigned int altmqsum = 0;
        unsigned int altproperPairs = 0;
        long double altReadMismatchSum = 0;
        long double altReadSNPSum = 0;
        long double altReadIndelSum = 0;
        unsigned int altObsCount = 0;
        map<string, int> altObsBySequencingTechnology;

        // TODO we need a partial obs structure to annotate partial obs
        map<string, vector<Allele*> >::iterator f = alleleGroups.find(altbase);
        if (f != alleleGroups.end()) {
            vector<Allele*>& alternateAlleles = alleleGroups.at(altbase);
            // TODO XXX XXX adjust to use partial observations
            altObsCount = alternateAlleles.size();
            for (vector<Allele*>::iterator app = alternateAlleles.begin(); app != alternateAlleles.end(); ++app) {
                Allele& allele = **app;
                altReadMismatchSum += allele.readMismatchRate;
                altReadSNPSum += allele.readSNPRate;
                altReadIndelSum += allele.readIndelRate;
				// TODO: add altReadSoftClipRate (avg)
                if (allele.isProperPair) {
                    ++altproperPairs;
                }
                if (!allele.sequencingTechnology.empty()) {
                    ++altObsBySequencingTechnology[allele.sequencingTechnology];
                }
                altBasesLeft += allele.basesLeft;
                altBasesRight += allele.basesRight;
                if (allele.basesLeft >= allele.basesRight) {
                    altReadsLeft += 1;
                    if (allele.strand == STRAND_FORWARD) {
                        altEndLeft += 1;
                    } else {
                        altEndRight += 1;
                    }
                } else {
                    altReadsRight += 1;
                    if (allele.strand == STRAND_FORWARD) {
                        altEndRight += 1;
                    } else {
                        altEndLeft += 1;
                    }
                }
                altmqsum += allele.mapQuality;
            }
        }

        long double altReadMismatchRate = (altObsCount == 0 ? 0 : altReadMismatchSum / altObsCount);
        long double altReadSNPRate = (altObsCount == 0 ? 0 : altReadSNPSum / altObsCount);
        long double altReadIndelRate = (altObsCount == 0 ? 0 : altReadIndelSum / altObsCount);
        
        //var.info["XAM"].push_back(convert(altReadMismatchRate));
        //var.info["XAS"].push_back(convert(altReadSNPRate));
        //var.info["XAI"].push_back(convert(altReadIndelRate));

        // alt/ref ratios
        //var.info["ARM"].push_back(convert(refReadMismatchRate == 0 ? 0 : altReadMismatchRate / refReadMismatchRate));
        //var.info["ARS"].push_back(convert(refReadSNPRate == 0 ? 0 : altReadSNPRate / refReadSNPRate));
        //var.info["ARI"].push_back(convert(refReadIndelRate == 0 ? 0 : altReadIndelRate / refReadIndelRate));

        //string refbase = parser->currentReferenceBase();
        // positional information
        // CHROM  POS  ID  REF  ALT  QUAL  FILTER  INFO  FORMAT
        //out.setf(ios::fixed,ios::floatfield);
        //out.precision(5);

        var.info["AC"].push_back(convert(alternateCount));
        var.info["AN"].clear(); var.info["AN"].push_back(convert(alleleCount)); // XXX hack...
        var.info["AF"].push_back(convert((alleleCount == 0) ? 0 : (double) alternateCount / (double) alleleCount));
        var.info["AO"].push_back(convert(altObsCount));
        var.info["PAO"].push_back(convert(samples.partialObservationCount(altbase)));
        var.info["QA"].push_back(convert(samples.qualSum(altbase)));
        var.info["PQA"].push_back(convert(samples.partialQualSum(altbase)));
        if (homRefSamples > 0 && hetAltSamples + homAltSamples > 0) {
            double altSampleAverageDepth = (double) altSampleObsCount
                / ( (double) hetAltSamples + (double) homAltSamples );
            double refSampleAverageDepth = (double) refSampleObsCount / (double) homRefSamples;
            var.info["DPRA"].push_back(convert(altSampleAverageDepth / refSampleAverageDepth));
        } else {
            var.info["DPRA"].push_back(convert(0));
        }

        var.info["SRP"].clear(); // XXX hack
        var.info["SRF"].clear();
        var.info["SRR"].clear();
        var.info["SRF"].push_back(convert(baseCountsTotal.forwardRef));
        var.info["SRR"].push_back(convert(baseCountsTotal.reverseRef));
        var.info["SRP"].push_back(convert((refObsCount == 0) ? 0 : nan2zero(ln2phred(hoeffdingln(baseCountsTotal.forwardRef, refObsCount, 0.5)))));
        var.info["SAF"].push_back(convert(baseCountsTotal.forwardAlt));
        var.info["SAR"].push_back(convert(baseCountsTotal.reverseAlt));
        var.info["SAP"].push_back(convert((altObsCount == 0) ? 0 : nan2zero(ln2phred(hoeffdingln(baseCountsTotal.forwardAlt, altObsCount, 0.5)))));
        var.info["AB"].push_back(convert((hetAllObsCount == 0) ? 0 : nan2zero((double) hetAlternateObsCount / (double) hetAllObsCount )));
        var.info["ABP"].push_back(convert((hetAllObsCount == 0) ? 0 : nan2zero(ln2phred(hoeffdingln(hetAlternateObsCount, hetAllObsCount, 0.5)))));
        var.info["RUN"].push_back(convert(parser->homopolymerRunLeft(altbase) + 1 + parser->homopolymerRunRight(altbase)));
        var.info["MQM"].push_back(convert((altObsCount == 0) ? 0 : nan2zero((double) altmqsum / (double) altObsCount)));
        var.info["RPP"].push_back(convert((altObsCount == 0) ? 0 : nan2zero(ln2phred(hoeffdingln(altReadsLeft, altReadsRight + altReadsLeft, 0.5)))));
        var.info["RPR"].push_back(convert(altReadsRight));
		var.info["RPL"].push_back(convert(altReadsLeft));
        var.info["EPP"].push_back(convert((altBasesLeft + altBasesRight == 0) ? 0 : nan2zero(ln2phred(hoeffdingln(altEndLeft, altEndLeft + altEndRight, 0.5)))));
        var.info["PAIRED"].push_back(convert((altObsCount == 0) ? 0 : nan2zero((double) altproperPairs / (double) altObsCount)));
        var.info["CIGAR"].push_back(adjustedCigar[altAllele.base()]);
        var.info["MEANALT"].push_back(convert((hetAltSamples + homAltSamples == 0) ? 0 : nan2zero((double) uniqueAllelesInAltSamples / (double) (hetAltSamples + homAltSamples))));

        for (vector<string>::iterator st = sequencingTechnologies.begin();
             st != sequencingTechnologies.end(); ++st) { string& tech = *st;
            var.info["technology." + tech].push_back(convert((altObsCount == 0) ? 0
                                                             : nan2zero((double) altObsBySequencingTechnology[tech] / (double) altObsCount )));
        }

        // allele class
        if (altAllele.type == ALLELE_DELETION) {
            var.info["TYPE"].push_back("del");
            // what is the class of deletion
            // microsatellite repeat?
            // "novel"?
            // how large is the repeat, if there is one?
        } else if (altAllele.type == ALLELE_INSERTION) {
            var.info["TYPE"].push_back("ins");
        } else if (altAllele.type == ALLELE_COMPLEX) {
            var.info["TYPE"].push_back("complex");
        } else if (altAllele.type == ALLELE_SNP) {
            var.info["TYPE"].push_back("snp");

            /*
            // CpG
            if (parser->isCpG(altbase)) {
                var.infoFlags["CpG"] = true;
            }
            */
        } else if (altAllele.type == ALLELE_MNP) {
            var.info["TYPE"].push_back("mnp");
        } else {
            /*
            cerr << "What is this?"
                 << "type: " << altAllele.type << " "
                 << "allele: " << altAllele << endl;
            */
        }
        var.info["LEN"].push_back(convert(altAllele.length));

    }


    // set up site-wide INFO tags, non-multiple

    // info variables

    // site-wide coverage
    int samplesWithData = 0;
    int refAlleleObservations = 0;
    for (vector<string>::iterator sampleName = sampleNames.begin(); sampleName != sampleNames.end(); ++sampleName) {
        GenotypeComboMap::iterator gc = comboMap.find(*sampleName);
        //cerr << "alternate count for " << altbase << " and " << *genotype << " is " << genotype->alleleCount(altbase) << endl;
        if (gc != comboMap.end()) {
            Genotype* genotype = gc->second->genotype;

            Sample& sample = *gc->second->sample;
            //refAlleleObservations += sample.observationCount(refbase);
            refAlleleObservations += sample.observationCount(refbase);

            ++samplesWithData;
        }
    }

    var.info["NS"].push_back(convert(samplesWithData));
    var.info["DP"].push_back(convert(coverage));
    var.info["RO"].push_back(convert(refAlleleObservations));
    var.info["PRO"].push_back(convert(samples.partialObservationCount(refbase)));
    var.info["QR"].push_back(convert(samples.qualSum(refbase)));
    var.info["PQR"].push_back(convert(samples.partialQualSum(refbase)));

    // tally partial observations to get a mean coverage per bp of reference
    int haplotypeLength = refbase.size();
    int basesInObservations = 0;
    
    for (map<string, vector<Allele*> >::iterator g = alleleGroups.begin(); g != alleleGroups.end(); ++g) {
        for (vector<Allele*>::iterator a = g->second.begin(); a != g->second.end(); ++a) {
            basesInObservations += (*a)->alternateSequence.size();
        }
    }

    for (map<Allele*, set<Allele*> >::iterator p = partialObservationSupport.begin(); p != partialObservationSupport.end(); ++p) {
        basesInObservations += p->first->alternateSequence.size();
    }
 
    double depthPerBase = (double) basesInObservations / (double) haplotypeLength;
    var.info["DPB"].push_back(convert(depthPerBase));

    // number of alternate alleles
    var.info["NUMALT"].push_back(convert(altAlleles.size()));

    if (parameters.showReferenceRepeats && !repeats.empty()) {
        stringstream repeatsstr;
        for (map<string, int>::iterator c = repeats.begin(); c != repeats.end(); ++c) {
            repeatsstr << c->first << ":" << c->second << "|";
        }
        string repeatstr = repeatsstr.str();
        TRY { repeatstr = repeatstr.substr(0, repeatstr.size() - 1); } CATCH;
        var.info["REPEAT"].clear();
        var.info["REPEAT"].push_back(repeatstr);
    }
コード例 #14
0
ファイル: Phenotype.cpp プロジェクト: 222464/Ants
void Phenotype::createFromGenotype(const Genotype &genotype) {
	_numInputs = genotype.getNumInputs();
	_numOutputs = genotype.getNumOutputs();

	std::unordered_map<size_t, size_t> inputNodeIDToInputIndex;

	for (size_t i = 0; i < genotype._inputNodeIDs.size(); i++)
		inputNodeIDToInputIndex[genotype._inputNodeIDs[i]] = i;

	// Flood fill backward from outputs to build minimal network
	std::list<size_t> openNodeIDs;

	std::unordered_map<size_t, size_t> nodeIDToIndex;

	std::unordered_set<size_t> recurrentNodeIndicesSet;

	for (size_t i = 0; i < genotype._outputNodeIDs.size(); i++)
		openNodeIDs.push_back(genotype._outputNodeIDs[i]);

	while (!openNodeIDs.empty()) {
		size_t currentNodeID = openNodeIDs.front();

		openNodeIDs.pop_front();

		std::shared_ptr<Node> newNode = std::make_shared<Node>();

		_nodes.push_back(newNode);

		nodeIDToIndex[currentNodeID] = _nodes.size() - 1;

		std::shared_ptr<const Genotype::Node> genotypeNode = genotype._nodes.at(currentNodeID);

		newNode->_bias = genotypeNode->_bias;
		newNode->_functionIndex = genotypeNode->_functionIndex;
		newNode->_output = 0.0f;

		for (std::unordered_map<size_t, float>::const_iterator cit0 = genotypeNode->_connections.begin(); cit0 != genotypeNode->_connections.end(); cit0++) {
			std::unordered_map<size_t, size_t>::const_iterator cit1 = nodeIDToIndex.find(cit0->first);

			Connection c;

			std::unordered_map<size_t, size_t>::const_iterator cit2 = inputNodeIDToInputIndex.find(cit0->first);

			if (cit2 == inputNodeIDToInputIndex.end()) {
				if (cit1 == nodeIDToIndex.end()) {
					c._fetchType = _intermediate;
					c._fetchIndex = cit0->first; // Set to ID for now, will be mapped to an index later (when all nodes have been explored)
				}
				else {
					c._fetchType = _recurrent;
					c._fetchIndex = cit1->second;
				}
			}
			else {
				c._fetchType = _input;

				c._fetchIndex = cit2->second;
			}
			
			c._weight = cit0->second;

			newNode->_connections.push_back(c);

			if (c._fetchType != _input) {
				if (cit1 == nodeIDToIndex.end()) {
					// Visit this node (if it exists)
					if (genotype._nodes.find(cit0->first) != genotype._nodes.end())
						openNodeIDs.push_back(cit0->first);
				}
				else { // Add as recurrent source node
					std::unordered_set<size_t>::const_iterator cit3 = recurrentNodeIndicesSet.find(cit1->second);

					if (cit3 == recurrentNodeIndicesSet.end()) {
						recurrentNodeIndicesSet.insert(cit1->second);
						_recurrentNodeIndices.push_back(cit1->second);
					}
				}
			}
		}
	}

	for (size_t i = 0; i < _nodes.size(); i++)
	for (size_t j = 0; j < _nodes[i]->_connections.size(); j++)
	if (_nodes[i]->_connections[j]._fetchType == _intermediate)
		_nodes[i]->_connections[j]._fetchIndex = nodeIDToIndex[_nodes[i]->_connections[j]._fetchIndex];

	// Flip nodes around
	std::reverse(_nodes.begin(), _nodes.end());

	// Convert node IDs in the phenotype to true node indices using conversion map. Take flipped node order into account. Do not change if it is an input connection
	for (size_t ni = 0; ni < _nodes.size(); ni++)
	for (size_t ci = 0; ci < _nodes[ni]->_connections.size(); ci++)
	if (_nodes[ni]->_connections[ci]._fetchType != _input) // If is input connection
		_nodes[ni]->_connections[ci]._fetchIndex = _nodes.size() - _nodes[ni]->_connections[ci]._fetchIndex - 1;

	for (size_t i = 0; i < _recurrentNodeIndices.size(); i++)
		_recurrentNodeIndices[i] = _nodes.size() - _recurrentNodeIndices[i] - 1;
}
コード例 #15
0
void TanCTRNNetwork::reconstructNetwork(Genotype& genotype) {

	//  Initialise the parameters according to the genotype.
	int gene = 0;

	/* Reconstruct the weights between the neurons.
	 * Note these GENE NUMBERS are expected to be at the begining of the genome by 
	 * writeWeightsToGenome.
	 */
	for (int dest = 0; dest < myNoNeurons; dest++) {
		for (int source = 0; source < myNoNeurons; source++) {
			// Note inputs now have incoming weights. Is this good?
			myWeights[dest * myNoNeurons + source] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
			gene++;
		}
	}

	// If requested, on first construction remap the inital weights to an alternative
	// bounds.
	if((genotype.myHasEverBeenConstructed == false) &&
	   (abs(myInitialWeightModifier - myMaxWeight) > 0.000001)) {
		
		for (int dest = 0; dest < myNoNeurons; dest++) {
			for (int source = 0; source < myNoNeurons; source++) {
				myWeights[dest * myNoNeurons + source] = myWeights[dest * myNoNeurons + source]
				                                         * (myInitialWeightModifier / myMaxWeight);
			}
		}
		// And write these weights back into the genenome.
		writeWeightsToGenotype(genotype);
	}
	
	// If requested, impose a feedforward structure.
	if(myNetworkIsFeedforward) {
		for (int dest = 0; dest < myNoNeurons; dest++) {
			for (int source = 0; source < myNoNeurons; source++) {
				int destLayer, sourceLayer;
				if(dest < myNoInputs) { destLayer = 0; }
				else if(dest < myNoInputs + myFFLayer1Size) { destLayer = 1; }
				else if(dest < myNoInputs + myFFLayer1Size + myFFLayer2Size) { destLayer = 2; }
				else { destLayer = 3; }
				if(source < myNoInputs) { sourceLayer = 0; }
				else if(source < myNoInputs + myFFLayer1Size) { sourceLayer = 1; }
				else if(source < myNoInputs + myFFLayer1Size + myFFLayer2Size) { sourceLayer = 2; }
				else { sourceLayer = 3; }
				
				if(sourceLayer != (destLayer - 1)) {
					myWeights[dest * myNoNeurons + source] = 0.0;
				}
			}
		}
	}
	
	// Sensor weights.
	mySensorWeight = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
	gene++;

	// Output weights and biases (if required).
	if(myUseOutputWeightsAndBiases) {
		for(int o = 0; o < myNoOutputs; o++) {
			myOutputWeights[o] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
			gene++;
			myOutputBiases[o] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
			gene++;
		}
	}

	/* If the population should be seeded with centre-crossing networks, and the network has never been constructed
	 * before, ALTER THE GENES so the bias genes are centre crossing.
	 */
	for (int i = 0; i < myNoNeurons; i++) {
		if (mySeedWithCentreCrossing && genotype.myHasEverBeenConstructed == false) {
			genotype.setGene(gene, 0.5); // XXX IS THIS RIGHT FOR CENTRE CROSSING IN TANH???
		}
		
		myBiases[i] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
		gene++;

		// Set time constants, scaled exponentially to [exp(0) exp(5)]
		myTimeConstants[i] = exp(genotype.myGenes.at(gene) * 5);
		gene++;
		
		// If requested, actually set the time constants to 1.
		if(mySetTausToOne) { myTimeConstants[i] = 1.0; }
		
		// If requested, actually set the biases to 0.
		if(mySetBiasesToZero) { myBiases[i] = 0.0; }
	}
	
	// Record in the genotype that it has been reconstructed.
	genotype.myHasEverBeenConstructed = true;
}
コード例 #16
0
void GeneticAlgorithm::Xover ( int a, int b )
{
	int pointA; // parent A crossover point
	int pointB; // parent B crossover point

	// 
	//  Select the crossover point.
	//

	//			A	  B
	//point -- [XXX[P)XX)
	//

	pointA = rand () % ( population[a].getGene()->size() );
	//cout<< pointA << "\n";

	pointB = (pointA % NTOOLS) +
		NTOOLS * ( rand () % ( population[b].getGene()->size() / NTOOLS) ) ;
	//cout<< pointB << "\n";

	// 
	//  Splice the parents together 
	//

	Genotype g;

	g.assignGene(population[a].spliceGene(0, pointA), population[b].spliceGene(pointB, population[b].getGene()->size()));

	vector<int>::iterator geneIterator;
	int points = 0;

	for(geneIterator = g.getGene()->begin(); geneIterator != g.getGene()->end(); ++geneIterator)
		{

			points += *geneIterator;

		}

	if(points <= pointLimit)
		population.push_back(g);


	/*	Method to allow 4 parents and cut/splice

	cout<<one<<two<< point[0][0]<< "\n";
	point[0][0] = ( rand ( ) % ( population[one].getGene()->size() - 3 ) ) + 1;
	cout<< point[0][0]<< "\n";

	point[1][0] = (point [0][0] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( population[two].getGene()->size() / NTOOLS) ) ;
	cout<< point[0][0]<< "\n";
	point[2][0] = (point [0][0] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( population[three].getGene()->size() / NTOOLS) ) ;
	cout<< point[0][0]<< "\n";
	point[3][0] = (point [0][0] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( population[four].getGene()->size() / NTOOLS) ) ;
	cout<< point[0][0]<< "\n";

	//point[0][1]
	point[0][1] = (point[0][0]) + 
	( rand ( ) % ( population[one].getGene()->size() - 2 - point[0][0]) ) + 1;
	cout<< point[0][0]<< "\n";
	//point[1][1]
	if(point[1][0] > (point[0][1] % NTOOLS) )
	point[1][1] = (point [0][1] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) 
	- (point[1][0] / NTOOLS) - 1) 
	+ (point[1][0] / NTOOLS) + 1) ;
	else
	point[1][1] = (point [0][1] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) 
	- (point[1][0] / NTOOLS) ) 
	+ point[1][0] / NTOOLS) ;
	cout<< point[0][0]<< "\n";
	//point[2][1]
	if(point[2][0] > (point[0][1] % NTOOLS) )
	point[2][1] = (point [0][1] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) 
	- (point[2][0] / NTOOLS) - 1) 
	+ (point[2][0] / NTOOLS) + 1) ;
	else
	point[2][1] = (point [0][1] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) 
	- (point[2][0] / NTOOLS) ) 
	+ point[2][0] / NTOOLS ) ;

	//point[3][1]
	if(point[3][0] > (point[0][1] % NTOOLS) )
	point[3][1] = (point [0][1] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) 
	- (point[3][0] / NTOOLS) - 1) 
	+ (point[3][0] / NTOOLS) + 1) ;
	else
	point[3][1] = (point [0][1] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) 
	- (point[3][0] / NTOOLS) ) + point[3][0] / NTOOLS ) ;


	//point[0][2]
	point[0][2] = (point[0][1]) +
	( rand ( ) % ( population[one].getGene()->size() - 1 ) ) + 1;
	//point[1][2]
	if(point[1][1] > (point[0][2] % NTOOLS) )
	point[1][2] = (point [0][2] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) 
	- (point[1][1] / NTOOLS) - 1) + (point[1][1] / NTOOLS) + 1) ;
	else
	point[1][2] = (point [0][2] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) 
	- (point[1][1] / NTOOLS) ) + point[1][1] / NTOOLS ) ;


	//point[2][2]
	if(point[2][1] > (point[0][2] % NTOOLS) )
	point[2][2] = (point [0][2] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) 
	- (point[2][1] / NTOOLS) - 1) + (point[2][1] / NTOOLS) + 1) ;
	else
	point[2][2] = (point [0][2] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) 
	- (point[2][1] / NTOOLS) ) + point[2][1] / NTOOLS ) ;

	//point[3][2]
	if(point[3][1] > (point[0][2] % NTOOLS) )
	point[3][2] = (point [0][2] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) 
	- (point[3][1] / NTOOLS) - 1) + (point[3][1] / NTOOLS) + 1) ;
	else
	point[3][2] = (point [0][2] % NTOOLS) +
	NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) 
	- (point[3][1] / NTOOLS) ) + point[3][1] / NTOOLS ) ;

	for (int i = 0; i < 4; i++ )
	{
	struct genotype g;

	g.fitness = 0;
	g.cfitness = 0;
	g.rfitness = 0;

	std::list<int>::iterator beginIterator;
	std::list<int>::iterator endIterator;

	//Part 1 of new gene
	endIterator = population[one].gene.begin();
	std::advance(endIterator, point[0][0] + 1);
	for(beginIterator = population[one].gene.begin(); 
	beginIterator != endIterator; 
	++beginIterator)
	{
	g.gene.push_back(*beginIterator);
	}

	//Part 2
	beginIterator = population[two].gene.begin();
	endIterator = population[two].gene.begin();
	std::advance(beginIterator, point[1][0]);
	std::advance(endIterator, point[1][1] + 1);
	for(; 
	beginIterator != endIterator; 
	++beginIterator)
	{
	g.gene.push_back(*beginIterator);
	}

	//Part 3
	beginIterator = population[three].gene.begin();
	endIterator = population[three].gene.begin();
	std::advance(beginIterator, point[2][1]);
	std::advance(endIterator, point[2][2] + 1);
	for(; 
	beginIterator != endIterator; 
	++beginIterator)
	{
	g.gene.push_back(*beginIterator);
	}

	//Part 4
	beginIterator = population[four].gene.begin();
	std::advance(beginIterator, point[3][2]);
	for(; 
	beginIterator != population[four].gene.end(); 
	++beginIterator)
	{
	g.gene.push_back(*beginIterator);
	}

	population.push_back(g);
	//r8_swap ( &population[one].gene[i], &population[two].gene[i] );
	}*/


	return;
}
コード例 #17
0
ファイル: genotypes.cpp プロジェクト: jrabbit/Critterding
Genotype* Genotypes::newg( Settings* settings )
{
// cerr << " gentotype start" << endl;
	Genotype* gt = new Genotype();

	gt->bodyArch->retinasize = settings->getCVar("critter_retinasize");
	gt->bodyArch->color = colorH.randomColorRGB();

	gt->bodyArch->buildArch();
	gt->registerBrainInputOutputs();

	gt->brainzArch->maxNeurons					= settings->getCVar("brain_maxneurons");
	gt->brainzArch->minSynapses					= settings->getCVar("brain_minsynapses");
	gt->brainzArch->maxSynapses					= settings->getCVar("brain_maxsynapses");
	gt->brainzArch->minNeuronsAtBuildtime				= settings->getCVar("brain_minneuronsatbuildtime");
	gt->brainzArch->maxNeuronsAtBuildtime				= settings->getCVar("brain_maxneuronsatbuildtime");

	gt->brainzArch->minSynapsesAtBuildtime				= settings->getCVar("brain_minsynapsesatbuildtime");
// 		brain.mutate_minSynapsesAtBuildtime		= settings->getCVar("brain_mutate_minsynapsesatbuildtime");

	gt->brainzArch->maxSynapsesAtBuildtime				= settings->getCVar("brain_maxsynapsesatbuildtime");
// 		brain.mutate_maxSynapsesAtBuildtime		= settings->getCVar("brain_mutate_maxsynapsesatbuildtime");

	gt->brainzArch->percentChanceInhibitoryNeuron			= settings->getCVar("brain_percentchanceinhibitoryneuron");
		gt->brainzArch->mutate_percentChanceInhibitoryNeuron	= settings->getCVar("brain_mutate_percentchanceinhibitoryneuron");

	gt->brainzArch->percentChanceMotorNeuron				= settings->getCVar("brain_percentchancemotorneuron");
		gt->brainzArch->mutate_percentChanceMotorNeuron		= settings->getCVar("brain_mutate_percentchancemotorneuron");

	gt->brainzArch->percentChancePlasticNeuron			= settings->getCVar("brain_percentchanceplasticneuron");
		gt->brainzArch->mutate_percentChancePlasticNeuron			= settings->getCVar("brain_mutate_percentchanceplasticneuron");

	gt->brainzArch->minPlasticityStrengthen				= settings->getCVar("brain_minplasticitystrengthen");
	gt->brainzArch->maxPlasticityStrengthen				= settings->getCVar("brain_maxplasticitystrengthen");
	gt->brainzArch->minPlasticityWeaken				= settings->getCVar("brain_minplasticityweaken");
	gt->brainzArch->maxPlasticityWeaken				= settings->getCVar("brain_maxplasticityweaken");
		gt->brainzArch->mutate_PlasticityFactors				= settings->getCVar("brain_mutate_plasticityfactors");

	gt->brainzArch->minFiringThreshold				= settings->getCVar("brain_minfiringthreshold");
		gt->brainzArch->mutate_minFiringThreshold				= settings->getCVar("brain_mutate_minfiringthreshold");

	gt->brainzArch->maxFiringThreshold				= settings->getCVar("brain_maxfiringthreshold");
		gt->brainzArch->mutate_maxFiringThreshold				= settings->getCVar("brain_mutate_maxfiringthreshold");

	gt->brainzArch->maxDendridicBranches				= settings->getCVar("brain_maxdendridicbranches");
		gt->brainzArch->mutate_maxDendridicBranches		= settings->getCVar("brain_mutate_maxdendridicbranches");

	gt->brainzArch->percentChanceConsistentSynapses			= settings->getCVar("brain_percentchanceconsistentsynapses");
		gt->brainzArch->mutate_percentChanceConsistentSynapses		= settings->getCVar("brain_mutate_percentchanceconsistentsynapses");

	gt->brainzArch->percentChanceInhibitorySynapses			= settings->getCVar("brain_percentchanceinhibitorysynapses");
		gt->brainzArch->mutate_percentChanceInhibitorySynapses		= settings->getCVar("brain_mutate_percentchanceinhibitorysynapses");

	gt->brainzArch->percentChanceSensorySynapse			= settings->getCVar("brain_percentchancesensorysynapse");
		gt->brainzArch->mutate_percentChanceSensorySynapse	= settings->getCVar("brain_mutate_percentchancesensorysynapse");

	gt->brainzArch->percentMutateEffectAddNeuron			= settings->getCVar("brain_percentmutateeffectaddneuron");
	gt->brainzArch->percentMutateEffectRemoveNeuron			= settings->getCVar("brain_percentmutateeffectremoveneuron");
	gt->brainzArch->percentMutateEffectAlterNeuron			= settings->getCVar("brain_percentmutateeffectalterneuron");
	gt->brainzArch->percentMutateEffectAddSynapse			= settings->getCVar("brain_percentmutateeffectaddsynapse");
	gt->brainzArch->percentMutateEffectRemoveSynapse			= settings->getCVar("brain_percentmutateeffectremovesynapse");
		gt->brainzArch->mutate_MutateEffects				= settings->getCVar("brain_mutate_mutateeffects");

	gt->brainzArch->buildArch();

	gt->count=1;
	gt->adamdist=0;
	gt->speciescolor = colorH.randomColorRGB().getNormalized();

	list.push_back(gt);
// 	cerr << " new genotype " << list.size() << endl;
	return gt;
}
コード例 #18
0
ファイル: multireml.cpp プロジェクト: einon/dissect
void MultiREML::computeRegional()
{
  if(this->reml != NULL)
  {
    delete this->reml;
  }
  
  //Load genotypes and GRM
  Genotype * genotype = loadGenotypeUsingOptions();
  Kernel * grm;
  if(options.grmFile == "")
  {
    grm = new Kernel(genotype);
  }
  else
  {
    grm = new Kernel(options.grmFile);
    genotype->normalizeGenotypes();
  }
  
  if(grm->individualIds != genotype->individualIds)
  {
    misc.error("Error: The individuals in GRM and genotypes are not the same. Maybe you are not using the proper GRM or genotype file?", 0);
  }

  if( grm->diagonalized == true )
  {
    if(options.forceUseDiagonalizedKernels == true)
    {
      grm->recoverKernelFromEigenDecomposition();
    }
    else
    {
      misc.error("Error: Sorry, this analysis cannot be performed with diagonal GRMs. You can use the option --force-use-diag-grms to force converting diagonal GRMs to their non-diagonalized form before starting the analysis.", 0);
    }
  }
  
  //Create groups
  genotype->groupSNPs(options.regionBy);
  
  //Start iterations over groups
  std::string baseOutFile = options.outFile;
  int nGroups = genotype->groupedSNPs.size();
  communicator->broadcast(&nGroups);
  std::map<std::string, std::set<std::string> >::iterator it = genotype->groupedSNPs.begin();
  for(int i = 0; i < nGroups; i++)
  {
    std::string group = "";
    if(communicator->mpiRoot)
    {
      group = it->first;
      it++;
    }
    communicator->broadcast(group);
    
    options.outFile = baseOutFile + "." + group;
    
    this->reml = new REML();
    
    Genotype *regionalGenotype = new Genotype();
    genotype->genotypeOfSNPsGroup(group, regionalGenotype);
    double proportionRegionalTotal = double(regionalGenotype->nSNPs)/double(genotype->nSNPs);
    Kernel *regionalGRM = new Kernel(regionalGenotype);
    Kernel *globalGRM = new Kernel();
    globalGRM->addKernels(1., grm, -1., regionalGRM);
    delete regionalGenotype;
    globalGRM->name = "Global GRM";
    regionalGRM->name = "Regional GRM";
    
    std::vector<std::string> snpIntersection = orderVectorAsTemplate(regionalGRM->randomVarNames, grm->randomVarNames);
    if( snpIntersection != regionalGRM->randomVarNames )
    {
      misc.error("Error: SNPs in regional GRM are not present in global GRM. This could be due that some SNPs in genotype file are not present in the the GRM (i.e. they are not used for computing the global GRM).", 0);
    }
    
    std::vector<Kernel*> kernels;
    globalGRM->name = "Global GRM";
    kernels.push_back(globalGRM);
    regionalGRM->name = "Regional GRM";
    kernels.push_back(regionalGRM);
    
    std::vector<double> weights;
    weights.push_back(1. - proportionRegionalTotal);
    weights.push_back(proportionRegionalTotal);
    
    std::vector<std::pair<std::string, std::string> > covariateFiles;
    if(options.covarsFiles.size() != options.qCovarsFiles.size())
    {
      misc.error("Error: An internal error was happened. Inconsistent number of covars and qcovars.", 0);
    }
    for(int i = 0; i<options.covarsFiles.size(); i++)
    {
      covariateFiles.push_back(std::pair<std::string, std::string>(options.covarsFiles[i], options.qCovarsFiles[i]));
    }
    
    if(options.computeIndividualsBLUP == true)
    {
      this->reml->individualBLUPNames.push_back(globalGRM->name);
      this->reml->individualBLUPNames.push_back(regionalGRM->name);
    }
    
    bool prepared = this->reml->prepare(multipleBivarREMLType, kernels, weights, options.phenotypeColumns, options.initialh2Traits, covariateFiles);
    
    if( prepared == true )
    {
      this->reml->computeREML();
    }
    else
    {
      misc.message << "WARNING: Sorry, a problem was happened while preparing data for performing REML on region " << group << ". The MLM cannot be fitted on this region. Please, check the logs." << std::endl;
    }
    
    delete this->reml;
    this->reml = NULL;
  }
  
  delete grm;
  delete genotype;
}