File: ref_coord.h

package info (click to toggle)
centrifuge 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,864 kB
  • sloc: cpp: 51,936; perl: 1,919; python: 1,538; makefile: 618; sh: 352
file content (424 lines) | stat: -rw-r--r-- 10,053 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
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
253
254
255
256
257
258
259
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
 *
 * This file is part of Bowtie 2.
 *
 * Bowtie 2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bowtie 2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Bowtie 2.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef REF_COORD_H_
#define REF_COORD_H_

#include <stdint.h>
#include <iostream>
#include <limits>
#include "assert_helpers.h"

typedef int64_t TRefId;
typedef int64_t TRefOff;

/**
 * Encapsulates a reference coordinate; i.e. identifiers for
 *  (a) a reference sequence, and
 *  (b) a 0-based offset into that sequence.
 */
class Coord {

public:

	Coord() { reset(); }

	Coord(const Coord& c) { init(c); }
	
	Coord(TRefId rf, TRefOff of, bool fw) { init(rf, of, fw); }

	/**
	 * Copy given fields into this Coord.
	 */
	void init(TRefId rf, TRefOff of, bool fw) {
		ref_ = rf;
		off_ = of;
		orient_ = (fw ? 1 : 0);
	}

	/**
	 * Copy contents of given Coord into this one.
	 */
	void init(const Coord& c) {
		ref_ = c.ref_;
		off_ = c.off_;
		orient_ = c.orient_;
	}
	
	/**
	 * Return true iff this Coord is identical to the given Coord.
	 */
	bool operator==(const Coord& o) const {
		assert(inited());
		assert(o.inited());
		return ref_ == o.ref_ && off_ == o.off_ && fw() == o.fw();
	}

	/**
	 * Return true iff this Coord is less than the given Coord.  One Coord is
	 * less than another if (a) its reference id is less, (b) its orientation is
	 * less, or (c) its offset is less.
	 */
	bool operator<(const Coord& o) const {
		if(ref_ < o.ref_) return true;
		if(ref_ > o.ref_) return false;
		if(orient_ < o.orient_) return true;
		if(orient_ > o.orient_) return false;
		if(off_ < o.off_) return true;
		if(off_ > o.off_) return false;
		return false;
	}
	
	/**
	 * Return the opposite result from operator<.
	 */
	bool operator>=(const Coord& o) const {
		return !((*this) < o);
	}
	
	/**
	 * Return true iff this Coord is greater than the given Coord.  One Coord
	 * is greater than another if (a) its reference id is greater, (b) its
	 * orientation is greater, or (c) its offset is greater.
	 */
	bool operator>(const Coord& o) const {
		if(ref_ > o.ref_) return true;
		if(ref_ < o.ref_) return false;
		if(orient_ > o.orient_) return true;
		if(orient_ < o.orient_) return false;
		if(off_ > o.off_) return true;
		if(off_ < o.off_) return false;
		return false;
	}
	
	/**
	 * Return the opposite result from operator>.
	 */
	bool operator<=(const Coord& o) const {
		return !((*this) > o);
	}
	
	/**
	 * Reset this coord to uninitialized state.
	 */
	void reset() {
		ref_ = std::numeric_limits<TRefId>::max();
		off_ = std::numeric_limits<TRefOff>::max();
		orient_ = -1;
	}
	
	/**
	 * Return true iff this Coord is initialized (i.e. ref and off have both
	 * been set since the last call to reset()).
	 */
	bool inited() const {
		if(ref_ != std::numeric_limits<TRefId>::max() &&
		   off_ != std::numeric_limits<TRefOff>::max())
		{
			assert(orient_ == 0 || orient_ == 1);
			return true;
		}
		return false;
	}
	
	/**
	 * Get orientation of the Coord.
	 */
	bool fw() const {
		assert(inited());
		assert(orient_ == 0 || orient_ == 1);
		return orient_ == 1;
	}
	
#ifndef NDEBUG
	/**
	 * Check that coord is internally consistent.
	 */
	bool repOk() const {
		if(ref_ != std::numeric_limits<TRefId>::max() &&
		   off_ != std::numeric_limits<TRefOff>::max())
		{
			assert(orient_ == 0 || orient_ == 1);
		}
		return true;
	}
#endif
	
	/**
	 * Check whether an interval defined by this coord and having
	 * length 'len' is contained within an interval defined by
	 * 'inbegin' and 'inend'.
	 */
	bool within(int64_t len, int64_t inbegin, int64_t inend) const {
		return off_ >= inbegin && off_ + len <= inend;
	}
	
	inline TRefId  ref()    const { return ref_; }
	inline TRefOff off()    const { return off_; }
	inline int     orient() const { return orient_; }
	
	inline void setRef(TRefId  id)  { ref_ = id;  }
	inline void setOff(TRefOff off) { off_ = off; }

	inline void adjustOff(TRefOff off) { off_ += off; }

protected:

	TRefId  ref_;    // which reference?
	TRefOff off_;    // 0-based offset into reference
	int     orient_; // true -> Watson strand
};

std::ostream& operator<<(std::ostream& out, const Coord& c);

/**
 * Encapsulates a reference interval, which consists of a Coord and a length.
 */
class Interval {

public:
	
	Interval() { reset(); }
	
	explicit Interval(const Coord& upstream, TRefOff len) {
		init(upstream, len);
	}

	explicit Interval(TRefId rf, TRefOff of, bool fw, TRefOff len) {
		init(rf, of, fw, len);
	}

	void init(const Coord& upstream, TRefOff len) {
		upstream_ = upstream;
		len_ = len;
	}
	
	void init(TRefId rf, TRefOff of, bool fw, TRefOff len) {
		upstream_.init(rf, of, fw);
		len_ = len;
	}
	
	/**
	 * Set offset.
	 */
	void setOff(TRefOff of) {
		upstream_.setOff(of);
	}

	/**
	 * Set length.
	 */
	void setLen(TRefOff len) {
		len_ = len;
	}

	/**
	 * Reset this interval to uninitialized state.
	 */
	void reset() {
		upstream_.reset();
		len_ = 0;
	}
	
	/**
	 * Return true iff this Interval is initialized.
	 */
	bool inited() const {
		if(upstream_.inited()) {
			assert_gt(len_, 0);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Return true iff this Interval is equal to the given Interval,
	 * i.e. if they cover the same set of positions.
	 */
	bool operator==(const Interval& o) const {
		return upstream_ == o.upstream_ &&
		       len_ == o.len_;
	}

	/**
	 * Return true iff this Interval is less than the given Interval.
	 * One interval is less than another if its upstream location is
	 * prior to the other's or, if their upstream locations are equal,
	 * if its length is less than the other's.
	 */
	bool operator<(const Interval& o) const {
		if(upstream_ < o.upstream_) return true;
		if(upstream_ > o.upstream_) return false;
		if(len_ < o.len_) return true;
		return false;
	}
	
	/**
	 * Return opposite result from operator<.
	 */
	bool operator>=(const Interval& o) const {
		return !((*this) < o);
	}

	/**
	 * Return true iff this Interval is greater than than the given
	 * Interval.  One interval is greater than another if its upstream
	 * location is after the other's or, if their upstream locations
	 * are equal, if its length is greater than the other's.
	 */
	bool operator>(const Interval& o) const {
		if(upstream_ > o.upstream_) return true;
		if(upstream_ < o.upstream_) return false;
		if(len_ > o.len_) return true;
		return false;
	}

	/**
	 * Return opposite result from operator>.
	 */
	bool operator<=(const Interval& o) const {
		return !((*this) > o);
	}
	
	/**
	 * Set upstream Coord.
	 */
	void setUpstream(const Coord& c) {
		upstream_ = c;
	}

	/**
	 * Set length.
	 */
	void setLength(TRefOff l) {
		len_ = l;
	}
	
	inline TRefId  ref()    const { return upstream_.ref(); }
	inline TRefOff off()    const { return upstream_.off(); }
	inline TRefOff dnoff()  const { return upstream_.off() + len_; }
	inline int     orient() const { return upstream_.orient(); }

	/**
	 * Return a Coord encoding the coordinate just past the downstream edge of
	 * the interval.
	 */
	inline Coord downstream() const {
		return Coord(
			upstream_.ref(),
			upstream_.off() + len_,
			upstream_.orient());
	}
	
	/**
	 * Return true iff the given Coord is inside this Interval.
	 */
	inline bool contains(const Coord& c) const {
		return
			c.ref()    == ref() &&
			c.orient() == orient() &&
			c.off()    >= off() &&
			c.off()    <  dnoff();
	}

	/**
	 * Return true iff the given Coord is inside this Interval, without
	 * requiring orientations to match.
	 */
	inline bool containsIgnoreOrient(const Coord& c) const {
		return
			c.ref()    == ref() &&
			c.off()    >= off() &&
			c.off()    <  dnoff();
	}

	/**
	 * Return true iff the given Interval is inside this Interval.
	 */
	inline bool contains(const Interval& c) const {
		return
			c.ref()    == ref() &&
			c.orient() == orient() &&
			c.off()    >= off() &&
			c.dnoff()  <= dnoff();
	}

	/**
	 * Return true iff the given Interval is inside this Interval, without
	 * requiring orientations to match.
	 */
	inline bool containsIgnoreOrient(const Interval& c) const {
		return
			c.ref()    == ref() &&
			c.off()    >= off() &&
			c.dnoff()  <= dnoff();
	}

	/**
	 * Return true iff the given Interval overlaps this Interval.
	 */
	inline bool overlaps(const Interval& c) const {
		return
			c.ref()    == upstream_.ref() &&
			c.orient() == upstream_.orient() &&
			((off() <= c.off()   && dnoff() > c.off())   ||
			 (off() <= c.dnoff() && dnoff() > c.dnoff()) ||
			 (c.off() <= off()   && c.dnoff() > off())   ||
			 (c.off() <= dnoff() && c.dnoff() > dnoff()));
	}

	/**
	 * Return true iff the given Interval overlaps this Interval, without
	 * requiring orientations to match.
	 */
	inline bool overlapsIgnoreOrient(const Interval& c) const {
		return
			c.ref()    == upstream_.ref() &&
			((off() <= c.off()   && dnoff() > c.off())   ||
			 (off() <= c.dnoff() && dnoff() > c.dnoff()) ||
			 (c.off() <= off()   && c.dnoff() > off())   ||
			 (c.off() <= dnoff() && c.dnoff() > dnoff()));
	}
	
	inline const Coord&  upstream()   const { return upstream_; }
	inline TRefOff       len()      const { return len_;      }

#ifndef NDEBUG
	/**
	 * Check that the Interval is internally consistent.
	 */
	bool repOk() const {
		assert(upstream_.repOk());
		assert_geq(len_, 0);
		return true;
	}
#endif

	inline void adjustOff(TRefOff off) { upstream_.adjustOff(off); }

protected:

	Coord   upstream_;
	TRefOff len_;
};

std::ostream& operator<<(std::ostream& out, const Interval& c);

#endif /*ndef REF_COORD_H_*/