-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibrary.cpp
217 lines (204 loc) · 4.74 KB
/
library.cpp
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
/* Copyright (c) 2013-2014 Y. William Yu. Released under CC0 1.0 Universal. */
/* library.cpp */
#include "global.h"
std::string time_now( const char* format )
{
std::time_t t = std::time(0) ;
char cstr[128] ;
std::strftime( cstr, sizeof(cstr), format, std::localtime(&t) ) ;
return cstr ;
}
void int64checker()
{
if (CHAR_BIT!=8) {
std::cerr << "Error: CHAR_BIT !=8." << std::endl;
std::cerr << "This implementation assumes 8 bits per byte." << std::endl;
std::cerr << "Your system is not compatibile. Please run on a standard x86-64 machine." << std::endl;
exit(-8);
}
if ((sizeof(unsigned long) * CHAR_BIT )!=64) {
std::cerr << "Error: sizeof(unsigned long) != 64." << std::endl;
std::cerr << "This implementation assumes 64 bit machines." << std::endl;
std::cerr << "Your system is not compatibile. Please run on a standard x86-64 machine." << std::endl;
exit(-64);
}
}
std::vector<readseq> encode_read_vector(const char *bases)
{
std::vector<readseq> anslist;
anslist.reserve(100);
readseq ans=0;
int i=0;
while ((*bases))
{
switch (*bases++) {
case 'A': case 'a': // store as 00
ans >>= 2;
break;
case 'C': case 'c': // store as 01
ans = (ans>>2)+(1ul<<62);
break;
case 'G': case 'g': // store as 10
ans = (ans>>2)+(2ul<<62);
break;
case 'T': case 't': // store as 11
ans = (ans>>2)+(3ul<<62);
break;
case '\n': case '\t': case 'N':
return anslist;
default: // if improper input, return empty
std::vector<readseq> temp;
return temp;
}
if (++i>=32)
anslist.push_back(ans);
}
return anslist;
}
std::deque<readseq> encode_read(const char *bases)
{
std::deque<readseq> anslist;
readseq ans=0;
int i=0;
while ((*bases))
{
switch (*bases++) {
case 'A': case 'a': // store as 00
ans >>= 2;
break;
case 'C': case 'c': // store as 01
ans = (ans>>2)+(1ul<<62);
break;
case 'G': case 'g': // store as 10
ans = (ans>>2)+(2ul<<62);
break;
case 'T': case 't': // store as 11
ans = (ans>>2)+(3ul<<62);
break;
case '\n': case '\t':
return anslist;
default: // if improper input, return empty
std::deque<readseq> temp;
return temp;
}
if (++i>=32)
anslist.push_back(ans);
}
return anslist;
}
std::string decode_read(readseq e)
{
char a[33];
a[32]=0;
for (int i=31; i>=0; --i)
{
switch ((e & (3ul<<62))>>62) {
case 0:
a[i]='A';
break;
case 1:
a[i]='C';
break;
case 2:
a[i]='G';
break;
case 3:
a[i]='T';
break;
}
e <<= 2;
}
std::string ans = a;
return ans;
}
// This function *only* works on unsigned 64-bit integers!!!
readseq rev_compl(const readseq orig)
{
static uint16_t table[65536] = {};
static bool initialised = false;
if (!initialised) {
initialised = true;
uint16_t x;
uint16_t y;
for (unsigned int i = 0; i<65536; i++) {
x = 0;
y = i;
for (unsigned int j=0; j<8; j++) {
x<<=2;
switch ( y&3) {
case 0:
x+=3;
break;
case 1:
x+=2;
break;
case 2:
x+=1;
break;
case 3:
break;
}
y>>=2;
}
table[i]=x;
//printf (" 0x%X ,", x);
//if (i%16 == 15) printf("\n");
}
}
readseq ans;
uint16_t *c_orig = (uint16_t*)(&orig);
uint16_t *c_ans = (uint16_t*)(&ans);
for (unsigned int i=0; i<4; ++i) {
c_ans[i] = table[c_orig[3-i]];
}
return ans;
}
// Return negative number of differences if more than one diff
// Return position of difference if exactly one difference (0-indexed)
// Return -1 if two sequences are identical
int subst_find(const readseq a, const readseq b)
{
static unsigned char table[65536] = {};
static bool initialised = false;
if (!initialised) {
initialised = true;
table[0]=0;
for (unsigned int i = 0; i<65536; i++) {
table[i]= (i & 1) + table[i / 2];
}
}
// Don't need all this logic if the two are identical
if (a == b)
return -1;
//printf("%lx\n%lx\n", a, b);
readseq c;
readseq d;
c = a ^ b; // XOR to find the differences
d = c >> 1; // SHIFT a copy by 1
//printf("c=a^b:\t%lx\n d=c>>1: %lx\n", c, d);
c |= d; // OR the two together
//printf("c |= d: %lx\n", c);
//printf("mask: %lx\n", 0x5555555555555555);
c &= 0x5555555555555555; // Mask out every other bit
//printf("masked c: %lx\n", c); fflush(stdout);
int count;
uint16_t *c_16 = (uint16_t*)(&c);
count = table[c_16[0]] + table[c_16[1]] + table[c_16[2]] + table[c_16[3]];
//printf("count: %i\n", count);
if (count>1)
return (-count);
else if (count<1)
return -1;
else {
// Find location of set bit if exactly 1 difference
for (unsigned int i = 0; i<32; ++i) {
if (c&1)
return (i);
else
c >>= 2;
}
// Control should NEVER get here.
std::cerr << "Internal sanity check failed in function subst_find" << std::endl;
exit(-10);
}
}