bool SampleFile::takeSample() { //we're only operating on one file, so the idx is zero. _inputFile = new FileRecordMgr(0, _context); if (!_inputFile->open()) { return false; // FRM will give the error and die anyway, //no error message needed here. } _samples.resize(_numSamples, NULL); //Context object takes care of the seed, either user given or randomly //generated, and seeds the call to srand with it, so we don't have to //here. if (!_context->hasConstantSeed()) { _context->getUnspecifiedSeed(); } _context->determineOutputType(); _outputMgr = new RecordOutputMgr(); _outputMgr->init(_context); while (!_inputFile->eof()) { Record *record = _inputFile->allocateAndGetNextRecord(); if (record == NULL) { continue; } if (!keepRecord(record)) { _inputFile->deleteRecord(record); } _currRecordNum++; } if (_currRecordNum < _numSamples) { //die with error; cerr << "\n***** ERROR: Input file has fewer records than the requested number of output records. *****" << endl << endl; exit(1); } //If the output type is BAM, must sort the output records. if (_context->getOutputFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) { sort(_samples.begin(), _samples.end(), SampleRecordLtFn); } // Now output all the kept records, then do cleanup. for (size_t i=0; i < _numSamples; i++) { _outputMgr->printRecord(_samples[i]); } delete _outputMgr; _inputFile->close(); //clean up. delete _inputFile; return true; }
bool SampleFile::findNext(RecordKeyVector &hits) { while (!_inputFile->eof()) { Record *record = _inputFile->getNextRecord(); if (record == NULL) { continue; } else { _currRecordNum++; if (!keepRecord(record)) { _inputFile->deleteRecord(record); } return true; } } return false; }