ContextClosest.cpp 7.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * ContextClosest.cpp
 *
 *  Created on: Sep 25, 2014
 *      Author: nek3d
 */

#include "ContextClosest.h"

ContextClosest::ContextClosest()
: 	_haveTieMode(false),
	_ignoreOverlaps(false),
	_ignoreUpstream(false),
	_ignoreDownstream(false),
	_reportDistance(false),
	_signDistance(false),
	_haveStrandedDistMode(false),
	_diffNames(false),
	_tieMode(ALL_TIES),
20
	_strandedDistMode(REF_DIST),
21
22
	_multiDbMode(EACH_DB),
	_numClosestHitsWanted(1)
23
{
24
25
	// closest requires sorted input
	setSortedInput(true);
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
53
54
55
56
57
58

}

ContextClosest::~ContextClosest(){

}

bool ContextClosest::parseCmdArgs(int argc, char **argv, int skipFirstArgs){

	for (_i=_skipFirstArgs; _i < argc; _i++) {
		if (isUsed(_i - _skipFirstArgs)) {
			continue;
		}
		if (strcmp(_argv[_i], "-c") == 0) {
			//bypass intersect's use of the -c option, because -c
			//means writeCount for intersect, but means columns for map.
			if (!ContextBase::handle_c()) return false;
		}
        else if (strcmp(_argv[_i], "-d") == 0) {
           if (!handle_d()) return false;
        }
        else if (strcmp(_argv[_i], "-D") == 0) {
        	if (!handle_D()) return false;
        }
        else if (strcmp(_argv[_i], "-io") == 0) {
        	if (!handle_io()) return false;
        }
        else if (strcmp(_argv[_i], "-iu") == 0) {
        	if (!handle_iu()) return false;
        }
        else if (strcmp(_argv[_i], "-id") == 0) {
        	if (!handle_id()) return false;
        }
59
60
61
62
63
64
65
        else if (strcmp(_argv[_i], "-fu") == 0) {
        	if (!handle_fu()) return false;
        }
        else if (strcmp(_argv[_i], "-fd") == 0) {
        	if (!handle_fd()) return false;
        }
       else if (strcmp(_argv[_i], "-N") == 0) {
66
67
68
69
70
        	if (!handle_N()) return false;
        }
        else if (strcmp(_argv[_i], "-t") == 0) {
        	if (!handle_t()) return false;
        }
71
72
73
        else if (strcmp(_argv[_i], "-mdb") == 0) {
        	if (!handle_mdb()) return false;
        }
74
75
76
        else if (strcmp(_argv[_i], "-k") == 0) {
        	if (!handle_k()) return false;
        }
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	}
	return ContextIntersect::parseCmdArgs(argc, argv, _skipFirstArgs);
}

bool ContextClosest::isValidState(){
	if (!ContextIntersect::isValidState()) return false;

   // make sure we have both input files
	if (_haveTieMode && (_tieMode != ALL_TIES) && (_tieMode != FIRST_TIE)
					&& (_tieMode != LAST_TIE)) {
		_errorMsg = "\n*****\n*****ERROR: Request \"all\" or \"first\" or \"last\" for Tie Mode (-t)\n*****\n";
		return false;
	}

	if (_haveStrandedDistMode && (_strandedDistMode != A_DIST) && (_strandedDistMode != B_DIST)
							 && (_strandedDistMode != REF_DIST)) {
		_errorMsg = "\n*****\n*****ERROR: Request \"a\" or \"b\" or \"ref\" for Stranded Distance Mode (-D)\n*****\n";
		return false;
	}

	if (_ignoreUpstream && _ignoreDownstream) {
		_errorMsg = "\n*****\n*****ERROR: Request either -iu OR -id, not both.\n*****\n";
		return false;
	}

	if ((_ignoreUpstream || _ignoreDownstream) && ! _haveStrandedDistMode) {
		_errorMsg  = "\n*****\n*****ERROR: When requesting -iu or -id, you also need to specify -D.\n*****\n";
		return false;
	}

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	if ((_forceUpstream || _forceDownstream) && ! _haveStrandedDistMode) {
		_errorMsg  = "\n*****\n*****ERROR: When requesting -fu or -fd, you also need to specify -D.\n*****\n";
		return false;
	}

	if (_ignoreUpstream && _forceUpstream) {
		_errorMsg  = "\n*****\n*****ERROR: Can't both ignore upstream and force upstream.\n*****\n";
		return false;
	}

	if (_ignoreDownstream && _forceDownstream) {
		_errorMsg  = "\n*****\n*****ERROR: Can't both ignore downstream and force downstream.\n*****\n";
		return false;
	}

	if (_sortOutput && _reportDistance) {
		_errorMsg  = "\n*****\n*****ERROR: -sortout (sorted output) is not valid with distance reporting.\n*****\n";
		return false;
	}
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
177
	return true;
}

bool ContextClosest::handle_d() {
    _reportDistance = true;
    markUsed(_i - _skipFirstArgs);
    return true;
}

bool ContextClosest::handle_D() {
	bool strandError = false;
    if ((_i+1) < _argc) {
        _reportDistance = true;
        _signDistance = true;
        _haveStrandedDistMode = true;
        QuickString modeStr(_argv[_i + 1]);
        if (modeStr == "ref") {
        	_strandedDistMode = REF_DIST;
        } else if (modeStr == "a") {
        	_strandedDistMode = A_DIST;
        } else if (modeStr == "b") {
        	_strandedDistMode = B_DIST;
        } else {
        	strandError = true;
        }
    } else {
    	strandError = true;
    }
    if (!strandError) {
        markUsed(_i - _skipFirstArgs);
        _i++;
        markUsed(_i - _skipFirstArgs);
        return true;
    }
    _errorMsg = "*****ERROR: -D option must be followed with \"ref\", \"a\", or \"b\"";
    return false;
}

bool ContextClosest::handle_io() {
    _ignoreOverlaps = true;
    markUsed(_i - _skipFirstArgs);
    return true;
}

bool ContextClosest::handle_iu() {
	_ignoreUpstream = true;
    markUsed(_i - _skipFirstArgs);
    return true;
}

bool ContextClosest::handle_id() {
178
	_ignoreDownstream = true;
179
180
181
182
    markUsed(_i - _skipFirstArgs);
    return true;
}

183
184
185
186
187
188
189
190
191
192
193
194
bool ContextClosest::handle_fu() {
	_forceUpstream = true;
    markUsed(_i - _skipFirstArgs);
    return true;
}

bool ContextClosest::handle_fd() {
	_forceDownstream = true;
    markUsed(_i - _skipFirstArgs);
    return true;
}

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
bool ContextClosest::handle_N() {
    _diffNames = true;
    markUsed(_i - _skipFirstArgs);
    return true;
}

bool ContextClosest::handle_t()
{
	bool tieError = false;
    if ((_i+1) < _argc) {
        _haveTieMode = true;
        QuickString tieStr(_argv[_i+1]);
        if (tieStr == "all") {
        	_tieMode = ALL_TIES;
        } else if (tieStr == "first") {
        	_tieMode = FIRST_TIE;
        } else if (tieStr == "last") {
        	_tieMode = LAST_TIE;
        } else {
        	tieError = true;
        }
    } else {
    	tieError = true;
    }
    if (!tieError) {
        markUsed(_i - _skipFirstArgs);
        _i++;
        markUsed(_i - _skipFirstArgs);
        return true;
    }
	_errorMsg = "*****ERROR: Request \"all\", \"first\", \"last\" for Tie Mode (-t)";
	return false;
}
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

bool ContextClosest::handle_mdb()
{
	bool mdbError = false;
    if ((_i+1) < _argc) {
        QuickString mdbStr(_argv[_i+1]);
        if (mdbStr == "each") {
        	_multiDbMode = EACH_DB;
        } else if (mdbStr == "all") {
        	_multiDbMode = ALL_DBS;
        } else {
        	mdbError = true;
        }
    } else {
    	mdbError = true;
    }
    if (!mdbError) {
        markUsed(_i - _skipFirstArgs);
        _i++;
        markUsed(_i - _skipFirstArgs);
        return true;
    }
	_errorMsg = "*****ERROR: Request \"each\" or \"last\" for Multiple Database Mode (-mdb)";
	return false;
}
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

bool ContextClosest::handle_k()
{
    if ((_i+1) < _argc) {
    	if (isNumeric(_argv[_i+1])) {
			_numClosestHitsWanted = atoi(_argv[_i+1]);
			if (_numClosestHitsWanted > 0) {
				markUsed(_i - _skipFirstArgs);
				_i++;
				markUsed(_i - _skipFirstArgs);
				return true;
			}
    	}
    }
	_errorMsg = "\n***** ERROR: -k option must be followed by a postive integer value *****";
	return false;
}