obidmscolumn_qual.c 5.53 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 * OBIDMS_column_qual functions                                             *
 ****************************************************************************/

/**
 * @file obidsmcolumn_qual.c
 * @author Celine Mercier
 * @date May 4th 2016
9
 * @brief Functions handling OBIColumns containing data in the form of indices referring to sequence qualities.
10 11 12 13 14 15 16
 */


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

17
#include "obidmscolumn_qual.h"
18 19
#include "obidmscolumn.h"
#include "obitypes.h"
20
#include "uint8_indexer.h"
21 22 23 24 25 26 27 28


/**********************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P U B L I C   F U N C T I O N S
 *
 **********************************************************************/

29
int obi_column_set_obiqual_char_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, const char* value, int offset)
30 31 32 33 34 35
{
	uint8_t* int_value;
	int      int_value_length;
	int 	 i;
	int		 ret_value;

36 37 38
	if (offset == -1)
		offset = QUALITY_ASCII_BASE;

Celine Mercier committed
39
	// Check NA value
40 41 42 43 44 45 46 47 48 49 50
	if (value == OBIQual_char_NA)
	{
		ret_value = obi_column_set_obiqual_int_with_elt_idx(column, line_nb, element_idx, OBIQual_int_NA, 0);
	}
	else
	{
		int_value_length = strlen(value);
		int_value = (uint8_t*) malloc(int_value_length * sizeof(uint8_t));

		// Convert in uint8_t array to index in that format
		for (i=0; i<int_value_length; i++)
51
			int_value[i] = ((uint8_t)(value[i])) - offset;
52 53 54
		ret_value = obi_column_set_obiqual_int_with_elt_idx(column, line_nb, element_idx, int_value, int_value_length);
		free(int_value);
	}
55 56

	return ret_value;
57 58 59
}


60
int obi_column_set_obiqual_int_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, const uint8_t* value, int value_length)
61
{
62 63
	index_t idx;
	char*   new_indexer_name;
64

65
	if (obi_column_prepare_to_set_value(column, line_nb, element_idx) < 0)
66
		return -1;
67

68
	if (value == OBIQual_int_NA)
69
	{
70 71 72 73 74 75 76
		idx = OBIIdx_NA;
	}
	else
	{
		// Add the value in the indexer
		idx = obi_index_uint8(column->indexer, value, value_length);
		if (idx == -1)	// An error occurred
77
		{
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
			if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR)
			{
				// TODO PUT IN A COLUMN FUNCTION
				// If the error is that the indexer is read-only, clone it
				new_indexer_name = obi_build_indexer_name((column->header)->name, (column->header)->version);
				if (new_indexer_name == NULL)
					return -1;
				column->indexer = obi_clone_indexer(column->indexer, new_indexer_name);	// TODO Need to lock this somehow?
				strcpy((column->header)->indexer_name, new_indexer_name);
				free(new_indexer_name);
				obi_set_errno(0);

				// Add the value in the new indexer
				idx = obi_index_uint8(column->indexer, value, value_length);
				if (idx == -1)
					return -1;
			}
			else
96 97 98
				return -1;
		}
	}
99

100 101
	// Add the value's index in the column
	*(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = idx;
102 103 104 105 106

	return 0;
}


107
char* obi_column_get_obiqual_char_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, int offset)
108 109 110 111 112
{
	char* 		   value;
	const uint8_t* int_value;
	int			   int_value_length;
	int			   i;
113

114 115 116
	if (offset == -1)
		offset = QUALITY_ASCII_BASE;

117 118
	int_value = obi_column_get_obiqual_int_with_elt_idx(column, line_nb, element_idx, &int_value_length);

119 120 121
	// Check NA
	if (int_value == OBIQual_int_NA)
		return OBIQual_char_NA;
122

123 124 125 126
	value = (char*) malloc((int_value_length + 1) * sizeof(char));

	// Encode int quality to char quality
	for (i=0; i<int_value_length; i++)
127
		value[i] = (char)(int_value[i] + offset);
128 129

	value[i] = '\0';
130 131 132 133 134

	return value;
}


135
const uint8_t* obi_column_get_obiqual_int_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, int* value_length)
136 137 138 139 140 141 142 143 144 145 146 147
{
	index_t idx;

	if (obi_column_prepare_to_get_value(column, line_nb) < 0)
		return OBIQual_int_NA;

	idx = *(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);

	// Check NA
	if (idx == OBIIdx_NA)
		return OBIQual_int_NA;

148
	return obi_retrieve_uint8(column->indexer, idx, value_length);
149 150 151
}


152
int obi_column_set_obiqual_char_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, const char* value, int offset)
153 154 155 156 157
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;

158
	return obi_column_set_obiqual_char_with_elt_idx(column, line_nb, element_idx, value, offset);
159 160 161
}


162
int obi_column_set_obiqual_int_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, const uint8_t* value, int value_length)
163 164 165 166 167
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;

168
	return obi_column_set_obiqual_int_with_elt_idx(column, line_nb, element_idx, value, value_length);
169 170 171
}


172
char* obi_column_get_obiqual_char_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, int offset)
173 174 175 176 177
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIQual_char_NA;

178
	return obi_column_get_obiqual_char_with_elt_idx(column, line_nb, element_idx, offset);
179 180 181
}


182
const uint8_t* obi_column_get_obiqual_int_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, int* value_length)
183 184 185 186 187
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIQual_int_NA;

188
	return obi_column_get_obiqual_int_with_elt_idx(column, line_nb, element_idx, value_length);
189 190
}