void VcfMaterializer::_loadLevels(int rID) { currentLevels.clear(); std::stringstream ssTop, ssBottom; ssTop << sequenceName(faiIndex, rID) << "/TOP"; unsigned idx = 0; if (!getIdByName(idx, methFaiIndex, ssTop.str().c_str())) throw MasonIOException("Could not find top levels in methylation FASTA."); readSequence(currentLevels.forward, methFaiIndex, idx); ssBottom << sequenceName(faiIndex, rID) << "/BOT"; if (!getIdByName(idx, methFaiIndex, ssBottom.str().c_str())) throw MasonIOException("Could not find bottom levels in methylation FASTA."); readSequence(currentLevels.reverse, methFaiIndex, idx); }
long GdbiCommands::NextRDBMSSequenceNumber( FdoString* adbSequenceName ) { bool rc = false; /* return code */ int switched_to_2 = FALSE; int tran_begun = FALSE; int select_begun = FALSE; int sqlid_coc = -1; FdoStringP sql_buf; /* place to format SQL command */ int rows_proc; gdbi_full_seq_def *gptr = &mFeatureSeq; long number = -1; int CURSOR = -1; FdoStringP sequenceName(adbSequenceName); CheckDB(); #ifndef USE_NONRDBMS_HEADER if (SupportsUnicode()) { if ( ::rdbi_get_next_seqW ( m_pRdbiContext, sequenceName, &number ) != RDBI_SUCCESS ) ThrowException(); } else { if ( ::rdbi_get_next_seq ( m_pRdbiContext, sequenceName, &number ) != RDBI_SUCCESS ) ThrowException(); } #else if ( (gptr->next < gptr->size) && (FdoCommonOSUtil::wcsicmp(adbSequenceName, gptr->seq_name) == 0) ) { number = gptr->sequence[gptr->next]; gptr->next++; rc = true; goto the_exit; } if (CURSOR == -1) { if( ::rdbi_est_cursor(m_pRdbiContext, &CURSOR) != RDBI_SUCCESS ) goto the_exit; select_begun = TRUE; /* build SQL select command */ sql_buf = FdoStringP::Format(L"select %ls.nextval from %ls", adbSequenceName, ADB_SEQUENCE_TABLEW); /* add the where clause for other than 'feature' numbers */ if (FdoCommonOSUtil::wcsicmp(adbSequenceName, ADB_FEATURESEQW) != 0) sql_buf += FdoStringP::Format(L" where %ls = '%ls'", ADB_SEQID_COLUMNW, adbSequenceName); if (SupportsUnicode()) { if( ::rdbi_sql_vaW( m_pRdbiContext, 0, CURSOR, sql_buf, /* bind variables */ RDBI_VA_EOL, /* define variables */ RDBI_LONG, 0, gptr->sequence, RDBI_VA_EOL ) != RDBI_SUCCESS ) goto the_exit; } else { if( ::rdbi_sql_va( m_pRdbiContext, 0, CURSOR, sql_buf, /* bind variables */ RDBI_VA_EOL, /* define variables */ RDBI_LONG, 0, gptr->sequence, RDBI_VA_EOL ) != RDBI_SUCCESS ) goto the_exit; } } if ( (0 == FdoCommonOSUtil::wcsicmp (adbSequenceName, ADB_FEATURESEQW)) || (0 == FdoCommonOSUtil::wcsicmp (adbSequenceName, ADB_USERSEQW))) { /* select next feature ids */ gptr->size = ADB_SN_ALLOC_INCREMENT; if( ::rdbi_exec_fetch(m_pRdbiContext, CURSOR, gptr->size, FALSE, &rows_proc) != RDBI_SUCCESS ) goto the_exit; if(rows_proc == 0) goto the_exit; gptr->next = 0; } else { if( ::rdbi_exec_fetch(m_pRdbiContext, CURSOR, 1, FALSE, &rows_proc) != RDBI_SUCCESS ) goto the_exit; if(rows_proc == 0) goto the_exit; gptr->size = 1; if(rows_proc == 0) goto the_exit; gptr->next = 0; } select_begun = TRUE; number = gptr->sequence[gptr->next]; gptr->next++; wcscpy( gptr->seq_name, adbSequenceName ); rc = true; the_exit: if (select_begun) { ::rdbi_end_select(m_pRdbiContext, CURSOR); ::rdbi_fre_cursor(m_pRdbiContext, CURSOR); } if( ! rc ) ThrowException(); #endif return number; }
int main(int argc, char const ** argv) { double startTime = 0; // ----------------------------------------------------------------------- // Parse command line. // ----------------------------------------------------------------------- FxSamCoverageOptions options; seqan::ArgumentParser::ParseResult res = parseArgs(options, argc, argv); if (res != seqan::ArgumentParser::PARSE_OK) return res == seqan::ArgumentParser::PARSE_ERROR; // 1 on errors, 0 otherwise // ----------------------------------------------------------------------- // Show options. // ----------------------------------------------------------------------- if (options.verbosity >= 1) { std::cerr << "____OPTIONS___________________________________________________________________\n" << "\n" << "VERBOSITY " << options.verbosity << "\n" << "GENOME " << options.inGenomePath << "\n" << "SAM " << options.inSamPath << "\n" << "OUT " << options.outPath << "\n" << "WINDOW SIZE " << options.windowSize << "\n"; } // ----------------------------------------------------------------------- // Load Genome FAI Index // ----------------------------------------------------------------------- std::cerr << "\n" << "___PREPRATION_____________________________________________________________________\n" << "\n" << "Indexing GENOME file " << options.inGenomePath << " ..."; seqan::FaiIndex faiIndex; if (build(faiIndex, toCString(options.inGenomePath)) != 0) { std::cerr << "Could not build FAI index.\n"; return 1; } std::cerr << " OK\n"; // Prepare bins. seqan::String<seqan::String<BinData> > bins; resize(bins, numSeqs(faiIndex)); // ----------------------------------------------------------------------- // Compute C+G content // ----------------------------------------------------------------------- std::cerr << "\n" << "___C+G CONTENT COMPUTATION________________________________________________________\n" << "\n"; for (unsigned i = 0; i < numSeqs(faiIndex); ++i) { std::cerr << "[" << sequenceName(faiIndex, i) << "] ..."; unsigned numBins = (sequenceLength(faiIndex, i) + options.windowSize - 1) / options.windowSize; resize(bins[i], numBins); seqan::Dna5String contigSeq; if (readSequence(contigSeq, faiIndex, i) != 0) { std::cerr << "\nERROR: Could not read sequence " << sequenceName(faiIndex, i) << " from file!\n"; return 1; } for (unsigned bin = 0; bin < numBins; ++bin) { unsigned cgCounter = 0; unsigned binSize = 0; bins[i][bin].length = options.windowSize; if ((bin + 1) * options.windowSize > length(contigSeq)) bins[i][bin].length = length(contigSeq) - bin * options.windowSize; for (unsigned pos = bin * options.windowSize; pos < length(contigSeq) && pos < (bin + 1) * options.windowSize; ++pos, ++binSize) cgCounter += (contigSeq[pos] == 'C' || contigSeq[pos] == 'G'); bins[i][bin].cgContent = 1.0 * cgCounter / binSize; } std::cerr << "DONE\n"; } // ----------------------------------------------------------------------- // Compute Coverage // ----------------------------------------------------------------------- std::cerr << "\n" << "___COVERAGE COMPUATATION________________________________________________________\n" << "\n" << "Computing Coverage..."; seqan::BamStream bamStream(toCString(options.inSamPath)); if (!isGood(bamStream)) { std::cerr << "Could not open " << options.inSamPath << "!\n"; return 1; } seqan::BamAlignmentRecord record; while (!atEnd(bamStream)) { if (readRecord(record, bamStream) != 0) { std::cerr << "ERROR: Could not read record from BAM file!\n"; return 1; } if (hasFlagUnmapped(record) || hasFlagSecondary(record) || record.rId == seqan::BamAlignmentRecord::INVALID_REFID) continue; // Skip these records. int contigId = 0; seqan::CharString const & contigName = nameStore(bamStream.bamIOContext)[record.rId]; if (!getIdByName(faiIndex, contigName, contigId)) { std::cerr << "ERROR: Alignment to unknown contig " << contigId << "!\n"; return 1; } unsigned binNo = record.pos / options.windowSize; bins[contigId][binNo].coverage += 1; } std::cerr << "DONE\n"; // ----------------------------------------------------------------------- // Write Output // ----------------------------------------------------------------------- std::ostream * out = &std::cout; std::ofstream outFile; if (options.outPath != "-") { outFile.open(toCString(options.outPath), std::ios::binary | std::ios::out); if (!outFile.good()) { std::cerr << "ERROR: Could not open output file " << options.outPath << "!\n"; return 1; } out = &outFile; } (*out) << "#BIN\tREF_NAME\tREF_BIN\tBIN_BEGIN\tBIN_LENGTH\tCOVERAGE\tCG_CONTENT\n"; for (unsigned i = 0, globalBin = 0; i < length(bins); ++i) { for (unsigned refBin = 0; refBin < length(bins[i]); ++refBin, ++globalBin) { (*out) << globalBin << '\t' << sequenceName(faiIndex, i) << '\t' << refBin << '\t' << refBin * options.windowSize << '\t' << bins[i][refBin].length << '\t' << bins[i][refBin].coverage << '\t' << bins[i][refBin].cgContent << '\n'; } } if (options.verbosity >= 2) std::cerr << "Took " << (sysTime() - startTime) << " s\n"; return 0; }