Branch to refactor and debug (AVLs bugged)
This commit is contained in:
133
src/encode.c
133
src/encode.c
@ -64,6 +64,12 @@ byte_t* encode_seq_on_2_bits(char* seq, int32_t length)
|
||||
length_b = ceil((double) length / (double) 4.0);
|
||||
|
||||
seq_b = (byte_t*) malloc(length_b * sizeof(byte_t));
|
||||
if (seq_b == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obidebug(1, "\nError allocating memory for an encoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Initialize all the bits to 0
|
||||
memset(seq_b, 0, length_b);
|
||||
@ -93,6 +99,7 @@ byte_t* encode_seq_on_2_bits(char* seq, int32_t length)
|
||||
seq_b[i/4] |= NUC_T_2b;
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_ENCODE_ERROR); // TODO
|
||||
obidebug(1, "\nInvalid nucleotide base when encoding (not [atgcATGC])");
|
||||
return NULL;
|
||||
}
|
||||
@ -116,6 +123,12 @@ char* decode_seq_on_2_bits(byte_t* seq_b, int32_t length_seq)
|
||||
uint8_t nuc;
|
||||
|
||||
seq = (char*) malloc((length_seq+1) * sizeof(char));
|
||||
if (seq == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obidebug(1, "\nError allocating memory for a decoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i=0; i<length_seq; i++)
|
||||
{
|
||||
@ -138,6 +151,7 @@ char* decode_seq_on_2_bits(byte_t* seq_b, int32_t length_seq)
|
||||
seq[i] = 't';
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_DECODE_ERROR); // TODO
|
||||
obidebug(1, "\nInvalid nucleotide base when decoding");
|
||||
return NULL;
|
||||
}
|
||||
@ -159,6 +173,12 @@ byte_t* encode_seq_on_4_bits(char* seq, int32_t length)
|
||||
length_b = ceil((double) length / (double) 2.0);
|
||||
|
||||
seq_b = (byte_t*) malloc(length_b * sizeof(byte_t));
|
||||
if (seq_b == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obidebug(1, "\nError allocating memory for an encoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Initialize all the bits to 0
|
||||
memset(seq_b, 0, length_b);
|
||||
@ -232,6 +252,7 @@ byte_t* encode_seq_on_4_bits(char* seq, int32_t length)
|
||||
seq_b[i/2] |= NUC_N_4b;
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_ENCODE_ERROR); // TODO
|
||||
obidebug(1, "\nInvalid nucleotide base when encoding (not IUPAC)");
|
||||
return NULL;
|
||||
}
|
||||
@ -255,6 +276,12 @@ char* decode_seq_on_4_bits(byte_t* seq_b, int32_t length_seq)
|
||||
uint8_t nuc;
|
||||
|
||||
seq = (char*) malloc((length_seq+1) * sizeof(char));
|
||||
if (seq == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obidebug(1, "\nError allocating memory for a decoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i=0; i<length_seq; i++)
|
||||
{
|
||||
@ -310,6 +337,7 @@ char* decode_seq_on_4_bits(byte_t* seq_b, int32_t length_seq)
|
||||
seq[i] = 'n';
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_DECODE_ERROR); // TODO
|
||||
obidebug(1, "\nInvalid nucleotide base when decoding");
|
||||
return NULL;
|
||||
}
|
||||
@ -321,6 +349,111 @@ char* decode_seq_on_4_bits(byte_t* seq_b, int32_t length_seq)
|
||||
}
|
||||
|
||||
|
||||
Obi_byte_array_p obi_byte_array(byte_t* encoded_value, uint8_t element_size, int32_t length_encoded_value, int32_t length_decoded_value)
|
||||
{
|
||||
Obi_byte_array_p byte_array;
|
||||
|
||||
// Allocate the memory for the byte array structure
|
||||
byte_array = (Obi_byte_array_p) malloc(sizeof(Obi_byte_array_t) + length_encoded_value);
|
||||
if (byte_array == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obidebug(1, "\nError allocating memory for a byte array");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Store the number of bits on which each element is encoded
|
||||
byte_array->element_size = element_size;
|
||||
|
||||
// Store the length (in bytes) of the encoded value
|
||||
byte_array->length_encoded_value = length_encoded_value;
|
||||
|
||||
// Store the initial length (in bytes) of the decoded value
|
||||
byte_array->length_decoded_value = length_decoded_value;
|
||||
|
||||
// Store the encoded value
|
||||
memcpy(byte_array->value, encoded_value, length_encoded_value);
|
||||
|
||||
return byte_array;
|
||||
}
|
||||
|
||||
|
||||
Obi_byte_array_p obi_str_to_obibytes(char* value)
|
||||
{
|
||||
Obi_byte_array_p value_b;
|
||||
int32_t length;
|
||||
|
||||
// Compute the number of bytes on which the value will be encoded
|
||||
length = strlen(value) + 1; // +1 to store \0 at the end (makes retrieving faster)
|
||||
|
||||
value_b = obi_byte_array(value, ELEMENT_SIZE_STR, length, length);
|
||||
if (value_b == NULL)
|
||||
{
|
||||
obidebug(1, "\nError encoding a character string in a byte array");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return value_b;
|
||||
}
|
||||
|
||||
|
||||
const char* obi_obibytes_to_str(Obi_byte_array_p value_b)
|
||||
{
|
||||
fprintf(stderr, "\n%s", value_b->value);
|
||||
return value_b->value;
|
||||
}
|
||||
|
||||
|
||||
Obi_byte_array_p obi_seq_to_obibytes(char* seq)
|
||||
{
|
||||
Obi_byte_array_p value_b;
|
||||
int32_t length_encoded_seq; // length of the encoded sequence in bytes
|
||||
int32_t seq_length;
|
||||
byte_t* encoded_seq;
|
||||
|
||||
seq_length = strlen(seq);
|
||||
|
||||
// Check if just ATGC and encode accordingly
|
||||
if (only_ATGC(seq))
|
||||
{
|
||||
// Compute the length (in bytes) of the encoded sequence
|
||||
length_encoded_seq = ceil((double) seq_length / (double) 4.0);
|
||||
// Encode
|
||||
encoded_seq = encode_seq_on_2_bits(seq, seq_length);
|
||||
if (encoded_seq == NULL)
|
||||
return NULL;
|
||||
value_b = obi_byte_array(encoded_seq, ELEMENT_SIZE_SEQ_2, length_encoded_seq, seq_length);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Compute the length (in bytes) of the encoded sequence
|
||||
length_encoded_seq = ceil((double) seq_length / (double) 2.0);
|
||||
// Encode
|
||||
encoded_seq = encode_seq_on_4_bits(seq, seq_length);
|
||||
if (encoded_seq == NULL)
|
||||
return NULL;
|
||||
value_b = obi_byte_array(encoded_seq, ELEMENT_SIZE_SEQ_4, length_encoded_seq, seq_length);
|
||||
}
|
||||
|
||||
free(encoded_seq);
|
||||
|
||||
return value_b;
|
||||
}
|
||||
|
||||
|
||||
const char* obi_obibytes_to_seq(Obi_byte_array_p value_b)
|
||||
{
|
||||
// Decode
|
||||
if (value_b->element_size == 2)
|
||||
return decode_seq_on_2_bits(value_b->value, value_b->length_decoded_value);
|
||||
else
|
||||
return decode_seq_on_4_bits(value_b->value, value_b->length_decoded_value);
|
||||
}
|
||||
|
||||
|
||||
// TODO same for int
|
||||
|
||||
|
||||
///////////////////// FOR DEBUGGING ///////////////////////////
|
||||
//NOTE: The first byte is printed the first (at the left-most).
|
||||
|
||||
|
Reference in New Issue
Block a user