encode.c 7.06 KB
 Eric Coissac committed Nov 23, 2012 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 ``````/* * encode.c * * Created on: 11 juil. 2012 * Author: coissac */ #include #include #include "orgasm.h" #include "_sse.h" //#include "debug.h" /** * Round an unsigned 32 bits int x to the minimum * multiple of 16 greater or equal to x */ uint32_t round16(uint32_t x) { // This just check the special case where // x is an exact multiple of 16 if ((x & 0xF) == 0) return x; return (x & 0xFFFFFFF0) + 0x10; } /** * Round an unsigned 32 bits int x to the minimum * multiple of 8 greater or equal to x */ uint32_t round8(uint32_t x) { // This just check the special case where // x is an exact multiple of 16 if ((x & 0x7) == 0) return x; return (x & 0xFFFFFFF8) + 0x8; } /** * Round an unsigned 32 bits int x to the minimum * multiple of 4 greater or equal to x */ uint32_t round4(uint32_t x) { // This just check the special case where // x is an exact multiple of 4 if ((x & 0x3) == 0) return x; return (x & 0xFFFFFFFC) + 0x4; } /** * Round an unsigned 32 bits int x to the minimum * power of 2 greater or equal to x * */ uint32_t round2(uint32_t x) { uint32_t p; // This just check the special case where // x is an exact power of 2 if (!(x & (x-1))) return x; `````` Eric Coissac committed Feb 10, 2014 83 ``````/* `````` Eric Coissac committed Nov 23, 2012 84 85 86 87 88 89 90 91 ``````#ifdef __intel__ // on Intel platform, the bsr instruction return the index // of the first bit set to 1 on the right side of the word asm("bsr %%eax, %%ebx;" : "=b" (p) : "a" (x)); p++; #else `````` Eric Coissac committed Feb 10, 2014 92 ``````*/ `````` Eric Coissac committed Nov 23, 2012 93 94 `````` p=0; while(x) x>>=1,p++; `````` Eric Coissac committed Feb 10, 2014 95 ``````/*#endif*/ `````` Eric Coissac committed Nov 23, 2012 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 `````` return (1 << p); } /* * Encode a four nucleotides string in a byte * based on a 2 bits code per nucleotide * Just A, C, G and T nucleotides can be encoded * UIPAC code return undetermined code. * the encoding is not sensible to the case. * 065 101 41 01000001 A 067 103 43 01000011 C 071 107 47 01000111 G 084 124 54 01010100 T 097 141 61 01100001 a 099 143 63 01100011 c 103 147 67 01100111 g 116 164 74 01110100 t ** The two bits in the binary ASCII code pointed out by the two stars can be selected to encode the nucleotide quickly on to bytes codes independently of upper or lower case: a : 00 -> 0 c : 01 -> 1 t : 10 -> 2 g : 11 -> 3 */ uint8_t encode4nuc(const char* seq) { uint32_t out; out=*((uint32_t*)seq); #ifdef LITTLE_END out = out >> 24 | \ out << 24 | \ ((out >> 8) & 0x0000FF00) | \ ((out << 8) & 0x00FF0000); #endif out>>=1; out&=0x03030303; out= (out | (out >> 6)) & 0x0F0F0F0F; out= (out | (out >> 12)) & 0x000000FF; return (uint8_t)out; } /** * MACRO define for computing binary equivalence operator * * eq | 0 | 1 | * -----+---+---| * 0 | 1 | 0 | * -----+---+---| * 1 | 0 | 1 | * -----+---+---| */ #define EQ(x,y) ((x) & (y)) | ((~(x)) & (~(y))) /** * MACRO based on EQ macro testing equivalence of the * for bytes constituting a 32bits word with four time * the same 8bits word. */ #define EQNUC(x,n) EQ(out,((n) | ((n) << 8) | ((n) << 16) | ((n) << 24))) /** * Check if the four next nucleotides pointed by * the seq pointer are all A, C, G or T. * * The function return a value equal to zero if * at least one of the four is not an A, G, G or T */ uint8_t is4ACGT(const char* seq) { // FIXME: there is a logical bug in this function // if the nucleotides are equal the eq operator // return 0xFF but not 0 if they are different uint32_t out; out=*((uint32_t*)seq); out&=~(0xDFDFDFDF); out = EQNUC(out,'A') | \ EQNUC(out,'C') | \ EQNUC(out,'G') | \ EQNUC(out,'T'); return out==0xFFFFFFFF; } uint8_t is16ACGT(const char* seq) { um128 out; // Load the 16 nucleotides out.i = _MM_LOAD_SI128((const __m128i*)seq); //Switch tu Uppercase out.i = _MM_AND_SI128(out.i,_MM_SET1_EPI8(0xDF)); // Compare with A, C, G, T and 0 out.i = _MM_OR_SI128( _MM_OR_SI128( _MM_OR_SI128( _MM_OR_SI128(_MM_CMPEQ_EPI8(out.i,_MM_SET1_EPI8('A')), _MM_CMPEQ_EPI8(out.i,_MM_SET1_EPI8('C'))), _MM_CMPEQ_EPI8(out.i,_MM_SET1_EPI8('G'))), _MM_CMPEQ_EPI8(out.i,_MM_SET1_EPI8('T'))), _MM_CMPEQ_EPI8(out.i,_MM_SETZERO_SI128())); // Return true if the 16 nucleotides are in [A,C,G,T,0] return _MM_MOVEMASK_EPI8(out.i) == 0xFFFF; } /** * encode 16 nucleotide in parallel using SSE instructions. * the seq pointer has to be 16 bytes aligned. * The function use the same two bits code than the encode4nuc * function encoded above. * */ uint32_t encode16nuc(const char* seq) { um128 out; uint32_t result; out.i = _MM_LOAD_SI128((const __m128i*)seq); DEBUG("%16s",out.c); DEBUG("%:vX",out.i); DEBUG("%:08vlX",out.i); #ifdef LITTLE_END out.i = _MM_OR_SI128( \ _MM_OR_SI128( \ _MM_OR_SI128(_MM_SRLI_EPI32(out.i,24), \ _MM_SLLI_EPI32(out.i,24)), \ _MM_AND_SI128(_MM_SRLI_EPI32(out.i,8),_MM_SET1_EPI32(0x0000FF00))), \ _MM_AND_SI128(_MM_SLLI_EPI32(out.i,8),_MM_SET1_EPI32(0x00FF0000))); #endif DEBUG("%16s",out.c); DEBUG("%:vX",out.i); DEBUG("%:08vlX",out.i); /* * Equivalent to : * * out>>=1; * out&=0x03030303; */ out.i = _MM_AND_SI128(_MM_SRLI_EPI32(out.i,1),_MM_SET1_EPI8(3)); DEBUG("%:vX",out.i); DEBUG("%:08vlX",out.i); /* * Equivalent to : * * out= (out | (out >> 6)) & 0x0F0F0F0F; */ out.i = _MM_AND_SI128(_MM_OR_SI128(_MM_SRLI_EPI32(out.i,6),out.i),_MM_SET1_EPI8(0xF)); DEBUG("%:vX",out.i); DEBUG("%:08vlX",out.i); /* * Equivalent to : * * out= (out | (out >> 12)) & 0x000000FF; */ out.i = _MM_AND_SI128(_MM_OR_SI128(_MM_SRLI_EPI32(out.i,12),out.i),_MM_SET1_EPI32(0xFF)); DEBUG("%:vX",out.i); DEBUG("%:08vlX",out.i); #ifdef LITTLE_END result = ((uint32_t) out.u8[12] << 24) | \ ((uint32_t) out.u8[ 8] << 16) | \ ((uint32_t) out.u8[ 4] << 8) | \ ((uint32_t) out.u8[ 0]); #else result = ((uint32_t) out.u8[ 0] << 24) | \ ((uint32_t) out.u8[ 4] << 16) | \ ((uint32_t) out.u8[ 8] << 8) | \ ((uint32_t) out.u8[12]); #endif return result; } /* * Encode a full sequence in binary format using the two bits code * define in the comment of the encode4nuc function * */ pnuc encodeSequence(pnuc dest, char* src, uint32_t length) { static char buffer[512]; char read[512]; char *pread; uint32_t recordLength; uint32_t* encodedseqs; char* bufferend; char *index; pread = (char*) PTR16(read); bzero(read,512); strncpy(pread,src,length); if (dest == NULL) dest = (pnuc) PTR16(buffer); ASSERT(((size_t)dest & 0xF)==0,"Pointer %p is not 16bytes aligned",src) encodedseqs = (uint32_t*)dest; recordLength = round16(length); bufferend = pread + recordLength; if (checkACGT(pread,recordLength)) { for (index=pread; index < bufferend; index+=16, encodedseqs++) *encodedseqs=encode16nuc(index); } else return NULL; return dest; }``````