Debian Codegraph @

bcftools/variantkey.h

raw symbols SCIP
1 // VariantKey
2 //
3 // variantkey.h
4 //
5 // @category Libraries
6 // @author Nicola Asuni <[email protected]>
7 // @link https://github.com/tecnickcom/variantkey
8 // @license MIT [LICENSE](https://raw.githubusercontent.com/tecnickcom/variantkey/main/LICENSE)
9 // @copyright 2017-2018 GENOMICS plc, 2018-2023 Nicola Asuni - Tecnick.com
10 //
11 // LICENSE
12 //
13 // Copyright (c) 2017-2018 GENOMICS plc
14 // Copyright (c) 2018-2023 Nicola Asuni - Tecnick.com
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining a copy
17 // of this software and associated documentation files (the "Software"), to deal
18 // in the Software without restriction, including without limitation the rights
19 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 // copies of the Software, and to permit persons to whom the Software is
21 // furnished to do so, subject to the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be included in
24 // all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 // THE SOFTWARE.
33
34 /**
35 * @file variantkey.h
36 * @brief VariantKey main functions.
37 *
38 * The functions provided here allow the generation and processing of a 64 bit Unsigned Integer Keys for Human Genetic Variants.
39 * The VariantKey is sortable for chromosome and position,
40 * and it is also fully reversible for variants with up to 11 bases between Reference and Alternate alleles.
41 * It can be used to sort, search and match variant-based data easily and very quickly.
42 */
43
44 #ifndef VARIANTKEY_H
45 #define VARIANTKEY_H
46
47 #include <inttypes.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include "hex.h"
51
52 #define VKMASK_CHROM 0xF800000000000000 //!< VariantKey binary mask for CHROM [ 11111000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ]
53 #define VKMASK_POS 0x07FFFFFF80000000 //!< VariantKey binary mask for POS [ 00000111 11111111 11111111 11111111 10000000 00000000 00000000 00000000 ]
54 #define VKMASK_CHROMPOS 0xFFFFFFFF80000000 //!< VariantKey binary mask for CHROM+POS [ 11111111 11111111 11111111 11111111 10000000 00000000 00000000 00000000 ]
55 #define VKMASK_REFALT 0x000000007FFFFFFF //!< VariantKey binary mask for REF+ALT [ 00000000 00000000 00000000 00000000 01111111 11111111 11111111 11111111 ]
56 #define VKSHIFT_CHROM 59 //!< CHROM LSB position from the VariantKey LSB
57 #define VKSHIFT_POS 31 //!< POS LSB position from the VariantKey LSB
58 #define MAXUINT32 0xFFFFFFFF //!< Maximum value for uint32_t
59
60 /**
61 * VariantKey struct.
62 * Contains the numerically encoded VariantKey components (CHROM, POS, REF+ALT).
63 */
64 typedef struct variantkey_t
65 {
66 uint8_t chrom; //!< Chromosome encoded number (only the LSB 5 bit are used)
67 uint32_t pos; //!< Reference position, with the first base having position 0 (only the LSB 28 bit are used)
68 uint32_t refalt; //!< Code for Reference and Alternate allele (only the LSB 31 bits are used)
69 } variantkey_t;
70
71 /**
72 * Struct containing the minimum and maximum VariantKey values for range searches.
73 */
74 typedef struct vkrange_t
75 {
76 uint64_t min; //!< Minimum VariantKey value for any given REF+ALT encoding
77 uint64_t max; //!< Maximum VariantKey value for any given REF+ALT encoding
78 } vkrange_t;
79
80 /** @brief Returns the encoding for a numerical chromosome input.
81 *
82 * @param chrom Chromosome. An identifier from the reference genome, no white-space permitted.
83 * @param size Length of the chrom string, excluding the terminating null byte.
84 *
85 * @return CHROM code
86 */
87 static inline uint8_t encode_numeric_chrom(const char *chrom, size_t size)
88 {
89 size_t i;
90 uint8_t v = (chrom[0] - '0');
91 for (i = 1; i < size; i++)
92 {
93 if ((chrom[i] > '9') || (chrom[i] < '0'))
94 {
95 return 0; // NA: a character that is not a number was found.
96 }
97 v = ((v * 10) + (chrom[i] - '0'));
98 }
99 return v;
100 }
101
102
103 /** @brief Returns a true value (1) if the input chrom has 'chr' prefix (case insensitive).
104 *
105 * @param chrom Chromosome. An identifier from the reference genome, no white-space permitted.
106 * @param size Length of the chrom string, excluding the terminating null byte.
107 *
108 * @return True (1) if the chr prefix is present.
109 */
110 static inline int has_chrom_chr_prefix(const char *chrom, size_t size)
111 {
112 return ((size > 3)
113 && ((chrom[0] == 'c') || (chrom[0] == 'C'))
114 && ((chrom[1] == 'h') || (chrom[1] == 'H'))
115 && ((chrom[2] == 'r') || (chrom[2] == 'R')));
116 }
117
118 /** @brief Returns chromosome numerical encoding.
119 *
120 * @param chrom Chromosome. An identifier from the reference genome, no white-space permitted.
121 * @param size Length of the chrom string, excluding the terminating null byte.
122 *
123 * @return CHROM code or 0 in case of invalid input.
124 */
125 static inline uint8_t encode_chrom(const char *chrom, size_t size)
126 {
127 // X = 23; Y = 24; M = 25; any other letter is mapped to 0:
128 static const uint8_t onecharmap[] =
129 {
130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132 /* M X Y */
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,23,24, 0, 0, 0, 0, 0, 0,
134 /* m x y */
135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,23,24, 0, 0, 0, 0, 0, 0,
136 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140 };
141 if (has_chrom_chr_prefix(chrom, size))
142 {
143 // remove "chr" prefix
144 chrom += 3;
145 size -= 3;
146 }
147 if (size == 0)
148 {
149 return 0;
150 }
151 if ((chrom[0] <= '9') && (chrom[0] >= '0'))
152 {
153 return encode_numeric_chrom(chrom, size);
154 }
155 if ((size == 1) || ((size == 2) && ((chrom[1] == 'T') || (chrom[1] == 't'))))
156 {
157 return onecharmap[((uint8_t)chrom[0])];
158 }
159 return 0; // NA
160 }
161
162 /** @brief Decode the chromosome numerical code.
163 *
164 * @param code CHROM code.
165 * @param chrom CHROM string buffer to be returned. Its size should be enough to contain the results (max 4 bytes).
166 *
167 * @return If successful, the total number of characters written is returned,
168 * excluding the null-character appended at the end of the string,
169 * otherwise a negative number is returned in case of failure.
170 */
171 static inline size_t decode_chrom(uint8_t code, char *chrom)
172 {
173 if ((code < 1) || (code > 25))
174 {
175 return sprintf(chrom, "NA");
176 }
177 if (code < 23)
178 {
179 return sprintf(chrom, "%" PRIu8, code);
180 }
181 static const char *map[] = {"X", "Y", "MT"};
182 return sprintf(chrom, "%s", map[(code - 23)]);
183 }
184
185 static inline uint32_t encode_base(const uint8_t c)
186 {
187 /*
188 Encode base:
189 A = 0
190 C = 1
191 G = 2
192 T = 3
193 */
194 static const uint32_t map[] =
195 {
196 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
197 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
198 /*A C G T*/
199 4,0,4,1,4,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,
200 /*a c g t*/
201 4,0,4,1,4,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,
202 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
203 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
204 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
205 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
206 };
207 return map[c];
208 }
209
210 static inline int encode_allele(uint32_t *h, uint8_t *bitpos, const char *str, size_t size)
211 {
212 uint32_t v;
213 while (size--)
214 {
215 v = encode_base(*str++);
216 if (v > 3)
217 {
218 return -1;
219 }
220 *bitpos -= 2;
221 *h |= (v << *bitpos);
222 }
223 return 0;
224 }
225
226 static inline uint32_t encode_refalt_rev(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
227 {
228 //[******** ******** ******** ******** *RRRRAAA A1122334 45566778 8990011*]
229 uint32_t h = 0;
230 h |= ((uint32_t)(sizeref) << 27); // RRRR: length of (REF - 1)
231 h |= ((uint32_t)(sizealt) << 23); // AAAA: length of (ALT - 1)
232 uint8_t bitpos = 23;
233 if ((encode_allele(&h, &bitpos, ref, sizeref) < 0) || (encode_allele(&h, &bitpos, alt, sizealt) < 0))
234 {
235 return MAXUINT32; // error code
236 }
237 return h;
238 }
239
240 // Mix two 32 bit hash numbers using a MurmurHash3-like algorithm
241 static inline uint32_t muxhash(uint32_t k, uint32_t h)
242 {
243 k *= 0xcc9e2d51;
244 k = (k >> 17) | (k << 15);
245 k *= 0x1b873593;
246 h ^= k;
247 h = (h >> 19) | (h << 13);
248 return ((h * 5) + 0xe6546b64);
249 }
250
251 static inline uint32_t encode_packchar(int c)
252 {
253 if (c < 'A')
254 {
255 return 27;
256 }
257 if (c >= 'a')
258 {
259 return (uint32_t)(c - 'a' + 1);
260 }
261 return (uint32_t)(c - 'A' + 1);
262 }
263
264 // pack blocks of 6 characters in 32 bit (6 x 5 bit + 2 spare bit) [ 01111122 22233333 44444555 55666660 ]
265 static inline uint32_t pack_chars_tail(const char *str, size_t size)
266 {
267 uint32_t h = 0;
268 const char *pos = (str + size - 1);
269 switch (size)
270 {
271 case 5:
272 h ^= encode_packchar(*pos--) << (1 + (5 * 1));
273 // fall through
274 case 4:
275 h ^= encode_packchar(*pos--) << (1 + (5 * 2));
276 // fall through
277 case 3:
278 h ^= encode_packchar(*pos--) << (1 + (5 * 3));
279 // fall through
280 case 2:
281 h ^= encode_packchar(*pos--) << (1 + (5 * 4));
282 // fall through
283 case 1:
284 h ^= encode_packchar(*pos) << (1 + (5 * 5));
285 }
286 return h;
287 }
288
289 static inline uint32_t pack_chars(const char *str)
290 {
291 const char *pos = (str + 5);
292 return ((encode_packchar(*pos) << 1)
293 ^ (encode_packchar(*(pos-1)) << (1 + (5 * 1)))
294 ^ (encode_packchar(*(pos-2)) << (1 + (5 * 2)))
295 ^ (encode_packchar(*(pos-3)) << (1 + (5 * 3)))
296 ^ (encode_packchar(*(pos-4)) << (1 + (5 * 4)))
297 ^ (encode_packchar(*(pos-5)) << (1 + (5 * 5))));
298 }
299
300 // Return a 32 bit hash of a nucleotide string
301 static inline uint32_t hash32(const char *str, size_t size)
302 {
303 uint32_t h = 0;
304 size_t len = 6;
305 while (size >= len)
306 {
307 h = muxhash(pack_chars(str), h);
308 str += len;
309 size -= len;
310 }
311 if (size > 0)
312 {
313 h = muxhash(pack_chars_tail(str, size), h);
314 }
315 return h;
316 }
317
318 static inline uint32_t encode_refalt_hash(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
319 {
320 // 0x3 is the separator character between REF and ALT [00000000 00000000 00000000 00000011]
321 uint32_t h = muxhash(hash32(alt, sizealt), muxhash(0x3, hash32(ref, sizeref)));
322 // MurmurHash3 finalization mix - force all bits of a hash block to avalanche
323 h ^= h >> 16;
324 h *= 0x85ebca6b;
325 h ^= h >> 13;
326 h *= 0xc2b2ae35;
327 h ^= h >> 16;
328 return ((h >> 1) | 0x1); // 0x1 is the set bit to indicate HASH mode [00000000 00000000 00000000 00000001]
329 }
330
331 /** @brief Returns reference+alternate numerical encoding.
332 *
333 * @param ref Reference allele. String containing a sequence of nucleotide letters.
334 * The value in the pos field refers to the position of the first nucleotide in the String.
335 * Characters must be A-Z, a-z or *
336 * @param sizeref Length of the ref string, excluding the terminating null byte.
337 * @param alt Alternate non-reference allele string.
338 * Characters must be A-Z, a-z or *
339 * @param sizealt Length of the alt string, excluding the terminating null byte.
340 *
341 * @return REF+ALT code
342 */
343 static inline uint32_t encode_refalt(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
344 {
345 if ((sizeref + sizealt) <= 11)
346 {
347 uint32_t h = encode_refalt_rev(ref, sizeref, alt, sizealt);
348 if (h != MAXUINT32)
349 {
350 return h;
351 }
352 }
353 return encode_refalt_hash(ref, sizeref, alt, sizealt);
354 }
355
356 static inline char decode_base(uint32_t code, int bitpos)
357 {
358 static const char base[4] = {'A', 'C', 'G', 'T'};
359 return base[((code >> bitpos) & 0x3)]; // 0x3 is the 2 bit mask [00000011]
360 }
361
362 static inline size_t decode_refalt_rev(uint32_t code, char *ref, size_t *sizeref, char *alt, size_t *sizealt)
363 {
364 *sizeref = (size_t)((code & 0x78000000) >> 27); // [01111000 00000000 00000000 00000000]
365 *sizealt = (size_t)((code & 0x07800000) >> 23); // [00000111 10000000 00000000 00000000]
366 switch (*sizeref)
367 {
368 case 10:
369 ref[9] = decode_base(code, (3 + (2 * 0)));
370 // fall through
371 case 9:
372 ref[8] = decode_base(code, (3 + (2 * 1)));
373 // fall through
374 case 8:
375 ref[7] = decode_base(code, (3 + (2 * 2)));
376 // fall through
377 case 7:
378 ref[6] = decode_base(code, (3 + (2 * 3)));
379 // fall through
380 case 6:
381 ref[5] = decode_base(code, (3 + (2 * 4)));
382 // fall through
383 case 5:
384 ref[4] = decode_base(code, (3 + (2 * 5)));
385 // fall through
386 case 4:
387 ref[3] = decode_base(code, (3 + (2 * 6)));
388 // fall through
389 case 3:
390 ref[2] = decode_base(code, (3 + (2 * 7)));
391 // fall through
392 case 2:
393 ref[1] = decode_base(code, (3 + (2 * 8)));
394 // fall through
395 case 1:
396 ref[0] = decode_base(code, (3 + (2 * 9)));
397 }
398 ref[*sizeref] = 0;
399 uint8_t bitpos = (23 - ((*sizeref) << 1));
400 switch (*sizealt)
401 {
402 case 10:
403 alt[9] = decode_base(code, bitpos - (2 * 10));
404 // fall through
405 case 9:
406 alt[8] = decode_base(code, bitpos - (2 * 9));
407 // fall through
408 case 8:
409 alt[7] = decode_base(code, bitpos - (2 * 8));
410 // fall through
411 case 7:
412 alt[6] = decode_base(code, bitpos - (2 * 7));
413 // fall through
414 case 6:
415 alt[5] = decode_base(code, bitpos - (2 * 6));
416 // fall through
417 case 5:
418 alt[4] = decode_base(code, bitpos - (2 * 5));
419 // fall through
420 case 4:
421 alt[3] = decode_base(code, bitpos - (2 * 4));
422 // fall through
423 case 3:
424 alt[2] = decode_base(code, bitpos - (2 * 3));
425 // fall through
426 case 2:
427 alt[1] = decode_base(code, bitpos - (2 * 2));
428 // fall through
429 case 1:
430 alt[0] = decode_base(code, bitpos - (2 * 1));
431 }
432 alt[*sizealt] = 0;
433 return (*sizeref + *sizealt);
434 }
435
436 /** @brief Decode the 32 bit REF+ALT code if reversible (if it has 11 or less bases in total and only contains ACGT letters).
437 *
438 * @param code REF+ALT code
439 * @param ref REF string buffer to be returned.
440 * @param sizeref Pointer to the size of the ref buffer, excluding the terminating null byte.
441 * This will contain the final ref size.
442 * @param alt ALT string buffer to be returned.
443 * @param sizealt Pointer to the size of the alt buffer, excluding the terminating null byte.
444 * This will contain the final alt size.
445 *
446 * @return If the code is reversible, then the total number of characters of REF+ALT is returned.
447 * Otherwise 0 is returned.
448 */
449 static inline size_t decode_refalt(uint32_t code, char *ref, size_t *sizeref, char *alt, size_t *sizealt)
450 {
451 if (code & 0x1) // check last bit
452 {
453 return 0; // non-reversible encoding
454 }
455 return decode_refalt_rev(code, ref, sizeref, alt, sizealt);
456 }
457
458 /** @brief Returns a 64 bit variant key based on the pre-encoded CHROM, POS (0-based) and REF+ALT.
459 *
460 * @param chrom Encoded Chromosome (see encode_chrom).
461 * @param pos Position. The reference position, with the first base having position 0.
462 * @param refalt Encoded Reference + Alternate (see encode_refalt).
463 *
464 * @return VariantKey 64 bit code.
465 */
466 static inline uint64_t encode_variantkey(uint8_t chrom, uint32_t pos, uint32_t refalt)
467 {
468 return (((uint64_t)chrom << VKSHIFT_CHROM) | ((uint64_t)pos << VKSHIFT_POS) | (uint64_t)refalt);
469 }
470
471 /** @brief Extract the CHROM code from VariantKey.
472 *
473 * @param vk VariantKey code.
474 *
475 * @return CHROM code.
476 */
477 static inline uint8_t extract_variantkey_chrom(uint64_t vk)
478 {
479 return (uint8_t)((vk & VKMASK_CHROM) >> VKSHIFT_CHROM);
480 }
481
482 /** @brief Extract the POS code from VariantKey.
483 *
484 * @param vk VariantKey code.
485 *
486 * @return POS.
487 */
488 static inline uint32_t extract_variantkey_pos(uint64_t vk)
489 {
490 return (uint32_t)((vk & VKMASK_POS) >> VKSHIFT_POS);
491 }
492
493 /** @brief Extract the REF+ALT code from VariantKey.
494 *
495 * @param vk VariantKey code.
496 *
497 * @return REF+ALT code.
498 */
499 static inline uint32_t extract_variantkey_refalt(uint64_t vk)
500 {
501 return (uint32_t)(vk & VKMASK_REFALT);
502 }
503
504 /** @brief Decode a VariantKey code and returns the components as variantkey_t structure.
505 *
506 * @param code VariantKey code.
507 * @param vk Decoded variantkey structure.
508 */
509 static inline void decode_variantkey(uint64_t code, variantkey_t *vk)
510 {
511 vk->chrom = extract_variantkey_chrom(code);
512 vk->pos = extract_variantkey_pos(code);
513 vk->refalt = extract_variantkey_refalt(code);
514 }
515
516 /**
517 * Returns a 64 bit variant key based on CHROM, POS (0-based), REF, ALT.
518 * The variant should be already normalized (see normalize_variant or use normalized_variantkey).
519 *
520 * @param chrom Chromosome. An identifier from the reference genome, no white-space or leading zeros permitted.
521 * @param sizechrom Length of the chrom string, excluding the terminating null byte.
522 * @param pos Position. The reference position, with the first base having position 0.
523 * @param ref Reference allele. String containing a sequence of nucleotide letters.
524 * The value in the pos field refers to the position of the first nucleotide in the String.
525 * Characters must be A-Z, a-z or *
526 * @param sizeref Length of the ref string, excluding the terminating null byte.
527 * @param alt Alternate non-reference allele string.
528 * Characters must be A-Z, a-z or *
529 * @param sizealt Length of the alt string, excluding the terminating null byte.
530 *
531 * @return VariantKey 64 bit code.
532 */
533 static inline uint64_t variantkey(const char *chrom, size_t sizechrom, uint32_t pos, const char *ref, size_t sizeref, const char *alt, size_t sizealt)
534 {
535 return encode_variantkey(encode_chrom(chrom, sizechrom), pos, encode_refalt(ref, sizeref, alt, sizealt));
536 }
537
538 /** @brief Returns minimum and maximum VariantKeys for range searches.
539 *
540 * @param chrom Chromosome encoded number.
541 * @param pos_min Start reference position, with the first base having position 0.
542 * @param pos_max End reference position, with the first base having position 0.
543 * @param range VariantKey range values.
544 */
545 static inline void variantkey_range(uint8_t chrom, uint32_t pos_min, uint32_t pos_max, vkrange_t *range)
546 {
547 uint64_t c = ((uint64_t)chrom << VKSHIFT_CHROM);
548 range->min = (c | ((uint64_t)pos_min << VKSHIFT_POS));
549 range->max = (c | ((uint64_t)pos_max << VKSHIFT_POS) | VKMASK_REFALT);
550 }
551
552 static inline int8_t compare_uint64_t(uint64_t a, uint64_t b)
553 {
554 return (a < b) ? -1 : (a > b);
555 }
556
557 /** @brief Compares two VariantKeys by chromosome only.
558 *
559 * @param vka The first VariantKey to be compared.
560 * @param vkb The second VariantKey to be compared.
561 *
562 * @return -1 if the first chromosome is smaller than the second, 0 if they are equal and 1 if the first is greater than the second.
563 */
564 static inline int8_t compare_variantkey_chrom(uint64_t vka, uint64_t vkb)
565 {
566 return compare_uint64_t((vka >> VKSHIFT_CHROM), (vkb >> VKSHIFT_CHROM));
567 }
568
569 /** @brief Compares two VariantKeys by chromosome and position.
570 *
571 * @param vka The first VariantKey to be compared.
572 * @param vkb The second VariantKey to be compared.
573 *
574 * @return -1 if the first CHROM+POS is smaller than the second, 0 if they are equal and 1 if the first is greater than the second.
575 */
576 static inline int8_t compare_variantkey_chrom_pos(uint64_t vka, uint64_t vkb)
577 {
578 return compare_uint64_t((vka >> VKSHIFT_POS), (vkb >> VKSHIFT_POS));
579 }
580
581 /** @brief Returns VariantKey hexadecimal string (16 characters).
582 *
583 * The string represent a 64 bit number or:
584 * - 5 bit for CHROM
585 * - 28 bit for POS
586 * - 31 bit for REF+ALT
587 *
588 * @param vk VariantKey code.
589 * @param str String buffer to be returned (it must be sized 17 bytes at least).
590 *
591 * @return Upon successful return, these function returns the number of characters processed
592 * (excluding the null byte used to end output to strings).
593 * If the buffer size is not sufficient, then the return value is the number of characters required for
594 * buffer string, including the terminating null byte.
595 */
596 static inline size_t variantkey_hex(uint64_t vk, char *str)
597 {
598 return hex_uint64_t(vk, str);
599 }
600
601 /** @brief Parses a VariantKey hexadecimal string and returns the code.
602 *
603 * @param vs VariantKey hexadecimal string (it must contain 16 hexadecimal characters).
604 *
605 * @return A VariantKey code.
606 */
607 static inline uint64_t parse_variantkey_hex(const char *vs)
608 {
609 return parse_hex_uint64_t(vs);
610 }
611
612 #endif // VARIANTKEY_H