ecotax.c 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "ecoPCR.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

static ecotx_t *readnext_ecotaxon(FILE *f,ecotx_t *taxon);

 /**
 * Open the taxonomy database
 * @param	pointer to the database (.tdx file)
 * @return	a ecotxidx_t structure
 */
13
ecotxidx_t     *read_taxonomyidx(const char *filename,const char *filename2)
14 15
{
	int32_t      count;
16
	int32_t      count2;
17
	FILE         *f;
18
	FILE         *f2;
19 20 21
	ecotxidx_t *index;
	int32_t      i;

22 23
	f  = open_ecorecorddb(filename,&count,1);
	f2 = open_ecorecorddb(filename2,&count2,0);
24

25 26 27


	index = (ecotxidx_t*) ECOMALLOC(sizeof(ecotxidx_t) + sizeof(ecotx_t) * (count+count2-1),
28 29
	                                  "Allocate taxonomy");

30 31 32
	index->count=count+count2;

	fprintf(stderr,"Reading %d taxa...\n",count);
33 34
	for (i=0; i < count; i++){
		readnext_ecotaxon(f,&(index->taxon[i]));
35
		index->taxon[i].parent=index->taxon + (int32_t)index->taxon[i].parent;
36
	}
37 38 39 40 41 42 43 44 45 46 47

	if (count2>0)
		fprintf(stderr,"Reading %d local taxa...\n",count2);
	else
		fprintf(stderr,"No local taxon\n");

	for (i=0; i < count2; i++){
		readnext_ecotaxon(f2,&(index->taxon[count+i]));
		index->taxon[count+i].parent=index->taxon + (int32_t)index->taxon[count+i].parent;
	}

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
	return index;
}


int32_t delete_taxonomy(ecotxidx_t *index)
{
	int32_t i;

	if (index)
	{
		for (i=0; i< index->count; i++)
			if (index->taxon[i].name)
				ECOFREE(index->taxon[i].name,"Free scientific name");

		ECOFREE(index,"Free Taxonomy");

		return 0;
	}

	return 1;
}



int32_t delete_taxon(ecotx_t *taxon)
{
	if (taxon)
	{
		if (taxon->name)
			ECOFREE(taxon->name,"Free scientific name");

		ECOFREE(taxon,"Free Taxon");

		return 0;
	}

	return 1;
}


/**
 * Read the database for a given taxon a save the data
 * into the taxon structure(if any found)
 * @param	*f	pointer to FILE type returned by fopen
 * @param	*taxon	pointer to the structure
 *
 * @return	a ecotx_t structure if any taxon found else NULL
 */
ecotx_t *readnext_ecotaxon(FILE *f,ecotx_t *taxon)
{

	ecotxformat_t *raw;
	int32_t  rs;

	raw = read_ecorecord(f,&rs);

	if (!raw)
		return NULL;

	if (is_big_endian())
	{
		raw->namelength = swap_int32_t(raw->namelength);
		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;
	taxon->rank   = raw->rank;

	taxon->name   = ECOMALLOC((raw->namelength+1) * sizeof(char),
	                          "Allocate taxon scientific name");

	strncpy(taxon->name,raw->name,raw->namelength);

	return taxon;
}


ecotaxonomy_t    *read_taxonomy(const char *prefix,int32_t readAlternativeName)
{
	ecotaxonomy_t *tax;
	char          *filename;
132
	char          *filename2;
133 134 135
	int           buffsize;

	tax = ECOMALLOC(sizeof(ecotaxonomy_t),
136
					"Allocate taxonomy structure");
137 138 139 140

	buffsize = strlen(prefix)+10;

	filename = ECOMALLOC(buffsize,
141 142 143
						 "Allocate filename");
	filename2= ECOMALLOC(buffsize,
						 "Allocate filename");
144 145 146 147 148 149

	snprintf(filename,buffsize,"%s.rdx",prefix);

	tax->ranks = read_rankidx(filename);

	snprintf(filename,buffsize,"%s.tdx",prefix);
150
	snprintf(filename2,buffsize,"%s.ldx",prefix);
151

152
	tax->taxons = read_taxonomyidx(filename,filename2);
153 154 155

	if (readAlternativeName)
	{
156
	   snprintf(filename,buffsize,"%s.ndx",prefix);
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
	   tax->names=read_nameidx(filename,tax);
	}
	else
	   tax->names=NULL;
	return tax;
}



int32_t delete_ecotaxonomy(ecotaxonomy_t *taxonomy)
{
	if (taxonomy)
	{
		if (taxonomy->ranks)
			ECOFREE(taxonomy->ranks,"Free rank index");

		if (taxonomy->taxons)
			ECOFREE(taxonomy->taxons,"Free taxon index");

		ECOFREE(taxonomy,"Free taxonomy structure");

		return 0;
	}

	return 1;
}

ecotx_t *eco_findtaxonatrank(ecotx_t *taxon,
                                int32_t rankidx)
{
	ecotx_t *current_taxon;
	ecotx_t *next_taxon;

	current_taxon = taxon;
	next_taxon    = current_taxon->parent;

	while ((current_taxon!=next_taxon) &&  // I' am the root node
		   (current_taxon->rank!=rankidx))
		   {
		   	current_taxon = next_taxon;
		   	next_taxon    = current_taxon->parent;
		   }

	if (current_taxon->rank==rankidx)
		return current_taxon;
	else
		return NULL;
}

/**
 * Get back information concerning a taxon from a taxonomic id
 * @param 	*taxonomy 	the taxonomy database
 * @param	taxid		the taxonomic id
 *
 * @result	a ecotx_t structure containing the taxonimic information
 **/
ecotx_t *eco_findtaxonbytaxid(ecotaxonomy_t *taxonomy,
							  int32_t taxid)
{
	ecotx_t    *current_taxon;
	int32_t     taxoncount;
	int32_t     i;

	taxoncount=taxonomy->taxons->count;

	for (current_taxon=taxonomy->taxons->taxon,
	     i=0;
	     i < taxoncount;
	     i++,
	     current_taxon++){
	     if (current_taxon->taxid==taxid){
	     	return current_taxon;
	     }
	 }

	return (ecotx_t*)NULL;
}

/**
 * Find out if taxon is son of other taxon (identified by its taxid)
 * @param	*taxon son 		taxon
 * @param	parent_taxid 	taxonomic id of the other taxon
 *
 * @return 	1 is the other taxid math a parent taxid, else 0
 **/
int eco_isundertaxon(ecotx_t *taxon,
						int other_taxid)
{
	ecotx_t *next_parent;

	next_parent = taxon->parent;

	while ( (other_taxid != next_parent->taxid) &&
			(strcmp(next_parent->name, "root")) )
	{
		next_parent = next_parent->parent;
	}

	if (other_taxid == next_parent->taxid)
		return 1;
	else
		return 0;
}

ecotx_t *eco_getspecies(ecotx_t *taxon,
						ecotaxonomy_t *taxonomy)
{
	static ecotaxonomy_t *tax=NULL;
	static int32_t		 rankindex=-1;

	if (taxonomy && tax!=taxonomy)
	{
		rankindex = rank_index("species",taxonomy->ranks);
		tax=taxonomy;
	}

	if (!tax || rankindex < 0)
		ECOERROR(ECO_ASSERT_ERROR,"No taxonomy defined");

	return eco_findtaxonatrank(taxon,rankindex);
}

ecotx_t *eco_getgenus(ecotx_t *taxon,
						ecotaxonomy_t *taxonomy)
{
	static ecotaxonomy_t *tax=NULL;
	static int32_t		 rankindex=-1;

	if (taxonomy && tax!=taxonomy)
	{
		rankindex = rank_index("genus",taxonomy->ranks);
		tax=taxonomy;
	}

	if (!tax || rankindex < 0)
		ECOERROR(ECO_ASSERT_ERROR,"No taxonomy defined");

	return eco_findtaxonatrank(taxon,rankindex);
}


ecotx_t *eco_getfamily(ecotx_t *taxon,
						ecotaxonomy_t *taxonomy)
{
	static ecotaxonomy_t *tax=NULL;
	static int32_t		 rankindex=-1;

	if (taxonomy && tax!=taxonomy)
	{
		rankindex = rank_index("family",taxonomy->ranks);
		tax=taxonomy;
	}

	if (!tax || rankindex < 0)
		ECOERROR(ECO_ASSERT_ERROR,"No taxonomy defined");

	return eco_findtaxonatrank(taxon,rankindex);
}

ecotx_t *eco_getkingdom(ecotx_t *taxon,
						ecotaxonomy_t *taxonomy)
{
	static ecotaxonomy_t *tax=NULL;
	static int32_t		 rankindex=-1;

	if (taxonomy && tax!=taxonomy)
	{
		rankindex = rank_index("kingdom",taxonomy->ranks);
		tax=taxonomy;
	}

	if (!tax || rankindex < 0)
		ECOERROR(ECO_ASSERT_ERROR,"No taxonomy defined");

	return eco_findtaxonatrank(taxon,rankindex);
}

ecotx_t *eco_getsuperkingdom(ecotx_t *taxon,
						ecotaxonomy_t *taxonomy)
{
	static ecotaxonomy_t *tax=NULL;
	static int32_t		 rankindex=-1;

	if (taxonomy && tax!=taxonomy)
	{
		rankindex = rank_index("superkingdom",taxonomy->ranks);
		tax=taxonomy;
	}

	if (!tax || rankindex < 0)
		ECOERROR(ECO_ASSERT_ERROR,"No taxonomy defined");

	return eco_findtaxonatrank(taxon,rankindex);
}