obidmscolumn_seq.c 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/****************************************************************************
 * OBIDMS_column_seq functions                                              *
 ****************************************************************************/

/**
 * @file obidsmcolumn_seq.c
 * @author Celine Mercier
 * @date November 18th 2015
 * @brief Functions handling OBIColumns containing data in the form of indices referring to DNA sequences.
 */


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

#include "obidmscolumn.h"
#include "obitypes.h"
Celine Mercier's avatar
Celine Mercier committed
18
#include "dna_seq_indexer.h"
19 20 21 22 23 24 25 26


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

27
int obi_column_set_obiseq_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, const char* value)
28
{
29
	index_t idx;
30

31 32
	if (obi_column_prepare_to_set_value(column, line_nb) < 0)
		return -1;
33

34
	// Add the value in the indexer
Celine Mercier's avatar
Celine Mercier committed
35
	idx = obi_index_dna_seq(column->indexer, value);
36 37 38 39
	if (idx == -1)
		return -1;

	// Add the value's index in the column
Celine Mercier's avatar
Celine Mercier committed
40
	*(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = idx;
41 42 43 44 45

	return 0;
}


46
char* obi_column_get_obiseq_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx)
47
{
Celine Mercier's avatar
Celine Mercier committed
48
	index_t idx;
49

50
	if (obi_column_prepare_to_get_value(column, line_nb) < 0)
Celine Mercier's avatar
Celine Mercier committed
51
		return OBISeq_NA;
52

Celine Mercier's avatar
Celine Mercier committed
53
	idx = *(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
54 55 56

	// Check NA
	if (idx == OBIIdx_NA)
Celine Mercier's avatar
Celine Mercier committed
57
		return OBISeq_NA;
58

Celine Mercier's avatar
Celine Mercier committed
59
	return obi_retrieve_dna_seq(column->indexer, idx);
60 61 62
}


63
int obi_column_set_obiseq_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, const char* value)
64
{
65
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
66 67
	if (element_idx == OBIIdx_NA)
		return -1;
Celine Mercier's avatar
Celine Mercier committed
68

69
	return obi_column_set_obiseq_with_elt_idx(column, line_nb, element_idx, value);
70 71 72
}


73
char* obi_column_get_obiseq_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name)
74
{
75
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
76
	if (element_idx == OBIIdx_NA)
Celine Mercier's avatar
Celine Mercier committed
77 78
		return OBISeq_NA;

79
	return obi_column_get_obiseq_with_elt_idx(column, line_nb, element_idx);
Celine Mercier's avatar
Celine Mercier committed
80 81
}