From 2a1ea3ba3fd7ba5b9b39026980cfe3af09137349 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Thu, 9 Jun 2016 14:22:36 +0200 Subject: [PATCH] Setting NA values is now handled properly for OBI_SEQ, OBI_STR and OBI_QUAL columns --- .../obitools3/obidms/_obidmscolumn_qual.pyx | 18 ++--- python/obitools3/obidms/_obidmscolumn_seq.pyx | 9 +-- python/obitools3/obidms/_obidmscolumn_str.pyx | 9 +-- python/obitools3/obidms/_obiseq.pxd | 40 +++++----- python/obitools3/obidms/_obiseq.pyx | 26 +++---- src/obidmscolumn_qual.c | 75 +++++++++++-------- src/obidmscolumn_seq.c | 46 +++++++----- src/obidmscolumn_str.c | 45 ++++++----- 8 files changed, 143 insertions(+), 125 deletions(-) diff --git a/python/obitools3/obidms/_obidmscolumn_qual.pyx b/python/obitools3/obidms/_obidmscolumn_qual.pyx index 90d4762..c5c9a8e 100644 --- a/python/obitools3/obidms/_obidmscolumn_qual.pyx +++ b/python/obitools3/obidms/_obidmscolumn_qual.pyx @@ -58,26 +58,24 @@ cdef class OBIDMS_column_qual(OBIDMS_column): cdef uint8_t* value_b cdef int value_length if value is None : - value_b = OBIQual_int_NA - value_length = 0 + if obi_column_set_obiqual_int_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, OBIQual_int_NA, 0) < 0: + raise Exception("Problem setting a value in a column") else : value_length = len(value) value_b = malloc(value_length * sizeof(uint8_t)) for i in range(value_length) : value_b[i] = value[i] - if obi_column_set_obiqual_int_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, value_b, value_length) < 0: - raise Exception("Problem setting a value in a column") - if value is not None : + if obi_column_set_obiqual_int_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, value_b, value_length) < 0: + raise Exception("Problem setting a value in a column") free(value_b) cpdef set_str_line(self, index_t line_nb, object value): - cdef bytes value_b if value is None : - value_b = OBIQual_char_NA + if obi_column_set_obiqual_int_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, OBIQual_int_NA, 0) < 0: + raise Exception("Problem setting a value in a column") else : - value_b = str2bytes(value) - if obi_column_set_obiqual_char_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, value_b) < 0: - raise Exception("Problem setting a value in a column") + if obi_column_set_obiqual_char_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, str2bytes(value)) < 0: + raise Exception("Problem setting a value in a column") # TODO diff --git a/python/obitools3/obidms/_obidmscolumn_seq.pyx b/python/obitools3/obidms/_obidmscolumn_seq.pyx index 6f50269..9bea8d3 100644 --- a/python/obitools3/obidms/_obidmscolumn_seq.pyx +++ b/python/obitools3/obidms/_obidmscolumn_seq.pyx @@ -31,13 +31,12 @@ cdef class OBIDMS_column_seq(OBIDMS_column): return result cpdef set_line(self, index_t line_nb, object value): - cdef bytes value_b if value is None : - value_b = OBISeq_NA + if obi_column_set_obiseq_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, OBISeq_NA) < 0: + raise Exception("Problem setting a value in a column") else : - value_b = str2bytes(value) - if obi_column_set_obiseq_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, value_b) < 0: - raise Exception("Problem setting a value in a column") + if obi_column_set_obiseq_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, str2bytes(value)) < 0: + raise Exception("Problem setting a value in a column") # TODO choose alignment type (lcs or other) with supplementary argument cpdef align(self, diff --git a/python/obitools3/obidms/_obidmscolumn_str.pyx b/python/obitools3/obidms/_obidmscolumn_str.pyx index 0678916..2382ca9 100644 --- a/python/obitools3/obidms/_obidmscolumn_str.pyx +++ b/python/obitools3/obidms/_obidmscolumn_str.pyx @@ -26,13 +26,12 @@ cdef class OBIDMS_column_str(OBIDMS_column): return result cpdef set_line(self, index_t line_nb, object value): - cdef bytes value_b if value is None : - value_b = OBIStr_NA + if obi_column_set_obistr_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, OBIStr_NA) < 0: + raise Exception("Problem setting a value in a column") else : - value_b = str2bytes(value) - if obi_column_set_obistr_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, value_b) < 0: - raise Exception("Problem setting a value in a column") + if obi_column_set_obistr_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, str2bytes(value)) < 0: + raise Exception("Problem setting a value in a column") cdef class OBIDMS_column_multi_elts_str(OBIDMS_column_multi_elts): diff --git a/python/obitools3/obidms/_obiseq.pxd b/python/obitools3/obidms/_obiseq.pxd index 8ba239a..4c79b1d 100644 --- a/python/obitools3/obidms/_obiseq.pxd +++ b/python/obitools3/obidms/_obiseq.pxd @@ -4,35 +4,35 @@ from ._obidms cimport OBIView_line cdef class OBI_Seq(dict) : - cdef str id - cdef str definition - cdef str sequence + cdef object id + cdef object definition + cdef object sequence - cpdef set_id(self, str id) - cpdef get_id(self) - cpdef set_definition(self, str definition) - cpdef get_definition(self) - cpdef get_sequence(self) + cpdef set_id(self, object id) + cpdef object get_id(self) + cpdef set_definition(self, object definition) + cpdef object get_definition(self) + cpdef object get_sequence(self) cdef class OBI_Nuc_Seq(OBI_Seq) : cdef object quality - #cpdef str reverse_complement(self) - cpdef set_sequence(self, str sequence) + #cpdef object reverse_complement(self) + cpdef set_sequence(self, object sequence) cpdef set_quality(self, object quality) - cpdef get_quality(self) + cpdef object get_quality(self) cdef class OBI_Nuc_Seq_Stored(OBIView_line) : - cpdef set_id(self, str id) - cpdef get_id(self) - cpdef set_definition(self, str definition) - cpdef get_definition(self) - cpdef set_sequence(self, str sequence) - cpdef get_sequence(self) + cpdef set_id(self, object id) + cpdef object get_id(self) + cpdef set_definition(self, object definition) + cpdef object get_definition(self) + cpdef set_sequence(self, object sequence) + cpdef object get_sequence(self) cpdef set_quality(self, object quality) - cpdef get_quality(self) - cpdef get_str_quality(self) + cpdef object get_quality(self) + cpdef object get_str_quality(self) - # cpdef str reverse_complement(self) + # cpdef object reverse_complement(self) diff --git a/python/obitools3/obidms/_obiseq.pyx b/python/obitools3/obidms/_obiseq.pyx index 8a09d4c..d649ebb 100644 --- a/python/obitools3/obidms/_obiseq.pyx +++ b/python/obitools3/obidms/_obiseq.pyx @@ -9,20 +9,20 @@ from .capi.obiview cimport NUC_SEQUENCE_COLUMN, \ cdef class OBI_Seq(dict) : - def __init__(self, str id, str seq, str definition=None) : + def __init__(self, object id, object seq, object definition=None) : self.set_id(id) self.set_sequence(seq) if definition is not None : self.set_definition(definition) - cpdef set_id(self, str id) : + cpdef set_id(self, object id) : self.id = id self[bytes2str(ID_COLUMN)] = id cpdef get_id(self) : return self.id - cpdef set_definition(self, str definition) : + cpdef set_definition(self, object definition) : self.definition = definition self[bytes2str(DEFINITION_COLUMN)] = definition @@ -38,7 +38,7 @@ cdef class OBI_Seq(dict) : cdef class OBI_Nuc_Seq(OBI_Seq) : - cpdef set_sequence(self, str sequence) : + cpdef set_sequence(self, object sequence) : self.sequence = sequence self[bytes2str(NUC_SEQUENCE_COLUMN)] = sequence @@ -57,34 +57,34 @@ cdef class OBI_Nuc_Seq_Stored(OBIView_line) : # TODO store the str version of column name macros - cpdef set_id(self, str id) : + cpdef set_id(self, object id) : self[bytes2str(ID_COLUMN)] = id - cpdef get_id(self) : + cpdef object get_id(self) : return self[bytes2str(ID_COLUMN)] - cpdef set_definition(self, str definition) : + cpdef set_definition(self, object definition) : self[bytes2str(DEFINITION_COLUMN)] = definition - cpdef get_definition(self) : + cpdef object get_definition(self) : return self[bytes2str(DEFINITION_COLUMN)] - cpdef set_sequence(self, str sequence) : + cpdef set_sequence(self, object sequence) : self[bytes2str(NUC_SEQUENCE_COLUMN)] = sequence - cpdef get_sequence(self) : + cpdef object get_sequence(self) : return self[bytes2str(NUC_SEQUENCE_COLUMN)] cpdef set_quality(self, object quality) : - if type(quality) == list : + if (type(quality) == list) or (quality is None) : self[bytes2str(QUALITY_COLUMN)] = quality else : # Quality is in str form (((self.view).columns)[bytes2str(QUALITY_COLUMN)]).set_str_line(self.index, quality) - cpdef get_quality(self) : + cpdef object get_quality(self) : return self[bytes2str(QUALITY_COLUMN)] - cpdef get_str_quality(self) : + cpdef object get_str_quality(self) : return ((self.view).columns)[bytes2str(QUALITY_COLUMN)].get_str_line(self.index) # def __str__(self) : diff --git a/src/obidmscolumn_qual.c b/src/obidmscolumn_qual.c index 3559524..876061a 100644 --- a/src/obidmscolumn_qual.c +++ b/src/obidmscolumn_qual.c @@ -33,18 +33,21 @@ int obi_column_set_obiqual_char_with_elt_idx(OBIDMS_column_p column, index_t lin int i; int ret_value; - // TODO NA + 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)); - 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; iindexer, value, value_length); - if (idx == -1) // An error occurred + if (value == OBIQual_int_NA) { - if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR) + 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 { - // 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); + 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) + // Add the value in the new indexer + idx = obi_index_uint8(column->indexer, value, value_length); + if (idx == -1) + return -1; + } + else 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; @@ -100,7 +109,9 @@ char* obi_column_get_obiqual_char_with_elt_idx(OBIDMS_column_p column, index_t l int_value = obi_column_get_obiqual_int_with_elt_idx(column, line_nb, element_idx, &int_value_length); - // TODO NA + // Check NA + if (int_value == OBIQual_int_NA) + return OBIQual_char_NA; value = (char*) malloc((int_value_length + 1) * sizeof(char)); diff --git a/src/obidmscolumn_seq.c b/src/obidmscolumn_seq.c index 7bfaacd..20069d8 100644 --- a/src/obidmscolumn_seq.c +++ b/src/obidmscolumn_seq.c @@ -32,32 +32,38 @@ int obi_column_set_obiseq_with_elt_idx(OBIDMS_column_p column, index_t line_nb, if (obi_column_prepare_to_set_value(column, line_nb) < 0) return -1; - // TODO NA - - // Add the value in the indexer - idx = obi_index_dna_seq(column->indexer, value); - if (idx == -1) // An error occurred + if (value == OBISeq_NA) { - if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR) + idx = OBIIdx_NA; + } + else + { + // Add the value in the indexer + idx = obi_index_dna_seq(column->indexer, value); + if (idx == -1) // An error occurred { - // 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); + 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_dna_seq(column->indexer, value); - if (idx == -1) + // Add the value in the new indexer + idx = obi_index_dna_seq(column->indexer, value); + if (idx == -1) + return -1; + } + else 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; diff --git a/src/obidmscolumn_str.c b/src/obidmscolumn_str.c index 81a006a..c092323 100644 --- a/src/obidmscolumn_str.c +++ b/src/obidmscolumn_str.c @@ -32,31 +32,36 @@ int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, if (obi_column_prepare_to_set_value(column, line_nb) < 0) return -1; - // TODO NA - - // Add the value in the indexer - idx = obi_index_char_str(column->indexer, value); - if (idx == -1) // An error occurred + if (value == OBIStr_NA) { - if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR) + idx = OBIIdx_NA; + } + else + { + // Add the value in the indexer + idx = obi_index_char_str(column->indexer, value); + if (idx == -1) // An error occurred { - // 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); + 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_char_str(column->indexer, value); - if (idx == -1) + // Add the value in the new indexer + idx = obi_index_char_str(column->indexer, value); + if (idx == -1) + return -1; + } + else return -1; } - else - return -1; } // Add the value's index in the column