void Motif::freq_matrix_extended(double *fm) const{ const vector<vector <int> >& seq = seqset.seq(); int i, col, j; int fm_size = (width + 2 * ncols()) * 4; for(i = 0; i < fm_size; i++) fm[i] = 0.0; if(number() == 0) return; for(i = 0; i < number(); i++) {//i = site number int c = chrom(i); int p = posit(i); bool s = strand(i); for(j = 0, col = -ncols(); col < width + ncols(); col++, j += 4) { if(s) { if((p + col <= seqset.len_seq(c) - 1) && (p + col >= 0)) { assert(j + seq[c][p + col] < fm_size); fm[j + seq[c][p + col]] += 1.0; } else { for(int k = 0; k < 4; k++) { fm[j + k] += 0.25; } } } else { if((p + width - 1 - col <= seqset.len_seq(c) - 1) && (p + width - 1 - col >= 0)) { assert(j + 3 - seq[c][p + width - 1 - col] < fm_size); fm[j + 3 - seq[c][p + width - 1 - col]] += 1.0; } else { for(int k = 0; k < 4; k++) { fm[j + k] += 0.25; } } } } } for(i = 0; i < fm_size; i++) fm[i] /= (double) number(); }
int main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 16; unsigned i; // a chromosome randomizer eoInitPermutation <Chrom> random(CHROM_SIZE); // the population: eoPop<Chrom> pop; // Evaluation eoEvalFuncPtr<Chrom> eval( real_value ); for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); std::cout << " Initial chromosome n°" << i << " : " << chrom << "..." << std::endl; random(chrom); eval(chrom); std::cout << " ... becomes : " << chrom << " after initialization" << std::endl; check_permutation(chrom); pop.push_back(chrom); } return 0; }
void site_crawler:: dump_state(std::ostream& os) const { const std::string& afile(_si.file); os << "LOCUS_CRAWLER STATE:\n"; os << "\tchrom: " << chrom(); os << "\tposition: " << pos() << " offset: " << _locus_offset << "\n"; os << "\tis_indel: " << is_indel() << "\n"; os << "\tfile: '" << afile << "'\n"; os << "\tline: '"; dump_line(os); os << "'\n"; }
main() { Pop pop; EORandom<float> random; Chrom chrom(NUM_GENES, BITSxGEN); for (unsigned i = 0; i < POP_SIZE; i++) { random(chrom); chrom.fitness(i); pop.push_back(chrom); } Bin fitness; EOEvalAll<Chrom> eval(fitness); EOStat<Chrom> graphEval( eval, true ); // True is for verbose EORank<Chrom> transform(1); // EOMutation<Chrom> mutation(0.05); // transform.addOp((EOOp<Chrom>*)&mutation); NxOver<Chrom> nxover(2); transform.addOp(&nxover); EOGenTerm<Chrom> term(100); EOAgeGA<Chrom> agega(graphEval, transform, term); try { agega(pop); } catch(UException& e) { cout << e.what() << endl; } cout << "the best: " << pop.front() << endl; return 0; }
main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 4; unsigned i; eoBinRandom<Chrom> random; eoPop<Chrom> pop; // Create the population for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); random(chrom); BinaryValue()(chrom); pop.push_back(chrom); } // print population std::cout << "population:" << std::endl; for (i = 0; i < pop.size(); ++i) std::cout << pop[i] << " " << pop[i].fitness() << std::endl; // Declare 1-selectors eoUniformSelect<Chrom> uSelect; Chrom aChrom; aChrom = uSelect( pop ); std::cout << "Uniform Select " << aChrom << " " << aChrom.fitness() << std::endl; eoStochTournament<Chrom> sSelect(0.7); aChrom = sSelect( pop ); std::cout << "Stochastic Tournament " << aChrom << " " << aChrom.fitness() << std::endl; eoDetTournament<Chrom> dSelect(3); aChrom = dSelect( pop ); std::cout << "Deterministic Tournament " << aChrom << " " << aChrom.fitness() << std::endl; return 0; }
void Fitness_test::testFitness() { unsigned int aPrecision = 4; std::vector<double> adomain; adomain.push_back(1.0); adomain.push_back(2.0); adomain.push_back(3.0); adomain.push_back(6.0); adomain.push_back(8.0); adomain.push_back(16.0); std::vector<unsigned int> length; unsigned int aNDIM = 3; Chromosome_Length chrom(aPrecision, aNDIM, adomain); chrom.buildChromosome(); length = chrom.getLength(); Chromosome achromosome = chrom.getChromosome(); for(unsigned int i=0; i<achromosome.size(); i++) { if(i%2==0) { achromosome[i]=true; } else achromosome[i]=false; } Function afunction; Fittness_value<Function> fitness(length, aNDIM, adomain, afunction); double result; result = fitness.computeValue(achromosome); std:: cout << "the final value is = " << result << std::endl; }
int main() { const unsigned POP_SIZE = 3, CHROM_SIZE = 8; unsigned i; // a chromosome randomizer eoInitPermutation <Chrom> random(CHROM_SIZE); // the population: eoPop<Chrom> pop; // Evaluation //eoEvalFuncPtr<Chrom> eval( real_value ); for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); random(chrom); //eval(chrom); pop.push_back(chrom); } // a shift mutation eoOrderXover<Chrom> cross; for (i = 0; i < POP_SIZE; ++i) std::cout << " Initial chromosome n�" << i << " : " << pop[i] << "..." << std::endl; cross(pop[0],pop[1]); cross(pop[1],pop[2]); for (i = 0; i < POP_SIZE; ++i) { std::cout << " Initial chromosome n�" << i << " becomes : " << pop[i] << " after orderXover" << std::endl; check_permutation(pop[i]); } return 0; }
int main() { const unsigned SIZE = 8; eoBooleanGenerator gen; eo::rng.reseed( time( 0 ) ); Chrom chrom(SIZE), chrom2(SIZE); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); std::cout << chrom << std::endl; std::cout << chrom2 << std::endl; // Virus Mutation VirusBitFlip<float> vf; unsigned i; for ( i = 0; i < 10; i++ ) { vf( chrom ); std::cout << chrom << std::endl; } // Chrom Mutation std::cout << "Chrom mutation--------" << std::endl; VirusMutation<float> vm; for ( i = 0; i < 10; i++ ) { vm( chrom ); std::cout << chrom << std::endl; } // Chrom Transmision std::cout << "Chrom transmission--------" << std::endl; VirusTransmission<float> vt; vt( chrom2, chrom ); std::cout << chrom2 << std::endl; return 0; }
main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 16; unsigned i; // a chromosome randomizer eoBinRandom<Chrom> random; // the populations: eoPop<Chrom> pop; // Evaluation eoEvalFuncPtr<Chrom> eval( binary_value ); for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); random(chrom); eval(chrom); pop.push_back(chrom); } std::cout << "population:" << std::endl; for (i = 0; i < pop.size(); ++i) std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; // selection eoLottery<Chrom> lottery; // breeder eoBinBitFlip<Chrom> bitflip; eoBinCrossover<Chrom> xover; eoProportionalOpSel<Chrom> propSel; eoBreeder<Chrom> breeder( propSel ); propSel.addOp(bitflip, 0.25); propSel.addOp(xover, 0.75); // replacement eoInclusion<Chrom> inclusion; // Terminators eoFitTerm<Chrom> term( pow(2.0, CHROM_SIZE), 1 ); // GA generation eoEasyEA<Chrom> ea(lottery, breeder, inclusion, eval, term); // evolution try { ea(pop); } catch (std::exception& e) { std::cout << "exception: " << e.what() << std::endl;; exit(EXIT_FAILURE); } std::cout << "pop" << std::endl; for (i = 0; i < pop.size(); ++i) std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; return 0; }
void main_function() { const unsigned SIZE = 8; unsigned i, j; eoBooleanGenerator gen; Chrom chrom(SIZE), chrom2; chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); std::cout << "chrom: " << chrom << std::endl; chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom)); std::cout << "chrom: " << chrom << std::endl; chrom[0] = chrom[SIZE - 1] = false; chrom.fitness(binary_value(chrom)); std::cout << "chrom: " << chrom << std::endl; chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom)); std::cout << "chrom.className() = " << chrom.className() << std::endl; std::cout << "chrom: " << chrom << std::endl << "chrom2: " << chrom2 << std::endl; std::ostringstream os; os << chrom; std::istringstream is(os.str()); is >> chrom2; chrom.fitness(binary_value(chrom2)); std::cout << "\nTesting reading, writing\n"; std::cout << "chrom: " << chrom << "\nchrom2: " << chrom2 << '\n'; std::fill(chrom.begin(), chrom.end(), false); std::cout << "--------------------------------------------------" << std::endl << "eoMonOp's aplied to .......... " << chrom << std::endl; eoInitFixedLength<Chrom> random(chrom.size(), gen); random(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBinRandom ............ " << chrom << std::endl; eoOneBitFlip<Chrom> bitflip; bitflip(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBitFlip .............. " << chrom << std::endl; eoBitMutation<Chrom> mutation(0.5); mutation(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBinMutation(0.5) ..... " << chrom << std::endl; eoBitInversion<Chrom> inversion; inversion(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBinInversion ......... " << chrom << std::endl; eoBitNext<Chrom> next; next(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBinNext .............. " << chrom << std::endl; eoBitPrev<Chrom> prev; prev(chrom); chrom.fitness(binary_value(chrom)); std::cout << "after eoBinPrev .............. " << chrom << std::endl; std::fill(chrom.begin(), chrom.end(), false); chrom.fitness(binary_value(chrom)); std::fill(chrom2.begin(), chrom2.end(), true); chrom2.fitness(binary_value(chrom2)); std::cout << "--------------------------------------------------" << std::endl << "eoBinOp's aplied to ... " << chrom << " " << chrom2 << std::endl; eo1PtBitXover<Chrom> xover; std::fill(chrom.begin(), chrom.end(), false); std::fill(chrom2.begin(), chrom2.end(), true); xover(chrom, chrom2); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); std::cout << "eoBinCrossover ........ " << chrom << " " << chrom2 << std::endl; for (i = 1; i < SIZE; i++) { eoNPtsBitXover<Chrom> nxover(i); std::fill(chrom.begin(), chrom.end(), false); std::fill(chrom2.begin(), chrom2.end(), true); nxover(chrom, chrom2); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); std::cout << "eoBinNxOver(" << i << ") ........ " << chrom << " " << chrom2 << std::endl; } for (i = 1; i < SIZE / 2; i++) for (j = 1; j < SIZE / 2; j++) { eoBitGxOver<Chrom> gxover(i, j); std::fill(chrom.begin(), chrom.end(), false); std::fill(chrom2.begin(), chrom2.end(), true); gxover(chrom, chrom2); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); std::cout << "eoBinGxOver(" << i << ", " << j << ") ..... " << chrom << " " << chrom2 << std::endl; } // test SGA algorithm eoGenContinue<Chrom> continuator1(50); eoFitContinue<Chrom> continuator2(65535.f); eoCombinedContinue<Chrom> continuator(continuator1, continuator2); eoCheckPoint<Chrom> checkpoint(continuator); eoStdoutMonitor monitor; checkpoint.add(monitor); eoSecondMomentStats<Chrom> stats; monitor.add(stats); checkpoint.add(stats); eoProportionalSelect<Chrom> select; eoEvalFuncPtr<Chrom> eval(binary_value); eoSGA<Chrom> sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint); eoInitFixedLength<Chrom> init(16, gen); eoPop<Chrom> pop(100, init); apply<Chrom>(eval, pop); sga(pop); pop.sort(); std::cout << "Population " << pop << std::endl; std::cout << "\nBest: " << pop[0].fitness() << '\n'; /* Commented this out, waiting for a definite decision what to do with the mOp's // Check multiOps eoMultiMonOp<Chrom> mOp( &next ); mOp.adOp( &bitflip ); std::cout << "before multiMonOp............ " << chrom << std::endl; mOp( chrom ); std::cout << "after multiMonOp .............. " << chrom << std::endl; eoBinGxOver<Chrom> gxover(2, 4); eoMultiBinOp<Chrom> mbOp( &gxover ); mOp.adOp( &bitflip ); std::cout << "before multiBinOp............ " << chrom << " " << chrom2 << std::endl; mbOp( chrom, chrom2 ); std::cout << "after multiBinOp .............. " << chrom << " " << chrom2 <<std::endl; */ }
// [[Rcpp::export]] Rcpp::DataFrame vcf_body(std::string x, Rcpp::NumericVector stats) { // Read in the fixed and genotype portion of the file. // Stats contains: // "meta", "header", "variants", "columns" Rcpp::CharacterVector chrom(stats[2]); Rcpp::IntegerVector pos(stats[2]); Rcpp::StringVector id(stats[2]); Rcpp::StringVector ref(stats[2]); Rcpp::StringVector alt(stats[2]); Rcpp::NumericVector qual(stats[2]); Rcpp::StringVector filter(stats[2]); Rcpp::StringVector info(stats[2]); // if(stats[3]) Rcpp::CharacterMatrix gt(stats[2], stats[3] - 8); std::string line; // String for reading file into // Open file. std::ifstream myfile; myfile.open (x.c_str(), std::ios::in); if (!myfile.is_open()){ Rcpp::Rcout << "Unable to open file"; } // Iterate past meta. int i = 0; int j = 0; while ( i < stats[0] ){ getline (myfile,line); i++; } // Get header. getline (myfile,line); std::string header = line; // Get body. i = 0; char buffer [50]; while ( getline (myfile,line) ){ Rcpp::checkUserInterrupt(); std::vector < std::string > temps = tabsplit(line, stats[3]); if(temps[0] == "."){ chrom[i] = NA_STRING; } else { chrom[i] = temps[0]; } if(temps[1] == "."){ pos[i] = NA_INTEGER; } else { pos[i] = atoi(temps[1].c_str()); } if(temps[2] == "."){ id[i] = NA_STRING; } else { id[i] = temps[2]; } if(temps[3] == "."){ ref[i] = NA_STRING; } else { ref[i] = temps[3]; } if(temps[4] == "."){ alt[i] = NA_STRING; } else { alt[i] = temps[4]; } if(temps[5] == "."){ qual[i] = NA_REAL; } else { qual[i] = atof(temps[5].c_str()); } if(temps[6] == "."){ filter[i] = NA_STRING; } else { filter[i] = temps[6]; } if(temps[7] == "."){ info[i] = NA_STRING; } else { info[i] = temps[7]; } for(j=8; j<stats[3]; j++){ gt(i, j-8) = temps[j]; // body(i, j-8) = temps[j]; } i++; if( i % nreport == 0){ Rcpp::Rcout << "\rProcessed variant: " << i; } } myfile.close(); Rcpp::Rcout << "\rProcessed variant: " << i; Rcpp::Rcout << "\nAll variants processed\n"; Rcpp::DataFrame df1 = Rcpp::DataFrame::create( Rcpp::_["CHROM"]= chrom, Rcpp::_["POS"]= pos, Rcpp::_["ID"] = id, Rcpp::_["REF"] = ref, Rcpp::_["ALT"] = alt, Rcpp::_["QUAL"] = qual, Rcpp::_["FILTER"] = filter, Rcpp::_["INFO"] = info, gt); std::vector < std::string > temps = tabsplit(header, stats[3]); temps[0].erase(0,1); df1.names() = temps; Rcpp::Rcout << "Rcpp::DataFrame created.\n"; return df1; }
bool positionMatch(const Variant& rhs) const { return chrom() == rhs.chrom() && start() == rhs.start() && stop() == rhs.stop(); }
void BedGenomeCoverage::CoverageBam(string bamFile) { ResetChromCoverage(); // open the BAM file BamReader reader; if (!reader.Open(bamFile)) { cerr << "Failed to open BAM file " << bamFile << endl; exit(1); } // get header & reference information string header = reader.GetHeaderText(); RefVector refs = reader.GetReferenceData(); // load the BAM header references into a BEDTools "genome file" _genome = new GenomeFile(refs); // convert each aligned BAM entry to BED // and compute coverage on B BamAlignment bam; while (reader.GetNextAlignment(bam)) { // skip if the read is unaligned if (bam.IsMapped() == false) continue; bool _isReverseStrand = bam.IsReverseStrand(); //changing second mate's strand to opposite if( _dUTP && bam.IsPaired() && bam.IsMateMapped() && bam.IsSecondMate()) _isReverseStrand = !bam.IsReverseStrand(); // skip if we care about strands and the strand isn't what // the user wanted if ( (_filterByStrand == true) && ((_requestedStrand == "-") != _isReverseStrand) ) continue; // extract the chrom, start and end from the BAM alignment string chrom(refs.at(bam.RefID).RefName); CHRPOS start = bam.Position; CHRPOS end = bam.GetEndPosition(false, false) - 1; // are we on a new chromosome? if ( chrom != _currChromName ) StartNewChrom(chrom); if(_pair_chip_) { // Skip if not a proper pair if (bam.IsPaired() && (!bam.IsProperPair() or !bam.IsMateMapped()) ) continue; // Skip if wrong coordinates if( ( (bam.Position<bam.MatePosition) && bam.IsReverseStrand() ) || ( (bam.MatePosition < bam.Position) && bam.IsMateReverseStrand() ) ) { //chemically designed: left on positive strand, right on reverse one continue; } /*if(_haveSize) { if (bam.IsFirstMate() && bam.IsReverseStrand()) { //put fragmentSize in to the middle of pair end_fragment int mid = bam.MatePosition+abs(bam.InsertSize)/2; if(mid<_fragmentSize/2) AddCoverage(0, mid+_fragmentSize/2); else AddCoverage(mid-_fragmentSize/2, mid+_fragmentSize/2); } else if (bam.IsFirstMate() && bam.IsMateReverseStrand()) { //put fragmentSize in to the middle of pair end_fragment int mid = start+abs(bam.InsertSize)/2; if(mid<_fragmentSize/2) AddCoverage(0, mid+_fragmentSize/2); else AddCoverage(mid-_fragmentSize/2, mid+_fragmentSize/2); } } else */ if (bam.IsFirstMate() && bam.IsReverseStrand()) { //prolong to the mate to the left AddCoverage(bam.MatePosition, end); } else if (bam.IsFirstMate() && bam.IsMateReverseStrand()) { //prolong to the mate to the right AddCoverage(start, start + abs(bam.InsertSize) - 1); } } else if (_haveSize) { if(bam.IsReverseStrand()) { if(end<_fragmentSize) { //sometimes fragmentSize is bigger :( AddCoverage(0, end); } else { AddCoverage(end + 1 - _fragmentSize, end ); } } else { AddCoverage(start,start+_fragmentSize - 1); } } else // add coverage accordingly. if (!_only_5p_end && !_only_3p_end) { bedVector bedBlocks; // we always want to split blocks when a D CIGAR op is found. // if the user invokes -split, we want to also split on N ops. if (_obeySplits) { // "D" true, "N" true GetBamBlocks(bam, refs.at(bam.RefID).RefName, bedBlocks, true, true); } else { // "D" true, "N" false GetBamBlocks(bam, refs.at(bam.RefID).RefName, bedBlocks, true, false); } AddBlockedCoverage(bedBlocks); } else if (_only_5p_end) { CHRPOS pos = ( !bam.IsReverseStrand() ) ? start : end; AddCoverage(pos,pos); } else if (_only_3p_end) { CHRPOS pos = ( bam.IsReverseStrand() ) ? start : end; AddCoverage(pos,pos); } } // close the BAM reader.Close(); // process the results of the last chromosome. ReportChromCoverage(_currChromCoverage, _currChromSize, _currChromName, _currChromDepthHist); // report all empty chromsomes PrintEmptyChromosomes(); // report the overall coverage if asked. PrintFinalCoverage(); }