-
Notifications
You must be signed in to change notification settings - Fork 2
/
bxhash.h
115 lines (101 loc) · 3.45 KB
/
bxhash.h
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
/* The MIT License
Copyright (c) 2017 Genome Research Ltd.
Author: Petr Danecek <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef __BXHASH_H__
#define __BXHASH_H__
#include <stdio.h>
#include <stdint.h>
#include <htslib/khash.h>
#include "bxcheck.h"
KHASH_MAP_INIT_INT(int2int, int)
typedef khash_t(int2int) bxhash_t;
bxhash_t *bxhash_init(char *fname);
void bxhash_destroy(bxhash_t *hash);
int bxhash_size(bxhash_t *hash);
static inline uint8_t char2nt(char nt)
{
if ( nt=='A' || nt=='a' ) return 0;
if ( nt=='C' || nt=='c' ) return 1;
if ( nt=='G' || nt=='g' ) return 2;
if ( nt=='T' || nt=='t' ) return 3;
return 4;
}
static inline uint8_t char2nt_c(char nt)
{
if ( nt=='A' || nt=='a' ) return 3;
if ( nt=='C' || nt=='c' ) return 2;
if ( nt=='G' || nt=='g' ) return 1;
if ( nt=='T' || nt=='t' ) return 0;
return 4;
}
static inline uint32_t bxhash_seq2key(char *seq)
{
uint32_t key = 0, i;
for (i=0; i<16; i++)
{
int base = char2nt(seq[i]);
if ( base>3 ) return -1;
key |= base << (i*2);
}
return key;
}
static inline void bxhash_key2seq(uint32_t num, char *seq)
{
int i;
for (i=0; i<16; i++) seq[i] = "ACGT"[ (num >> (i*2)) & 0x3 ];
seq[16] = 0;
}
static inline int bxhash_has_key(bxhash_t *hash, uint32_t key)
{
khint_t k = kh_get(int2int, hash, key);
return k == kh_end(hash) ? 0 : k + 1;
}
static inline int _bxhash_has_seq(bxhash_t *hash, char *seq, int complement, int reverse)
{
uint32_t key = 0;
int i;
for (i=0; i<16; i++)
{
int base = reverse ? seq[-i] : seq[i];
base = complement ? char2nt_c(base) : char2nt(base);
if ( base>3 ) return 0;
key |= base << (i*2);
}
return bxhash_has_key(hash, key);
}
#define bxhash_has_seq(hash,seq) _bxhash_has_seq(hash,seq,0,0)
#define bxhash_has_seq_c(hash,seq) _bxhash_has_seq(hash,seq,1,0)
#define bxhash_has_seq_r(hash,seq) _bxhash_has_seq(hash,seq,0,1)
#define bxhash_has_seq_cr(hash,seq) _bxhash_has_seq(hash,seq,1,1)
static inline int bxhash_put_key(bxhash_t *hash, uint32_t key)
{
int ret;
khint_t k = kh_put(int2int, hash, key, &ret);
if ( ret < 0 ) return -1;
if ( ret==0 ) kh_val(hash, k)++;
else kh_val(hash, k) = 1;
return 0;
}
static inline int bxhash_put_seq(bxhash_t *hash, char *seq)
{
uint32_t key = bxhash_seq2key(seq);
if ( key < 0 ) return key;
return bxhash_put_key(hash, key);
}
#endif