encode.c 7.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/****************************************************************************
 * Encoding functions                                                       *
 ****************************************************************************/

/**
 * @file encode.c
 * @author Celine Mercier
 * @date November 18th 2015
 * @brief Functions encoding DNA sequences.
 */


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>

#include "encode.h"
19 20
#include "obierrno.h"
#include "obitypes.h"	// For byte_t type
21 22 23 24 25 26 27 28 29 30
#include "obidebug.h"


#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)


// TODO: endianness problem?



31
bool only_ATGC(const char* seq)
32
{
33
	const char* c = seq;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

	while (*c)
	{
		if (!((*c == 'A') || \
			  (*c == 'T') || \
			  (*c == 'G') || \
			  (*c == 'C') || \
			  (*c == 'a') || \
			  (*c == 't') || \
			  (*c == 'g') || \
			  (*c == 'c')))
		{
			return 0;
		}
		else
		{
			c++;
		}
	}
	return 1;
}


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
bool only_IUPAC_DNA(const char* seq)
{
	const char* c = seq;

	while (*c)
	{
		if (!((*c == 'A') || \
			  (*c == 'T') || \
			  (*c == 'G') || \
			  (*c == 'C') || \
			  (*c == 'U') || \
			  (*c == 'R') || \
			  (*c == 'Y') || \
			  (*c == 'S') || \
			  (*c == 'W') || \
			  (*c == 'K') || \
			  (*c == 'M') || \
			  (*c == 'B') || \
			  (*c == 'D') || \
			  (*c == 'H') || \
			  (*c == 'V') || \
			  (*c == 'N') || \
			  (*c == 'a') || \
			  (*c == 't') || \
			  (*c == 'g') || \
			  (*c == 'c') || \
			  (*c == 'u') || \
			  (*c == 'r') || \
			  (*c == 'y') || \
			  (*c == 's') || \
			  (*c == 'w') || \
			  (*c == 'k') || \
			  (*c == 'm') || \
			  (*c == 'b') || \
			  (*c == 'd') || \
			  (*c == 'h') || \
			  (*c == 'v') || \
			  (*c == 'n') || \
			  (*c == '.') || \
			  (*c == '-')))
		{
			return 0;
		}
		else
		{
			c++;
		}
	}
	return 1;
}


bool is_a_DNA_seq(const char* seq)
{
	if (only_ATGC(seq))
		return true;
	return only_IUPAC_DNA(seq);
}


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
byte_t get_nucleotide_from_encoded_seq(byte_t* seq, int32_t idx, uint8_t encoding)
{
	uint8_t shift;
	uint8_t mask;
	byte_t  nuc;

	if (encoding == 2)
	{
		shift = 6 - 2*(idx % 4);
		mask = NUC_MASK_2B << shift;
		nuc = (seq[idx/4] & mask) >> shift;
	}
	else if (encoding == 4)
	{
		shift = 4 - 4*(idx % 2);
		mask = NUC_MASK_4B << shift;
		nuc = (seq[idx/2] & mask) >> shift;
	}
	else
	{
		obi_set_errno(OBI_DECODE_ERROR);
		obidebug(1, "\nInvalid encoding base: must be on 2 bits or 4 bits");
		return -1;
	}

	return nuc;
}


Celine Mercier committed
146
byte_t* encode_seq_on_2_bits(const char* seq, int32_t length)
147 148
{
	byte_t*  seq_b;
149
	uint8_t  modulo;
150
	int32_t  length_b;
151
	int32_t  i;
152 153 154

	length_b = ceil((double) length / (double) 4.0);

155
	seq_b = (byte_t*) calloc(length_b, sizeof(byte_t));
156 157
	if (seq_b == NULL)
	{
158
		obi_set_errno(OBI_MALLOC_ERROR);
159 160 161
		obidebug(1, "\nError allocating memory for an encoded DNA sequence");
		return NULL;
	}
162 163 164

	for (i=0; i<length; i++)
	{
165 166
		// Shift of 2 to make place for new nucleotide
		seq_b[i/4] <<= 2;
167

168
		// Add new nucleotide
169 170 171 172
		switch (seq[i])
		{
		case 'a':
		case 'A':
173
			seq_b[i/4] |= NUC_A_2b;
174 175 176
			break;
		case 'c':
		case 'C':
177
			seq_b[i/4] |= NUC_C_2b;
178 179 180
			break;
		case 'g':
		case 'G':
181
			seq_b[i/4] |= NUC_G_2b;
182 183 184
			break;
		case 't':
		case 'T':
185
			seq_b[i/4] |= NUC_T_2b;
186 187
			break;
		default:
188
			obi_set_errno(OBI_ENCODE_ERROR);
189 190 191 192 193
			obidebug(1, "\nInvalid nucleotide base when encoding (not [atgcATGC])");
			return NULL;
		}
	}

194 195 196 197 198
	// Final shift for the last byte if needed
	modulo = (length % 4);
	if (modulo)
		seq_b[(i-1)/4] <<= (2*(4 - modulo));

199 200 201 202 203 204 205
	return seq_b;
}


char* decode_seq_on_2_bits(byte_t* seq_b, int32_t length_seq)
{
	char*   seq;
206
	int32_t i;
207 208 209 210 211
	uint8_t shift;
	uint8_t mask;
	uint8_t nuc;

	seq = (char*) malloc((length_seq+1) * sizeof(char));
212 213
	if (seq == NULL)
	{
214
		obi_set_errno(OBI_MALLOC_ERROR);
215 216 217
		obidebug(1, "\nError allocating memory for a decoded DNA sequence");
		return NULL;
	}
218 219 220 221

	for (i=0; i<length_seq; i++)
	{
		shift = 6 - 2*(i % 4);
222
		mask = NUC_MASK_2B << shift;
223 224 225 226
		nuc = (seq_b[i/4] & mask) >> shift;

		switch (nuc)
		{
227
		case NUC_A_2b:
228 229
			seq[i] = 'a';
			break;
230
		case NUC_C_2b:
231 232
			seq[i] = 'c';
			break;
233
		case NUC_G_2b:
234 235
			seq[i] = 'g';
			break;
236
		case NUC_T_2b:
237 238 239
			seq[i] = 't';
			break;
		default:
240
			obi_set_errno(OBI_DECODE_ERROR);
241 242 243 244 245 246 247 248 249 250 251
			obidebug(1, "\nInvalid nucleotide base when decoding");
			return NULL;
		}
	}

	seq[length_seq] = '\0';

	return seq;
}


Celine Mercier committed
252
byte_t* encode_seq_on_4_bits(const char* seq, int32_t length)
253 254 255 256 257 258 259 260
{
	byte_t*  seq_b;
	uint8_t  modulo;
	int32_t  length_b;
	int32_t  i;

	length_b = ceil((double) length / (double) 2.0);

261
	seq_b = (byte_t*) calloc(length_b, sizeof(byte_t));
262 263
	if (seq_b == NULL)
	{
264
		obi_set_errno(OBI_MALLOC_ERROR);
265 266 267
		obidebug(1, "\nError allocating memory for an encoded DNA sequence");
		return NULL;
	}
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

	for (i=0; i<length; i++)
	{
		// Shift of 4 to make place for new nucleotide
		seq_b[i/2] <<= 4;

		// Add new nucleotide
		switch (seq[i])
		{
		case 'a':
		case 'A':
			seq_b[i/2] |= NUC_A_4b;
			break;
		case 'c':
		case 'C':
			seq_b[i/2] |= NUC_C_4b;
			break;
		case 'g':
		case 'G':
			seq_b[i/2] |= NUC_G_4b;
			break;
		case 't':
		case 'T':
			seq_b[i/2] |= NUC_T_4b;
			break;
		case 'r':
		case 'R':
			seq_b[i/2] |= NUC_R_4b;
			break;
		case 'y':
		case 'Y':
			seq_b[i/2] |= NUC_Y_4b;
			break;
		case 's':
		case 'S':
			seq_b[i/2] |= NUC_S_4b;
			break;
		case 'w':
		case 'W':
			seq_b[i/2] |= NUC_W_4b;
			break;
		case 'k':
		case 'K':
			seq_b[i/2] |= NUC_K_4b;
			break;
		case 'm':
		case 'M':
			seq_b[i/2] |= NUC_M_4b;
			break;
		case 'b':
		case 'B':
			seq_b[i/2] |= NUC_B_4b;
			break;
		case 'd':
		case 'D':
			seq_b[i/2] |= NUC_D_4b;
			break;
		case 'h':
		case 'H':
			seq_b[i/2] |= NUC_H_4b;
			break;
		case 'v':
		case 'V':
			seq_b[i/2] |= NUC_V_4b;
			break;
		case 'n':
		case 'N':
			seq_b[i/2] |= NUC_N_4b;
			break;
		default:
338
			obi_set_errno(OBI_ENCODE_ERROR);
339
			obidebug(1, "\nInvalid nucleotide base when encoding (not IUPAC), sequence: %s", seq);
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
			return NULL;
		}
	}

	// Final shift for the last byte if needed
	modulo = (length % 2);
	if (modulo)
		seq_b[(i-1)/2] <<= (4*modulo);

	return seq_b;
}


char* decode_seq_on_4_bits(byte_t* seq_b, int32_t length_seq)
{
	char*   seq;
	int32_t i;
	uint8_t shift;
	uint8_t mask;
	uint8_t nuc;

	seq = (char*) malloc((length_seq+1) * sizeof(char));
362 363
	if (seq == NULL)
	{
364
		obi_set_errno(OBI_MALLOC_ERROR);
365 366 367
		obidebug(1, "\nError allocating memory for a decoded DNA sequence");
		return NULL;
	}
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

	for (i=0; i<length_seq; i++)
	{
		shift = 4 - 4*(i % 2);
		mask = NUC_MASK_4B << shift;
		nuc = (seq_b[i/2] & mask) >> shift;

		switch (nuc)
		{
		case NUC_A_4b:
			seq[i] = 'a';
			break;
		case NUC_C_4b:
			seq[i] = 'c';
			break;
		case NUC_G_4b:
			seq[i] = 'g';
			break;
		case NUC_T_4b:
			seq[i] = 't';
			break;
		case NUC_R_4b:
			seq[i] = 'r';
			break;
		case NUC_Y_4b:
			seq[i] = 'y';
			break;
		case NUC_S_4b:
			seq[i] = 's';
			break;
		case NUC_W_4b:
			seq[i] = 'w';
			break;
		case NUC_K_4b:
			seq[i] = 'k';
			break;
		case NUC_M_4b:
			seq[i] = 'm';
			break;
		case NUC_B_4b:
			seq[i] = 'b';
			break;
		case NUC_D_4b:
			seq[i] = 'd';
			break;
		case NUC_H_4b:
			seq[i] = 'h';
			break;
		case NUC_V_4b:
			seq[i] = 'v';
			break;
		case NUC_N_4b:
			seq[i] = 'n';
			break;
		default:
423
			obi_set_errno(OBI_DECODE_ERROR);
424 425 426 427 428 429 430 431 432 433 434
			obidebug(1, "\nInvalid nucleotide base when decoding");
			return NULL;
		}
	}

	seq[length_seq] = '\0';

	return seq;
}


435 436
///////////////////// FOR DEBUGGING ///////////////////////////
//NOTE: The first byte is printed the first (at the left-most).
437
// TODO Move to utils
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

void print_bits(void* ptr, int32_t size)
{
	uint8_t* b = (uint8_t*) ptr;
	uint8_t byte;
    int32_t i, j;

    fprintf(stderr, "\n");
    for (i=0;i<size;i++)
    {
        for (j=7;j>=0;j--)
        {
            byte = b[i] & (1<<j);
            byte >>= j;
            fprintf(stderr, "%u", byte);
        }
        fprintf(stderr, " ");
    }
    fprintf(stderr, "\n");
}