obidms_taxonomy.c 103 KB
Newer Older
1 2 3 4 5 6 7 8
/********************************************************************
 * OBIDMS taxonomy functions                                        *
 ********************************************************************/

/**
 * @file obidms_taxonomy.c
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 * @date March 2nd 2016
9
 * @brief Functions for handling the reading and writing of taxonomy files.
10 11 12 13 14 15 16 17
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
18 19 20
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
21 22 23 24 25

#include "obidms_taxonomy.h"
#include "obidms.h"
#include "obidebug.h"
#include "obierrno.h"
26
#include "utils.h"
27 28 29 30


#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)

31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/**************************************************************************
 *
 * D E C L A R A T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 **************************************************************************/


/**
 * @brief Internal function comparing two rank names.
 *
 * @param label1 A char* pointer on the first rank name.
 * @param label2 A char** pointer on a second pointer, that second char* pointer being on the second rank name.
 * 				 (making the function usable with an ecorankidx_t structure and functions like bsearch)
 *
 * @returns A value < 0 if label1 < label2,
 * 			a value > 0 if label1 > label2,
 * 			and 0 if label1 == label2.
 */
static int cmp_rank_labels(const void* label1, const void* label2);


/**
 * @brief Internal function comparing two taxids, one of them stored in an ecotx_t structure.
 *
 * @param ptaxid The first taxid.
 * @param ptaxon A pointer on an ecotx_t structure where the second taxid is stored.
 *
 * @returns A value < 0 if taxid1 < taxid2,
 * 			a value > 0 if taxid1 > taxid2,
 * 			and 0 if taxid1 == taxid2.
 */
static int cmp_taxids_in_ecotx_t(const void* ptaxid, const void* ptaxon);


/**
 * @brief Internal function comparing two taxids, one of them stored in an ecomerged_t structure.
 *
 * @param ptaxid The first taxid.
 * @param ptaxon A pointer on an ecomerged_t structure where the second taxid is stored.
 *
 * @returns A value < 0 if taxid1 < taxid2,
 * 			a value > 0 if taxid1 > taxid2,
 * 			and 0 if taxid1 == taxid2.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int cmp_taxids_in_ecomerged_t(const void* ptaxid, const void* ptaxon);


/**
 * @brief Internal function comparing two character strings pointed to by char** pointers.
 *
 * @param s1 A char** pointer on a second pointer, that second char* pointer being on the first character string.
 * @param s2 A char** pointer on a second pointer, that second char* pointer being on the second character string.
 *
 * @returns A value < 0 if s1 < s2,
 * 			a value > 0 if s1 > s2,
 * 			and 0 if s1 == s2.
 */
static int cmp_str(const void* s1, const void* s2);


/**
 * @brief Internal function comparing two taxon names stored in econame_t structures.
 *
 * @param n1 A pointer on the first econame_t structure.
 * @param n2 A pointer on the second econame_t structure.
 *
 * @returns A value < 0 if n1 < n2,
 * 			a value > 0 if n1 > n2,
 * 			and 0 if n1 == n2.
 *
 * @since 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int cmp_names(const void* n1, const void* n2);


/**
 * @brief Internal function comparing returning the ecotx_t structure associated with a taxid.
 *
 * This function only looks for the taxid in the modern taxonomy, it does not consider deprecated
 * and old taxids, unlike obi_taxo_get_taxon_with_taxid().
 *
 * @param taxonomy A pointer on the taxonomy structure.
 * @param taxid The taxid of the taxon wanted.
 *
 * @returns A pointer on the ecotx_t structure associated with a taxid.
 *
 * @see obi_taxo_get_taxon_with_taxid()
 */
static ecotx_t* get_taxon_with_current_taxid(OBIDMS_taxonomy_p taxonomy, int32_t taxid);


/**
 * @brief Internal function returning the complete path to a taxonomy directory in a DMS.
 *
 * @param dms A pointer on the DMS.
 * @param tax_name The name of the taxonomy.
 *
 * @returns The complete path to the taxonomy directory.
 * @retval NULL if an error occurred.
 *
 * @since 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* get_taxonomy_path(OBIDMS_p dms, const char* tax_name);


/**
 * @brief Internal function returning the index of a rank in an ecorankidx_t structure.
 *
 * @param label The name of the rank.
 * @param ranks A pointer on an ecorankidx_t structure.
 *
 * @returns The index of a rank in the ecorankidx_t structure.
 * @retval -1 if the rank was not found.
 */
151
static int32_t rank_label_to_index(const char* label, ecorankidx_t* ranks);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461


/**
 * @brief Internal function opening a binary taxonomy file (.tdx, .rdx, .ndx, .adx, .pdx, .ldx).
 *
 * @param file_name The file path.
 * @param count A pointer on an integer that the function will set to the number of records in the file.
 * @param abort_on_open_error A boolean indicating whether the function should trigger an error if the file can't be open.
 *
 * @returns The FILE object.
 * @retval NULL if an error occurred or if the file was not found.
 */
static FILE* open_ecorecorddb(const char* file_name, int32_t* count, int32_t abort_on_open_error);


/**
 * @brief Internal function returning the next record in a binary taxonomy file (.tdx, .rdx, .ndx, .adx, .pdx, .ldx).
 *
 * @param f The file object with the offset at the start of a record.
 * @param record_size A pointer on an integer that the function will set to the size of the record.
 *
 * @returns A pointer on the read record.
 * @retval NULL if an error occurred.
 */
static void* read_ecorecord(FILE* f, int32_t* record_size);


/**
 * @brief Internal function reading the next taxon record in a .tdx binary taxonomy file.
 *
 * @param f The file object with the offset at the start of a record.
 * @param taxon A pointer on an empty, allocated ecotx_t structure that the function will fill.
 *
 * @returns A pointer on the read record.
 * @retval NULL if an error occurred.
 */
static ecotx_t* readnext_ecotaxon(FILE* f, ecotx_t* taxon);


/**
 * @brief Internal function reading the next taxon name record in a .ndx binary taxonomy file.
 *
 * @param f The file object with the offset at the start of a record.
 * @param name A pointer on an empty, allocated econame_t structure that the function will fill.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on the read record.
 * @retval NULL if an error occurred.
 */
static econame_t* readnext_econame(FILE* f, econame_t* name, OBIDMS_taxonomy_p taxonomy);


/**
 * @brief Internal function reading the next taxon preferred name record in a .pdx binary taxonomy file.
 *
 * @param f The file object with the offset at the start of a record.
 * @param name A pointer on an empty, allocated econame_t structure that the function will fill.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on the read record.
 * @retval NULL if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static econame_t* readnext_ecopreferredname(FILE* f, econame_t* name, OBIDMS_taxonomy_p taxonomy);


/**
 * @brief Internal function reading a taxonomic ranks (.rdx) binary taxonomy file.
 *
 * @param ranks_file_name The name of the .rdx file to read.
 *
 * @returns A pointer on an ecorankidx_t structure.
 * @retval NULL if an error occurred.
 */
static ecorankidx_t* read_ranks_idx(const char* ranks_file_name);


/**
 * @brief Internal function reading the taxa (.tdx, .ldx) binary taxonomy file.
 *
 * @param taxa_file_name The name of the .tdx file to read.
 * @param local_taxa_file_name The name of the .ldx file containing the local taxa to read if there is one.
 *
 * @returns A pointer on an ecotxidx_t structure.
 * @retval NULL if an error occurred.
 */
static ecotxidx_t* read_taxonomy_idx(const char* taxa_file_name, const char* local_taxa_file_name);


/**
 * @brief Internal function reading a names (.ndx) binary taxonomy file.
 *
 * @param file_name The name of the .ndx file to read.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on an econameidx_t structure.
 * @retval NULL if an error occurred.
 */
static econameidx_t* read_names_idx(const char* file_name, OBIDMS_taxonomy_p taxonomy);


/**
 * @brief Internal function reading a preferred names (.pdx) binary taxonomy file.
 *
 * @param file_name The name of the .pdx file to read.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on an econameidx_t structure.
 * @retval NULL if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static econameidx_t* read_preferred_names_idx(const char* file_name, OBIDMS_taxonomy_p taxonomy);


/**
 * @brief Internal function reading a merged index (.adx) binary taxonomy file.
 *
 * @param file_name The name of the .adx file to read.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on an ecomergedidx_t structure.
 * @retval NULL if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static ecomergedidx_t* read_merged_idx(const char* file_name, OBIDMS_taxonomy_p taxonomy);


/**
 * @brief Internal function writing a rank index (.rdx) binary taxonomy file.
 *
 * @param dms A pointer on the DMS.
 * @param tax A pointer on the taxonomy structure.
 * @param taxonomy_name The name of the taxonomy.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 */
static int write_ranks_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);


/**
 * @brief Internal function writing a taxonomy index (.tdx) binary taxonomy file.
 *
 * @param dms A pointer on the DMS.
 * @param tax A pointer on the taxonomy structure.
 * @param taxonomy_name The name of the taxonomy.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 */
static int write_taxonomy_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);


/**
 * @brief Internal function writing a local taxonomy index (.ldx) binary taxonomy file.
 *
 * @param dms A pointer on the DMS.
 * @param tax A pointer on the taxonomy structure.
 * @param taxonomy_name The name of the taxonomy.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int write_local_taxonomy_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);


/**
 * @brief Internal function writing a names index (.ndx) binary taxonomy file.
 *
 * @param dms A pointer on the DMS.
 * @param tax A pointer on the taxonomy structure.
 * @param taxonomy_name The name of the taxonomy.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 */
static int write_names_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);


/**
 * @brief Internal function writing a preferred names index (.pdx) binary taxonomy file.
 *
 * @param dms A pointer on the DMS.
 * @param tax A pointer on the taxonomy structure.
 * @param taxonomy_name The name of the taxonomy.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int write_preferred_names_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);


/**
 * @brief Internal function writing a merged index (.adx) binary taxonomy file.
 *
 * @param dms A pointer on the DMS.
 * @param tax A pointer on the taxonomy structure.
 * @param taxonomy_name The name of the taxonomy.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int write_merged_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);


/**
 * @brief Internal function reading the 'nodes.dmp' file from an NCBI taxdump.
 *
 * @param taxdump The path to the taxdump.
 * @param tax A pointer on the taxonomy structure.
 * @param rank_names_p A char*** pointer on a non allocated char* array where the function will store rank names.
 * @param parent_taxids_p An int** pointer on a non allocated int array where the function will store parent taxids.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int read_nodes_dmp(const char* taxdump, OBIDMS_taxonomy_p tax, char*** rank_names_p, int** parent_taxids_p);


/**
 * @brief Internal function reading the 'delnodes.dmp' file from an NCBI taxdump.
 *
 * @param taxdump The path to the taxdump.
 * @param tax A pointer on the taxonomy structure.
 * @param delnodes_p An int** pointer on a non allocated int array where the function will store deleted taxids.
 * @param delnodes_count An int* pointer where the function will store the number of deleted taxids.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int read_delnodes_dmp(const char* taxdump, OBIDMS_taxonomy_p tax, int32_t** delnodes_p, int32_t* delnodes_count);


/**
 * @brief Internal function reading the 'merged.dmp' file from an NCBI taxdump.
 *
 * @warning Should be used AFTER read_nodes_dmp() and read_delnodes_dmp().
 *
 * The function merges the information about current nodes previously read in read_nodes_dmp(),
 * the information about deleted nodes previously read in read_delnodes_dmp(), and the information read
 * in the 'merged.dmp' file, to build the final merged taxon index in the taxonomy structure.
 *
 * @param taxdump The path to the taxdump.
 * @param tax A pointer on the taxonomy structure.
 * @param delnodes An int* pointer containing the deleted taxids.
 * @param delnodes_count The number of deleted taxids.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int read_merged_dmp(const char* taxdump, OBIDMS_taxonomy_p tax, int32_t* delnodes, int32_t delnodes_count);


/**
 * @brief Internal function reading the 'names.dmp' file from an NCBI taxdump.
 *
 * @param taxdump The path to the taxdump.
 * @param tax A pointer on the taxonomy structure.
 *
 * @returns An integer value indicating the success of the operation.
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int read_names_dmp(const char* taxdump, OBIDMS_taxonomy_p tax);


/************************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 ************************************************************************/


static int cmp_rank_labels(const void* label1, const void* label2)
462
{
463 464 465
	return strcmp((const char*)label1,*(const char**)label2);
}

466

467
static int cmp_taxids_in_ecotx_t(const void* ptaxid, const void* ptaxon)
468 469 470 471
{
  ecotx_t* current_taxon = (ecotx_t*) ptaxon;
  int32_t  taxid = (int32_t) ((size_t) ptaxid);
  return taxid - current_taxon->taxid;
472 473 474
}


475 476 477 478 479 480 481 482
static int cmp_taxids_in_ecomerged_t(const void* ptaxid, const void* ptaxon)
{
  ecomerged_t* current_taxon = (ecomerged_t*) ptaxon;
  int32_t  taxid = (int32_t) ((size_t) ptaxid);
  return taxid - current_taxon->taxid;
}


483
static int cmp_str(const void* s1, const void* s2)
484
{
485
    return strcmp(*((char**)s1), *((char**)s2));
486
}
487 488


489
static int cmp_names(const void* n1, const void* n2)
490
{
491 492 493 494
	econame_t name1 = *((econame_t*)n1);
	econame_t name2 = *((econame_t*)n2);

    return strcmp(name1.name, name2.name);
495 496 497
}


498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static ecotx_t* get_taxon_with_current_taxid(OBIDMS_taxonomy_p taxonomy, int32_t taxid)
{
	ecotx_t *current_taxon;
	int32_t  count;

	count = (taxonomy->taxa)->count;

	current_taxon = (ecotx_t*) bsearch((const void *) ((size_t) taxid),
                                       (const void *) taxonomy->taxa->taxon,
                                       count,
                                       sizeof(ecotx_t),
									   cmp_taxids_in_ecotx_t);
	return current_taxon;
}


static char* get_taxonomy_path(OBIDMS_p dms, const char* tax_name)
515 516 517 518 519
{
	char*   all_tax_dir_path;
	char*   tax_path;

	all_tax_dir_path = obi_dms_get_full_path(dms, TAXONOMY_DIR_NAME);
520 521 522
	if (all_tax_dir_path == NULL)
		return NULL;

523
	tax_path = (char*) malloc((strlen(all_tax_dir_path) + strlen(tax_name) + 2)*sizeof(char));
524 525 526 527 528 529 530 531
	if (tax_path == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for taxonomy path");
		free(all_tax_dir_path);
		return NULL;
	}

532 533 534 535 536 537 538 539 540 541 542 543 544 545
	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;
}


546
static int32_t rank_label_to_index(const char* label, ecorankidx_t* ranks)
547 548 549
{
	char **rep;

550
	rep = bsearch(label, ranks->label, ranks->count, sizeof(char*), cmp_rank_labels);
551 552

	if (rep)
553
		return rep-ranks->label;
554 555 556 557 558

	return -1;
}


559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
static FILE* open_ecorecorddb(const char* file_name,
                       int32_t*    count,
                       int32_t     abort_on_open_error)
{
    FILE*		f;
	int32_t     read;

	f = fopen(file_name, "rb");

	if (!f)
	{
		if (abort_on_open_error)
		{
	 		obi_set_errno(OBI_TAXONOMY_ERROR);
	 		obidebug(1, "\nCouldn't open a taxonomy file");
	 		fclose(f);
	 		return NULL;
		}
	 	else
	 	{
	 		*count = 0;
	 		fclose(f);
	 		return NULL;
	 	}
	}

	read = fread(count,
	      		 sizeof(int32_t),
				 1,
	      		 f);

	if (read != 1)
	{
 		obi_set_errno(OBI_TAXONOMY_ERROR);
 		obidebug(1, "\nError reading taxonomy record size");
 		fclose(f);
 		return NULL;
	}

	return f;
}


static void* read_ecorecord(FILE* f, int32_t* record_size)
603 604 605 606 607 608 609 610 611 612 613 614 615 616
{
	static void* buffer = NULL;
	int32_t      buffer_size = 0;
	int32_t      read;

	if (!record_size)
	{
 		obi_set_errno(OBI_TAXONOMY_ERROR);
 		obidebug(1, "\nError reading a taxonomy file: record_size can not be NULL");
 		return NULL;
	}

	read = fread(record_size,
	      		 sizeof(int32_t),
617
				 1,
618 619 620
	             f);

	if (feof(f))
621 622 623 624 625
	{
 		obi_set_errno(OBI_TAXONOMY_ERROR);
 		obidebug(1, "\nError reading a taxonomy file: reached end of file");
 		return NULL;
	}
626

627
	if (read != 1)
628 629 630 631 632 633 634 635 636 637 638 639 640 641
	{
 		obi_set_errno(OBI_TAXONOMY_ERROR);
 		obidebug(1, "\nError reading a taxonomy file: error reading record size");
 		return NULL;
	}

	if (buffer_size < *record_size)
	{
		if (buffer)
			buffer = realloc(buffer, *record_size);
		else
			buffer = malloc(*record_size);
		if (buffer == NULL)
		{
642
	 		obi_set_errno(OBI_MALLOC_ERROR);
643 644 645 646 647 648 649
	 		obidebug(1, "\nError reading a taxonomy file: error allocating memory");
	 		return NULL;
		}
	}

	read = fread(buffer,
				 *record_size,
650
				 1,
651 652
				 f);

653
	if (read != 1)
654 655 656
	{
 		obi_set_errno(OBI_TAXONOMY_ERROR);
 		obidebug(1, "\nError reading a taxonomy file: error reading a record %d, %d", read, *record_size);
657
 		free(buffer);
658 659 660 661 662 663 664
 		return NULL;
	}

	return buffer;
};


665
static ecotx_t* readnext_ecotaxon(FILE* f, ecotx_t* taxon)
666 667 668 669 670 671 672 673 674 675 676 677 678 679
{
	ecotxformat_t* raw;
	int32_t        record_length;

	raw = read_ecorecord(f, &record_length);
	if (!raw)
		return NULL;

	taxon->parent = (ecotx_t*) ((size_t) raw->parent);
	taxon->taxid  = raw->taxid;
	taxon->rank   = raw->rank;
	taxon->farest = -1;

	taxon->name   = malloc((raw->name_length+1) * sizeof(char));
680 681 682 683 684 685
	if (taxon->name == NULL)
	{
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError reading a taxonomy file: error allocating memory");
 		return NULL;
	}
686 687

	strncpy(taxon->name, raw->name, raw->name_length);
688
	taxon->name[raw->name_length] = 0;					// TODO note: this line is probably missing in ROBITaxonomy and source of a bug
689 690 691 692 693

	return taxon;
}


694
static econame_t* readnext_econame(FILE* f, econame_t* name, OBIDMS_taxonomy_p taxonomy)
695
{
696 697
	econameformat_t* raw;
	int32_t          record_length;
698

699 700 701
	raw = read_ecorecord(f, &record_length);
	if (raw == NULL)
 		return NULL;
702

703 704 705 706
	name->is_scientific_name = raw->is_scientific_name;

	name->name = malloc((raw->name_length + 1) * sizeof(char));
	if (name->name == NULL)
707
	{
708 709 710 711
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError allocating memory for a taxon name");
 		free(raw);
 		return NULL;
712
	}
713 714
	strncpy(name->name, raw->names, raw->name_length);
	name->name[raw->name_length] = 0;
715

716 717 718 719 720 721 722 723 724 725 726
	name->class_name = malloc((raw->class_length+1) * sizeof(char));
	if (name->class_name == NULL)
	{
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError allocating memory for a taxon class name");
 		free(name->name);
 		free(raw);
 		return NULL;
	}
	strncpy(name->class_name,(raw->names + raw->name_length), raw->class_length);
	name->class_name[raw->class_length] = 0;
727

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	name->taxon = taxonomy->taxa->taxon + raw->taxid;

	return name;
}


static econame_t* readnext_ecopreferredname(FILE* f, econame_t* name, OBIDMS_taxonomy_p taxonomy)
{
	econameformat_t* raw;
	int32_t          record_length;

	raw = read_ecorecord(f, &record_length);
	if (raw == NULL)
 		return NULL;

	name->is_scientific_name = raw->is_scientific_name;

	name->name = malloc((raw->name_length + 1) * sizeof(char));
	if (name->name == NULL)
747
	{
748 749 750
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError allocating memory for a taxon preferred name");
 		free(raw);
751 752
 		return NULL;
	}
753 754
	strncpy(name->name, raw->names, raw->name_length);
	name->name[raw->name_length] = 0;
755

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	name->class_name = malloc((raw->class_length+1) * sizeof(char));
	if (name->class_name == NULL)
	{
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError allocating memory for a taxon class name");
 		free(name->name);
 		free(raw);
 		return NULL;
	}
	strncpy(name->class_name,(raw->names + raw->name_length), raw->class_length);
	name->class_name[raw->class_length] = 0;

	name->taxon = taxonomy->taxa->taxon + raw->taxid;

	// Add the preferred name in the taxon structure 	// TODO discuss: couldn't they all use the same pointer?
	(taxonomy->taxa->taxon + raw->taxid)->preferred_name = malloc((raw->name_length + 1) * sizeof(char));
	if ((taxonomy->taxa->taxon + raw->taxid)->preferred_name == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for a taxon preferred name");
		free(name->name);
		free(name->class_name);
		free(raw);
		return NULL;
	}
	strcpy((taxonomy->taxa->taxon + raw->taxid)->preferred_name, name->name);

	return name;
784 785 786
}


787
static ecorankidx_t* read_ranks_idx(const char* ranks_file_name)
788 789 790 791 792 793 794 795 796 797 798 799
{
	int32_t      			count;
	FILE*        			ranks_file;
	ecorankidx_t*           ranks_index;
	int32_t      			i;
	int32_t      			rank_length;
	char*					buffer;

	ranks_file = open_ecorecorddb(ranks_file_name, &count, 0);
	if (ranks_file==NULL)
		return NULL;

800
	ranks_index = (ecorankidx_t*) malloc(sizeof(ecorankidx_t) + sizeof(char*) * count);
801 802 803 804 805 806 807
	if (ranks_index == NULL)
	{
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError allocating memory for taxonomy rank structure");
 		fclose(ranks_file);
 		return NULL;
	}
808 809 810 811 812 813

	ranks_index->count = count;

	for (i=0; i < count; i++)
	{
		buffer = read_ecorecord(ranks_file, &rank_length);
814 815 816 817 818 819 820 821
		if (buffer == NULL)
		{
	 		obi_set_errno(OBI_TAXONOMY_ERROR);
	 		obidebug(1, "\nError reading a value in a taxonomy file");
	 		fclose(ranks_file);
	 		free(ranks_index);
	 		return NULL;
		}
822
		ranks_index->label[i] = (char*) malloc(rank_length+1);
823 824 825 826 827 828 829 830 831
		if (ranks_index->label[i] == NULL)
		{
	 		obi_set_errno(OBI_MALLOC_ERROR);
	 		obidebug(1, "\nError allocating memory for taxonomy rank label");
	 		fclose(ranks_file);
	 		free(ranks_index);
	 		free(buffer);
	 		return NULL;
		}
832
		strncpy(ranks_index->label[i], buffer, rank_length);
833
		(ranks_index->label[i])[rank_length] = 0;
834 835
	}

836 837
	fclose(ranks_file);

838 839 840 841
	return ranks_index;
}


842
static ecotxidx_t* read_taxonomy_idx(const char* taxa_file_name, const char* local_taxa_file_name)
843 844 845 846 847 848 849 850 851 852
{
	int32_t      	  count_taxa;
	int32_t      	  count_local_taxa;
	FILE*			  f_taxa;
	FILE* 			  f_local_taxa;
	ecotxidx_t*		  taxa_index;
	struct ecotxnode* t;
	int32_t      	  i;
	int32_t      	  j;

853
	f_taxa = open_ecorecorddb(taxa_file_name, &count_taxa, 1);
854 855 856 857 858 859 860 861
	if (f_taxa == NULL)
	{
 		obidebug(1, "\nError reading taxonomy taxa file");
 		return NULL;
	}

	f_local_taxa = open_ecorecorddb(local_taxa_file_name, &count_local_taxa, 0);

862
	taxa_index = (ecotxidx_t*) malloc(sizeof(ecotxidx_t) + sizeof(ecotx_t) * (count_taxa + count_local_taxa));
863 864 865 866 867 868 869 870
	if (taxa_index == NULL)
	{
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError allocating memory for taxonomy structure");
 		fclose(f_taxa);
 		fclose(f_local_taxa);
 		return NULL;
	}
871 872

	taxa_index->count = count_taxa + count_local_taxa;
873 874
	taxa_index->ncbi_count = count_taxa;
	taxa_index->local_count = count_local_taxa;
875 876 877 878 879 880 881
	taxa_index->buffer_size = taxa_index->count;

	taxa_index->max_taxid = 0;
	printf("Reading %d taxa...\n", count_taxa);
	for (i=0; i<count_taxa; i++)
	{
		readnext_ecotaxon(f_taxa, &(taxa_index->taxon[i]));
882
		taxa_index->taxon[i].idx = i;
883 884 885 886
		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)
			taxa_index->max_taxid = taxa_index->taxon[i].taxid;
887
		taxa_index->taxon[i].preferred_name = NULL;
888 889 890 891 892 893 894 895 896 897 898
	}

	if (count_local_taxa > 0)
		printf("Reading %d local taxa...\n", count_local_taxa);
	else
		printf("No local taxa\n");

	count_taxa = taxa_index->count;

	for (; i < count_taxa; i++){
		readnext_ecotaxon(f_local_taxa, &(taxa_index->taxon[i]));
899
		taxa_index->taxon[i].idx = i;
900 901 902 903
		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)
			taxa_index->max_taxid = taxa_index->taxon[i].taxid;
904
		taxa_index->taxon[i].preferred_name = NULL;
905 906 907 908 909 910 911 912 913
	}

	for (i=0; i < count_taxa; i++)
	{
		t = taxa_index->taxon+i;
		if (t->farest == -1)
		{
			t->farest=0;
            while (t->parent != t)
914 915 916 917 918 919 920 921 922 923 924
            {
            	j = t->farest + 1;
            	if (j > t->parent->farest)
            	{
            		t->parent->farest = j;
            		t=t->parent;
            	}
            	else
            		t = taxa_index->taxon;
            }
		}
925 926
	}

927 928 929 930 931
	fclose(f_taxa);
	if (f_local_taxa != NULL)
		fclose(f_local_taxa);

	return taxa_index;
932 933 934
}


935
static econameidx_t* read_names_idx(const char *file_name, OBIDMS_taxonomy_p taxonomy)
936 937 938 939 940 941 942 943
{
	int32_t      		count;
	FILE*				f;
	econameidx_t*		index_names;
	int32_t      		i;

	f = open_ecorecorddb(file_name, &count, 0);
	if (f == NULL)
944 945 946 947
		return NULL;

	index_names = (econameidx_t*) malloc(sizeof(econameidx_t) + sizeof(econame_t) * count);
	if (index_names == NULL)
948
	{
949
 		obi_set_errno(OBI_MALLOC_ERROR);
950 951 952
 		obidebug(1, "\nError reading taxonomy name file");
 		return NULL;
	}
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	index_names->count = count;

	for (i=0; i < count; i++)
	{
		readnext_econame(f, (index_names->names)+i, taxonomy);
		if ((index_names->names)+i == NULL)
		{
	 		obi_set_errno(OBI_TAXONOMY_ERROR);
	 		obidebug(1, "\nError reading taxonomy name file");
	 		free(index_names);
	 		return NULL;
		}
	}

	fclose(f);

	return index_names;
}


974
static econameidx_t* read_preferred_names_idx(const char *file_name, OBIDMS_taxonomy_p taxonomy)
975 976 977 978 979 980 981 982 983 984
{
	int32_t      		count;
	FILE*				f;
	econameidx_t*		index_names;
	int32_t      		i;

	f = open_ecorecorddb(file_name, &count, 0);
	if (f == NULL)
		return NULL;

985
	index_names = (econameidx_t*) malloc(sizeof(econameidx_t) + sizeof(econame_t) * count);
986 987 988 989 990 991
	if (index_names == NULL)
	{
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError reading taxonomy name file");
 		return NULL;
	}
992 993 994 995

	index_names->count = count;

	for (i=0; i < count; i++)
996
	{
997
		readnext_ecopreferredname(f, (index_names->names)+i, taxonomy);
998 999 1000 1001 1002 1003 1004 1005
		if ((index_names->names)+i == NULL)
		{
	 		obi_set_errno(OBI_TAXONOMY_ERROR);
	 		obidebug(1, "\nError reading taxonomy name file");
	 		free(index_names);
	 		return NULL;
		}
	}
1006

1007
	fclose(f);
1008

1009
	return index_names;
1010 1011 1012
}


1013
static ecomergedidx_t* read_merged_idx(const char *file_name, OBIDMS_taxonomy_p taxonomy)
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
{
	int32_t      		count;
	FILE*				f;
	ecomergedidx_t*		index_merged_idx;
	ecomerged_t*		merged_idx;
	int32_t      		i;
	int32_t             record_length;

	f = open_ecorecorddb(file_name, &count, 0);
	if (f == NULL)
	{
 		obidebug(1, "\nError reading taxonomy name file");
 		return NULL;
	}

	index_merged_idx = (ecomergedidx_t*) malloc(sizeof(ecomergedidx_t) + (sizeof(ecomerged_t) * count));
	if (index_merged_idx == NULL)
	{
 		obi_set_errno(OBI_MALLOC_ERROR);
 		obidebug(1, "\nError reading taxonomy name file");
 		return NULL;
	}

	index_merged_idx->count = count;

	for (i=0; i < count; i++)
	{
		merged_idx = read_ecorecord(f, &record_length);
		memcpy((index_merged_idx->merged)+i, merged_idx, record_length);
		if ((index_merged_idx->merged)+i == NULL)
		{
	 		obi_set_errno(OBI_TAXONOMY_ERROR);
	 		obidebug(1, "\nError reading taxonomy name file");
	 		free(index_merged_idx);
	 		return NULL;
		}
	}

	fclose(f);

	return index_merged_idx;
}


1058
static int write_ranks_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name)		// TODO prefix in taxonomy struct?
1059
{
1060 1061 1062 1063 1064 1065
	int     i;
	char* 	file_name;
	int 	file_descriptor;
	off_t 	file_size;
	char*   taxonomy_path;
	int32_t	length;
1066

1067 1068 1069
	// Compute file size
	file_size = sizeof(int32_t);
	for (i=0; i < (tax->ranks)->count; i++)
1070
	{
1071 1072
		file_size = file_size + sizeof(int32_t);	// To store label size
		file_size = file_size + strlen(((tax->ranks)->label)[i]);	// To store label
1073
	}
1074

1075
	// Build the taxonomy directory path
1076
	taxonomy_path = get_taxonomy_path(dms, taxonomy_name);
1077

1078
	file_name =	(char*) malloc((strlen(taxonomy_path) + strlen(taxonomy_name) + 6)*sizeof(char));
1079
	if (file_name == NULL)
1080
	{
1081 1082 1083
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating the memory for a binary taxonomy file name");
		return -1;
1084 1085
	}

1086 1087
	// Build the file path
	if (sprintf(file_name, "%s/%s.rdx", taxonomy_path, taxonomy_name) < 0)
1088
	{
1089 1090 1091
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError building a binary taxonomy file name");
		return -1;
1092
	}
1093 1094 1095 1096 1097 1098

	free(taxonomy_path);

	// Create file
	file_descriptor = open(file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
	if (file_descriptor < 0)
1099
	{
1100 1101 1102 1103
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError creating a binary taxonomy file %s", file_name);
		free(file_name);
		return -1;
1104
	}
1105 1106 1107 1108 1109

	free(file_name);

	// Truncate the file to the right size
	if (ftruncate(file_descriptor, file_size) < 0)
1110
	{
1111 1112 1113 1114
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError truncating a binary taxonomy file");
		close(file_descriptor);
		return -1;
1115
	}
1116 1117 1118

	// Write rank count
	if (write(file_descriptor, &((tax->ranks)->count), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
1119
	{
1120 1121 1122 1123
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError writing in a binary taxonomy file");
		close(file_descriptor);
		return -1;
1124 1125
	}

1126 1127
	// Write ranks
	for (i=0; i < (tax->ranks)->count; i++)
1128
	{
1129 1130 1131 1132
		length = strlen(((tax->ranks)->label)[i]);

		// Write rank size
		if (write(file_descriptor, &length, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
1133
		{
1134 1135 1136 1137
			obi_set_errno(OBI_TAXONOMY_ERROR);
			obidebug(1, "\nError writing in a binary taxonomy file");
			close(file_descriptor);
			return -1;
1138
		}
1139 1140
		// Write rank label
		if (write(file_descriptor, ((tax->ranks)->label)[i], length) < ((ssize_t) length))
1141
		{
1142 1143 1144 1145
			obi_set_errno(OBI_TAXONOMY_ERROR);
			obidebug(1, "\nError writing in a binary taxonomy file");
			close(file_descriptor);
			return -1;
1146 1147 1148
		}
	}

1149 1150 1151
	// Close file
	if (close(file_descriptor) < 0)
	{
1152 1153
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError closing an rdx taxonomy file");
1154 1155
		return -1;
	}
1156

1157
	return 0;
1158 1159 1160
}


1161
static int write_taxonomy_idx(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?
1162
{
1163 1164 1165 1166 1167 1168 1169
	int     i;
	char* 	file_name;
	int 	file_descriptor;
	off_t 	file_size;
	char*   taxonomy_path;
	int32_t	name_length;
	int32_t record_size;
1170

1171 1172 1173
	// Compute file size
	file_size = sizeof(int32_t);	// To store record count
	for (i=0; i < (tax->taxa)->ncbi_count; i++)
1174
	{
1175 1176 1177
		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
	}
1178

1179 1180
	// Build the taxonomy directory path
	taxonomy_path = get_taxonomy_path(dms, taxonomy_name);
1181

1182
	file_name =	(char*) malloc((strlen(taxonomy_path) + strlen(taxonomy_name) + 6)*sizeof(char));
1183 1184 1185 1186 1187
	if (file_name == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating the memory for a binary taxonomy file name");
		return -1;
1188 1189
	}

1190 1191
	// Build the file path
	if (sprintf(file_name, "%s/%s.tdx", taxonomy_path, taxonomy_name) < 0)
1192 1193
	{
		obi_set_errno(OBI_TAXONOMY_ERROR);
1194 1195
		obidebug(1, "\nError building a binary taxonomy file name");
		return -1;
1196 1197
	}

1198
	free(taxonomy_path);
1199

1200 1201 1202
	// Create file
	file_descriptor = open(file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
	if (file_descriptor < 0)
1203 1204
	{
		obi_set_errno(OBI_TAXONOMY_ERROR);
1205 1206 1207
		obidebug(1, "\nError creating a binary taxonomy file");
		free(file_name);
		return -1;
1208 1209
	}

1210
	free(file_name);
1211

1212 1213
	// Truncate the file to the right size
	if (ftruncate(file_descriptor, file_size) < 0)
1214 1215
	{
		obi_set_errno(OBI_TAXONOMY_ERROR);
1216 1217 1218
		obidebug(1, "\nError truncating a binary taxonomy file");
		close(file_descriptor);
		return -1;
1219 1220
	}

1221 1222
	// Write record count
	if (write(file_descriptor, &(tax->taxa->ncbi_count), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
1223 1224
	{
		obi_set_errno(OBI_TAXONOMY_ERROR);
1225 1226 1227
		obidebug(1, "\nError writing in a binary taxonomy file");
		close(file_descriptor);
		return -1;
1228 1229
	}

1230 1231
	// Write records
	for (i=0; i < (tax->taxa)->ncbi_count; i++)
1232
	{
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
		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;
		}
1284 1285
	}

1286 1287
	// Close file
	if (close(file_descriptor) < 0)
1288
	{
1289 1290
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError closing a tdx taxonomy file");
1291
		return -1;
1292 1293
	}

1294
	return 0;
1295 1296
}

1297

1298
static int write_local_taxonomy_idx(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?
1299 1300 1301 1302 1303 1304
{
	int     i;
	char* 	file_name;
	int 	file_descriptor;
	off_t 	file_size;
	char*   taxonomy_path;
1305 1306
	int32_t	name_length;
	int32_t record_size;
1307 1308

	// Compute file size
1309 1310
	file_size = sizeof(int32_t);	// To store record count
	for (i=(tax->taxa)->ncbi_count; i < (tax->taxa)->count; i++)
1311
	{
1312 1313
		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
1314 1315 1316 1317 1318
	}

	// Build the taxonomy directory path
	taxonomy_path = get_taxonomy_path(dms, taxonomy_name);

1319
	file_name =	(char*) malloc((strlen(taxonomy_path) + strlen(taxonomy_name) + 6)*sizeof(char));
1320 1321 1322 1323 1324 1325 1326 1327
	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
1328
	if (sprintf(file_name, "%s/%s.ldx", taxonomy_path, taxonomy_name) < 0)
1329 1330 1331 1332 1333 1334 1335 1336 1337
	{
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError building a binary taxonomy file name");
		return -1;
	}

	free(taxonomy_path);

	// Create file
1338
	file_descriptor = open(file_name, O_RDWR | O_CREAT, 0777);
1339 1340 1341
	if (file_descriptor < 0)
	{
		obi_set_errno(OBI_TAXONOMY_ERROR);
1342
		obidebug(1, "\nError creating a binary taxonomy file");
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
		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;
	}

1358 1359
	// Write record count
	if (write(file_descriptor, &((tax->taxa)->local_count), sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
1360 1361 1362 1363 1364 1365 1366
	{
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError writing in a binary taxonomy file");
		close(file_descriptor);
		return -1;
	}

1367 1368
	// Write records
	for (i=(tax->taxa)->ncbi_count; i < (tax->taxa)->count; i++)
1369
	{
1370 1371
		name_length = strlen(tax->taxa->taxon[i].name);
		record_size = 4*sizeof(int32_t) + name_length;
1372

1373 1374
		// Write record size
		if (write(file_descriptor, &record_size, sizeof(int32_t)) < ((ssize_t) sizeof(int32_t)))
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
		{
			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)
	{
1426 1427
		obi_set_errno(OBI_TAXONOMY_ERROR);
		obidebug(1, "\nError closing a ldx taxonomy file");
1428 1429 1430 1431 1432 1433 1434
		return -1;
	}

	return 0;
}


1435
static int write_names_idx(OBIDMS_p dms, OBIDMS_taxonomy_p tax