-
Notifications
You must be signed in to change notification settings - Fork 1
/
KmerCo.h
191 lines (178 loc) · 4.38 KB
/
KmerCo.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
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
//#include "mask/mask5.h"
//#include "mask/mask6.h"
//#include "mask/mask7.h"
#include "mask/mask8.h"
//#include "mask/mask9.h"
//#include "mask/mask10.h"
//#include "mask/mask12.h"
//#include "mask/mask14.h"
//#include "mask/mask16.h"
#include "murmur.h"
//#include "initBF.h"
static int x;
static int y;
static unsigned long int nc=8; //total number of counters
static unsigned long int bc=8; // total number bits per counters
int seed[8]={7689571,15485863,104395303,49979693,67867967,141650939,870889,899939};
void dim(int p, int q)
{
x=p;
y=q;
}
unsigned long int **allocate()
{
int i,j,k;
unsigned long int **a=(unsigned long int **)malloc(x*sizeof(unsigned long int *));
if(a==NULL)
{
printf("Unable to allocate!\n");
return NULL;
}
for(i=0;i<x;i++)
{
a[i]=(unsigned long int *)malloc(y*sizeof(unsigned long int));
if(a[i]==NULL)
{
printf("Unable to allocate!\n");
return NULL;
}
}
for(i=0;i<x;i++)
for(j=0;j<y;j++)
a[i][j]=0;
printf("\nAllocated and Initilized 2DBF Successfully...\n");
return a;
}
//Insertion function of KmerCo with only K-mer as input
void _set_(unsigned long int **a,int kmer_len,char *kmer, int k)
{
unsigned long int h;
int i,j,pos,loop;
unsigned long int c;
for(loop=0;loop<k;loop++){
h=murmur2(kmer,kmer_len,seed[loop]);
i=h%x;
j=h%y;
pos=h%nc;
c=a[i][j]&em[pos];
c=c>>(bc*(unsigned long int)pos);
c=c+1UL;
if(c==0xFF)
return;
c=c<<(bc*pos);
a[i][j]=a[i][j]&rm[pos];
a[i][j]=a[i][j] | c;
}
}
//if( (c==0x1F && bc==5) || (c==0xFF && bc==8) || (c==0x3F && bc==6) || (c==0x7F && bc==7) || (c==0x1FF && bc==9)|| (c==0x3FF && bc==10) || (c==0xFFF && bc==12) || (c==0x3FFF && bc==14) || (c==0xFFFF && bc==16) ) //Write the condition of current experiment at the beginning for quicker time
//Query function of KmerCo with only K-mer as input
unsigned long int _test_(unsigned long int **a,int kmer_len,char *kmer, int k)
{
unsigned long int h;
int i,j,pos,loop;
unsigned long int c,count[k],min;
for(loop=0;loop<k;loop++){
h=murmur2(kmer,kmer_len,seed[loop]);
i=h%x;
j=h%y;
pos=h%nc;
c=a[i][j]&em[pos];
count[loop]=c>>(bc*(unsigned long int)pos);
if(count[loop]==0)
return 0;
}
switch(k){
case 1:
return count[0];
case 2:
if(count[0]<count[1])
return count[0];
else
return count[1];
default:
min=count[0];
for(loop=1;loop<k;loop++){
if (min>count[loop])
min=count[loop];
}
return min;
}
}
//Insertion function of KmerCo with K-mer and reverse complement of K-mer as input
int _set_canonical_(unsigned long int **a,int kmer_len,char *kmer, char *rev_kmer,int k) //h(kmer)<h(rev_kmer)?return 0: return 1
{
unsigned long int h,h1;
int i,j,pos,loop,result=0;
unsigned long int c;
h=murmur2(kmer,kmer_len,seed[0]);
h1=murmur2(rev_kmer,kmer_len,seed[0]);
if (h1<h){
h=h1;
result=1;
kmer=rev_kmer;
}
for(loop=0;loop<k;loop++){
i=h%x;
j=h%y;
pos=h%nc;
c=a[i][j]&em[pos];
c=c>>(bc*(unsigned long int)pos);
c=c+1UL;
if(c==0xFF)
return result;
c=c<<(bc*pos);
a[i][j]=a[i][j]&rm[pos];
a[i][j]=a[i][j] | c;
if (k>1)
h=murmur2(kmer,kmer_len,seed[loop+1]);
}
return result;
}
//if( (c==0x1F && bc==5) || (c==0xFF && bc==8) || (c==0x3F && bc==6) || (c==0x7F && bc==7) || (c==0x1FF && bc==9)|| (c==0x3FF && bc==10)|| (c==0xFFF && bc==12) || (c==0x3FFF && bc==14) || (c==0xFFFF && bc==16)) //Write the condition of current experiment at the beginning for quicker time
//Query function of KmerCo with K-mer and reverse complement of K-mer as input
unsigned long int _test_canonical_(unsigned long int **a,int kmer_len,char *kmer, char *rev_kmer, int k, int *result)
{
unsigned long int h,h1;
int i,j,pos,loop;
unsigned long int c,count[k],min;
*result=0;
h=murmur2(kmer,kmer_len,seed[0]);
h1=murmur2(rev_kmer,kmer_len,seed[0]);
if (h1<h){
h=h1;
*result=1;
kmer=rev_kmer;
}
for(loop=0;loop<k;loop++){
i=h%x;
j=h%y;
pos=h%nc;
c=a[i][j]&em[pos];
count[loop]=c>>(bc*(unsigned long int)pos);
if(count[loop]==0)
return 0;
if (k>1)
h=murmur2(kmer,kmer_len,seed[loop+1]);
}
switch(k){
case 1:
return count[0];
case 2:
if(count[0]<count[1])
return count[0];
else
return count[1];
default:
min=count[0];
for(loop=1;loop<k;loop++){
if (min>count[loop])
min=count[loop];
}
return min;
}
}
void _free_(unsigned long int **a)
{
free(a);
printf("\nMemory freed successfully...\n");
}