|
|
|
@ -51,6 +51,27 @@ int compareRankLabel(const void *label1, const void *label2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* get_taxonomy_path(OBIDMS_p dms, const char* tax_name)
|
|
|
|
|
{
|
|
|
|
|
char* all_tax_dir_path;
|
|
|
|
|
char* tax_path;
|
|
|
|
|
|
|
|
|
|
all_tax_dir_path = obi_dms_get_full_path(dms, TAXONOMY_DIR_NAME);
|
|
|
|
|
tax_path = (char*) malloc((strlen(all_tax_dir_path) + strlen(tax_name) + 2)*sizeof(char));
|
|
|
|
|
if (sprintf(tax_path, "%s/%s", all_tax_dir_path, tax_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError building taxonomy path");
|
|
|
|
|
free(all_tax_dir_path);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(all_tax_dir_path);
|
|
|
|
|
|
|
|
|
|
return tax_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int32_t rank_index(const char* label, ecorankidx_t* ranks)
|
|
|
|
|
{
|
|
|
|
|
char **rep;
|
|
|
|
@ -58,7 +79,7 @@ int32_t rank_index(const char* label, ecorankidx_t* ranks)
|
|
|
|
|
rep = bsearch(label, ranks->label, ranks->count, sizeof(char*), compareRankLabel);
|
|
|
|
|
|
|
|
|
|
if (rep)
|
|
|
|
|
return rep-ranks->label; // TODO what???
|
|
|
|
|
return rep-ranks->label;
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
@ -93,8 +114,8 @@ void* read_ecorecord(FILE* f, int32_t* record_size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (!(obi_is_little_endian())) // TODO
|
|
|
|
|
if (is_big_endian())
|
|
|
|
|
*record_size=swap_int32_t(*record_size);
|
|
|
|
|
// if (is_big_endian())
|
|
|
|
|
// *record_size=swap_int32_t(*record_size);
|
|
|
|
|
|
|
|
|
|
if (buffer_size < *record_size)
|
|
|
|
|
{
|
|
|
|
@ -137,13 +158,13 @@ ecotx_t* readnext_ecotaxon(FILE* f, ecotx_t* taxon)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// if (!(obi_is_little_endian())) // TODO
|
|
|
|
|
if (is_big_endian())
|
|
|
|
|
{
|
|
|
|
|
raw->name_length = swap_int32_t(raw->name_length);
|
|
|
|
|
raw->parent = swap_int32_t(raw->parent);
|
|
|
|
|
raw->rank = swap_int32_t(raw->rank);
|
|
|
|
|
raw->taxid = swap_int32_t(raw->taxid);
|
|
|
|
|
}
|
|
|
|
|
// if (is_big_endian())
|
|
|
|
|
// {
|
|
|
|
|
// raw->name_length = swap_int32_t(raw->name_length);
|
|
|
|
|
// raw->parent = swap_int32_t(raw->parent);
|
|
|
|
|
// raw->rank = swap_int32_t(raw->rank);
|
|
|
|
|
// raw->taxid = swap_int32_t(raw->taxid);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
taxon->parent = (ecotx_t*) ((size_t) raw->parent);
|
|
|
|
|
taxon->taxid = raw->taxid;
|
|
|
|
@ -195,8 +216,8 @@ FILE* open_ecorecorddb(const char* file_name,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (!(obi_is_little_endian())) // TODO
|
|
|
|
|
if (is_big_endian())
|
|
|
|
|
*count = swap_int32_t(*count);
|
|
|
|
|
// if (is_big_endian())
|
|
|
|
|
// *count = swap_int32_t(*count);
|
|
|
|
|
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
@ -225,6 +246,7 @@ ecorankidx_t* read_rankidx(const char* ranks_file_name)
|
|
|
|
|
buffer = read_ecorecord(ranks_file, &rank_length);
|
|
|
|
|
ranks_index->label[i] = (char*) malloc(rank_length+1);
|
|
|
|
|
strncpy(ranks_index->label[i], buffer, rank_length);
|
|
|
|
|
(ranks_index->label[i])[rank_length] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ranks_index;
|
|
|
|
@ -277,6 +299,7 @@ ecotxidx_t* read_taxonomyidx(const char* taxa_file_name, const char* local_taxa_
|
|
|
|
|
|
|
|
|
|
for (; i < count_taxa; i++){
|
|
|
|
|
readnext_ecotaxon(f_local_taxa, &(taxa_index->taxon[i]));
|
|
|
|
|
taxa_index->taxon[i].idx = i;
|
|
|
|
|
taxa_index->taxon[i].parent = taxa_index->taxon + (size_t) taxa_index->taxon[i].parent;
|
|
|
|
|
taxa_index->taxon[i].parent->farest=0;
|
|
|
|
|
if (taxa_index->taxon[i].taxid > taxa_index->max_taxid)
|
|
|
|
@ -321,13 +344,13 @@ econame_t* readnext_econame(FILE* f, econame_t* name, OBIDMS_taxonomy_p taxonomy
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// if (!(obi_is_little_endian())) // TODO
|
|
|
|
|
if (is_big_endian())
|
|
|
|
|
{
|
|
|
|
|
raw->is_scientific_name = swap_int32_t(raw->is_scientific_name);
|
|
|
|
|
raw->name_length = swap_int32_t(raw->name_length);
|
|
|
|
|
raw->class_length = swap_int32_t(raw->class_length);
|
|
|
|
|
raw->taxid = swap_int32_t(raw->taxid);
|
|
|
|
|
}
|
|
|
|
|
// if (is_big_endian())
|
|
|
|
|
// {
|
|
|
|
|
// raw->is_scientific_name = swap_int32_t(raw->is_scientific_name);
|
|
|
|
|
// raw->name_length = swap_int32_t(raw->name_length);
|
|
|
|
|
// raw->class_length = swap_int32_t(raw->class_length);
|
|
|
|
|
// raw->taxid = swap_int32_t(raw->taxid);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
name->is_scientific_name = raw->is_scientific_name;
|
|
|
|
|
|
|
|
|
@ -382,7 +405,6 @@ static int bcomptaxon (const void* ptaxid, const void* ptaxon)
|
|
|
|
|
OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, bool read_alternative_names)
|
|
|
|
|
{
|
|
|
|
|
OBIDMS_taxonomy_p tax;
|
|
|
|
|
char* main_taxonomy_dir_path;
|
|
|
|
|
char* taxonomy_path;
|
|
|
|
|
char* ranks_file_name;
|
|
|
|
|
char* taxa_file_name;
|
|
|
|
@ -398,16 +420,7 @@ OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, boo
|
|
|
|
|
|
|
|
|
|
buffer_size = 2048; // TODO
|
|
|
|
|
|
|
|
|
|
main_taxonomy_dir_path = obi_dms_get_full_path(dms, TAXONOMY_DIR_NAME);
|
|
|
|
|
taxonomy_path = (char*) malloc((strlen(main_taxonomy_dir_path) + strlen(taxonomy_name) + strlen(taxonomy_name) + 3)*sizeof(char));
|
|
|
|
|
if (sprintf(taxonomy_path, "%s/%s/%s", main_taxonomy_dir_path, taxonomy_name, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
free(main_taxonomy_dir_path);
|
|
|
|
|
obi_close_taxonomy(tax);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(main_taxonomy_dir_path);
|
|
|
|
|
taxonomy_path = get_taxonomy_path(dms, taxonomy_name);
|
|
|
|
|
|
|
|
|
|
// Read ranks
|
|
|
|
|
ranks_file_name = (char*) malloc(buffer_size*sizeof(char));
|
|
|
|
@ -417,7 +430,7 @@ OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, boo
|
|
|
|
|
obi_close_taxonomy(tax);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (snprintf(ranks_file_name, buffer_size, "%s.rdx", taxonomy_path) < 0)
|
|
|
|
|
if (snprintf(ranks_file_name, buffer_size, "%s/%s.rdx", taxonomy_path, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
free(taxonomy_path);
|
|
|
|
|
free(ranks_file_name);
|
|
|
|
@ -441,7 +454,7 @@ OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, boo
|
|
|
|
|
obi_close_taxonomy(tax);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (snprintf(taxa_file_name, buffer_size,"%s.tdx", taxonomy_path) < 0)
|
|
|
|
|
if (snprintf(taxa_file_name, buffer_size, "%s/%s.tdx", taxonomy_path, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
free(taxonomy_path);
|
|
|
|
|
free(taxa_file_name);
|
|
|
|
@ -456,7 +469,7 @@ OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, boo
|
|
|
|
|
obi_close_taxonomy(tax);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (snprintf(local_taxa_file_name, buffer_size,"%s.ldx", taxonomy_path) < 0)
|
|
|
|
|
if (snprintf(local_taxa_file_name, buffer_size, "%s/%s.ldx", taxonomy_path, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
free(taxonomy_path);
|
|
|
|
|
free(taxa_file_name);
|
|
|
|
@ -486,7 +499,7 @@ OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, boo
|
|
|
|
|
obi_close_taxonomy(tax);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (snprintf(alter_names_file_name, buffer_size,"%s.ndx", taxonomy_path) < 0)
|
|
|
|
|
if (snprintf(alter_names_file_name, buffer_size, "%s/%s.ndx", taxonomy_path, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
free(taxonomy_path);
|
|
|
|
|
free(alter_names_file_name);
|
|
|
|
@ -514,7 +527,7 @@ int obi_close_taxonomy(OBIDMS_taxonomy_p taxonomy)
|
|
|
|
|
if (taxonomy)
|
|
|
|
|
{
|
|
|
|
|
if (taxonomy->ranks)
|
|
|
|
|
free(taxonomy->ranks); // TODO those don't free everything but mapping will replace anyway
|
|
|
|
|
free(taxonomy->ranks); // TODO those don't free everything
|
|
|
|
|
|
|
|
|
|
if (taxonomy->names)
|
|
|
|
|
free(taxonomy->names);
|
|
|
|
@ -527,7 +540,7 @@ int obi_close_taxonomy(OBIDMS_taxonomy_p taxonomy)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO no closing files?
|
|
|
|
|
// close files
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
@ -699,3 +712,395 @@ ecotx_t* obi_taxo_get_superkingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
|
|
|
|
|
return obi_taxo_get_parent_at_rank(taxon, rankindex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Functions to write taxonomy structure to binary files
|
|
|
|
|
|
|
|
|
|
int write_rankidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name) // TODO prefix in taxonomy struct?
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char* file_name;
|
|
|
|
|
int file_descriptor;
|
|
|
|
|
off_t file_size;
|
|
|
|
|
char* taxonomy_path;
|
|
|
|
|
int32_t length;
|
|
|
|
|
|
|
|
|
|
// Compute file size
|
|
|
|
|
file_size = sizeof(int32_t);
|
|
|
|
|
for (i=0; i < (tax->ranks)->count; i++)
|
|
|
|
|
{
|
|
|
|
|
file_size = file_size + sizeof(int32_t); // To store label size
|
|
|
|
|
file_size = file_size + strlen(((tax->ranks)->label)[i]); // To store label
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the taxonomy directory path
|
|
|
|
|
taxonomy_path = get_taxonomy_path(dms, taxonomy_name);
|
|
|
|
|
|
|
|
|
|
file_name = (char*) malloc((strlen(taxonomy_path) + strlen(taxonomy_name) + 5)*sizeof(char));
|
|
|
|
|
if (file_name == NULL)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_MALLOC_ERROR);
|
|
|
|
|
obidebug(1, "\nError allocating the memory for a binary taxonomy file name");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the file path
|
|
|
|
|
if (sprintf(file_name, "%s/%s.rdx", taxonomy_path, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError building a binary taxonomy file name");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(taxonomy_path);
|
|
|
|
|
|
|
|
|
|
// Create file
|
|
|
|
|
file_descriptor = open(file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
|
|
|
|
|
if (file_descriptor < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError creating a binary taxonomy file");
|
|
|
|
|
free(file_name);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(file_name);
|
|
|
|
|
|
|
|
|
|
// Truncate the file to the right size
|
|
|
|
|
if (ftruncate(file_descriptor, file_size) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError truncating a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write rank count
|
|
|
|
|
if (write(file_descriptor, &((tax->ranks)->count), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write ranks
|
|
|
|
|
for (i=0; i < (tax->ranks)->count; i++)
|
|
|
|
|
{
|
|
|
|
|
length = strlen(((tax->ranks)->label)[i]);
|
|
|
|
|
|
|
|
|
|
// Write rank size
|
|
|
|
|
if (write(file_descriptor, &length, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write rank label
|
|
|
|
|
if (write(file_descriptor, ((tax->ranks)->label)[i], length) < ((ssize_t) length))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
|
if (close(file_descriptor) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
|
obidebug(1, "\nError closing a DMS information file");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int write_taxonomyidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name) // TODO prefix in taxonomy struct? keep argument but if NULL, use the one in struct?
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char* file_name;
|
|
|
|
|
int file_descriptor;
|
|
|
|
|
off_t file_size;
|
|
|
|
|
char* taxonomy_path;
|
|
|
|
|
int32_t name_length;
|
|
|
|
|
int32_t record_size;
|
|
|
|
|
|
|
|
|
|
// Compute file size
|
|
|
|
|
file_size = sizeof(int32_t); // To store record count
|
|
|
|
|
for (i=0; i < (tax->taxa)->count; i++)
|
|
|
|
|
{
|
|
|
|
|
file_size = file_size + sizeof(int32_t) * 5; // To store record size, taxid, rank index, parent index, and name length
|
|
|
|
|
file_size = file_size + strlen(tax->taxa->taxon[i].name); // To store name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the taxonomy directory path
|
|
|
|
|
taxonomy_path = get_taxonomy_path(dms, taxonomy_name);
|
|
|
|
|
|
|
|
|
|
file_name = (char*) malloc((strlen(taxonomy_path) + strlen(taxonomy_name) + 5)*sizeof(char));
|
|
|
|
|
if (file_name == NULL)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_MALLOC_ERROR);
|
|
|
|
|
obidebug(1, "\nError allocating the memory for a binary taxonomy file name");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the file path
|
|
|
|
|
if (sprintf(file_name, "%s/%s.tdx", taxonomy_path, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError building a binary taxonomy file name");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(taxonomy_path);
|
|
|
|
|
|
|
|
|
|
// Create file
|
|
|
|
|
file_descriptor = open(file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
|
|
|
|
|
if (file_descriptor < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError creating a binary taxonomy file");
|
|
|
|
|
free(file_name);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(file_name);
|
|
|
|
|
|
|
|
|
|
// Truncate the file to the right size
|
|
|
|
|
if (ftruncate(file_descriptor, file_size) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError truncating a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write record count
|
|
|
|
|
if (write(file_descriptor, &(tax->taxa->count), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write records
|
|
|
|
|
for (i=0; i < tax->taxa->count; i++)
|
|
|
|
|
{
|
|
|
|
|
name_length = strlen(tax->taxa->taxon[i].name);
|
|
|
|
|
record_size = 4*sizeof(int32_t) + name_length;
|
|
|
|
|
|
|
|
|
|
// Write record size
|
|
|
|
|
if (write(file_descriptor, &record_size, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write taxid
|
|
|
|
|
if (write(file_descriptor, &(tax->taxa->taxon[i].taxid), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write rank index
|
|
|
|
|
if (write(file_descriptor, &(tax->taxa->taxon[i].rank), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write parent index
|
|
|
|
|
if (write(file_descriptor, &((tax->taxa->taxon[i].parent)->idx), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write name length
|
|
|
|
|
if (write(file_descriptor, &name_length, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write name
|
|
|
|
|
if (write(file_descriptor, tax->taxa->taxon[i].name, name_length) < ((ssize_t) name_length))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
|
if (close(file_descriptor) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
|
obidebug(1, "\nError closing a DMS information file");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int write_nameidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name) // TODO prefix in taxonomy struct? keep argument but if NULL, use the one in struct?
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char* file_name;
|
|
|
|
|
int file_descriptor;
|
|
|
|
|
off_t file_size;
|
|
|
|
|
char* taxonomy_path;
|
|
|
|
|
int32_t name_length;
|
|
|
|
|
int32_t class_length;
|
|
|
|
|
int32_t record_size;
|
|
|
|
|
|
|
|
|
|
// Compute file size
|
|
|
|
|
file_size = sizeof(int32_t); // To store record count
|
|
|
|
|
for (i=0; i < (tax->names)->count; i++)
|
|
|
|
|
{
|
|
|
|
|
file_size = file_size + sizeof(int32_t) * 5; // To store record size, taxid, rank index, parent index, and name length
|
|
|
|
|
file_size = file_size + strlen(tax->names->names[i].name); // To store name
|
|
|
|
|
file_size = file_size + strlen(tax->names->names[i].class_name); // To store name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the taxonomy directory path
|
|
|
|
|
taxonomy_path = get_taxonomy_path(dms, taxonomy_name);
|
|
|
|
|
|
|
|
|
|
file_name = (char*) malloc((strlen(taxonomy_path) + strlen(taxonomy_name) + 5)*sizeof(char));
|
|
|
|
|
if (file_name == NULL)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_MALLOC_ERROR);
|
|
|
|
|
obidebug(1, "\nError allocating the memory for a binary taxonomy file name");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the file path
|
|
|
|
|
if (sprintf(file_name, "%s/%s.ndx", taxonomy_path, taxonomy_name) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError building a binary taxonomy file name");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(taxonomy_path);
|
|
|
|
|
|
|
|
|
|
// Create file
|
|
|
|
|
file_descriptor = open(file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
|
|
|
|
|
if (file_descriptor < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError creating a binary taxonomy file");
|
|
|
|
|
free(file_name);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(file_name);
|
|
|
|
|
|
|
|
|
|
// Truncate the file to the right size
|
|
|
|
|
if (ftruncate(file_descriptor, file_size) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError truncating a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write record count
|
|
|
|
|
if (write(file_descriptor, &(tax->names->count), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write records
|
|
|
|
|
for (i=0; i < tax->names->count; i++)
|
|
|
|
|
{
|
|
|
|
|
name_length = strlen(tax->names->names[i].name);
|
|
|
|
|
class_length = strlen(tax->names->names[i].class_name);
|
|
|
|
|
record_size = 4*sizeof(int32_t) + name_length + class_length;
|
|
|
|
|
|
|
|
|
|
// Write record size
|
|
|
|
|
if (write(file_descriptor, &record_size, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write if the name is a scientific name
|
|
|
|
|
if (write(file_descriptor, &(tax->names->names[i].is_scientific_name), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write name length
|
|
|
|
|
if (write(file_descriptor, &name_length, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write class length
|
|
|
|
|
if (write(file_descriptor, &class_length, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write taxid index
|
|
|
|
|
if (write(file_descriptor, &(tax->names->names[i].taxon->idx), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write name
|
|
|
|
|
if (write(file_descriptor, tax->names->names[i].name, name_length) < ((ssize_t) name_length))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// Write class
|
|
|
|
|
if (write(file_descriptor, tax->names->names[i].class_name, class_length) < ((ssize_t) class_length))
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
|
|
|
|
obidebug(1, "\nError writing in a binary taxonomy file");
|
|
|
|
|
close(file_descriptor);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
|
if (close(file_descriptor) < 0)
|
|
|
|
|
{
|
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
|
obidebug(1, "\nError closing a DMS information file");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|