obidms_taxonomy.h 14.4 KB
Newer Older
1
/********************************************************************
2
 * OBIDMS taxonomy header file                                      *
3 4 5 6 7 8
 ********************************************************************/

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


#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#include "obidms.h"


20 21 22 23
#define MIN_LOCAL_TAXID (10000000)     	  /**< The minimum taxid for a taxon added locally (i.e. not an NCBI taxon).
                                	       */
#define TAX_NAME_LEN (1024)				  /**< The maximum length for the taxonomy name.
                                	       */
24 25


26 27 28
/**
 * @brief Structure for a taxon as stored in a .tdx file.
 */
29
typedef struct {
30 31 32 33 34 35 36 37 38 39
	int32_t  taxid;        /**< Taxid.
	 	 	    		    */
	int32_t  rank;         /**< Rank index.
	    				    */
	int32_t	 parent;       /**< Index, in the taxid index, of the parent node in the taxonomic tree.
	    					*/
	int32_t  name_length;  /**< Length of the taxon scientific name.
							*/
	char     name[];	   /**< Scientific name of the taxon.
							*/
40 41 42
} ecotxformat_t;


43 44 45
/**
 * @brief Structure for a taxon as stored in a taxonomy structure.
 */
46
typedef struct ecotxnode {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	int32_t           taxid;		 	/**< Taxid.		// TODO discuss that this is will be the current taxid even if the struct was accessed through a deprecated one
	    								 */
	int32_t           rank;		        /**< Rank index in ecorankidx_t structure.
	 	 	 	 	 	 	 	 	 	 */
	int32_t  		  farest;		    /**< Longest branch length, used to compute distances between taxa faster.
	 	 	 	 	 	 	 	 	 	 */
	int32_t			  idx;			    /**< Index in the ecotxidx_t structure.
	 	 	 	 	 	 	 	 	 	 */
	struct ecotxnode* parent;			/**< Pointer on the parent node in the taxonomic tree.
	 	 	 	 	 	 	 	 	 	 */
	char*			  name;		   		/**< Scientific name of the taxon.
										 */
	char*			  preferred_name;	/**< Preferred name of the taxon if there is one, otherwise NULL.
										 */
	bool			  local;			/**< A boolean indicating whether the taxon is local or not.
	 	 	 	 	 	 	 	 	 	 */
63 64 65
} ecotx_t;


66 67 68
/**
 * @brief Structure for the taxon index in a taxonomy structure.
 */
69
typedef struct {
70 71 72 73 74 75 76 77 78 79 80 81
	int32_t count;			/**< Number of taxa.
	 	 	 	 	 	 	 */
	int32_t ncbi_count;		/**< Number of NCBI taxa.
	 	 	 	 	 	 	 */
	int32_t local_count;	/**< Number of taxa added locally.
 	 	 	 	 	 	 	 */
	int32_t max_taxid;		/**< Maximum taxid existing in the taxon index.
	 	 	 	 	 	 	 */
	int32_t buffer_size;	/**< Number of taxa.	// TODO kept this but not sure of its use
 	 	 	 	 	 	 	 */
	ecotx_t taxon[];		/**< Taxon array.
 	 	 	 	 	 	 	 */
82 83 84
} ecotxidx_t;


85 86 87
/**
 * @brief Structure for the rank index in a taxonomy structure.
 */
88
typedef struct {
89 90 91 92
	int32_t count;		/**< Number of ranks.
 	 	 	 	 	 	 */
	char*   label[];	/**< Array of rank names.
	 	 	 	 	 	 */
93 94 95
} ecorankidx_t;


96 97 98
/**
 * @brief Structure for a taxon name as stored in a .ndx file.
 */
99
typedef struct {
100 101 102 103 104 105 106 107 108 109
 	int32_t  is_scientific_name;	/**< A boolean indicating whether the name is a scientific name or not.
	 	 	 	 	 	 	 	 	 */
	int32_t  name_length;			/**< The name length.
	 	 	 	 	 	 	 	 	 */
	int32_t  class_length;			/**< The name class length.
	 	 	 	 	 	 	 	 	 */
	int32_t  taxid;       			/**< Index of the taxon in the taxid index.
									 */
	char     names[];       		/**< Taxon name and name class concatenated.
	 	 	 	 	 	 	 	 	 */
110 111 112
} econameformat_t;


113 114 115
/**
 * @brief Structure for a taxon name as stored in a taxonomy structure.
 */
116
typedef struct {
117 118 119 120 121 122 123 124
	char*   		  name;					/**< Taxon name.
	 	 	 	 	 	 	 	 	 	 	 */
	char*   		  class_name;			/**< Name class.
	 	 	 	 	 	 	 	 	 	 	 */
	int32_t 		  is_scientific_name;	/**< A boolean indicating whether the name is a scientific name or not.
	 	 	 	 	 	 	 	 	 	 	 */
	struct ecotxnode* taxon;				/**< Pointer on the taxon in the taxon index.
	 	 	 	 	 	 	 	 	 	 	 */
125 126 127
} econame_t;


128 129 130
/**
 * @brief Structure for the name index in a taxonomy structure.
 */
131
typedef struct {
132 133 134 135
	int32_t   count;		/**< Number of names.
	 	 	 	 	 	 	 */
	econame_t names[];		/**< Array of names.
	 	 	 	 	 	 	 */
136 137 138
} econameidx_t;


139 140 141
/**
 * @brief Structure for a taxid/index pair as stored in a taxonomy structure.
 */
142
typedef struct {
143 144 145 146
	int32_t taxid;		/**< Taxid.
	 	 	 	 	 	 */
	int32_t idx;		/**< Index of the taxid in the taxon index, -1 if the taxid is deprecated.
	 	 	 	 	 	 */
147 148 149
} ecomerged_t;


150 151 152 153 154 155 156
/**
 * @brief Structure for a merged taxid index in a taxonomy structure.
 *
 * This index includes all deprecated taxids that now refer to different taxids, and
 * the deprecated taxids that are deleted.
 *
 */
157
typedef struct {
158 159 160 161
	int32_t     count;		/**< Number of taxid/index pairs.
 	 	 	 	 	 	 	 */
	ecomerged_t merged[];	/**< Array of taxid/index pairs.
	 	 	 	 	 	 	 */
162 163 164
} ecomergedidx_t;


165 166 167
/**
 * @brief Structure for a taxonomy.
 */
168
typedef struct OBIDMS_taxonomy_t {
169 170 171 172 173 174 175 176 177 178 179 180 181 182
	char            tax_name[TAX_NAME_LEN];		/**< Taxonomy name.
	 	 	 	 	 	 	 	 	 	 	 	 */
	OBIDMS_p        dms;						/**< A pointer on the DMS to which the taxonomy belongs.
 	 	 	 	 	 	 	 	 	 	 	 	 */
	ecomergedidx_t* merged_idx;					/**< Merged taxid index.
	 	 	 	 	 	 	 	 	 	 	 	 */
	ecorankidx_t*   ranks;						/**< Taxonomic ranks.
	 	 	 	 	 	 	 	 	 	 	 	 */
	econameidx_t*   names;						/**< Taxon names.
 	 	 	 	 	 	 	 	 	 	 	 	 */
	econameidx_t*   preferred_names;			/**< Taxon preferred names (i.e. added locally).
	 	 	 	 	 	 	 	 	 	 	 	 */
	ecotxidx_t*     taxa;						/**< Taxa.
	 	 	 	 	 	 	 	 	 	 	 	 */
183 184 185
} OBIDMS_taxonomy_t, *OBIDMS_taxonomy_p;


186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

/**
 * @brief Function checking whether a taxonomy is already registered in a DMS using its name.
 *
 * @param dms The path to the taxdump directory.
 *
 * @param dms A pointer on the DMS.
 * @param taxonomy_name The name (prefix) of the taxonomy.
 *
 * @retval 1 if the taxonomy exists.
 * @retval 0 if the taxonomy does not exist
 * @retval -1 if an error occurred.
 *
 * @since June 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int obi_taxonomy_exists(OBIDMS_p dms, const char* taxonomy_name);


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
/**
 * @brief Function reading an NCBI taxdump and loading its information into a taxonomy structure.
 *
 * @param taxdump The path to the taxdump directory.
 *
 * @returns A pointer on the read taxonomy structure.
 * @retval NULL if an error occurred.
 *
 * @since 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
OBIDMS_taxonomy_p obi_read_taxdump(const char* taxdump);


/**
 * @brief Function reading a binary taxonomy database (i.e. a set of .tdx, .ndx, .rdx, .adx, .ldx, .pdx files)
 * 		  and loading its information into a taxonomy structure.
 *
 * @param dms A pointer on the DMS to which the taxonomy belongs.
 * @param taxonomy_name The name (prefix) of the taxonomy.
 * @param read_alternative_names A boolean indicating whether names other than scientific and preferred names should be read.
 *
 * @returns A pointer on the read taxonomy structure.
 * @retval NULL if an error occurred.
 *
 * @since 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
233 234
OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, bool read_alternative_names);

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

/**
 * @brief Function writing a binary taxonomy database (i.e. a set of .tdx, .ndx, .rdx, .adx, .ldx, .pdx files).
 *
 * @param dms A pointer on the DMS to which the taxonomy belongs.
 * @param tax A pointer on the taxonomy structure.
 * @param tax_name The name (prefix) 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)
 */
int obi_write_taxonomy(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* tax_name);


/**
 * @brief Function closing a taxonomy structure.
 *
 * This function writes all changes to the binary files (local taxa and preferred names) and free all allocated memory for the structure.
 *
 * @param taxonomy 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 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
267 268
int obi_close_taxonomy(OBIDMS_taxonomy_p taxonomy);

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

/**
 * @brief Function adding a local taxon to a taxonomy.
 *
 * @param tax A pointer on the taxonomy structure.
 * @param name The taxon scientific name.
 * @param rank_name The taxon rank name.
 * @param parent_taxid The taxid of the parent node in the taxonomic tree.
 * @param min_taxid The minimum taxid to give to the new taxon (the function will choose a new taxid >= min_taxid and >= MIN_LOCAL_TAXID).
 *
 * @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)
 */
int obi_taxo_add_local_taxon(OBIDMS_taxonomy_p tax, const char* name, const char* rank_name, int32_t parent_taxid, int32_t min_taxid);


/**
 * @brief Function adding a preferred name to a taxon in a taxonomy, referred to by its taxid.
 *
 * @param tax A pointer on the taxonomy structure.
 * @param taxid The taxid of the taxon that should have a new preferred name.
 * @param preferred_name The new preferred name.
 *
 * @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)
 */
int obi_taxo_add_preferred_name_with_taxid(OBIDMS_taxonomy_p tax, int32_t taxid, const char* preferred_name);


/**
 * @brief Function adding a preferred name to a taxon in a taxonomy, referred to by the taxon pointer.
 *
 * @param tax A pointer on the taxonomy structure.
 * @param taxon A pointer on the taxon that should have a new preferred name.
 * @param preferred_name The new preferred name.
 *
 * @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)
 */
int obi_taxo_add_preferred_name_with_taxon(OBIDMS_taxonomy_p tax, ecotx_t* taxon, const char* preferred_name);


323 324 325 326 327 328 329 330 331 332 333 334
/**
 * @brief Function returning the last common ancestor of two taxa.
 *
 * @param taxon1 A pointer on the first taxon.
 * @param taxon2 A pointer on the second taxon.
 *
 * @returns A pointer on the last common ancestor of the two taxa.
 * @retval NULL if an error occurred.
 */
ecotx_t* obi_taxo_get_lca(ecotx_t* taxon1, ecotx_t* taxon2);


335 336 337 338 339 340 341
/**
 * @brief Function returning the parent of a taxon at a given rank.
 *
 * @param taxon A pointer on the taxon.
 * @param rankidx The index of the rank wanted.
 *
 * @returns A pointer on the parent taxon at the wanted rank.
342
 * @retval NULL if no parent taxon was found at the wanted rank or if an error occurred.
343
 */
344 345
ecotx_t* obi_taxo_get_parent_at_rank(ecotx_t* taxon, int32_t rankidx);

346 347 348 349 350 351 352 353

/**
 * @brief Function returning a taxon given its taxid.
 *
 * @param taxonomy A pointer on the taxonomy.
 * @param taxid The taxid of the taxon.
 *
 * @returns A pointer on the wanted taxon.
354
 * @retval NULL if no taxon was found with the given taxid or if an error occurred.
355 356 357 358
 *
 * @since January 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
359 360 361
ecotx_t* obi_taxo_get_taxon_with_taxid(OBIDMS_taxonomy_p taxonomy, int32_t taxid);


362 363 364 365 366 367
/**
 * @brief Function checking whether a taxon is under another in the taxonomy tree.
 *
 * @param taxon A pointer on the first taxon.
 * @param other_taxid The taxid of the second taxon.
 *
368 369 370 371
 * @returns A value indicating whether the first taxon is under the second taxon in the taxonomy tree.
 * @retval 0 if the first taxon is not under the second taxon in the taxonomy tree.
 * @retval 1 if the first taxon is under the second taxon in the taxonomy tree.
 * @retval -1 if an error occurred.
372
 */
373
int obi_taxo_is_taxon_under_taxid(ecotx_t* taxon, int32_t other_taxid);
374 375


376 377 378 379 380 381 382 383 384 385
/**
 * @brief Function returning the parent of a taxon at the species level.
 *
 * @param taxon A pointer on the taxon.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on the parent taxon at the species level.
 * @retval NULL if no parent taxon was found at the wanted rank.
 */
ecotx_t* obi_taxo_get_species(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy);
386 387


388 389 390 391 392 393 394 395 396 397
/**
 * @brief Function returning the parent of a taxon at the genus level.
 *
 * @param taxon A pointer on the taxon.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on the parent taxon at the genus level.
 * @retval NULL if no parent taxon was found at the wanted rank.
 */
ecotx_t* obi_taxo_get_genus(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy);
398

399

400 401 402 403 404 405 406 407 408 409
/**
 * @brief Function returning the parent of a taxon at the family level.
 *
 * @param taxon A pointer on the taxon.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on the parent taxon at the family level.
 * @retval NULL if no parent taxon was found at the wanted rank.
 */
ecotx_t* obi_taxo_get_family(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy);
410 411


412 413 414 415 416 417 418 419 420 421
/**
 * @brief Function returning the parent of a taxon at the kingdom level.
 *
 * @param taxon A pointer on the taxon.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on the parent taxon at the kingdom level.
 * @retval NULL if no parent taxon was found at the wanted rank.
 */
ecotx_t* obi_taxo_get_kingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy);
422 423


424 425 426 427 428 429 430 431 432 433
/**
 * @brief Function returning the parent of a taxon at the superkingdom level.
 *
 * @param taxon A pointer on the taxon.
 * @param taxonomy A pointer on the taxonomy structure.
 *
 * @returns A pointer on the parent taxon at the superkingdom level.
 * @retval NULL if no parent taxon was found at the wanted rank.
 */
ecotx_t* obi_taxo_get_superkingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy);
434

435 436 437 438 439 440 441 442 443 444 445 446 447

/**
 * @brief Function returning the label of a rank in an ecorankidx_t structure.
 *
 * @param rank_idx The index of the rank.
 * @param ranks A pointer on an ecorankidx_t structure.
 *
 * @returns The label of a rank in the ecorankidx_t structure.
 * @retval NULL if there is no rank at that index.
 *
 * @see rank_label_to_index()
 */
const char* obi_taxo_rank_index_to_label(int32_t rank_idx, ecorankidx_t* ranks);
448 449 450 451 452 453 454


// TODO
int obi_taxo_is_taxid_included(OBIDMS_taxonomy_p taxonomy,
			     			   int32_t* restrict_to_taxids,
				    		   int32_t count,
					    	   int32_t taxid);