diff --git a/src/obidms_taxonomy.c b/src/obidms_taxonomy.c index e096a8c..bf9d773 100644 --- a/src/obidms_taxonomy.c +++ b/src/obidms_taxonomy.c @@ -996,6 +996,101 @@ int write_nameidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name } +int write_mergedidx(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; + + // Compute file size + file_size = sizeof(int32_t) + (sizeof(int32_t) * 3 * (tax->merged_idx)->count); + + // Build the taxonomy directory path + taxonomy_path = get_taxonomy_path(dms, taxonomy_name); + + file_name = (char*) malloc((strlen(taxonomy_path) + strlen(taxonomy_name) + 6)*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.adx", 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 %s", file_name); + 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 merged indices count + if (write(file_descriptor, &((tax->merged_idx)->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 merged indices + for (i=0; i < (tax->merged_idx)->count; i++) + { + // Write taxid + if (write(file_descriptor, &(((tax->merged_idx)->merged)[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 index corresponding to the taxid in the ecotxidx_t structure + if (write(file_descriptor, &(((tax->merged_idx)->merged)[i].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; + } + } + + // Close file + if (close(file_descriptor) < 0) + { + obi_set_errno(OBI_TAXONOMY_ERROR); + obidebug(1, "\nError closing a taxonomy file file"); + return -1; + } + + return 0; +} + int obi_write_taxonomy(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* tax_name) { @@ -1024,6 +1119,8 @@ int obi_write_taxonomy(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* tax_name return -1; if (write_nameidx(dms, tax, tax_name) < 0) return -1; + if (write_mergedidx(dms, tax, tax_name) < 0) + return -1; // Check if there are local taxa (if so last taxon is local) if ((tax->taxa)->local_count > 0) if (write_local_taxonomy_idx(dms, tax, tax_name) < 0) @@ -1331,7 +1428,7 @@ OBIDMS_taxonomy_p obi_read_taxdump(const char* taxdump) { if (strcmp(dp->d_name, "merged.dmp") == 0) { - merged_found = true; // TODO + merged_found = true; buffer_size = 10000; // Initializing the merged structure diff --git a/src/obidms_taxonomy.h b/src/obidms_taxonomy.h index 543a257..48d0293 100644 --- a/src/obidms_taxonomy.h +++ b/src/obidms_taxonomy.h @@ -122,11 +122,6 @@ ecotx_t* obi_taxo_get_kingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy); ecotx_t* obi_taxo_get_superkingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy); - -int write_rankidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name); -int write_taxonomyidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name); -int write_nameidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name); - int obi_write_taxonomy(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* tax_name); OBIDMS_taxonomy_p obi_read_taxdump(const char* taxdump);