-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest1.c
323 lines (265 loc) · 7.9 KB
/
test1.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <limits.h>
// map gnome seq to graph
// read k-mer from file
/*
mapping
A -> 0;
C -> 1;
G -> 2;
T -> 3;
*/
// we are representing kmer as as a pair of 4 unsigned long
// at max there can be 128 base pairs in kmer
typedef struct Pair_{
unsigned long a;
unsigned long b;
unsigned long c;
unsigned long d;
} Pair;
Pair genPair(char* kmer){
Pair ans;
ans.a =0;ans.b=0;
ans.c=0;ans.d=0;
unsigned long e;
int count = 0,i=0;
int index = strlen(kmer) -1;
//printf("%d\n",index);
unsigned long shiftop = 1;
//converting rightmost part of kmer into long and storing in d
for(i=0;i<4 && index>=0 ;i++){
shiftop =1;
count=0;
e=0;
for(;count<32 && index>=0;index--){
switch(kmer[index]){
case 'A': e+=shiftop*0;
break;
case 'C': e+=shiftop*1;
break;
case 'G': e+=shiftop*2;
break;
case 'T': e+=shiftop*3;
break;
}
shiftop=shiftop<<2; //multiply by 4
// printf("%lu \n",shiftop);
count++;
}
switch(i){
case 0: ans.d = e;break;
case 1: ans.c = e;break;
case 2: ans.b = e;break;
case 3: ans.a = e;break;
}
}
return ans;
}
void buildGraph(unsigned long* Node, unsigned int* Edge, unsigned int N,int SA,int SB,int SC,int SD){
//unsigned int idx = blockDim.x * blockIdx.x + threadIdx.x;
for(unsigned int idx=0;idx<N;idx++){
unsigned int k = 4*idx;
unsigned long a = Node[k];
unsigned long b = Node[k+1];
unsigned long c = Node[k+2];
unsigned long d = Node[k+3];
unsigned int e = 8*idx;
unsigned long end = 3;
unsigned long start = end<<62;
//at begining
for(unsigned long i=0;i<4;i++){
//add last base of C to first of D
//atleast size of 1 base 2 bits
unsigned long a1,b1,c1,d1;
a1=b1=c1=d1=0;
if(SD>1 && SC>1)
d1 = (d>>2) | ( (c & end)<<(SD-2));
if(SC>1 && SB>1)
c1= (c>>2) | ( (b & end)<<(SC-2));
if(SB>1 && SA>1)
b1 = (b>>2) | ( (a & end)<<(SB-2));
//Add here diff bases
if(SA>1)
a1 = (a>>2) | (i<<(SA-2));
else if(SB>1)
b1 = (b>>2) | (i<<(SB-2));
else if(SC>1)
c1 = (c>>2) | (i<<(SC-2));
else if(SD>1)
d1 = (d>>2) | (i<<(SD-2));
//search for a1,b1,c1,d1
//printf("idx:%u\t = a1:%lu\tb1:%lu\tc1:%lu\td1:%lu\n",idx+1 ,a1,b1,c1,d1);
int m,l,r;
l=0;
r=(int)N-1;
while ( l <= r)
{
m = l + (r-l)/2;
// Check if x is present at mid
if (Node[4*m] == a1 && Node[4*m+1] == b1 && Node[4*m+2] == c1 && Node[4*m+3] == d1 ){
Edge[8*idx+i]=m;
//if(idx==0||idx==1||idx==2)printf("%u->%u \n",idx+1,m +1);
break;
}
/*
// all are less go to left
if (Node[4*m] > a1 && Node[4*m+1] > b1 && Node[4*m+2] > c1 && Node[4*m+3] > d1){
r = m-1;
}
// any one is greater
else{
l = m + 1;
}
*/
else if(Node[4*m] >a1) r = m-1;
else if(Node[4*m] <a1) l = m +1;
else{
if(Node[4*m+1] >b1) r=m-1;
else if(Node[4*m+1] <b1) l = m +1;
else{
if(Node[4*m+2] >c1) r=m-1;
else if(Node[4*m+2] <c1) l = m +1;
else{
if(Node[4*m+3] >d1) r=m-1;
else l = m +1;
//all possible case is already taken care.
}
}
}
}
}
if(idx==8)printf("%lu\n",d);
//at end
for(unsigned long i=0;i<4;i++){
unsigned long a1,b1,c1,d1;
a1=b1=c1=d1=0;
unsigned long lim = 1;
if(SA>1)
a1 = (a<<2) | ( (b & start)>>62 );
if(SB>1)
b1 = (b<<2) | ( (c & start)>>62 );
if(SC>1)
c1 = (c<<2) | ( (d & start)>>62 );
if(SD>1)
d1 = (d<<2)| i ;
//removing additional bits if not full limit
if(SB>1 && SA<64)
a1 = a1 & ((lim<<SA)-1);
if(SC>1 && SB<64)
b1 = b1 & ((lim<<SB)-1);
if(SD>1 && SC<64)
c1 = c1 & ((lim<<SC)-1);
if(SD<64)
d1 = d1 & ((lim<<SD)-1);
//search now
//if(idx==0||idx==1||idx==2) printf("idx:%u\t = a1:%lu\tb1:%lu\tc1:%lu\td1:%lu\n",idx+1 ,a1,b1,c1,d1);
int m,l,r;
l=0;
r=(int)N-1;
if(idx==8) printf("idx:%u\t = a1:%lu\tb1:%lu\tc1:%lu\td1:%lu\n",idx+1 ,a1,b1,c1,d1);
while ( l <= r)
{
m = l + (r-l)/2;
if(idx==8)printf(" %d ",m);
// Check if x is present at mid
if (Node[4*m] == a1 && Node[4*m+1] == b1 && Node[4*m+2] == c1 && Node[4*m+3] == d1 ){
Edge[8*idx+4+i]=m;
//if(idx==0||idx==1||idx==2)printf("%u->%u \n",idx+1,m+1 );
if(idx==8) printf("found\n");
break;
}
/*
// all are less go to left
if (Node[4*m] > a1 && Node[4*m+1] > b1 && Node[4*m+2] > c1 && Node[4*m+3] > d1){
r = m-1;
}
// any one is greater
else{
l = m + 1;
}
*/
else if(Node[4*m] >a1) r = m-1;
else if(Node[4*m] <a1) l = m +1;
else{
if(Node[4*m+1] >b1) r=m-1;
else if(Node[4*m+1] <b1) l = m +1;
else{
if(Node[4*m+2] >c1) r=m-1;
else if(Node[4*m+2] <c1) l = m +1;
else{
if(Node[4*m+3] >d1) r=m-1;
else l = m +1;
//all possible case is already taken care.
}
}
}
}
printf("\n");
}
}
}
int main( ){
int K=5;
FILE* fp = fopen("pra1","r");
if(fp==NULL){
printf("couldn't open file inp ");
return 1;
}
int N=10;
// Array containing Nodes each 4 digit represents one seq
unsigned long* Node = (unsigned long*)malloc(sizeof(unsigned long)*N*4);
// Array contain edges for each node as 8 per Node
unsigned int* Edge = (unsigned int*) malloc(sizeof(unsigned int)*N*8);
memset(Edge,UINT_MAX,sizeof(Edge[0])*N*8);
printf("%u %u %u\n",Edge[0],Edge[1],Edge[4] );
char buffer[100];
unsigned int Nindex = 0;
while(fscanf(fp,"%s\n",buffer)!=EOF){
if(strlen(buffer)==K){
Pair p = genPair(buffer);
//printf(" %s: a = %lu\t b = %lu\t c = %lu\t d = %lu\n" ,buffer,p.a,p.b,p.c,p.d);
Node[Nindex] = p.a;
Node[Nindex+1]=p.b;
Node[Nindex+2]=p.c;
Node[Nindex+3]=p.d;
Nindex+=4;
}else{
// handel out of order strings
}
}
int A=0,B=0,C=0,D=0,l;
l=2*K;
for(int i=0;i<4 && l>0 ;i++){
int m=0;
if(l<=64) {m=l;l=0;}
else{
m=64;
l=l-64;
}
switch(i){
case 0: D=m;break;
case 1: C=m;break;
case 2: B=m;break;
case 3: A=m;break;
}
}
printf("%d %d %d %d\n",A,B,C,D );
//Builing graph
buildGraph(Node,Edge,N,A,B,C,D);
//Check the Graph
unsigned int i;
//for(i=0;i<N;i++){
//printf("FrontEdges(%u):%u\t%u\t%u\t%u\n",i+1,Edge[8*i],Edge[8*i+1],Edge[8*i+2],Edge[8*i+3]);
//printf("BackEdges(%u):%u\t%u\t%u\t%u\n",i+1,Edge[8*i+4],Edge[8*i+5],Edge[8*i+6],Edge[8*i+7]);
//printf("Edges(%u): %u\t %u\n",i,E);
//printf("\n");
// }
printf("%lu",Node[6*4+3]);
//https://www.geeksforgeeks.org/hierholzers-algorithm-directed-graph/
fclose(fp);
return 0;
}