ecoprimer.c 29.1 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * ecoprimer.c
 *
 *  Created on: 7 nov. 2008
 *      Author: coissac
 */

#include "libecoprimer/ecoprimer.h"
9
#include "libecoprimer/PrimerSets.h"
10 11 12 13 14 15 16
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#include <sys/time.h>
17
#include <dlfcn.h>
18 19 20
#include"libthermo/nnparams.h"
#include"libthermo/thermostats.h"

21

22
#define VERSION "0.3"
23
  /* TR: by default, statistics are made on species level*/
Eric Coissac committed
24 25 26
#define DEFAULTTAXONRANK "species"

static int cmpprintedpairs(const void* p1,const void* p2);
27
//float _Z27calculateMeltingTemperature_ (char * seq1, char * seq2);
Eric Coissac committed
28

29 30
void* lib_handle = NULL;
float (*calcMelTemp)(char*, char*);
31 32 33 34 35 36 37 38

/* ----------------------------------------------- */
/* printout help                                   */
/* ----------------------------------------------- */
#define PP fprintf(stdout,

static void PrintHelp()
{
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
			  PP      "------------------------------------------\n");
			  PP      " ecoPrimer Version %s\n", VERSION);
			  PP      "------------------------------------------\n");
              PP      "synopsis : finding primers and measureing the quality of primers and barcode region\n");
              PP      "usage: ./ecoPrimer [options] \n");
              PP      "------------------------------------------\n");
              PP      "options:\n");
              PP      "-d    : [D]atabase : to match the expected format, the database\n");
              PP      "        has to be formated first by the ecoPCRFormat.py program located.\n");
              PP      "        in the ecoPCR/tools directory.\n");
              PP      "        ecoPCRFormat.py creates three file types :\n");
              PP      "            .sdx : contains the sequences\n");
              PP      "            .tdx : contains information concerning the taxonomy\n");
              PP      "            .rdx : contains the taxonomy rank\n\n");
              PP      "        ecoPrimer needs all the file type. As a result, you have to write the\n");
Eric Coissac committed
54
              PP      "        database radical without any extension. For example /ecoPrimerDB/fstvert\n\n");
55 56
              PP      "-e    : [E]rror : max error allowed by oligonucleotide (0 by default)\n\n");
              PP      "-h    : [H]elp - print <this> help\n\n");
57
              PP      "-i    : [I]gnore the given taxonomy id (define the counterexample taxon set).\n\n");
58 59
              PP      "-l    : minimum [L]ength : define the minimum amplication length. \n\n");
              PP      "-L    : maximum [L]ength : define the maximum amplicationlength. \n\n");
60
              PP      "-r    : [R]estricts the search to the given taxonomic id (restrict the example taxon set).\n\n");
61
              PP      "-E    : [E]xception taxid allows to indicate than some subclade of example sequences are conterexamples.\n\n");
62
              PP      "-c    : Consider that the database sequences are [c]ircular\n\n");
63
              PP      "-3 	 : Three prime strict match\n\n");
64 65 66 67 68
              PP      "-q    : Strict matching [q]uorum, percentage of the sequences in which strict primers are found. By default it is 70\n\n");
              PP      "-s    : [S]ensitivity quorum\n\n");
              PP      "-t    : required [t]axon level for results, by default the results are computed at species level\n\n");
              PP      "-x    : false positive quorum\n\n");
              PP      "-D    : set in [d]ouble strand mode\n\n");
69
              PP      "-O    : set the primer length (default 18) \n\n");
70
              PP      "-S    : Set in [s]ingle strand mode\n\n");
71 72
              PP      "-m    : Salt correction method for Tm computation (SANTALUCIA : 1 or OWCZARZY:2, default=1)\n\n");
              PP      "-a    : Salt contentration in M for Tm computation (default 0.05 M)\n\n");
73
              PP      "-U    : No multi match\n\n");
74 75 76 77
              PP      "-U    : Define the [R]eference sequence identifier (must be part of example set)\n\n");
              PP      "-A    : Print the list of all identifier of sequences present in the database\n\n");
              PP      "-f    : Remove data mining step during  strict primer identification\n\n");
              PP      "-v    : Store statistic file about memory usage during strict primer identification\n\n");
78
              PP      "-p    : Print sets of primers\n\n");
79
			  PP      "-T    : Ignore pairs having specificity below this Threshold\n\n");
80 81 82
              PP      "\n");
              PP      "------------------------------------------\n");
              PP      "Table result description : \n");
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
              PP      "column  1 : serial number\n");
              PP      "column  2 : primer1\n");
              PP      "column  3 : primer2\n");
              PP      "column  4 : primer1 Tm without mismatch\n");
              PP      "column  5 : primer1 lowest Tm against exemple sequences\n");
              PP      "column  6 : primer2 Tm without mismatch\n");
              PP      "column  7 : primer2 lowest Tm against exemple sequences\n");
              PP      "column  8 : primer1 G+C count\n");
              PP      "column  9 : primer2 G+C count\n");
              PP      "column 10 : good/bad\n");
              PP      "column 11 : amplified example sequence count\n");
              PP      "column 12 : amplified counterexample sequence count\n");
              PP      "column 13 : yule\n");
              PP      "column 14 : amplified example taxa count\n");
              PP      "column 15 : amplified counterexample taxa count\n");
              PP      "column 16 : ratio of amplified example taxa versus all example taxa (Bc index)\n");
              PP      "column 17 : unambiguously identified example taxa count\n");
              PP      "column 18 : ratio of specificity unambiguously identified example taxa versus all example taxa (Bs index)\n");
              PP      "column 19 : minimum amplified length\n");
              PP      "column 20 : maximum amplified length\n");
              PP      "column 21 : average amplified length\n");
104 105
              PP      "------------------------------------------\n");
              PP		" http://www.grenoble.prabi.fr/trac/ecoPrimer/\n");
Eric Coissac committed
106
              PP      "------------------------------------------\n\n");
107 108
              PP      "\n");

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

static void ExitUsage(int stat)
{
        PP      "usage: ecoprimer [-d database] [-l value] [-L value] [-e value] [-r taxid] [-i taxid] [-R rank] [-t taxon level]\n");
        PP      "type \"ecoprimer -h\" for help\n");

        if (stat)
            exit(stat);
}

#undef  PP

void initoptions(poptions_t options)
{
124
	options->statistics=FALSE;
125
	options->filtering=TRUE;
126
	options->lmin=0;                   //< Amplifia minimal length
127
	options->lmax=1000;                   //< Amplifia maximal length
128 129 130 131
	options->error_max=3;              //**< maximum error count in fuzzy search
	options->primer_length=18;          //**< minimal length of the primers
	options->restricted_taxid=NULL;      //**< limit amplification below these taxid
	options->ignored_taxid=NULL;         //**< no amplification below these taxid
132
	options->exception_taxid=NULL;         //**< no amplification below these taxid
133
	options->prefix=NULL;
134 135
	options->reference=NULL;
	options->refseq=NULL;
136 137
	options->circular=0;
	options->doublestrand=1;
138
	options->strict_quorum=0.7;
139
	options->strict_exclude_quorum=0.1;
140
	options->sensitivity_quorum=0.9;
141
	options->false_positive_quorum=0.1;
142
	options->strict_three_prime=0;
143 144
	options->r=0;
	options->g=0;
145
	options->e=0;
146
	options->no_multi_match=FALSE;
147
	options->pnparm = NULL;
Eric Coissac committed
148
	strcpy(options->taxonrank, DEFAULTTAXONRANK);			/*taxon level for results, species by default*/
149
	options->saltmethod = SALT_METHOD_SANTALUCIA;
150
	options->salt = DEF_SALT;
151
	options->printAC=FALSE;
152
	options->print_sets_of_primers = FALSE;
153
	options->specificity_threshold = 0.6;
154 155
}

156 157
void printapair(int32_t index,ppair_t pair, poptions_t options)
{
Eric Coissac committed
158 159 160 161 162 163 164 165 166
	bool_t asdirect1=pair->asdirect1;
	bool_t asdirect2=pair->asdirect2;
	bool_t asdirecttmp;
	word_t w1=pair->p1->word;
	word_t w2=pair->p2->word;
	word_t wtmp;
	bool_t good1=pair->p1->good;
	bool_t good2=pair->p2->good;
	bool_t goodtmp;
167 168
	bool_t strand;
	uint32_t i;
169
	float temp;
170
	CNNParams nnparams;
171

172
	//nparam_InitParams(&nnparams, DEF_CONC_PRIMERS,DEF_CONC_SEQUENCES,DEF_SALT,SALT_METHOD_SANTALUCIA);
173 174

	char *c;
175 176
	char p1[32];
	char p2[32];
Eric Coissac committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

	if (!asdirect1)
		w1=ecoComplementWord(w1,options->primer_length);

	if (!asdirect2)
		w2=ecoComplementWord(w2,options->primer_length);


	if (w2 < w1)
	{
		wtmp=w1;
		w1=w2;
		w2=wtmp;

		asdirecttmp=asdirect1;
		asdirect1=asdirect2;
		asdirect2=asdirecttmp;

		goodtmp=good1;
		good1=good2;
		good2=goodtmp;
	}
Eric Coissac committed
199

200
	//print serial number
201 202
	printf("%6d\t",index);

203 204 205 206 207
	c = ecoUnhashWord(w1,options->primer_length);
	strcpy (p1, c);
	c = ecoUnhashWord(w2,options->primer_length);
	strcpy (p2, c);

208
	//print primer1
209
	printf("%s\t", p1);
210

211
	//print primer2
212
	printf("%s", p2);
213

214
	//print primer1 melting temperature
215
	printf ("\t%3.1f", pair->p1temp);
216

217
	//print minimum melting temperature of approximate versions of primer1
218 219
	printf ("\t%3.1f", pair->p1mintemp);

220
	//print primer2 melting temperature
221 222
	printf ("\t%3.1f", pair->p2temp);

223
	//print minimum melting temperature of approximate versions of primer2
224 225
	printf ("\t%3.1f", pair->p2mintemp);

226 227
	//print gc contents of primer1
	printf ("\t%d",nparam_CountGCContent(p1));
228

229 230
	//print gc contents of primer2
	printf ("\t%d",nparam_CountGCContent(p2));
231

232
	//print good/bad pair indicator
Eric Coissac committed
233
	printf("\t%c%c", "bG"[(int)good1],"bG"[(int)good2]);
234

235
	//print inexample count
236
	printf("\t%d", pair->inexample);
237

238
	//print out example count
239
	printf("\t%d", pair->outexample);
240

241
	//print yule
242 243
	printf("\t%4.3f", pair->yule);

244
	//print in taxa count
245
	printf("\t%d", pair->intaxa);
246

247
	//print out taxa count
248
	printf("\t%d", pair->outtaxa);
249

250
	//print coverage
Eric Coissac committed
251
	printf("\t%4.3f", (float)pair->bc);
252

253
	//print well identified taxa count
Eric Coissac committed
254
	printf("\t%d", pair->intaxa - pair->notwellidentifiedtaxa);
255

256
	//print specificity
Eric Coissac committed
257
	printf("\t%4.3f", pair->bs);
258

259
	//print min amplifia lenght
260
	printf("\t%d", pair->mind);
261

262
	//print max amplifia lenght
263
	printf("\t%d", pair->maxd);
264

265
	//print average amplifia lenght
266
	printf("\t%3.2f", (float)pair->sumd/pair->amplifiacount);
267

268
	//print amplifia information about reference sequence if specified
269 270 271
	if (options->refseq && pair->refsequence >=0)
	{
		printf("\t%s:",options->reference);
272
		strand = pair->pcr.amplifias[pair->refsequence].strand;
273

274
		if (strand)
275 276 277 278 279 280 281 282 283 284 285 286
			printf("join(");
		else
			printf("complement(");

		printf("%d..%d,%d..%d",pair->pcr.amplifias[pair->refsequence].begin - options->primer_length + 1,
				                 pair->pcr.amplifias[pair->refsequence].begin,
							  	 pair->pcr.amplifias[pair->refsequence].end + 2,
							  	 pair->pcr.amplifias[pair->refsequence].end + options->primer_length + 1
							  	 );
		printf(")");
		printf("\t");

287 288 289 290 291 292 293 294
		for (c=pair->pcr.amplifias[pair->refsequence].amplifia,
		     i=pair->pcr.amplifias[pair->refsequence].begin;
			 i<=pair->pcr.amplifias[pair->refsequence].end;
			 i++,
			 c+=(strand)? 1:-1)
			printf("%c","acgt"[(strand)? (*c):(~*c)&3]);


295
	}
296 297
	else
		printf("\t\t");
298 299

	printf("\n");
300 301 302

}

Eric Coissac committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static int cmpprintedpairs(const void* p1,const void* p2)
{
	float s1,s2;
	ppair_t pair1,pair2;

	pair1=*((ppair_t*)p1);
	pair2=*((ppair_t*)p2);

	s1 = pair1->yule * pair1->bs;
	s2 = pair2->yule * pair2->bs;

//	fprintf(stderr,"s1 : %4.3f  %4.3f   %4.3f\n",pair1->yule , pair1->bs,s1);
//	fprintf(stderr,"s2 : %4.3f  %4.3f   %4.3f\n\n",pair2->yule , pair2->bs,s2);

	if (s1 > s2) return -1;
	if (s1 < s2) return 1;
	return 0;
}

322
uint32_t filterandsortpairs(ppair_t* sortedpairs,uint32_t count, poptions_t options, pecodnadb_t seqdb)
323 324 325 326 327 328 329 330 331 332 333 334 335 336
{
	uint32_t i,j;
	float q,qfp;

	for (i=0,j=0;i < count;i++)
	{
		if (options->insamples)
			q = (float)sortedpairs[i]->inexample/options->insamples;
		else q=1.0;

		if (options->outsamples)
			qfp = (float)sortedpairs[i]->outexample/options->outsamples;
		else qfp=0.0;

337
		sortedpairs[i]->wellIdentifiedSeqs = NULL; //TR 05/09/10 - wellIdentified needed for primer sets
338 339
		sortedpairs[i]->quorumin = q;
		sortedpairs[i]->quorumout = qfp;
Eric Coissac committed
340
		sortedpairs[i]->yule = q - qfp;
341 342 343 344 345
		sortedpairs[j]=sortedpairs[i];

		if (q > options->sensitivity_quorum &&
			qfp < options->false_positive_quorum)
		{
346 347
			//TR 05/09/10 - wellIdentified needed for primer sets
			sortedpairs[j]->wellIdentifiedSeqs = ECOMALLOC(options->dbsize * sizeof(int),"Cannot allocate well_identified_array");
348
			(void)taxonomycoverage(sortedpairs[j],options);
349
			taxonomyspecificity(sortedpairs[j], seqdb, options->dbsize);
350 351 352 353 354
			//j++;
			//if specificity less than user provieded threshold (default 60%) then ignore this pair
			if (sortedpairs[j]->bs >= options->specificity_threshold)
				j++;
			
355
		}
Eric Coissac committed
356

357
	}
Eric Coissac committed
358
	qsort(sortedpairs,j,sizeof(ppair_t),cmpprintedpairs);
359 360 361
	return j;
}

Eric Coissac committed
362

363
void printpairs (ppairtree_t pairs, poptions_t options,ecotaxonomy_t *taxonomy, pecodnadb_t seqdb)
364 365 366 367 368
{
   ppair_t* sortedpairs;
   ppair_t* index;
   ppairlist_t pl;
   size_t i,j;
Eric Coissac committed
369
   size_t count;
Eric Coissac committed
370 371
   char *taxon[]={"taxon","taxa"};
   ecotx_t    *current_taxon;
372
   pairset pair_sets;
Eric Coissac committed
373 374

   //printf("Index\tPrimer1\tPrimer2\tGB\tInexampleCount\tOutexampleCount\tYule\tIntaxaCount\tOuttaxaCount\tCoverage\tSpecificity\tMinAmplifiedLength\tMaxAmplifiedLength\tAvgAmplifiedLength\n");
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

   fprintf(stderr,"Total pair count : %d\n",pairs->count);

   sortedpairs = ECOMALLOC(pairs->count*sizeof(ppair_t),"Cannot Allocate ordered pairs");
   index=sortedpairs;
   pl=pairs->first;
   j=0;
   while(pl->next)
   {
	   for (i=0;i<pl->paircount;i++,j++)
		   sortedpairs[j]=pl->pairs+i;
	   pl=pl->next;
   }

   for (i=0;i<pl->paircount;i++,j++)
	   sortedpairs[j]=pl->pairs+i;

392
   count=filterandsortpairs(sortedpairs,pairs->count,options, seqdb);
393
   getThermoProperties(sortedpairs, count, options);
394

Eric Coissac committed
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
   fprintf(stderr,"Total good pair count : %u\n",(uint32_t)count);

	printf("#\n");
	printf("# ecoPrimer version %s\n",VERSION);
	printf("# Rank level optimisation : %s\n", options->taxonrank);
	printf("# max error count by oligonucleotide : %d\n",options->error_max);
	printf("#\n");

	if (options->r)
	{
		printf("# Restricted to %s:\n",taxon[(options->r>1) ? 1:0]);
		for(i=0;i<(uint32_t)options->r;i++)
		{
			current_taxon=eco_findtaxonbytaxid(taxonomy,options->restricted_taxid[i]);
			printf("#     %d : %s (%s)\n", current_taxon->taxid,
										   current_taxon->name,
										   taxonomy->ranks->label[current_taxon->rank]
										 );
		}
		printf("#\n");
	}
	if (options->g)
	{
		printf("# Ignore  %s:\n",taxon[(options->g>1) ? 1:0]);
		for(i=0;i<(uint32_t)options->r;i++)
		{
			current_taxon=eco_findtaxonbytaxid(taxonomy,options->ignored_taxid[i]);
			printf("#     %d : %s (%s)\n", current_taxon->taxid,
										   current_taxon->name,
										   taxonomy->ranks->label[current_taxon->rank]
										 );
		}
		printf("#\n");
	}
	printf("# strict primer quorum  : %3.2f\n",options->strict_quorum);
	printf("# example quorum        : %3.2f\n",options->sensitivity_quorum);
	if (options->g + options->r)
		printf("# counterexample quorum : %3.2f\n",options->false_positive_quorum);

	printf("#\n");
	printf("# database : %s\n",options->prefix);
    printf("# Database is constituted of %5d examples        corresponding to %5d %s\n",options->insamples,
    		options->intaxa,options->taxonrank);
    printf("#                        and %5d counterexamples corresponding to %5d %s\n",options->outsamples,
    		options->outtaxa,options->taxonrank);
	printf("#\n");

	if (options->lmin && options->lmax)
		printf("# amplifiat length between [%d,%d] bp\n",options->lmin,options->lmax);
	else if (options->lmin)
		printf("# amplifiat length larger than %d bp\n",options->lmin);
	else if (options->lmax)
		printf("# amplifiat length smaller than %d bp\n",options->lmax);
	if (options->circular)
		printf("# DB sequences are considered as circular\n");
	else
		printf("# DB sequences are considered as linear\n");
452
	printf("# Pairs having specificity less than %0.2f will be ignored\n", options->specificity_threshold);
Eric Coissac committed
453 454
	printf("#\n");

455 456
   for (i=0;i < count;i++)
	   printapair(i,sortedpairs[i],options);
457 458 459 460 461 462
   
   if (options->print_sets_of_primers == TRUE)
   {
	   pair_sets = build_primers_set (sortedpairs, count, seqdb, options);
	   some_other_set_possibilities (&pair_sets, sortedpairs, count, seqdb, options);
   }
463 464 465
}


466 467
/*updateseqparams: This function counts the insample and outsample sequences
 *  and with each sequences adds a tag of the taxon to which the sequence beongs*/
468 469

void updateseqparams (pecodnadb_t seqdb, uint32_t seqdbsize, ecotaxonomy_t *taxonomy,
470 471 472 473 474 475 476
		poptions_t options, int32_t *insamples, int32_t *outsamples)
{
	uint32_t i;
	int32_t taxid;
	ecotx_t  *tmptaxon;

    for (i=0;i<seqdbsize;i++)
477
    {
478
    	seqdb[i]->isexample=isExampleTaxon(taxonomy,seqdb[i]->taxid,options);
479 480 481 482 483 484
    	if (seqdb[i]->isexample)
    		(*insamples)++;
    	else
    		(*outsamples)++;

    	taxid = taxonomy->taxons->taxon[seqdb[i]->taxid].taxid;
485
		tmptaxon = eco_findtaxonbytaxid(taxonomy, taxid);
486 487 488 489 490 491 492 493 494 495 496 497 498 499
		if (tmptaxon)
			tmptaxon = eco_findtaxonatrank(tmptaxon, options->taxonrankidx);
		if (tmptaxon)
			seqdb[i]->ranktaxonid = tmptaxon->taxid;
    }
}

void setresulttaxonrank (ecotaxonomy_t *taxonomy, poptions_t options)
{
	int32_t i;

    /*set taxon rank for which result is to be given*/
    for (i = 0; i < taxonomy->ranks->count; i++)
    {
500
    	if (strcmp(taxonomy->ranks->label[i], options->taxonrank) == 0)
501 502 503 504 505 506 507 508 509 510 511 512 513 514
    	{
    		options->taxonrankidx = i;
    		break;
    	}
    }

    if (i == taxonomy->ranks->count)
    {
    	fprintf(stderr,"\nUnknown taxon level: '%s'\n", options->taxonrank);
    	exit(0);
    }
}
/* to get db stats, totals of species, genus etc....*/

515 516 517 518 519
static void printAC(pecodnadb_t seqdb,uint32_t seqdbsize)
{
	uint32_t i;

	for (i=0; i< seqdbsize;i++)
520
		printf("%15s (%8d bp ): %s\n",seqdb[i]->AC,seqdb[i]->SQ_length,seqdb[i]->DE);
521
}
522

523 524 525 526 527 528 529 530 531 532 533 534 535
int main(int argc, char **argv)
{
	pecodnadb_t   seqdb; /* of type ecoseq_t */
	uint32_t        seqdbsize=0;
	ecotaxonomy_t *taxonomy;

	options_t     options;
	int           carg;
	int32_t       errflag=0;

	int32_t       insamples=0;
	int32_t       outsamples=0;
	uint32_t        i;
536 537

	pwordcount_t    words;
538
//	pwordcount_t    words2;
539 540 541
	pprimercount_t  primers;
	ppairtree_t		pairs;

542 543
	int32_t		  rankdbstats = 0;

544
	CNNParams nnparams;
545

546 547
	initoptions(&options);

548
    while ((carg = getopt(argc, argv, "hAfvcUDSpE:d:l:L:e:i:r:R:q:3:s:x:t:O:m:a:T:")) != -1) {
549 550

     switch (carg) {
551 552 553 554 555 556
								 /* ---------------------------- */
		 case 'v':               /* set in single strand mode    */
								 /* ---------------------------- */
			options.statistics=TRUE;
			break;

557 558 559 560
			 /* ---------------------------- */
		 case 'f':               /* set in single strand mode    */
			 /* ---------------------------- */
			 options.filtering=FALSE;
561 562 563 564 565 566
			 break;

			 /* ---------------------------- */
		 case 'A':               /* set in single strand mode    */
			 /* ---------------------------- */
			 options.printAC=TRUE;
567 568
			 break;

569 570 571 572 573 574 575 576 577 578 579
                                /* -------------------- */
        case 'd':               /* database name        */
                                /* -------------------- */
        	options.prefix = ECOMALLOC(strlen(optarg)+1,
                              "Error on prefix allocation");
           strcpy(options.prefix,optarg);
           break;

                                /* -------------------- */
        case 'h':               /* help                 */
                                /* -------------------- */
580 581 582
        	PrintHelp();
        	 exit(0);
        	 break;
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

                                /* ------------------------- */
        case 'l':               /* min amplification lenght  */
                                /* ------------------------- */
           sscanf(optarg,"%d",&(options.lmin));
           break;

                                /* -------------------------- */
        case 'L':               /* max amplification lenght   */
                                /* -------------------------- */
          sscanf(optarg,"%d",&(options.lmax));
          break;

                                /* -------------------- */
        case 'e':               /* error max            */
                                /* -------------------- */
          sscanf(optarg,"%d",&(options.error_max));
          break;

602 603 604 605 606 607

								/* ------------------------ */
		case '3':               /* three prime strict match */
                                /* ------------------------ */
			sscanf(optarg,"%d",&(options.strict_three_prime));
			break;
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

                                /* -------------------- */
        case 'q':               /* strict matching quorum           */
                                /* -------------------- */
          sscanf(optarg,"%f",&(options.strict_quorum));
          break;

								/* -------------------- */
        case 's':               /* strict matching quorum      */
								/* -------------------- */
        	sscanf(optarg,"%f",&(options.sensitivity_quorum));
        	break;

        						/* -------------------- */
        case 't':               /* required taxon level for results           */
        						/* -------------------- */
        	strncpy(options.taxonrank, optarg, 19);
        	options.taxonrank[19] = 0;
            break;
627

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
        						/* -------------------- */
        case 'x':               /* strict matching quorum           */
								/* -------------------- */
        	sscanf(optarg,"%f",&(options.false_positive_quorum));
        	break;

							    /* ---------------------------- */
		case 'D':               /* set in double strand mode    */
							    /* ---------------------------- */
			options.doublestrand=1;
			break;

								/* ---------------------------- */
		case 'S':               /* set in single strand mode    */
								/* ---------------------------- */
			options.doublestrand=0;
			break;

			                    /* ---------------------------- */
        case 'U':               /* set in single strand mode    */
			                    /* ---------------------------- */
            options.no_multi_match=TRUE;
            break;

         						/* ------------------------------------------ */
        case 'r':               /* stores the restricting search taxonomic id */
        						/* ------------------------------------------ */
          options.restricted_taxid = ECOREALLOC(options.restricted_taxid,sizeof(int32_t)*(options.r+1),
          									"Error on restricted_taxid reallocation");
          sscanf(optarg,"%d",&(options.restricted_taxid[options.r]));
          options.r++;
659 660
          break;

661 662 663 664 665 666 667 668 669
								/* ------------------------------------------ */
        case 'E':               /* stores the restricting search taxonomic id */
								/* ------------------------------------------ */
        	options.exception_taxid = ECOREALLOC(options.exception_taxid,sizeof(int32_t)*(options.e+1),
						            "Error on exception_taxid reallocation");
			sscanf(optarg,"%d",&(options.exception_taxid[options.e]));
			options.e++;
			break;

670 671 672 673 674 675
								/* -------------------- */
        case 'R':               /* reference sequence   */
                                /* -------------------- */
          options.reference = ECOMALLOC(strlen(optarg)+1,
                                     "Error on prefix allocation");
          strcpy(options.reference,optarg);
676 677 678 679 680 681 682 683 684 685 686
          break;

         						/* --------------------------------- */
        case 'i':               /* stores the taxonomic id to ignore */
        						/* --------------------------------- */
          options.ignored_taxid = ECOREALLOC(options.ignored_taxid,sizeof(int32_t)*(options.g+1),
          									"Error on excluded_taxid reallocation");
          sscanf(optarg,"%d",&(options.ignored_taxid[options.g]));
          options.g++;
          break;

Eric Coissac committed
687 688 689 690 691 692
			                    /* --------------------------------- */
        case 'O':               /* set primer size                   */
        						/* --------------------------------- */
			sscanf(optarg,"%d",&(options.primer_length));
			break;

693 694 695 696 697 698 699 700 701 702 703 704 705
								/* --------------------------------- */
        case 'm':               /* set salt method                   */
								/* --------------------------------- */
        	sscanf(optarg,"%d",&(options.saltmethod));
        	break;

        						/* --------------------------------- */
        case 'a':               /* set salt 	                     */
								/* --------------------------------- */
        	sscanf(optarg,"%f",&(options.salt));
        	break;

        						/* -------------------- */
706 707 708 709 710
        case 'c':               /* sequences are circular */
        						/* --------------------------------- */
		  options.circular = 1;
          break;

711 712 713 714 715
								/* -------------------- */
        case 'p':               /* print sets of primers */
								/* --------------------------------- */
        	options.print_sets_of_primers = TRUE;
        break;
716 717 718 719 720 721 722
			 
								/* --------------------------------- */
		case 'T':               /* Ignore pairs having specificity below this Threshold     */
								/* --------------------------------- */
			 sscanf(optarg,"%f",&(options.specificity_threshold));
		break;
			 
723 724 725 726 727 728
        case '?':               /* bad option           */
                                /* -------------------- */
            errflag++;
   		}

	}
729
    options.pnparm = &nnparams;
730 731
    if (options.saltmethod != 2) //if not SALT_METHOD_OWCZARZY
    	options.saltmethod = SALT_METHOD_SANTALUCIA; //then force SALT_METHOD_SANTALUCIA
732

733 734
    if (options.salt < 0.01 || options.salt > 0.3) //if salt value out of literature values
    	options.salt = DEF_SALT; //set to default
735

736 737
	nparam_InitParams(&nnparams, DEF_CONC_PRIMERS,DEF_CONC_SEQUENCES,options.salt,options.saltmethod);

738 739 740
    fprintf(stderr,"Reading taxonomy database ...");
    taxonomy = read_taxonomy(options.prefix,0);
    fprintf(stderr,"Ok\n");
741

742
    setresulttaxonrank(taxonomy, &options); /*TR: set rank level for statistics*/
743

744 745
    fprintf(stderr,"Reading sequence database ...\n");

746
    seqdb = readdnadb(options.prefix,taxonomy,&seqdbsize, &options);
747

748 749 750 751 752
	if (options.printAC)
	{
		printAC(seqdb,seqdbsize);
		exit(0);
	}
753 754 755 756 757 758 759 760 761
    if (options.reference)
    	for (i=0; i < seqdbsize;i++)
    		if (strcmp(seqdb[i]->AC,options.reference)==0)
    		{
    			options.refseq=seqdb[i];
    			options.refseqid=i;
    			fprintf(stderr,"Reference sequence %s identified\n",options.reference);
    		}

762 763
    fprintf(stderr,"Ok\n");
    fprintf(stderr,"Sequence read : %d\n",(int32_t)seqdbsize);
764

765
    updateseqparams(seqdb, seqdbsize, taxonomy, &options, &insamples , &outsamples);
766 767 768
    options.dbsize=seqdbsize;
    options.insamples=insamples;
    options.outsamples=outsamples;
769 770 771

    rankdbstats = getrankdbstats(seqdb, seqdbsize, taxonomy, &options);

772 773 774 775
    fprintf(stderr,"Database is constituted of %5d examples        corresponding to %5d %s\n",insamples,
    		options.intaxa,options.taxonrank);
    fprintf(stderr,"                       and %5d counterexamples corresponding to %5d %s\n",outsamples,
    		options.outtaxa,options.taxonrank);
776 777 778 779 780 781
    fprintf(stderr,"Total distinct %s count %d\n",options.taxonrank, rankdbstats);

    fprintf(stderr,"\nIndexing words in sequences\n");

    words = lookforStrictPrimer(seqdb,seqdbsize,insamples,&options);
    fprintf(stderr,"\n  Strict primer count : %d\n",words->size);
782

783 784 785 786 787 788 789 790 791 792 793
//    options.filtering=FALSE;
//    words2= lookforStrictPrimer(seqdb,seqdbsize,insamples,&options);
//    fprintf(stderr,"\n  Strict primer count : %d\n",words2->size);
//
//    fprintf(stderr,"\n\n  Primer sample : \n");
//    for (i=0; i<words->size; i++)
//    	fprintf(stderr,"  + Primer : %s   sequence count : %d\n",ecoUnhashWord(words->words[i],options.primer_length),words->strictcount[i]);
//    fprintf(stderr,"\n\n  Primer sample : \n");
//    for (i=0; i<words2->size; i++)
//    	fprintf(stderr,"  + Primer : %s   sequence count : %d\n",ecoUnhashWord(words2->words[i],options.primer_length),words2->strictcount[i]);

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
    if (options.no_multi_match)
    {
    	(void)filterMultiStrictPrimer(words);
    	fprintf(stderr,"\n  Strict primer with single match count : %d\n",words->size);
    }


    fprintf(stderr,"\n\n  Primer sample : \n");
    for (i=0; i<MINI(10,words->size); i++)
    	fprintf(stderr,"  + Primer : %s   sequence count : %d\n",ecoUnhashWord(words->words[i],options.primer_length),words->strictcount[i]);


    fprintf(stderr,"\nEncoding sequences for fuzzy pattern matching...\n");
    for (i=0;i<seqdbsize;i++)
    {
    	encodeSequence(seqdb[i]);
    	fprintf(stderr,"  Encoded sequences %5d/%5d          \r",(int32_t)i+1,(int32_t)seqdbsize);
    }

    ECOFREE(words->strictcount,"Free strict primer count table");

    primers = lookforAproxPrimer(seqdb,seqdbsize,insamples,words,&options);

    ECOFREE(words->words,"Free strict primer table");
    ECOFREE(words,"Free strict primer structure");
    fprintf(stderr,"\n\n  Approximate repeats :%d \n", primers->size);

    fprintf(stderr,"\n\n  Primer sample : \n");
    for (i=0; i<MINI(10,primers->size); i++)
    	fprintf(stderr,"  + Primer : %s   example sequence count : %5d counterexample sequence count : %5d  status : %s\n",ecoUnhashWord(primers->primers[i].word,options.primer_length),
                       primers->primers[i].inexample,
                       primers->primers[i].outexample,
                       primers->primers[i].good ? "good":"bad");

    fprintf(stderr,"\n");

830 831 832 833
   
    	pairs = buildPrimerPairs(seqdb, seqdbsize, primers, &options);
    	printpairs (pairs, &options,taxonomy, seqdb);
    
834 835
    return 0;
}