Newer
Older
nkindlon
committed
/*
* ContextBase.cpp
*
* Created on: Feb 12, 2013
* Author: nek3d
*/
#include "ContextBase.h"
#include <unistd.h>
#include <sys/types.h>
nkindlon
committed
ContextBase::ContextBase()
:
_program(UNSPECIFIED_PROGRAM),
_allFilesOpened(false),
nkindlon
committed
_genomeFile(NULL),
_outputFileType(FileRecordTypeChecker::UNKNOWN_FILE_TYPE),
_outputTypeDetermined(false),
_skipFirstArgs(0),
_showHelp(false),
_obeySplits(false),
_uncompressedBam(false),
_useBufferedOutput(true),
nkindlon
committed
_anyHit(false),
_noHit(false),
_writeA(false),
_writeB(false),
_leftJoin(false),
_writeCount(false),
_writeOverlap(false),
_writeAllOverlap(false),
_haveFraction(false),
_overlapFraction(0.0),
nkindlon
committed
_reciprocal(false),
_sameStrand(false),
_diffStrand(false),
_sortedInput(false),
_sortOutput(false),
_reportDBnameTags(false),
_reportDBfileNames(false),
nkindlon
committed
_printHeader(false),
_printable(true),
_explicitBedOutput(false),
_queryFileIdx(-1),
_bamHeaderAndRefIdx(-1),
_maxNumDatabaseFields(0),
_useFullBamTags(false),
_numOutputRecords(0),
_hasConstantSeed(false),
_seed(0),
_forwardOnly(false),
nkindlon
committed
_reverseOnly(false),
_desiredStrand(FileRecordMergeMgr::ANY_STRAND),
_maxDistance(0),
Neil Kindlon
committed
_useMergedIntervals(false),
_reportPrecision(-1)
nkindlon
committed
{
_programNames["intersect"] = INTERSECT;
_programNames["sample"] = SAMPLE;
_programNames["map"] = MAP;
nkindlon
committed
nkindlon
committed
if (hasColumnOpsMethods()) {
_keyListOps = new KeyListOps();
}
nkindlon
committed
}
ContextBase::~ContextBase()
{
delete _genomeFile;
_genomeFile = NULL;
//close all files and delete FRM objects.
for (int i=0; i < (int)_files.size(); i++) {
_files[i]->close();
delete _files[i];
_files[i] = NULL;
nkindlon
committed
}
nkindlon
committed
if (hasColumnOpsMethods()) {
delete _keyListOps;
_keyListOps = NULL;
}
nkindlon
committed
}
bool ContextBase::determineOutputType() {
if (_outputTypeDetermined) {
return true;
}
//test whether output should be BED or BAM.
//If the user explicitly requested BED, then it's BED.
if (getExplicitBedOutput()) {
setOutputFileType(FileRecordTypeChecker::SINGLE_LINE_DELIM_TEXT_FILE_TYPE);
_outputTypeDetermined = true;
return true;
}
nkindlon
committed
//Otherwise, if the input is BAM, then the output is BAM
if (getFile(0)->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) {
setOutputFileType(FileRecordTypeChecker::BAM_FILE_TYPE);
nkindlon
committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
}
//Okay, it's bed.
setOutputFileType(FileRecordTypeChecker::SINGLE_LINE_DELIM_TEXT_FILE_TYPE);
_outputTypeDetermined = true;
return true;
}
void ContextBase::openGenomeFile(const QuickString &genomeFilename)
{
_genomeFile = new NewGenomeFile(genomeFilename.c_str());
}
void ContextBase::openGenomeFile(const BamTools::RefVector &refVector)
{
_genomeFile = new NewGenomeFile(refVector);
}
bool ContextBase::parseCmdArgs(int argc, char **argv, int skipFirstArgs) {
_argc = argc;
_argv = argv;
_skipFirstArgs = skipFirstArgs;
setProgram(_programNames[argv[0]]);
_argsProcessed.resize(_argc - _skipFirstArgs, false);
for (_i=_skipFirstArgs; _i < argc; _i++) {
if (isUsed(_i - _skipFirstArgs)) {
continue;
}
if (strcmp(_argv[_i], "-i") == 0) {
if (!handle_i()) return false;
}
else if (strcmp(_argv[_i], "-g") == 0) {
if (!handle_g()) return false;
}
else if ((strcmp(_argv[_i], "-h") == 0) || (strcmp(_argv[_i], "--help") == 0)) {
if (!handle_h()) return false;
}
else if (strcmp(_argv[_i], "-split") == 0) {
if (!handle_split()) return false;
}
else if (strcmp(_argv[_i], "-bed") == 0) {
if (!handle_bed()) return false;
}
else if (strcmp(_argv[_i], "-ubam") == 0) {
if (!handle_ubam()) return false;
}
else if (strcmp(_argv[_i], "-fbam") == 0) {
if (!handle_fbam()) return false;
}
else if(strcmp(_argv[_i], "-sorted") == 0) {
if (!handle_sorted()) return false;
}
else if (strcmp(_argv[_i], "-nobuf") == 0) {
if (!handle_nobuf()) return false;
}
else if (strcmp(_argv[_i], "-iobuf") == 0) {
if (!handle_iobuf()) return false;
}
Neil Kindlon
committed
else if (strcmp(_argv[_i], "-prec") == 0) {
if (!handle_prec()) return false;
}
nkindlon
committed
else if (strcmp(_argv[_i], "-header") == 0) {
if (!handle_header()) return false;
}
else if (strcmp(_argv[_i], "-n") == 0) {
if (!handle_n()) return false;
}
else if (strcmp(_argv[_i], "-seed") == 0) {
if (!handle_seed()) return false;
}
nkindlon
committed
else if (strcmp(_argv[_i], "-o") == 0) {
if (!handle_o()) return false;
}
else if (strcmp(_argv[_i], "-c") == 0) {
if (!handle_c()) return false;
}
else if (strcmp(_argv[_i], "-null") == 0) {
if (!handle_null()) return false;
}
else if (strcmp(_argv[_i], "-delim") == 0) {
if (!handle_delim()) return false;
}
else if (strcmp(_argv[_i], "-sortout") == 0) {
if (!handle_sortout()) return false;
}
nkindlon
committed
nkindlon
committed
}
return true;
}
bool ContextBase::isValidState()
{
if (!openFiles()) {
return false;
}
if (!cmdArgsValid()) {
return false;
}
if (!determineOutputType()) {
return false;
}
nkindlon
committed
if (hasColumnOpsMethods()) {
Neil Kindlon
committed
// TBD: Adjust column ops for multiple databases.
// For now, use last file.
FileRecordMgr *dbFile = getFile(getNumInputFiles()-1);
_keyListOps->setDBfileType(dbFile->getFileType());
nkindlon
committed
if (!_keyListOps->isValidColumnOps(dbFile)) {
return false;
}
Neil Kindlon
committed
//if user specified a precision, pass it to
//keyList ops
if (_reportPrecision != -1) {
_keyListOps->setPrecision(_reportPrecision);
}
nkindlon
committed
}
return true;
nkindlon
committed
}
bool ContextBase::cmdArgsValid()
{
bool retval = true;
for (_i = _skipFirstArgs; _i < _argc; _i++) {
if (!isUsed(_i - _skipFirstArgs)) {
_errorMsg += "\n***** ERROR: Unrecognized parameter: ";
_errorMsg += _argv[_i];
_errorMsg += " *****";
retval = false;
}
}
return retval;
}
bool ContextBase::openFiles() {
//Make a vector of FileRecordMgr objects by going through the vector
//of filenames and opening each one.
if (_allFilesOpened) {
return true;
}
if (_fileNames.size() == 0) {
//No input was specified. Error and exit.
_errorMsg += "\n***** ERROR: No input file given. Exiting. *****";
return false;
}
_files.resize(_fileNames.size());
for (int i = 0; i < (int)_fileNames.size(); i++) {
FileRecordMgr *frm = getNewFRM(_fileNames[i], i);
if (hasGenomeFile()) {
frm->setGenomeFile(_genomeFile);
}
//If we're going to do column operations, and an input file
// is BAM, we'll need the full flags.
if (hasColumnOpsMethods()) {
setUseFullBamTags(true);
}
frm->setFullBamFlags(_useFullBamTags);
if (!frm->open()) {
return false;
}
_files[i] = frm;
}
_allFilesOpened = true;
return true;
}
nkindlon
committed
int ContextBase::getBamHeaderAndRefIdx() {
if (_bamHeaderAndRefIdx != -1) {
//already found which BAM file to use for the header
return _bamHeaderAndRefIdx;
}
if (hasIntersectMethods()) {
if (_files[_queryFileIdx]->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) {
_bamHeaderAndRefIdx = _queryFileIdx;
} else {
}
return _bamHeaderAndRefIdx;
}
if (_files[0]->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) {
_bamHeaderAndRefIdx = 0;
return _bamHeaderAndRefIdx;
nkindlon
committed
}
nkindlon
committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
return _bamHeaderAndRefIdx;
}
int ContextBase::getUnspecifiedSeed()
{
// thanks to Rob Long for the tip.
_seed = (unsigned)time(0)+(unsigned)getpid();
srand(_seed);
return _seed;
}
bool ContextBase::handle_bed()
{
setExplicitBedOutput(true);
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_fbam()
{
setUseFullBamTags(true);
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_g()
{
if (_argc <= _i+1) {
_errorMsg = "\n***** ERROR: -g option given, but no genome file specified. *****";
return false;
}
openGenomeFile(_argv[_i+1]);
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_h()
{
setShowHelp(true);
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_header()
{
setPrintHeader(true);
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_i()
{
if (_argc <= _i+1) {
_errorMsg = "\n***** ERROR: -i option given, but no input file specified. *****";
return false;
}
addInputFile(_argv[_i+1]);
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_n()
{
if (_argc <= _i+1) {
_errorMsg = "\n***** ERROR: -n option given, but no number of output records specified. *****";
return false;
}
setNumOutputRecords(atoi(_argv[_i + 1]));
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_nobuf()
{
setUseBufferedOutput(false);
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_iobuf()
{
if (_argc <= _i+1) {
_errorMsg = "\n***** ERROR: -iobuf option given, but size of input buffer not specified. *****";
return false;
}
if (!parseIoBufSize(_argv[_i + 1])) return false;
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
return true;
}
nkindlon
committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
bool ContextBase::handle_seed()
{
if (_argc <= _i+1) {
_errorMsg = "\n***** ERROR: -seed option given, but no seed specified. *****";
return false;
}
_hasConstantSeed = true;
_seed = atoi(_argv[_i+1]);
srand(_seed);
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_split()
{
setObeySplits(true);
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_sorted()
{
setSortedInput(true);
markUsed(_i - _skipFirstArgs);
return true;
}
bool ContextBase::handle_ubam()
{
setUncompressedBam(true);
markUsed(_i - _skipFirstArgs);
return true;
}
nkindlon
committed
// Methods specific to column operations.
// for col ops, -c is the string of columns upon which to operate
bool ContextBase::handle_c()
{
if (!hasColumnOpsMethods()) {
return false;
}
if ((_i+1) < _argc) {
_keyListOps->setColumns(_argv[_i + 1]);
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
nkindlon
committed
}
nkindlon
committed
}
// for col ops, -o is the string of operations to apply to the columns (-c)
bool ContextBase::handle_o()
{
if (!hasColumnOpsMethods()) {
return false;
}
if ((_i+1) < _argc) {
_keyListOps->setOperations(_argv[_i + 1]);
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
Neil Kindlon
committed
return true;
}
return false;
}
bool ContextBase::handle_prec()
{
if (!hasColumnOpsMethods()) {
return false;
}
if ((_i+1) < _argc) {
int prec = atoi(_argv[_i + 1]);
if (prec < 1) {
_errorMsg += "\n***** ERROR: -prec must be followed by a positive integer. Exiting. *****";
return false;
}
_reportPrecision = prec;
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
return true;
nkindlon
committed
}
Neil Kindlon
committed
_errorMsg += "\n***** ERROR: -prec must be followed by a positive integer. Exiting. *****";
return false;
nkindlon
committed
}
Neil Kindlon
committed
// for col ops, -null is a NULL value assigned
nkindlon
committed
// when no overlaps are detected.
bool ContextBase::handle_null()
{
if (!hasColumnOpsMethods()) {
return false;
}
if ((_i+1) < _argc) {
_keyListOps->setNullValue(_argv[_i + 1]);
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
nkindlon
committed
}
nkindlon
committed
}
//for col ops, delimStr will appear between each item in
//a collapsed but delimited list.
bool ContextBase::handle_delim()
{
if (!hasColumnOpsMethods()) {
return false;
}
if ((_i+1) < _argc) {
_keyListOps->setDelimStr(_argv[_i + 1]);
markUsed(_i - _skipFirstArgs);
_i++;
markUsed(_i - _skipFirstArgs);
}
return true;
}
bool ContextBase::handle_sortout()
{
setSortOutput(true);
markUsed(_i - _skipFirstArgs);
return true;
}
nkindlon
committed
void ContextBase::setColumnOpsMethods(bool val)
{
if (val && !_hasColumnOpsMethods) {
//was off, but we're turning it on.
nkindlon
committed
_keyListOps = new KeyListOps();
}
nkindlon
committed
}
const QuickString &ContextBase::getColumnOpsVal(RecordKeyVector &keyList) const {
nkindlon
committed
if (!hasColumnOpsMethods()) {
return _nullStr;
}
return _keyListOps->getOpVals(keyList);
}
FileRecordMgr *ContextBase::getNewFRM(const QuickString &filename, int fileIdx) {
if (_useMergedIntervals) {
FileRecordMergeMgr *frm = new FileRecordMergeMgr(filename);
frm->setStrandType(_desiredStrand);
frm->setMaxDistance(_maxDistance);
frm->setFileIdx(fileIdx);
return frm;
} else {
FileRecordMgr *frm = new FileRecordMgr(filename);
frm->setFileIdx(fileIdx);
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
bool ContextBase::parseIoBufSize(QuickString bufStr)
{
char lastChar = bufStr[bufStr.size()-1];
int multiplier = 1;
if (!isdigit(lastChar)) {
switch (lastChar) {
case 'K':
multiplier = 1 << 10;
break;
case 'M':
multiplier = 1 << 20;
break;
case 'G':
multiplier = 1 << 30;
break;
default:
_errorMsg = "\n***** ERROR: Unrecognized memory buffer size suffix \'";
_errorMsg += lastChar;
_errorMsg += "\' given. *****";
return false;
break;
}
//lop off suffix character
bufStr.resize(bufStr.size()-1);
}
if (!isNumeric(bufStr)) {
_errorMsg = "\n***** ERROR: argument passed to -iobuf is not numeric. *****";
return false;
}
_ioBufSize = str2chrPos(bufStr) * multiplier;
if (_ioBufSize < MIN_ALLOWED_BUF_SIZE) {
_errorMsg = "\n***** ERROR: specified buffer size is too small. *****";
return false;
}
return true;
}