Newer
Older
nkindlon
committed
/*
* ContextBase.cpp
*
* Created on: Feb 12, 2013
* Author: nek3d
*/
#include "ContextBase.h"
#include <unistd.h>
#include <sys/types.h>
ContextBase::ContextBase()
:
_program(UNSPECIFIED_PROGRAM),
_allFilesOpened(false),
nkindlon
committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
_genomeFile(NULL),
_outputFileType(FileRecordTypeChecker::UNKNOWN_FILE_TYPE),
_outputTypeDetermined(false),
_skipFirstArgs(0),
_showHelp(false),
_obeySplits(false),
_uncompressedBam(false),
_useBufferedOutput(true),
_anyHit(false),
_noHit(false),
_writeA(false),
_writeB(false),
_leftJoin(false),
_writeCount(false),
_writeOverlap(false),
_writeAllOverlap(false),
_haveFraction(false),
_overlapFraction(1E-9),
_reciprocal(false),
_sameStrand(false),
_diffStrand(false),
_sortedInput(false),
_printHeader(false),
_printable(true),
_explicitBedOutput(false),
_queryFileIdx(-1),
_databaseFileIdx(-1),
_bamHeaderAndRefIdx(-1),
_maxNumDatabaseFields(0),
_useFullBamTags(false),
_reportCount(false),
_reportNames(false),
_reportScores(false),
_numOutputRecords(0),
_hasConstantSeed(false),
_seed(0),
_forwardOnly(false),
nkindlon
committed
_reverseOnly(false),
_hasColumnOpsMethods(false),
_desiredStrand(FileRecordMergeMgr::ANY_STRAND),
_maxDistance(0),
_useMergedIntervals(false)
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
103
104
105
106
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
168
169
170
171
172
173
174
175
176
}
//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;
if (_argc < 2) {
setShowHelp(true);
return false;
}
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], "-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;
}
nkindlon
committed
}
return true;
}
bool ContextBase::isValidState()
{
if (!openFiles()) {
return false;
}
if (!cmdArgsValid()) {
return false;
}
if (!determineOutputType()) {
return false;
}
nkindlon
committed
if (hasColumnOpsMethods()) {
FileRecordMgr *dbFile = getFile(hasIntersectMethods() ? _databaseFileIdx : 0);
if (!_keyListOps->isValidColumnOps(dbFile)) {
return false;
}
}
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;
}
_files.resize(_fileNames.size());
for (int i = 0; i < (int)_fileNames.size(); i++) {
FileRecordMgr *frm = getNewFRM(_fileNames[i]);
if (hasGenomeFile()) {
frm->setGenomeFile(_genomeFile);
}
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 (_files[_queryFileIdx]->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) {
nkindlon
committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
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
_bamHeaderAndRefIdx = _queryFileIdx;
} else {
_bamHeaderAndRefIdx = _databaseFileIdx;
}
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_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);
}
return true;
}
// 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
}
//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;
}
void ContextBase::setColumnOpsMethods(bool val)
{
_hasColumnOpsMethods = val;
if (val) {
_keyListOps = new KeyListOps();
}
}
const QuickString &ContextBase::getColumnOpsVal(RecordKeyList &keyList) const {
if (!hasColumnOpsMethods()) {
return _nullStr;
}
return _keyListOps->getOpVals(keyList);
}
FileRecordMgr *ContextBase::getNewFRM(const QuickString &filename) {
if (!_useMergedIntervals) {
return new FileRecordMgr(filename);
} else {
FileRecordMergeMgr *frm = new FileRecordMergeMgr(filename);
frm->setStrandType(_desiredStrand);
frm->setMaxDistance(_maxDistance);
return frm;
}
}