-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEncode.c
137 lines (109 loc) · 2.65 KB
/
Encode.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
//order d c b a
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 'T': e+=shiftop*1;
break;
case 'G': e+=shiftop*2;
break;
case 'C': 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;
}
char getBase(int val){
char ch = '$';
switch(val){
case 0: ch= 'A';
break;
case 1: ch= 'T';
break;
case 2: ch= 'G';
break;
case 3: ch= 'C';
break;
}
return ch;
}
char* getDNAstring(Pair p,int sa,int sb,int sc,int sd){
char* str = (char*)malloc(sizeof(char)*150);
sprintf(str," ");
unsigned long k = 3;
for(int i=0;i<64 && i<sd;i+=2){
int d = (p.d>>i)& k;
sprintf(str,"%c%s",getBase(d),str);
}
for(int i=0;i<64&& i<sc;i+=2){
int d = (p.c>>i)& k;
sprintf(str,"%c%s",getBase(d),str);
}
for(int i=0;i<64 && i<sb;i+=2){
int d = (p.b>>i)& k;
sprintf(str,"%c%s",getBase(d),str);
}
for(int i=0;i<64 && i <sa;i+=2){
int d = (p.a>>i)& k;
sprintf(str,"%c%s",getBase(d),str);
}
return str;
}
//mod power of two as bitwise and
// num % 4 = num & 000011 //get last two bit
int main(){
char buff[100]="CT";
Pair p = genPair(buff);
printf("%lu %lu %lu %lu\n",p.a,p.b,p.c,p.d);
p.c = 1;
int k = strlen(buff)*2 - 2;
unsigned long end = 3;
unsigned long start = end<<62;
//extract last val from privous and add at begining
// same as below expr
unsigned long a1 = (p.d>>2) | ( (p.c & end)<<k) ;
printf("%lu %lu %lu\n",start,end,a1);
unsigned long lim = 1;
unsigned long i = 3;
unsigned long d1 = (p.d<<2)| i;
printf("%lu\n",d1);
d1 = d1 & ( (lim<< (strlen(buff)*2))-1);
printf("%lu\n",d1);
// Pair t; t.d=65; t.c=0;t.b=0;t.a=0;
// printf("%s\n",getDNAstring(p,0,0,0,4));
return 0;
}