bool NaughtyProcessMonitor::getLoad(ulong pid, uint &load) const
{
#ifdef __linux__
    QFile f("/proc/" + QString::number(pid) + "/stat");

    if(!f.open(IO_ReadOnly))
        return false;

    QTextStream t(&f);

    QString line(t.readLine());

    QStringList fields(QStringList::split(' ', line));

    uint userTime(fields[13].toUInt());
    uint sysTime(fields[14].toUInt());

    load = userTime + sysTime;

    return true;
#elif defined(__OpenBSD__)
    // use cache
    if(!d->cacheLoadMap_.contains(pid))
        return false;

    load = d->cacheLoadMap_[pid];
    return true;
#else
    Q_UNUSED(pid);
    Q_UNUSED(load);
    return false;
#endif
}
Esempio n. 2
0
int main(int argc, char const ** argv)
{
    double startTime = 0;
    
    // Parse command line.
    FxFaidxOptions 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

    // ---------------------------------------------------------------------------
    // Index I/O
    // ---------------------------------------------------------------------------

    // Load index, create if necessary.
    startTime = sysTime();
    seqan::FaiIndex faiIndex;
    if (load(faiIndex, toCString(options.inFastaPath), toCString(options.inFaiPath)) != 0)
    {
        if (options.verbosity >= 2)
            std::cerr << "Building Index        " << options.inFaiPath << " ...";
        if (buildIndex(toCString(options.inFastaPath), toCString(options.inFaiPath), seqan::Fai()) != 0)
        {
            std::cerr << "Could not build FAI index at " << options.inFaiPath
                      << " for FASTA file " << options.inFastaPath << "\n";
            return 1;
        }
        if (load(faiIndex, toCString(options.inFastaPath), toCString(options.inFaiPath)) != 0)
        {
            std::cerr << "Could not load FAI index we just build.\n";
            return 1;
        }
    }
    if (options.verbosity >= 3)
        std::cerr << "Took " << (startTime - sysTime()) << " s\n";

    // ---------------------------------------------------------------------------
    // Parse and Fetch Regions.
    // ---------------------------------------------------------------------------

    if (empty(options.regions))
        return 0;

    // Parse out regions.
    seqan::String<Region> regions;
    for (unsigned i = 0; i < length(options.regions); ++i)
    {
        Region region;
        if (!parseRegion(region, options.regions[i]))
        {
            std::cerr << "Could not parse region " << options.regions[i] << "\n";
            return 1;
        }
        unsigned seqId;
        if (!getIdByName(faiIndex, region.seqName, seqId))
        {
            std::cerr << "Unknown sequence for region " << options.regions[i] << "\n";
            return 1;
        }
        region.seqId = seqId;
        if (region.seqId < 0 || (unsigned)region.seqId >= length(faiIndex.indexEntryStore))
        {
            std::cerr << "Invalid region " << options.regions[i] << "\n";
            return 1;
        }
        appendValue(regions, region);
    }

    // Open output file.
    std::ostream * outPtr = &std::cout;
    std::ofstream outF;
    if (!empty(options.outFastaPath))
    {
        outF.open(toCString(options.outFastaPath), std::ios::binary | std::ios::out);
        if (!outF.good())
        {
            std::cerr << "Could not open output file " << options.outFastaPath << "\n";
            return 1;
        }
    }

    // Retrieve output infixes and write to result.
    for (unsigned i = 0; i < length(regions); ++i)
    {
        Region const & region = regions[i];
        seqan::CharString id = options.regions[i];
        seqan::CharString seq;
        unsigned beginPos = 0;
        if (region.beginPos > 0)
            beginPos = region.beginPos;
        unsigned endPos = sequenceLength(faiIndex, (unsigned)region.seqId);
        if (region.endPos > 0 && (unsigned)region.endPos < endPos)
            endPos = region.endPos;
        if (beginPos > endPos)
            endPos = beginPos;
        getSequenceInfix(seq, faiIndex, region.seqId, beginPos, endPos);
        if (writeRecord(*outPtr, id, seq, seqan::Fasta()) != 0)
        {
            std::cerr << "Could not write infix for region " << options.regions[i] << " to output.\n";
            return 1;
        }
    }

    return 0;
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
/*
 * A function implementing system time queries for different platforms.
 * Be aware that granularity of time measurement may vary on different
 * hardware and operating systems.  Returns FALSE, if system time can't
 * be retrieved (i.e. system call fails).  This function returns time
 * measured from arbitrary moment in the past.  This can be time of 
 * last boot or some other random epoch.
 */
Boolean ssh_time_measure_system_time(SshTimeVal timeval)
{
#if defined(WIN32)
  struct _timeb tv;
#elif defined(CHORUS)
  KnTimeVal tv;
#elif defined(HAVE_GETTIMEOFDAY)
  struct timeval tv;
#else /* !WIN32 && !CHORUS && !HAVE_GETTIMEOFDAY */
  SshTime tv;
#endif /* !WIN32 && !CHORUS && !HAVE_GETTIMEOFDAY */

#if defined(WIN32)
  _ftime(&tv);
  if (timeval != NULL)
    {
      timeval->seconds = (SshUInt64)tv.time;
      timeval->nanoseconds = ((SshUInt32)tv.millitm) * 1000000;
    }
  return TRUE;
#elif defined(CHORUS)
  if (sysTime(&tv) == K_OK)
    {
      if (timeval != NULL)
        {
          timeval->seconds = (SshUInt64)tv.tmSec;
          timeval->nanoseconds = (SshUInt32)tv.tmNSec;
        }
      return TRUE;
    }
  else
    {
      ssh_warning("ssh_time_measure_system_time: sysTime failed.");
      if (timeval != NULL)
        {
          timeval->seconds = 0;
          timeval->nanoseconds = 0;
        }
      return FALSE;
    }
#elif defined(HAVE_GETTIMEOFDAY)
  if (gettimeofday(&tv, NULL) == 0)
    {
      if (timeval != NULL)
        {
          timeval->seconds = (SshUInt64)tv.tv_sec;
          timeval->nanoseconds = ((SshUInt32)tv.tv_usec) * 1000;
        }
      return TRUE;
    }
  else
    {
      ssh_warning("ssh_time_measure_system_time: gettimeofday failed.");
      if (timeval != NULL)
        {
          timeval->seconds = 0;
          timeval->nanoseconds = 0;
        }
      return FALSE;
    }
#else /* !WIN32 && !CHORUS && !HAVE_GETTIMEOFDAY */
  tv = ssh_time();
  if (timeval != NULL)
    {
      timeval->seconds = (SshUInt64)tv;
      timeval->nanoseconds = (SshUint32)0;
    }
  return TRUE;
#endif /* !WIN32 && !CHORUS && !HAVE_GETTIMEOFDAY */
}