VariantKey  5.4.1
Numerical Encoding for Human Genetic Variants
variantkey.h
Go to the documentation of this file.
1 // VariantKey
2 //
3 // variantkey.h
4 //
5 // @category Libraries
6 // @author Nicola Asuni <nicola.asuni@genomicsplc.com>
7 // @copyright 2017-2018 GENOMICS plc
8 // @license MIT (see LICENSE)
9 // @link https://github.com/genomicsplc/variantkey
10 //
11 // LICENSE
12 //
13 // Copyright (c) 2017-2018 GENOMICS plc
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to deal
17 // in the Software without restriction, including without limitation the rights
18 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 // copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be included in
23 // all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 // THE SOFTWARE.
32 
43 #ifndef VARIANTKEY_VARIANTKEY_H
44 #define VARIANTKEY_VARIANTKEY_H
45 
46 #include <inttypes.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include "hex.h"
50 
51 #define VKMASK_CHROM 0xF800000000000000
52 #define VKMASK_POS 0x07FFFFFF80000000
53 #define VKMASK_CHROMPOS 0xFFFFFFFF80000000
54 #define VKMASK_REFALT 0x000000007FFFFFFF
55 #define VKSHIFT_CHROM 59
56 #define VKSHIFT_POS 31
57 #define MAXUINT32 0xFFFFFFFF
58 
59 
63 typedef struct variantkey_t
64 {
65  uint8_t chrom;
66  uint32_t pos;
67  uint32_t refalt;
68 } variantkey_t;
69 
73 typedef struct vkrange_t
74 {
75  uint64_t min;
76  uint64_t max;
77 } vkrange_t;
78 
86 static inline uint8_t encode_chrom(const char *chrom, size_t size)
87 {
88  // X > 23 ; Y > 24 ; M > 25
89  static const uint8_t onecharmap[] =
90  {
91  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,
92  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,
93  /* M X Y */
94  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,
95  /* m x y */
96  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,
97  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,
98  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,
99  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,
100  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,
101  };
102  // remove "chr" prefix
103  if ((size > 3)
104  && ((chrom[0] == 'c') || (chrom[0] == 'C'))
105  && ((chrom[1] == 'h') || (chrom[1] == 'H'))
106  && ((chrom[2] == 'r') || (chrom[2] == 'R')))
107  {
108  chrom += 3;
109  size -= 3;
110  }
111  if (size == 0)
112  {
113  return 0;
114  }
115  if ((chrom[0] <= '9') && (chrom[0] >= '0')) // Number
116  {
117  size_t i;
118  uint8_t v = (chrom[0] - '0');
119  for (i = 1; i < size; i++)
120  {
121  if ((chrom[i] > '9') || (chrom[i] < '0'))
122  {
123  return 0; // NA
124  }
125  v = ((v * 10) + (chrom[i] - '0'));
126  }
127  return v;
128  }
129  if ((size == 1) || ((size == 2) && ((chrom[1] == 'T') || (chrom[1] == 't'))))
130  {
131  return onecharmap[((uint8_t)chrom[0])];
132  }
133  return 0; // NA
134 }
135 
145 static inline size_t decode_chrom(uint8_t code, char *chrom)
146 {
147  if ((code < 1) || (code > 25))
148  {
149  return sprintf(chrom, "NA");
150  }
151  if (code < 23)
152  {
153  return sprintf(chrom, "%" PRIu8, code);
154  }
155  static const char *map[] = {"X", "Y", "MT"};
156  return sprintf(chrom, "%s", map[(code - 23)]);
157 }
158 
159 static inline uint32_t encode_base(const uint8_t c)
160 {
161  /*
162  Encode base:
163  A > 0
164  C > 1
165  G > 2
166  T > 3
167  */
168  static const uint32_t map[] =
169  {
170  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,
171  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,
172  /*A C G T*/
173  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,
174  /*a c g t*/
175  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,
176  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,
177  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,
178  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,
179  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,
180  };
181  return map[c];
182 }
183 
184 static inline int encode_allele(uint32_t *h, uint8_t *bitpos, const char *str, size_t size)
185 {
186  uint32_t v;
187  while (size--)
188  {
189  v = encode_base(*str++);
190  if (v > 3)
191  {
192  return -1;
193  }
194  *bitpos -= 2;
195  *h |= (v << *bitpos);
196  }
197  return 0;
198 }
199 
200 static inline uint32_t encode_refalt_rev(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
201 {
202  //[******** ******** ******** ******** *RRRRAAA A1122334 45566778 8990011*]
203  uint32_t h = 0;
204  h |= ((uint32_t)(sizeref) << 27); // RRRR: length of (REF - 1)
205  h |= ((uint32_t)(sizealt) << 23); // AAAA: length of (ALT - 1)
206  uint8_t bitpos = 23;
207  if ((encode_allele(&h, &bitpos, ref, sizeref) < 0) || (encode_allele(&h, &bitpos, alt, sizealt) < 0))
208  {
209  return MAXUINT32; // error code
210  }
211  return h;
212 }
213 
214 // Mix two 32 bit hash numbers using a MurmurHash3-like algorithm
215 static inline uint32_t muxhash(uint32_t k, uint32_t h)
216 {
217  k *= 0xcc9e2d51;
218  k = (k >> 17) | (k << 15);
219  k *= 0x1b873593;
220  h ^= k;
221  h = (h >> 19) | (h << 13);
222  return ((h * 5) + 0xe6546b64);
223 }
224 
225 static inline uint32_t encode_packchar(int c)
226 {
227  if (c < 'A')
228  {
229  return 27;
230  }
231  if (c >= 'a')
232  {
233  return (uint32_t)(c - 'a' + 1);
234  }
235  return (uint32_t)(c - 'A' + 1);
236 }
237 
238 // pack blocks of 6 characters in 32 bit (6 x 5 bit + 2 spare bit) [ 01111122 22233333 44444555 55666660 ]
239 static inline uint32_t pack_chars_tail(const char *str, size_t size)
240 {
241  uint32_t h = 0;
242  const char *pos = (str + size - 1);
243  switch (size)
244  {
245  case 5:
246  h ^= encode_packchar(*pos--) << (1 + (5 * 1));
247  // fall through
248  case 4:
249  h ^= encode_packchar(*pos--) << (1 + (5 * 2));
250  // fall through
251  case 3:
252  h ^= encode_packchar(*pos--) << (1 + (5 * 3));
253  // fall through
254  case 2:
255  h ^= encode_packchar(*pos--) << (1 + (5 * 4));
256  // fall through
257  case 1:
258  h ^= encode_packchar(*pos) << (1 + (5 * 5));
259  }
260  return h;
261 }
262 
263 static inline uint32_t pack_chars(const char *str)
264 {
265  const char *pos = (str + 5);
266  return ((encode_packchar(*pos) << 1)
267  ^ (encode_packchar(*(pos-1)) << (1 + (5 * 1)))
268  ^ (encode_packchar(*(pos-2)) << (1 + (5 * 2)))
269  ^ (encode_packchar(*(pos-3)) << (1 + (5 * 3)))
270  ^ (encode_packchar(*(pos-4)) << (1 + (5 * 4)))
271  ^ (encode_packchar(*(pos-5)) << (1 + (5 * 5))));
272 }
273 
274 // Return a 32 bit hash of a nucleotide string
275 static inline uint32_t hash32(const char *str, size_t size)
276 {
277  uint32_t h = 0;
278  size_t len = 6;
279  while (size >= len)
280  {
281  h = muxhash(pack_chars(str), h);
282  str += len;
283  size -= len;
284  }
285  if (size > 0)
286  {
287  h = muxhash(pack_chars_tail(str, size), h);
288  }
289  return h;
290 }
291 
292 static inline uint32_t encode_refalt_hash(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
293 {
294  // 0x3 is the separator character between REF and ALT [00000000 00000000 00000000 00000011]
295  uint32_t h = muxhash(hash32(alt, sizealt), muxhash(0x3, hash32(ref, sizeref)));
296  // MurmurHash3 finalization mix - force all bits of a hash block to avalanche
297  h ^= h >> 16;
298  h *= 0x85ebca6b;
299  h ^= h >> 13;
300  h *= 0xc2b2ae35;
301  h ^= h >> 16;
302  return ((h >> 1) | 0x1); // 0x1 is the set bit to indicate HASH mode [00000000 00000000 00000000 00000001]
303 }
304 
317 static inline uint32_t encode_refalt(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
318 {
319  if ((sizeref + sizealt) <= 11)
320  {
321  uint32_t h = encode_refalt_rev(ref, sizeref, alt, sizealt);
322  if (h != MAXUINT32)
323  {
324  return h;
325  }
326  }
327  return encode_refalt_hash(ref, sizeref, alt, sizealt);
328 }
329 
330 static inline char decode_base(uint32_t code, int bitpos)
331 {
332  static const char base[4] = {'A', 'C', 'G', 'T'};
333  return base[((code >> bitpos) & 0x3)]; // 0x3 is the 2 bit mask [00000011]
334 }
335 
336 static inline size_t decode_refalt_rev(uint32_t code, char *ref, size_t *sizeref, char *alt, size_t *sizealt)
337 {
338  *sizeref = (size_t)((code & 0x78000000) >> 27); // [01111000 00000000 00000000 00000000]
339  *sizealt = (size_t)((code & 0x07800000) >> 23); // [00000111 10000000 00000000 00000000]
340  switch (*sizeref)
341  {
342  case 10:
343  ref[9] = decode_base(code, (3 + (2 * 0)));
344  // fall through
345  case 9:
346  ref[8] = decode_base(code, (3 + (2 * 1)));
347  // fall through
348  case 8:
349  ref[7] = decode_base(code, (3 + (2 * 2)));
350  // fall through
351  case 7:
352  ref[6] = decode_base(code, (3 + (2 * 3)));
353  // fall through
354  case 6:
355  ref[5] = decode_base(code, (3 + (2 * 4)));
356  // fall through
357  case 5:
358  ref[4] = decode_base(code, (3 + (2 * 5)));
359  // fall through
360  case 4:
361  ref[3] = decode_base(code, (3 + (2 * 6)));
362  // fall through
363  case 3:
364  ref[2] = decode_base(code, (3 + (2 * 7)));
365  // fall through
366  case 2:
367  ref[1] = decode_base(code, (3 + (2 * 8)));
368  // fall through
369  case 1:
370  ref[0] = decode_base(code, (3 + (2 * 9)));
371  }
372  ref[*sizeref] = 0;
373  uint8_t bitpos = (23 - ((*sizeref) << 1));
374  switch (*sizealt)
375  {
376  case 10:
377  alt[9] = decode_base(code, bitpos - (2 * 10));
378  // fall through
379  case 9:
380  alt[8] = decode_base(code, bitpos - (2 * 9));
381  // fall through
382  case 8:
383  alt[7] = decode_base(code, bitpos - (2 * 8));
384  // fall through
385  case 7:
386  alt[6] = decode_base(code, bitpos - (2 * 7));
387  // fall through
388  case 6:
389  alt[5] = decode_base(code, bitpos - (2 * 6));
390  // fall through
391  case 5:
392  alt[4] = decode_base(code, bitpos - (2 * 5));
393  // fall through
394  case 4:
395  alt[3] = decode_base(code, bitpos - (2 * 4));
396  // fall through
397  case 3:
398  alt[2] = decode_base(code, bitpos - (2 * 3));
399  // fall through
400  case 2:
401  alt[1] = decode_base(code, bitpos - (2 * 2));
402  // fall through
403  case 1:
404  alt[0] = decode_base(code, bitpos - (2 * 1));
405  }
406  alt[*sizealt] = 0;
407  return (*sizeref + *sizealt);
408 }
409 
423 static inline size_t decode_refalt(uint32_t code, char *ref, size_t *sizeref, char *alt, size_t *sizealt)
424 {
425  if (code & 0x1) // check last bit
426  {
427  return 0; // non-reversible encoding
428  }
429  return decode_refalt_rev(code, ref, sizeref, alt, sizealt);
430 }
431 
440 static inline uint64_t encode_variantkey(uint8_t chrom, uint32_t pos, uint32_t refalt)
441 {
442  return (((uint64_t)chrom << VKSHIFT_CHROM) | ((uint64_t)pos << VKSHIFT_POS) | (uint64_t)refalt);
443 }
444 
451 static inline uint8_t extract_variantkey_chrom(uint64_t vk)
452 {
453  return (uint8_t)((vk & VKMASK_CHROM) >> VKSHIFT_CHROM);
454 }
455 
462 static inline uint32_t extract_variantkey_pos(uint64_t vk)
463 {
464  return (uint32_t)((vk & VKMASK_POS) >> VKSHIFT_POS);
465 }
466 
473 static inline uint32_t extract_variantkey_refalt(uint64_t vk)
474 {
475  return (uint32_t)(vk & VKMASK_REFALT);
476 }
477 
483 static inline void decode_variantkey(uint64_t code, variantkey_t *vk)
484 {
485  vk->chrom = extract_variantkey_chrom(code);
486  vk->pos = extract_variantkey_pos(code);
487  vk->refalt = extract_variantkey_refalt(code);
488 }
489 
507 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)
508 {
509  return encode_variantkey(encode_chrom(chrom, sizechrom), pos, encode_refalt(ref, sizeref, alt, sizealt));
510 }
511 
519 static inline void variantkey_range(uint8_t chrom, uint32_t pos_min, uint32_t pos_max, vkrange_t *range)
520 {
521  uint64_t c = ((uint64_t)chrom << VKSHIFT_CHROM);
522  range->min = (c | ((uint64_t)pos_min << VKSHIFT_POS));
523  range->max = (c | ((uint64_t)pos_max << VKSHIFT_POS) | VKMASK_REFALT);
524 }
525 
526 static inline int8_t compare_uint64_t(uint64_t a, uint64_t b)
527 {
528  return (a < b) ? -1 : (a > b);
529 }
530 
538 static inline int8_t compare_variantkey_chrom(uint64_t vka, uint64_t vkb)
539 {
540  return compare_uint64_t((vka >> VKSHIFT_CHROM), (vkb >> VKSHIFT_CHROM));
541 }
542 
550 static inline int8_t compare_variantkey_chrom_pos(uint64_t vka, uint64_t vkb)
551 {
552  return compare_uint64_t((vka >> VKSHIFT_POS), (vkb >> VKSHIFT_POS));
553 }
554 
570 static inline size_t variantkey_hex(uint64_t vk, char *str)
571 {
572  return hex_uint64_t(vk, str);
573 }
574 
581 static inline uint64_t parse_variantkey_hex(const char *vs)
582 {
583  return parse_hex_uint64_t(vs);
584 }
585 
586 #endif // VARIANTKEY_VARIANTKEY_H
static uint64_t parse_variantkey_hex(const char *vs)
Parses a VariantKey hexadecimal string and returns the code.
Definition: variantkey.h:581
static uint8_t extract_variantkey_chrom(uint64_t vk)
Extract the CHROM code from VariantKey.
Definition: variantkey.h:451
uint8_t chrom
Chromosome encoded number (only the LSB 5 bit are used)
Definition: variantkey.h:65
static uint32_t extract_variantkey_pos(uint64_t vk)
Extract the POS code from VariantKey.
Definition: variantkey.h:462
static uint64_t variantkey(const char *chrom, size_t sizechrom, uint32_t pos, const char *ref, size_t sizeref, const char *alt, size_t sizealt)
Definition: variantkey.h:507
struct variantkey_t variantkey_t
static uint32_t hash32(const char *str, size_t size)
Definition: variantkey.h:275
static uint64_t encode_variantkey(uint8_t chrom, uint32_t pos, uint32_t refalt)
Returns a 64 bit variant key based on the pre-encoded CHROM, POS (0-based) and REF+ALT.
Definition: variantkey.h:440
static char decode_base(uint32_t code, int bitpos)
Definition: variantkey.h:330
struct vkrange_t vkrange_t
static size_t decode_chrom(uint8_t code, char *chrom)
Decode the chromosome numerical code.
Definition: variantkey.h:145
static size_t decode_refalt(uint32_t code, char *ref, size_t *sizeref, char *alt, size_t *sizealt)
Decode the 32 bit REF+ALT code if reversible (if it has 11 or less bases in total and only contains A...
Definition: variantkey.h:423
static int8_t compare_variantkey_chrom_pos(uint64_t vka, uint64_t vkb)
Compares two VariantKeys by chromosome and position.
Definition: variantkey.h:550
static int encode_allele(uint32_t *h, uint8_t *bitpos, const char *str, size_t size)
Definition: variantkey.h:184
static uint32_t pack_chars(const char *str)
Definition: variantkey.h:263
#define VKMASK_REFALT
VariantKey binary mask for REF+ALT [ 00000000 00000000 00000000 00000000 01111111 11111111 11111111 1...
Definition: variantkey.h:54
uint64_t min
Minimum VariantKey value for any given REF+ALT encoding.
Definition: variantkey.h:75
uint32_t pos
Reference position, with the first base having position 0 (only the LSB 28 bit are used) ...
Definition: variantkey.h:66
Utility functions to manipulate strings.
#define MAXUINT32
Maximum value for uint32_t.
Definition: variantkey.h:57
static uint32_t extract_variantkey_refalt(uint64_t vk)
Extract the REF+ALT code from VariantKey.
Definition: variantkey.h:473
uint32_t refalt
Code for Reference and Alternate allele (only the LSB 31 bits are used)
Definition: variantkey.h:67
static size_t variantkey_hex(uint64_t vk, char *str)
Returns VariantKey hexadecimal string (16 characters).
Definition: variantkey.h:570
uint64_t max
Maximum VariantKey value for any given REF+ALT encoding.
Definition: variantkey.h:76
static void decode_variantkey(uint64_t code, variantkey_t *vk)
Decode a VariantKey code and returns the components as variantkey_t structure.
Definition: variantkey.h:483
static uint8_t encode_chrom(const char *chrom, size_t size)
Returns chromosome numerical encoding.
Definition: variantkey.h:86
static size_t decode_refalt_rev(uint32_t code, char *ref, size_t *sizeref, char *alt, size_t *sizealt)
Definition: variantkey.h:336
Definition: variantkey.h:73
#define VKSHIFT_POS
POS LSB position from the VariantKey LSB.
Definition: variantkey.h:56
static uint32_t muxhash(uint32_t k, uint32_t h)
Definition: variantkey.h:215
static int8_t compare_variantkey_chrom(uint64_t vka, uint64_t vkb)
Compares two VariantKeys by chromosome only.
Definition: variantkey.h:538
static uint32_t encode_refalt_rev(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
Definition: variantkey.h:200
#define VKMASK_POS
VariantKey binary mask for POS [ 00000111 11111111 11111111 11111111 10000000 00000000 00000000 00000...
Definition: variantkey.h:52
static uint32_t encode_refalt(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
Returns reference+alternate numerical encoding.
Definition: variantkey.h:317
#define VKSHIFT_CHROM
CHROM LSB position from the VariantKey LSB.
Definition: variantkey.h:55
static uint32_t pack_chars_tail(const char *str, size_t size)
Definition: variantkey.h:239
static uint64_t parse_hex_uint64_t(const char *s)
Parses a 16 chars hexadecimal string and returns the code.
Definition: hex.h:67
static uint32_t encode_refalt_hash(const char *ref, size_t sizeref, const char *alt, size_t sizealt)
Definition: variantkey.h:292
static void variantkey_range(uint8_t chrom, uint32_t pos_min, uint32_t pos_max, vkrange_t *range)
Returns minimum and maximum VariantKeys for range searches.
Definition: variantkey.h:519
Definition: variantkey.h:63
static uint32_t encode_base(const uint8_t c)
Definition: variantkey.h:159
#define VKMASK_CHROM
VariantKey binary mask for CHROM [ 11111000 00000000 00000000 00000000 00000000 00000000 00000000 000...
Definition: variantkey.h:51
static uint32_t encode_packchar(int c)
Definition: variantkey.h:225
static int8_t compare_uint64_t(uint64_t a, uint64_t b)
Definition: variantkey.h:526
static size_t hex_uint64_t(uint64_t n, char *str)
Returns uint64_t hexadecimal string (16 characters).
Definition: hex.h:56