ConfigFile::ConfigFile(std::string const& configFile) { std::ifstream file(configFile.c_str()); std::string line; std::string name; std::string value; std::string inSection; int posEqual; while (safeGetline(file, line)) { if (!line.length()) continue; if (line[0] == '#') continue; if (line[0] == ';') continue; if (line[0] == '[') { inSection = trim(line.substr(1, line.find(']') - 1)); continue; } posEqual = line.find('='); name = trim(line.substr(0, posEqual)); value = trim(line.substr(posEqual + 1)); content_[inSection + '/' + name] = Chameleon(value); } }
Properties parseToPropertiesMap(std::istream& file) { Properties properties; std::string line; while (safeGetline(file, line)) { line = stripComments(line); boost::char_separator<char> sep("="); boost::tokenizer<boost::char_separator<char>> tok(line, sep); auto word = tok.begin(); if (word == tok.end()) { continue; } std::string key = stripWhitespace(*word); if (key == "") { continue; } if (++word == tok.end()) { continue; } std::string value = stripWhitespace(*word); properties[key] = value; } return properties; }
short CheckBreakDancerFileFormat(const std::string& filename) { std::ifstream CheckbdFileFirst( filename.c_str() ); std::ifstream CheckbdFileSecond( filename.c_str() ); char firstChar = 'k'; std::string tempLine, errorLine; std::string Pos1, Pos2; while (!CheckbdFileFirst.eof()) { firstChar = CheckbdFileFirst.peek();// >> firstChar; if (firstChar == '#') { safeGetline(CheckbdFileFirst, tempLine); safeGetline(CheckbdFileSecond, tempLine); } else { safeGetline(CheckbdFileFirst, errorLine); //safeGetline(CheckbdFileSecond, errorLine); //std::cout << "errorLine/" << errorLine << "/" << std::endl; if (AtLeast6Fields(errorLine)) { //std::cout << "at least 6 fields" << std::endl; CheckbdFileSecond >> tempLine >> Pos1 >> tempLine >> tempLine >> Pos2 >> tempLine; safeGetline(CheckbdFileSecond, tempLine); // get rest of line //std::cout << errorLine << " " << Pos1 << " " << Pos2 << std::endl; if (isNumber(Pos1) && isNumber(Pos2)) { } else { std::cout << "1 something is wrong with this line \"" << firstChar << errorLine << "\"" << std::endl; return 1; } } else if (errorLine != "") { std::cout << "2 something is wrong with this line \"" << firstChar << errorLine << "\"" << std::endl; return 1; } } }
void WorldObject::SetModel(const char* modelFileName) { this->transformedNormalsAreValid = false; this->vertexCount = 0; this->faceCount = 0; std::ifstream f(modelFileName); int vn = 0; while(!f.eof()) { std::string line; safeGetline(f, line); if(line.length() < 2) continue; char token = line[0]; std::string restOfLine = line.substr(2); if(token == '#') // Comentario { } else if(token == 'v' && line[1] == 'n') // Vertice normal { restOfLine = line.substr(3); float x, y, z; sscanf(restOfLine.c_str(), "%f %f %f", &x, &y, &z); vector3 normal = { x, y, z }; this->vertexNormals[vn] = normal; this->vertexHasNormal[vn++] = true; } else if(token == 'v') // Vertice { float x, y, z; sscanf(restOfLine.c_str(), "%f %f %f", &x, &y, &z); this->AddVertex(x, y, z); } else if(token == 'f') // Face { int v1, v2, v3; sscanf(restOfLine.c_str(), "%i %i %i", &v1, &v2, &v3); this->AddFace(v1 - 1, v2 - 1, v3 - 1); } } }
bool getNextLine(std::ifstream& is, std::string& line) { bool more = true; while (more) { safeGetline(is, line); if (!is.eof()) { if (line.empty() || line[0] == '#') { continue; } more = false; } else { line.clear(); return false; } } return true; }
// the main converter function for c++ char* sass2scss (const string sass, const int options) { // local variables string line; string scss = ""; stringstream stream(sass); // create converter variable converter converter; // initialise all options converter.comma = false; converter.property = false; converter.selector = false; converter.semicolon = false; converter.end_of_file = false; converter.comment = ""; converter.whitespace = ""; converter.indents.push(""); converter.options = options; // read line by line and process them while(safeGetline(stream, line) && !stream.eof()) { scss += process(line, converter); } // create mutable string string closer = ""; // set the end of file flag converter.end_of_file = true; // process to close all open blocks scss += process(closer, converter); // allocate new memory on the heap // caller has to free it after use char * cstr = (char*) malloc (scss.length() + 1); // create a copy of the string strcpy (cstr, scss.c_str()); // return pointer return &cstr[0]; }
void ParseSNP::init() { // Process Fasta File ================================================ ifstream fastaFile; fastaFile.open(reffile.c_str(), ifstream::in); if (!fastaFile.good()) { cerr << "Fasta Parser: could not open file: `" << reffile.c_str() << "`" <<endl; exit(0); } clog << "reading the reference FASTA file : `" << reffile.c_str() << "`"<< endl; safeGetline(fastaFile , buffer, buffer_size); // fastaFile.getline(buffer, buffer_size); int chr_ref = 0; while (!fastaFile.eof()) { if (buffer[0] == '>') { string chr; chr.clear(); for (size_t i = 1; i < buffer_size && buffer[i] != '\0' && ( buffer[i] != '\r' ) && buffer[i] != '\n' && buffer[i] != ' '; i++) { chr += buffer[i]; } chromosome_vector[chr.c_str()] = chr_ref; // fill in a mapping : chromosome name [char] -> chr_ref [size_t] chr_ref++; } fastaFile.getline(buffer, buffer_size); } clog << "In reference, " << chromosome_vector.size() << " contigs have been detected." << endl; fastaFile.close(); // std::sort( chromosome_vector.begin(), chromosome_vector.end()); // read_register_table(); for (std::vector<string>::iterator it = exclude_contigs.begin(); it != exclude_contigs.end(); ++it){ if (chromosome_vector.erase( *it ) ) clog << endl << "Excluded contig: \t" << *it << endl; } }
void ParseSNP::parseVCF() { size_t COMMIT_EACH = 200; bool IMMEDIATE = false; ifstream vcfFile; vcfFile.open(snpfile.c_str(), ifstream::in); if (!vcfFile.good()) { clog << "SNP Parser: could not open file: " << snpfile.c_str() << endl; exit(0); } clog << "reading the VCF file" << endl; // vcfFile.getline(buffer, buffer_size); vector<int> tmp; string broken_chromosome = ""; // snps.resize(chromosome_vector.size(), tmp); Parser * mapped_file = 0; FastaParser * fasta = new FastaParser(reffile); string ref; FILE * plotFile = NULL; // sqlite3pp::transaction * xct; bool transaction_flag = false; if (db_flag){ clog << "writing to database: " << plot_file.c_str() << endl; db = new SqliteDb( plot_file.c_str() , verbose ); db->init_sql_table( sample_label ); db->init_register_table(); db->place_register_record_begin( snpfile , read_filename); db->init_contig_table(); if (!db) { throw std::range_error("null pointer to the database"); } else { clog<< "table has been successfully initialized\t" << db << endl;} ; } else { remove(plot_file.c_str()); plotFile = fopen(plot_file.c_str(), "a"); if (plotFile == NULL) { cerr << "Error in printing: The file or path that you set " // << output.c_str() << " is not valid. It can be that there is no disc space available." << endl; ios_base::failure("cannot open output file for writing!"); exit(0); } }; if (read_filename.find(".bam") != string::npos) { mapped_file = new BamParser(read_filename); } clog << "======================================" << endl; clog << mapped_file->get_header() << endl; clog << "======================================" << endl; // clog << "num of chr " << genome.size() << endl; // clog << "first chr size " << genome[0].size() << endl; clog << "`range` has been set to: " << range << endl; size_t chr_bam = 0; size_t temp_chr_ref = NA; size_t chr_ref = NA; int pos = 0; // int n_snp = 0; // <- global // ref = fasta->getChr(chr_ref); Coverage * cov; cov = new Coverage(range); while (!vcfFile.eof()) { // vcfFile.getline(buffer, buffer_size); safeGetline(vcfFile , buffer, buffer_size); if (buffer[0] == '#'){ if (verbose){ clog << "skipping comment" << endl ; std::string s( buffer ); if (s.find_last_of("\r") > 0){ clog << "this file contains Windows / MacOS specific end-of-line character \'\\r\'\n" << \ "@ position:\t" << s.find_last_of("\r") << "\n" << \ "consider running `dos2unix` or similar!" << endl; } } continue; } int field_count = 0; string current_chr; // read vcf file: // `i` -- runs for symbols // `field_count` -- runs for fields for (size_t i = 0; field_count< 2 && i < buffer_size && buffer[i] != '\0' && buffer[i] != '\n'; i++) { if (field_count == 0 && buffer[i] != '\t') { current_chr += buffer[i]; } if (field_count == 1 && buffer[i - 1] == '\t') { //start: pos column pos = atoi(&buffer[i]) - 1; break; } // end: pos column if (buffer[i] == '\t') { field_count++; } } // process chromosome: if (verbose) { clog << "reading\t" << current_chr.c_str() << "\t" << pos << endl;}; if ( chromosome_vector.count(current_chr.c_str()) > 0){ //found temp_chr_ref = chromosome_vector[current_chr.c_str()]; if (temp_chr_ref != chr_ref) { // new chromosome chr_ref = chromosome_vector[current_chr.c_str()]; clog << endl; // "; getting next bam chromosome..." << endl; chr_bam = mapped_file->GetReferenceID( current_chr); if (chr_ref != chr_bam ){ cerr << "contig: " << current_chr << "; [fasta#:] " << chr_ref << "; [bam#:] " << chr_bam << "; mismatch!" << endl; } if (verbose) { cout << endl; cout << "contig [fasta#:]\t " << chr_ref + 1 \ << "\t[bam#:]\t" << chr_bam + 1 \ << "\t[vcf:]\t" << current_chr.c_str() << "\t[fasta:]\t " << fasta->contig_name[chr_ref] << endl; } else { clog << "contig # " << chr_ref+1 << endl; } ref = fasta->getChr(chr_ref); if (db_flag) { if (transaction_flag){ clog << "commiting" << endl; if (!db) { throw std::range_error("null pointer to the database"); }; db->intermediate_commit(); } else { if (!db) { throw std::range_error("null pointer to the database"); }; db->new_transaction(); transaction_flag = true; } db->place_contig_table_record( chr_ref, current_chr); } n_snp = 0; } } else if (broken_chromosome.compare(current_chr)!=0){ broken_chromosome = current_chr; cerr << endl << "Contig not found: \"\t" << broken_chromosome << "\t\"" << endl; continue; } else { if (verbose){ cerr << "\rskipping:\t" << current_chr << "\t" << pos << endl; } continue; } // process position if (verbose){ if (num_test && (n_snp >= num_test )){ continue; } else { n_snp ++; } // clog << endl; // the info will be printed later in the `process_snp` routine } else { clog << "\r" << setfill(' ') << setw(8) << pos+1; } cov->reset(current_chr, chr_ref, pos); // cov->reset(chr_ref, pos); if (! process_snp(cov, ref, mapped_file, chr_ref, chr_bam) ){ if (verbose>1) { throw std::logic_error(" error while processing a snp!!! "); } else { cerr << " error while processing a snp! skipping... " << endl; } } if (db_flag){ db->print_cov_db( *cov ); } else { cov->print_cov(chr_ref, plotFile); } cov->estimate( read_length ); if ( !(n_snp % COMMIT_EACH) && n_snp >0 ){ clog << " | commiting" << endl; db->intermediate_commit(); } // finally: // vcfFile.getline(buffer, buffer_size); } clog << endl << "VCF file `" << snpfile.c_str() << "` has been successfully processed" << endl; vcfFile.close(); if (db_flag){ db->place_register_record( ); db->composite_index(); // xct->rollback(); // sqlite db->commit(); } else fclose(plotFile); }