Commit 87044b41 authored by Celine Mercier's avatar Celine Mercier

modified the encoding function on 2 bits a little

parent 6ab1c833
......@@ -53,51 +53,52 @@ bool only_ATGC(char* seq)
}
byte_t* encode_seq_on_2_bits(char* seq, int32_t length) // TODO shift = 2
byte_t* encode_seq_on_2_bits(char* seq, int32_t length)
{
byte_t* seq_b;
uint8_t shift;
uint8_t modulo;
int32_t length_b;
int32_t i;
// fprintf(stderr, "\n>>>>>>>>>>>>>>>>>>Encoding sequence %s", seq);
length_b = ceil((double) length / (double) 4.0);
// fprintf(stderr, "\nLength: %d", length_b);
seq_b = (byte_t*) malloc(length_b * sizeof(byte_t));
// Initialize all the bits to 0
memset(seq_b, 0, length_b);
for (i=0; i<length; i++)
{
shift = 6 - 2*(i%4);
// fprintf(stderr, "\nshift: %u", shift);
// Shift of 2 to make place for new nucleotide
seq_b[i/4] <<= 2;
// Add new nucleotide
switch (seq[i])
{
case 'a':
case 'A':
seq_b[i/4] |= NUC_A << shift;
seq_b[i/4] |= NUC_A;
// fprintf(stderr, "\nIn byte %d, writing A:", i/4);
// print_bits(seq_b, length_b);
break;
case 'c':
case 'C':
seq_b[i/4] |= NUC_C << shift;
seq_b[i/4] |= NUC_C;
// fprintf(stderr, "\nIn byte %d, writing C:", i/4);
// print_bits(seq_b, length_b);
break;
case 'g':
case 'G':
seq_b[i/4] |= NUC_G << shift;
seq_b[i/4] |= NUC_G;
// fprintf(stderr, "\nIn byte %d, writing G:", i/4);
// print_bits(seq_b, length_b);
break;
case 't':
case 'T':
seq_b[i/4] |= NUC_T << shift;
seq_b[i/4] |= NUC_T;
// fprintf(stderr, "\nIn byte %d, writing T:", i/4);
// print_bits(seq_b, length_b);
break;
......@@ -107,6 +108,11 @@ byte_t* encode_seq_on_2_bits(char* seq, int32_t length) // TODO shift = 2
}
}
// Final shift for the last byte if needed
modulo = (length % 4);
if (modulo)
seq_b[(i-1)/4] <<= (2*(4 - modulo));
// fprintf(stderr, "\n>>>>>>>>>Encoded:");
// print_bits(seq_b, length_b);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment