obidmscolumn_qual.c 4.9 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 29


/**********************************************************************
 *
 * 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
 *
 **********************************************************************/

int obi_column_set_obiqual_char_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, const char* value)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
{
	uint8_t* int_value;
	int      int_value_length;
	int 	 i;
	int		 ret_value;

	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++)
		int_value[i] = ((uint8_t)(value[i])) - QUALITY_ASCII_BASE;

	ret_value = obi_column_set_obiqual_int_with_elt_idx(column, line_nb, element_idx, int_value, int_value_length);

	free(int_value);

	return ret_value;
48 49 50
}


51
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)
52
{
53 54
	index_t idx;
	char*   new_indexer_name;
55

56 57
	if (obi_column_prepare_to_set_value(column, line_nb) < 0)
		return -1;
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	// Add the value in the indexer
	idx = obi_index_uint8(column->indexer, value, value_length);
	if (idx == -1)	// An error occurred
	{
		if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR)
		{
			// 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?
			// Add the value in the new indexer
			idx = obi_index_uint8(column->indexer, value, value_length);
			if (idx == -1)
				return -1;
		}
		else
			return -1;
	}
	// Add the value's index in the column
	*(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = idx;
80 81 82 83 84 85

	return 0;
}


char* obi_column_get_obiqual_char_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx)
86 87 88 89 90
{
	char* 		   value;
	const uint8_t* int_value;
	int			   int_value_length;
	int			   i;
91

92 93 94 95 96 97 98 99 100
	int_value = obi_column_get_obiqual_int_with_elt_idx(column, line_nb, element_idx, &int_value_length);

	value = (char*) malloc((int_value_length + 1) * sizeof(char));

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

	value[i] = '\0';
101 102 103 104 105

	return value;
}


106
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)
107 108 109 110 111 112 113 114 115 116 117 118
{
	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;

119
	return obi_retrieve_uint8(column->indexer, idx, value_length);
120 121 122 123 124 125 126 127 128 129 130 131 132
}


int obi_column_set_obiqual_char_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, const char* value)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;

	return obi_column_set_obiqual_char_with_elt_idx(column, line_nb, element_idx, value);
}


133
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)
134 135 136 137 138
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;

139
	return obi_column_set_obiqual_int_with_elt_idx(column, line_nb, element_idx, value, value_length);
140 141 142 143 144 145 146 147 148 149 150 151 152
}


char* obi_column_get_obiqual_char_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIQual_char_NA;

	return obi_column_get_obiqual_char_with_elt_idx(column, line_nb, element_idx);
}


153
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)
154 155 156 157 158
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIQual_int_NA;

159
	return obi_column_get_obiqual_int_with_elt_idx(column, line_nb, element_idx, value_length);
160 161
}