File: kmer.h

package info (click to toggle)
kmc 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,716 kB
  • sloc: cpp: 38,308; python: 664; makefile: 216; perl: 179; sh: 34
file content (652 lines) | stat: -rw-r--r-- 17,264 bytes parent folder | download
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
425
426
427
428
429
430
431
432
433
434
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
This file is a part of KMC software distributed under GNU GPL 3 licence.
The homepage of the KMC project is http://sun.aei.polsl.pl/kmc

Authors: Sebastian Deorowicz, Agnieszka Debudaj-Grabysz, Marek Kokot

Version: 3.2.4
Date   : 2024-02-09
*/

#ifndef _KMER_H
#define _KMER_H

// Important remark: there is no inheritance here to guarantee that all classes defined here are POD according to C++11

#include "defs.h"
#include "meta_oper.h"
#include <string>
#include <cstring>

// *************************************************************************
// Ckmer class for k > 32 with classic kmer counting
template<unsigned SIZE> struct CKmer {
	unsigned long long data[SIZE];

	typedef unsigned long long data_t;
	inline void set(const CKmer<SIZE> &x);

	inline void mask(const CKmer<SIZE> &x);
	inline uint32 end_mask(const uint32 mask);
	inline void set_2bits(const uint64 x, const uint32 p);
	inline uchar get_2bits(const uint32 p);
	inline uchar get_byte(const uint32 p);
	inline void set_byte(const uint32 p, uchar x);
	inline void set_bytes(const uint32 p, const uint32 n, uint32 x);
	inline void set_bits(const uint32 p, const uint32 n, uint64 x);

	inline void increment_at(uint32 suffix_bytes);

	inline void SHL_insert_2bits(const uint64 x);
	inline void SHR_insert_2bits(const uint64 x, const uint32 p);

	inline void SHR(const uint32 p);
	inline void SHL(const uint32 p);

	inline uint64 remove_suffix(const uint32 n) const;
	inline void set_n_1(const uint32 n);
	inline void set_n_01(const uint32 n);

	inline void store(uchar *&buffer, int32 n);
	inline void store(uchar *buffer, int32 p, int32 n);

	inline void load(uchar *&buffer, int32 n);

	inline void load_fast(uchar *&buffer, int32 n, bool little_endian);


	inline bool operator==(const CKmer<SIZE> &x);
	inline bool operator<(const CKmer<SIZE> &x)const;

	inline void clear(void);

	inline char get_symbol(int p);


	inline void set_prefix(CKmer<SIZE>& rhs, uint32 suffix_bytes);
};


// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set(const CKmer<SIZE> &x)
{
#ifdef USE_META_PROG
	IterFwd([&](const int &i){
		data[i] = x.data[i];
	}, uint_<SIZE - 1>());
#else
	for (uint32 i = 0; i < SIZE; ++i)
		data[i] = x.data[i];
#endif
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::mask(const CKmer<SIZE> &x)
{
#ifdef USE_META_PROG
	IterFwd([&](const int &i){
		data[i] &= x.data[i];
	}, uint_<SIZE - 1>());
#else
	for (uint32 i = 0; i < SIZE; ++i)
		data[i] &= x.data[i];
#endif
}

// *********************************************************************
template<unsigned SIZE> inline uint32 CKmer<SIZE>::end_mask(const uint32 mask)
{
	return data[0] & mask;
}
// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set_2bits(const uint64 x, const uint32 p)
{
	//	data[p >> 6] |= x << (p & 63);
	data[p >> 6] += x << (p & 63);
}

template<unsigned SIZE> inline uchar CKmer<SIZE>::get_2bits(const uint32 p)
{
	return (data[p >> 6] >> (p & 63)) & 3;
}
// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::SHR_insert_2bits(const uint64 x, const uint32 p)
{
#ifdef USE_META_PROG
	IterFwd([&](const int &i){
		data[i] >>= 2;
		//		data[i] |= data[i+1] << (64-2);
		data[i] += data[i + 1] << (64 - 2);
	}, uint_<SIZE - 2>());
#else
	for (uint32 i = 0; i < SIZE - 1; ++i)
	{
		data[i] >>= 2;
		//		data[i] |= data[i+1] << (64-2);
		data[i] += data[i + 1] << (64 - 2);
	}
#endif
	data[SIZE - 1] >>= 2;

	//	data[p >> 6] |= x << (p & 63);
	data[p >> 6] += x << (p & 63);
}



// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::SHR(const uint32 p)
{
#ifdef USE_META_PROG
	IterFwd([&](const int &i){
		data[i] >>= 2 * p;
		//		data[i] |= data[i+1] << (64-2*p);
		data[i] += data[i + 1] << (64 - 2 * p);
	}, uint_<SIZE - 2>());
#else
	for (uint32 i = 0; i < SIZE - 1; ++i)
	{
		data[i] >>= 2 * p;
		//		data[i] |= data[i+1] << (64-2*p);
		data[i] += data[i + 1] << (64 - 2 * p);
	}
#endif
	data[SIZE - 1] >>= 2 * p;
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::SHL(const uint32 p)
{
#ifdef USE_META_PROG
	IterRev([&](const int &i){
		data[i + 1] <<= p * 2;
		//		data[i+1] |= data[i] >> (64-p*2);
		data[i + 1] += data[i] >> (64 - p * 2);
	}, uint_<SIZE - 2>());
#else
	for (uint32 i = SIZE - 1; i > 0; --i)
	{
		data[i] <<= p * 2;
		//		data[i] |= data[i-1] >> (64-p*2);
		data[i] += data[i - 1] >> (64 - p * 2);
	}
#endif
	data[0] <<= p * 2;
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::SHL_insert_2bits(const uint64 x)
{
#ifdef USE_META_PROG
	IterRev([&](const int &i){
		data[i + 1] <<= 2;
		//		data[i+1] |= data[i] >> (64-2);
		data[i + 1] += data[i] >> (64 - 2);
	}, uint_<SIZE - 2>());
#else
	for (uint32 i = SIZE - 1; i > 0; --i)
	{
		data[i] <<= 2;
		//		data[i] |= data[i-1] >> (64-2);
		data[i] += data[i - 1] >> (64 - 2);
	}
#endif
	data[0] <<= 2;
	//	data[0] |= x;
	data[0] += x;
}

// *********************************************************************
template<unsigned SIZE> inline uchar CKmer<SIZE>::get_byte(const uint32 p)
{
	return (data[p >> 3] >> ((p << 3) & 63)) & 0xFF;
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set_byte(const uint32 p, uchar x)
{
	//	data[p >> 3] |= ((uint64) x) << ((p & 7) << 3);
	data[p >> 3] += ((uint64)x) << ((p & 7) << 3);
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set_bytes(const uint32 p, const uint32 n, uint32 x)
{
	data[p >> 3] += ((uint64)x) << ((p & 7) << 3);
	//if (8 - (p & 7) < n)
	if((p>>3) != ((p + n - 1) >> 3))
		data[(p >> 3) + 1] += ((uint64)x) >> ((8 - (p & 7)) << 3);
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set_bits(const uint32 p, const uint32 n, uint64 x)
{
	//	data[p >> 6] |= x << (p & 63);
	data[p >> 6] += x << (p & 63);
	if ((p >> 6) != ((p + n - 1) >> 6))
		//		data[(p >> 6) + 1] |= x >> (64 - (p & 63));
		data[(p >> 6) + 1] += x >> (64 - (p & 63));
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::increment_at(uint32 suffix_bytes)
{
	data[(suffix_bytes) >> 3] += 1ull << ((suffix_bytes & 7) << 3);
	if (((suffix_bytes) >> 3) < (SIZE - 1) && (data[(suffix_bytes) >> 3]) == 0) //overflow
		++data[SIZE - 1];

}

// *********************************************************************
template<unsigned SIZE> inline bool CKmer<SIZE>::operator==(const CKmer<SIZE> &x) {
	for (uint32 i = 0; i < SIZE; ++i)
	if (data[i] != x.data[i])
		return false;

	return true;
}

// *********************************************************************
template<unsigned SIZE> inline bool CKmer<SIZE>::operator<(const CKmer<SIZE> &x)const {
	for (int32 i = SIZE - 1; i >= 0; --i)
	if (data[i] < x.data[i])
		return true;
	else if (data[i] > x.data[i])
		return false;
	return false;
}



// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::clear(void)
{
#ifdef USE_META_PROG
	IterFwd([&](const int &i){
		data[i] = 0;
	}, uint_<SIZE - 1>());
#else	
	for (uint32 i = 0; i < SIZE; ++i)
		data[i] = 0;
#endif
}

// *********************************************************************
template<unsigned SIZE> inline uint64 CKmer<SIZE>::remove_suffix(const uint32 n) const
{
	uint32 p = n >> 6; // / 64;
	uint32 r = n & 63;	// % 64;

	if (p == SIZE - 1)
		return data[p] >> r;
	else
		//		return (data[p+1] << (64-r)) | (data[p] >> r);
		return (data[p + 1] << (64 - r)) + (data[p] >> r);
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set_n_1(const uint32 n)
{
	clear();

	for (uint32 i = 0; i < (n >> 6); ++i)
		data[i] = ~((uint64)0);

	uint32 r = n & 63;

	if (r)
		data[n >> 6] = (1ull << r) - 1;
}


// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set_n_01(const uint32 n)
{
	clear();

	for (uint32 i = 0; i < n; ++i)
	if (!(i & 1))
		//			data[i >> 6] |= (1ull << (i & 63));
		data[i >> 6] += (1ull << (i & 63));
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::store(uchar *&buffer, int32 n)
{
	for (int32 i = n - 1; i >= 0; --i)
		*buffer++ = get_byte(i);
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::store(uchar *buffer, int32 p, int32 n)
{
	for (int32 i = n - 1; i >= 0; --i)
		buffer[p++] = get_byte(i);
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::load(uchar *&buffer, int32 n)
{
	clear();
	for (int32 i = n - 1; i >= 0; --i)
		set_byte(i, *buffer++);
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::load_fast(uchar *&buffer, int32 n, bool little_endian)
{	
	uint32 p = ((n + 7) >> 3) - 1;
	for (uint32 i = SIZE - 1; i > p; --i)
		data[i] = 0;
	
	if (!(n & 7))
		++p;
	else
	{
		memcpy(&data[p], buffer, sizeof(uint64));
		if (little_endian)
			data[p] = _bswap_uint64(data[p]);
		data[p] >>= (sizeof(uint64)-(n & 7)) << 3;
		buffer += n & 7;
	}
	for (int i = p - 1; i >= 0; --i)
	{
		memcpy(&data[i], buffer, sizeof(uint64));
		if (little_endian)
			data[i] = _bswap_uint64(data[i]);
		buffer += 8;
	}
}


// *********************************************************************
template<unsigned SIZE> inline char CKmer<SIZE>::get_symbol(int p)
{
	uint32 x = (data[p >> 5] >> (2 * (p & 31))) & 0x03;

	switch (x)
	{
	case 0: return 'A';
	case 1: return 'C';
	case 2: return 'G';
	default: return 'T';
	}
}

// *********************************************************************
template<unsigned SIZE> inline void CKmer<SIZE>::set_prefix(CKmer<SIZE>& rhs, uint32 suffix_bytes)
{
	data[(suffix_bytes) >> 3] += rhs.data[(suffix_bytes) >> 3];
	if (((suffix_bytes) >> 3) < SIZE - 1)
		data[SIZE - 1] += rhs.data[SIZE - 1];
}
// *********************************************************************
// *********************************************************************
// *********************************************************************
// *********************************************************************
// Ckmer class for k <= 32 with classic kmer counting
template<> struct CKmer<1> {
	unsigned long long data;

	typedef unsigned long long data_t;
	static uint32 QUALITY_SIZE;
	static uint32 KMER_SIZE;

	void set(const CKmer<1> &x);	

	void mask(const CKmer<1> &x);
	uint32 end_mask(const uint32 mask);
	void set_2bits(const uint64 x, const uint32 p);
	uchar get_2bits(const uint32 p);
	uchar get_byte(const uint32 p);
	void set_byte(const uint32 p, uchar x);
	void set_bytes(const uint32 p, const uint32 n, uint32 x);
	void set_bits(const uint32 p, const uint32 n, uint64 x);

	void increment_at(uint32 suffix_bytes);

	void SHL_insert_2bits(const uint64 x);
	void SHR_insert_2bits(const uint64 x, const uint32 p);

	void SHR(const uint32 p);
	void SHL(const uint32 p);

	uint64 remove_suffix(const uint32 n) const;
	void set_n_1(const uint32 n);
	void set_n_01(const uint32 n);

	void store(uchar *&buffer, int32 n);
	void store(uchar *buffer, int32 p, int32 n);


	void load(uchar *&buffer, int32 n);

	void load_fast(uchar *&buffer, int32 n, bool little_endian);

	bool operator==(const CKmer<1> &x);
	bool operator<(const CKmer<1> &x)const;

	void clear(void);

	inline char get_symbol(int p);

	inline void set_prefix(CKmer<1>& rhs, uint32 suffix_bytes);
};


// *********************************************************************
inline void CKmer<1>::mask(const CKmer<1> &x)
{
	data &= x.data;
}


// *********************************************************************
inline uint32 CKmer<1>::end_mask(const uint32 mask)
{
	return data & mask;
}
// *********************************************************************
inline void CKmer<1>::set(const CKmer<1> &x)
{
	data = x.data;
}

// *********************************************************************
inline void CKmer<1>::set_2bits(const uint64 x, const uint32 p)
{
	//	data |= x << p;
	data += x << p;
}

inline uchar CKmer<1>::get_2bits(const uint32 p)
{
	return (data >> p) & 3;
}
// *********************************************************************
inline void CKmer<1>::SHR_insert_2bits(const uint64 x, const uint32 p)
{
	data >>= 2;
	//	data |= x << p;
	data += x << p;
}

// *********************************************************************
inline void CKmer<1>::SHR(const uint32 p)
{
	data >>= 2 * p;
}

// *********************************************************************
inline void CKmer<1>::SHL(const uint32 p)
{
	data <<= p * 2;
}
// *********************************************************************
inline void CKmer<1>::SHL_insert_2bits(const uint64 x)
{
	//	data = (data << 2) | x;
	data = (data << 2) + x;
}

// *********************************************************************
inline uchar CKmer<1>::get_byte(const uint32 p)
{
	return (data >> (p << 3)) & 0xFF;
}

// *********************************************************************
inline void CKmer<1>::set_byte(const uint32 p, uchar x)
{
	//	data |= ((uint64) x) << (p << 3);
	data += ((uint64)x) << (p << 3);
}

// *********************************************************************
inline void CKmer<1>::set_bytes(const uint32 p, const uint32 /*n*/, uint32 x)
{
	data += ((uint64) x) << (p << 3);
}

// *********************************************************************
inline void CKmer<1>::set_bits(const uint32 p, const uint32 /*n*/, uint64 x)
{
	//	data |= x << p;
	data += x << p;
}

// *********************************************************************
inline void CKmer<1>::increment_at(uint32 suffix_bytes)
{
	data += (1ull << (suffix_bytes << 3));
}
// *********************************************************************
inline bool CKmer<1>::operator==(const CKmer<1> &x) {
	return data == x.data;
}

// *********************************************************************
inline bool CKmer<1>::operator<(const CKmer<1> &x)const {
	return data < x.data;
}

// *********************************************************************
inline void CKmer<1>::clear(void)
{
	data = 0ull;
}

// *********************************************************************
inline uint64 CKmer<1>::remove_suffix(const uint32 n) const
{
	return data >> n;
}

// *********************************************************************
inline void CKmer<1>::set_n_1(const uint32 n)
{
	if (n == 64)
		data = ~(0ull);
	else
		data = (1ull << n) - 1;
}

// *********************************************************************
inline void CKmer<1>::set_n_01(const uint32 n)
{
	data = 0ull;

	for (uint32 i = 0; i < n; ++i)
	if (!(i & 1))
		data += (1ull << i);
}

// *********************************************************************
inline void CKmer<1>::store(uchar *&buffer, int32 n)
{
	for (int32 i = n - 1; i >= 0; --i)
		*buffer++ = get_byte(i);
}

// *********************************************************************
inline void CKmer<1>::store(uchar *buffer, int32 p, int32 n)
{
	for (int32 i = n - 1; i >= 0; --i)
		buffer[p++] = get_byte(i);
}

// *********************************************************************
inline void CKmer<1>::load_fast(uchar *&buffer, int32 n, bool little_endian)
{
	//for short k-mers n may be 0
	if (!n)
	{
		data = 0;
		return;
	}
	//It compiles to the same as data = *(uint64*)buffer; ->  mov	rax, QWORD PTR [rcx]
	//i am not sure about other platforms than x86
	memcpy(&data, buffer, sizeof(data)); 
	if (little_endian)
		data = _bswap_uint64(data);
	data >>= (sizeof(data)-n) << 3;
	buffer += n;
}

// *********************************************************************
inline void CKmer<1>::load(uchar *&buffer, int32 n)
{
	clear();

	if (!n)
		return;
	unsigned long long tmp;
	tmp = 0;

	switch (n-1)
	{
	case 7:
		data = ((uint64) *buffer++) << 56;
	case 6:
		tmp = ((uint64) *buffer++) << 48;
	case 5:
		data += ((uint64) *buffer++) << 40;
	case 4:
		tmp += ((uint64) *buffer++) << 32;
	case 3:
		data += ((uint64) *buffer++) << 24;
	case 2:
		tmp += ((uint64) *buffer++) << 16;
	case 1:
		data += ((uint64) *buffer++) << 8;
	}

	tmp += *buffer++;

	data += tmp;
}

// *********************************************************************
char CKmer<1>::get_symbol(int p)
{
	uint32 x = (data >> (2 * p)) & 0x03;

	switch (x)
	{
	case 0: return 'A';
	case 1: return 'C';
	case 2: return 'G';
	default: return 'T';
	}
}

// *********************************************************************
void CKmer<1>::set_prefix(CKmer<1>& rhs, uint32 /*suffix_bytes*/)
{
	data += rhs.data;
}
#endif

// ***** EOF