First version of automatic ID and COUNT columns, to discuss (for now,
columns created when NUC_SEQ views are closed if the columns don't already exist)
This commit is contained in:
102
src/obiview.c
102
src/obiview.c
@ -1072,6 +1072,26 @@ static int finish_view(Obiview_p view)
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Add count column if it's a NUC_SEQ_VIEW with no count column // TODO discuss
|
||||
if ((!strcmp((view->infos)->view_type, VIEW_TYPE_NUC_SEQS)) && (!obi_view_column_exists(view, COUNT_COLUMN)))
|
||||
{
|
||||
if (obi_create_auto_count_column(view) < 0)
|
||||
{
|
||||
obidebug(1, "\nError creating an automatic count column when finishing a view");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Add id column if it's a NUC_SEQ_VIEW with no id column // TODO discuss
|
||||
if ((!strcmp((view->infos)->view_type, VIEW_TYPE_NUC_SEQS)) && (!obi_view_column_exists(view, ID_COLUMN)))
|
||||
{
|
||||
if (obi_create_auto_id_column(view, NULL) < 0)
|
||||
{
|
||||
obidebug(1, "\nError creating an automatic id column when finishing a view");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Check predicates
|
||||
predicates = view_check_all_predicates(view);
|
||||
if (predicates == NULL)
|
||||
@ -1824,7 +1844,7 @@ Obiview_p obi_new_view_nuc_seqs(OBIDMS_p dms, const char* view_name, Obiview_p v
|
||||
return NULL;
|
||||
}
|
||||
// Adding id column
|
||||
if (obi_view_add_column(view, ID_COLUMN, -1, ID_COLUMN, OBI_STR, 0, 1, NULL, "", NULL, -1, "Ids", true) < 0)
|
||||
if (obi_view_add_column(view, ID_COLUMN, -1, ID_COLUMN, OBI_STR, 0, 1, NULL, "", NULL, -1, "Sequence identifiers", true) < 0)
|
||||
{
|
||||
obidebug(1, "Error adding an obligatory column in a nucleotide sequences view");
|
||||
return NULL;
|
||||
@ -2391,6 +2411,15 @@ OBIDMS_column_p obi_view_get_column(Obiview_p view, const char* column_name)
|
||||
}
|
||||
|
||||
|
||||
bool obi_view_column_exists(Obiview_p view, const char* column_name)
|
||||
{
|
||||
if (obi_view_get_column(view, column_name) == NULL)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
OBIDMS_column_p* obi_view_get_pointer_on_column_in_view(Obiview_p view, const char* column_name)
|
||||
{
|
||||
return (OBIDMS_column_p*)(ht_get(view->column_dict, column_name));
|
||||
@ -2459,8 +2488,8 @@ int obi_save_and_close_view(Obiview_p view)
|
||||
|
||||
int obi_create_auto_count_column(Obiview_p view)
|
||||
{
|
||||
index_t i;
|
||||
OBIDMS_p column;
|
||||
index_t i;
|
||||
OBIDMS_column_p column;
|
||||
|
||||
// Check that the view is not read-only
|
||||
if (view->read_only)
|
||||
@ -2470,7 +2499,7 @@ int obi_create_auto_count_column(Obiview_p view)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (obi_view_add_column(view, COUNT_COLUMN, -1, NULL, OBI_INT, 0, 1, NULL, NULL, NULL, NULL, "Sequence counts", true) < 0)
|
||||
if (obi_view_add_column(view, COUNT_COLUMN, -1, NULL, OBI_INT, 0, 1, NULL, NULL, NULL, -1, "Sequence counts", true) < 0)
|
||||
{
|
||||
obidebug(1, "Error adding an automatic count column in a view");
|
||||
return -1;
|
||||
@ -2486,7 +2515,7 @@ int obi_create_auto_count_column(Obiview_p view)
|
||||
// Fill the column with 1s
|
||||
for (i=0; i < (view->infos)->line_count; i++)
|
||||
{
|
||||
if (obi_column_set_obiint_with_elt_idx(column, i, 0, 1) < 0)
|
||||
if (obi_set_int_with_elt_idx_and_col_p_in_view(view, column, i, 0, 1) < 0)
|
||||
{
|
||||
obidebug(1, "Error adding an automatic count column in a view");
|
||||
return -1;
|
||||
@ -2497,6 +2526,69 @@ int obi_create_auto_count_column(Obiview_p view)
|
||||
}
|
||||
|
||||
|
||||
int obi_create_auto_id_column(Obiview_p view, const char* prefix)
|
||||
{
|
||||
index_t i;
|
||||
OBIDMS_column_p column;
|
||||
char* id;
|
||||
|
||||
// Check that the view is not read-only
|
||||
if (view->read_only)
|
||||
{
|
||||
obi_set_errno(OBIVIEW_ERROR);
|
||||
obidebug(1, "\nError trying to create an automatic count column in a read-only view");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Delete old ID column if it exists
|
||||
if (obi_view_get_column(view, ID_COLUMN) != NULL)
|
||||
{
|
||||
if (obi_view_delete_column(view, ID_COLUMN) < 0)
|
||||
{
|
||||
obidebug(1, "Error deleting an ID column to replace it in a view");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Create the new ID column
|
||||
if (obi_view_add_column(view, ID_COLUMN, -1, NULL, OBI_STR, 0, 1, NULL, NULL, NULL, -1, "Sequence identifiers", true) < 0)
|
||||
{
|
||||
obidebug(1, "Error adding an automatic ID column in a view");
|
||||
return -1;
|
||||
}
|
||||
|
||||
column = obi_view_get_column(view, ID_COLUMN);
|
||||
if (column == NULL)
|
||||
{
|
||||
obidebug(1, "Error adding an automatic ID column in a view");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If prefix is NULL, use default prefix
|
||||
if (prefix == NULL)
|
||||
prefix = ID_PREFIX;
|
||||
|
||||
// Fill the column with automatic ids
|
||||
for (i=0; i < (view->infos)->line_count; i++)
|
||||
{
|
||||
id = build_word_with_idx(prefix, i);
|
||||
if (id == NULL)
|
||||
{
|
||||
obidebug(1, "Error building an id for an automatic ID column");
|
||||
return -1;
|
||||
}
|
||||
if (obi_set_str_with_elt_idx_and_col_p_in_view(view, column, i, 0, id) < 0)
|
||||
{
|
||||
obidebug(1, "Error adding an automatic count column in a view");
|
||||
return -1;
|
||||
}
|
||||
free(id);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// TODO Move to another file?
|
||||
/*********** FOR BLOB COLUMNS ***********/
|
||||
|
||||
|
Reference in New Issue
Block a user