pairs.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * pairs.c
 *
 *  Created on: 15 dc. 2008
 *      Author: coissac
 */

#include  "ecoprimer.h"
#include  <string.h>
10
#include  <stdlib.h>
11
#include "../libthermo/thermostats.h"
12 13 14 15 16 17 18 19 20

static void buildPrimerPairsForOneSeq(uint32_t seqid,
									  pecodnadb_t seqdb,
		 							  pprimercount_t primers,
								 	  ppairtree_t pairs,
									  poptions_t options);



21 22 23



24 25 26 27 28 29 30 31 32
/*************************************
 *
 *       pair collection management
 *
 *************************************/

#ifdef MASKEDCODE

char *addamplifiasetelem (ppair_t pair, char* amplifia, int32_t taxid)
33 34 35 36
{
	uint32_t i;
	uint32_t j;
	char *ampused = NULL;
37

38 39 40 41 42 43
	if(pair->ampsetcount == 0)
	{
		pair->ampsetcount = 500;
		pair->ampsetindex = 0;
		pair->ampset = ECOMALLOC(pair->ampsetcount * sizeof(ampseqset_t),"Cannot allocate amplifia set");
	}
44

45 46 47 48 49 50 51 52
	for (i = 0; i < pair->ampsetindex; i++)
	{
		if (strcmp (pair->ampset[i].amplifia, amplifia) == 0)
		{
			ampused = pair->ampset[i].amplifia;
			break;
		}
	}
53

54 55 56 57 58 59
	if (i == 0)
	{
		pair->ampset[i].seqidcount = 100;
		pair->ampset[i].seqidindex = 0;
		pair->ampset[i].taxonids = ECOMALLOC(pair->ampset[i].seqidcount * sizeof(uint32_t),"Cannot allocate amplifia sequence table");
	}
60

61 62 63 64 65
	if (pair->ampsetindex == pair->ampsetcount)
	{
		pair->ampsetcount += 500;
		pair->ampset = ECOREALLOC(pair->ampset, pair->ampsetcount * sizeof(ampseqset_t), "Cannot allocate amplifia set");
	}
66

67 68 69 70 71
	if (pair->ampset[i].seqidindex == pair->ampset[i].seqidcount)
	{
		pair->ampset[i].seqidcount += 100;
		pair->ampset[i].taxonids = ECOREALLOC(pair->ampset[i].taxonids, pair->ampset[i].seqidcount * sizeof(int32_t), "Cannot allocate amplifia sequence table");
	}
72

73 74 75 76 77
	if (pair->ampset[i].amplifia == NULL)
	{
		pair->ampset[i].amplifia = amplifia;
		pair->ampsetindex++;
	}
78

79 80 81 82
	for (j = 0; j < pair->ampset[i].seqidindex; j++)
	{
		if (pair->ampset[i].taxonids[j] == taxid) break;
	}
83

84 85 86 87 88
	if (j == pair->ampset[i].seqidindex)
		pair->ampset[i].taxonids[pair->ampset[i].seqidindex++] = taxid;
	return ampused;
}

89
void addtaxampsetelem (ppair_t pair, int32_t taxid, char *amplifia)
90 91 92 93 94 95 96 97 98 99
{
	uint32_t i;
	uint32_t j;

	if(pair->taxsetcount == 0)
	{
		pair->taxsetcount = 500;
		pair->taxsetindex = 0;
		pair->taxset = ECOMALLOC(pair->taxsetcount * sizeof(taxampset_t),"Cannot allocate taxon set");
	}
100

101 102 103 104
	for (i = 0; i < pair->taxsetindex; i++)
	{
		if (pair->taxset[i].taxonid == taxid) break;
	}
105

106 107 108 109 110 111
	if (i == 0)
	{
		pair->taxset[i].amplifiacount = 100;
		pair->taxset[i].amplifiaindex = 0;
		pair->taxset[i].amplifia = ECOMALLOC(pair->taxset[i].amplifiacount * sizeof(char *),"Cannot allocate amplifia table");
	}
112

113 114 115 116 117
	if (pair->taxsetindex == pair->taxsetcount)
	{
		pair->taxsetcount += 500;
		pair->taxset = ECOREALLOC(pair->taxset, pair->taxsetcount * sizeof(taxampset_t), "Cannot allocate taxon set");
	}
118

119 120 121 122 123
	if (pair->taxset[i].amplifiaindex == pair->taxset[i].amplifiacount)
	{
		pair->taxset[i].amplifiacount += 100;
		pair->taxset[i].amplifia = ECOREALLOC(pair->taxset[i].amplifia, pair->taxset[i].amplifiacount * sizeof(char *), "Cannot allocate amplifia table");
	}
124

125 126 127 128 129
	if (pair->taxset[i].taxonid == 0)
	{
		pair->taxset[i].taxonid = taxid;
		pair->taxsetindex++;
	}
130

131 132 133 134
	for (j = 0; j < pair->taxset[i].amplifiaindex; j++)
	{
		if (strcmp(pair->taxset[i].amplifia[j], amplifia) == 0) break;
	}
135

136 137 138 139 140 141 142 143 144
	if (j == pair->taxset[i].amplifiaindex)
	{
		pair->taxset[i].amplifia[j] = amplifia;
		pair->taxset[i].amplifiaindex++;
	}
}

char *getamplifia (pecoseq_t seq, uint32_t start, uint32_t len)
{
145
	fprintf(stderr,"start : %d length : %d\n",start,len);
146 147
	char *amplifia = ECOMALLOC((len + 1) * sizeof(char),"Cannot allocate amplifia");
	char *seqc = &seq->SQ[start];
148

149 150 151 152
	strncpy(amplifia, seqc, len);
	return amplifia;
}

153
#endif
154 155

/*TR: Added*/
156
ppairtree_t buildPrimerPairs(pecodnadb_t seqdb,uint32_t seqdbsize,pprimercount_t primers,poptions_t options)
157 158
{
	uint32_t i;
159
	ppairtree_t primerpairs;
160
	
161
	primerpairs = initpairtree(NULL);
162 163 164

	for (i=0; i < seqdbsize; i++)
	{
165
		buildPrimerPairsForOneSeq(i, seqdb, primers, primerpairs, options);
166 167 168 169
	}
	return primerpairs;
}

170 171 172 173 174 175 176
#define DMAX (2000000000)

static void buildPrimerPairsForOneSeq(uint32_t seqid,
									  pecodnadb_t seqdb,
		 							  pprimercount_t primers,
								 	  ppairtree_t pairs,
									  poptions_t options)
177
{
178 179 180 181
	static uint32_t    paircount=0;
	uint32_t           i,j,k;
	uint32_t           matchcount=0;
	pprimermatch_t     matches = NULL;
182
	primermatchcount_t seqmatchcount;
183 184 185 186 187 188
	ppair_t            pcurrent;
	pair_t			   current;
	pprimer_t		   wswp;
	bool_t			   bswp;
	size_t			   distance;
	bool_t             strand;
189 190
	//char 			   prmr[50];
	//float			   mtemp;
191
	word_t 			   w1, w1a, omask = (0x1L << (options->strict_three_prime*2)) -1;
192 193
	word_t 			   w2, w2a, wtmp;
	uint32_t   		   bp1,bp2;
194 195 196
	
	//prmr[options->primer_length] = '\0';
	
197 198 199 200 201
	for (i=0;i < primers->size; i++)
	{
		matchcount+=primers->primers[i].directCount[seqid];
		matchcount+=primers->primers[i].reverseCount[seqid];
	}
202 203 204 205

	if (matchcount <= 0)
		return;

206 207 208 209 210 211 212 213
	matches = ECOMALLOC(matchcount * sizeof(primermatch_t),"Cannot allocate primers match table");

	for (i=0,j=0;i < primers->size; i++)
	{
		if (primers->primers[i].directCount[seqid])
		{
			if (primers->primers[i].directCount[seqid]==1)
			{
214 215
			matches[j].primer = primers->primers+i;
			matches[j].strand=TRUE;
216 217 218 219 220
				matches[j].position=primers->primers[i].directPos[seqid].value;
				j++;
			}
			else for (k=0; k < primers->primers[i].directCount[seqid]; k++,j++)
			{
221
				matches[j].primer = primers->primers+i;
222 223 224 225 226 227 228 229 230
				matches[j].strand=TRUE;
				matches[j].position=primers->primers[i].directPos[seqid].pointer[k];
			}
		}

		if (primers->primers[i].reverseCount[seqid])
		{
			if (primers->primers[i].reverseCount[seqid]==1)
			{
231 232
			matches[j].primer = primers->primers+i;
			matches[j].strand=FALSE;
233 234 235 236 237
				matches[j].position=primers->primers[i].reversePos[seqid].value;
				j++;
			}
			else for (k=0; k < primers->primers[i].reverseCount[seqid]; k++,j++)
			{
238
				matches[j].primer = primers->primers+i;
239 240 241 242 243 244
				matches[j].strand=FALSE;
				matches[j].position=primers->primers[i].reversePos[seqid].pointer[k];
			}
		}
	}

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
	if (matchcount>1)
	{
//		fprintf(stderr,"\n====================================\n");

		sortmatch(matches,matchcount); // sort in ascending order by position

		for (i=0; i < matchcount;i++)
		{
			// For all primers matching the sequence

			for(j=i+1;
			       (j<matchcount)
			    && ((distance=matches[j].position - matches[i].position - options->primer_length) < options->lmax);
			   j++
			   )

			   // For all not too far primers

		       if ( (matches[i].primer->good || matches[j].primer->good)
		    		&& (distance > options->lmin)
		    		)
		       {

		    	   // If possible primer pair

		    	   current.p1 = matches[i].primer;
		    	   current.asdirect1=matches[i].strand;
		    	   current.p2 = matches[j].primer;
		    	   current.asdirect2= !matches[j].strand;
		    	   current.maxd=DMAX;
		    	   current.mind=DMAX;
		    	   current.sumd=0;
	    		   current.inexample=0;
	    		   current.outexample=0;
279 280
				   current.curseqid = 0;
				   current.refsequence=-1;
281 282 283 284
				   //current.p1temp = 100;
				   //current.p1mintemp = 100;
				   //current.p2temp = 100;
				   //current.p2mintemp = 100;
285 286 287 288 289 290 291 292 293 294 295 296 297

		    	   // Standardize the pair
	    		   strand = current.p2->word > current.p1->word;
		    	   if (!strand)
		    	   {
		    		   wswp = current.p1;
		    		   current.p1=current.p2;
		    		   current.p2=wswp;

		    		   bswp = current.asdirect1;
		    		   current.asdirect1=current.asdirect2;
		    		   current.asdirect2=bswp;
		    	   }
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
		    	   
		    	   //Code to make sure that if -3 option is given then
		    	   //3' end must match upto given number of base pairs
		    	   if (options->strict_three_prime > 0)
		    	   {
		    		   w1 = current.p1->word;
		    		   w2 = current.p2->word;
		    		   if (!current.asdirect1) //make sure that word is from 5' to 3'
		    			   w1=ecoComplementWord(w1,options->primer_length);

		    		   if (!current.asdirect2) //make sure that word is from 5' to 3'
		    			   w2=ecoComplementWord(w2,options->primer_length);
		    		   //now both w1 and w2 are from 5' to 3' end
		    		   bp1 = matches[i].position;
		    		   bp2 = matches[j].position;
		    		   if (!strand)
		    		   {
			    		   bp1 = matches[j].position;
			    		   bp2 = matches[i].position;
		    		   }
		    		   //get word of first approximate repeat
		    		   w1a = extractSite(seqdb[seqid]->SQ,bp1,options->primer_length,strand);
		    		   //get word of second approximate repeat
		    		   w2a = extractSite(seqdb[seqid]->SQ,bp2,options->primer_length,!strand);
323
		    		   		    		  		    		   
324 325 326 327
		    		   w1 = w1 & omask;   //keep only strict_three_prime bases on the right (3') end
		    		   w2 = w2 & omask;   //keep only strict_three_prime bases on the right (3') end
		    		   w1a = w1a & omask; //keep only strict_three_prime bases on the right (3') end
		    		   w2a = w2a & omask; //keep only strict_three_prime bases on the right (3') end
328
		    		
329
		    		   //now check that both words and primers of amplifia have same bases on 3' end
330 331
		    		   if ((w1 ^ w1a) != 0) continue; 
		    		   if ((w2 ^ w2a) != 0) continue;
332 333 334
		    	   }
		    	   
		    	   
335 336 337 338 339 340 341 342 343

		    	   // Look for the new pair in already seen pairs

		    	   pcurrent = insertpair(current,pairs);


		    	   if (seqdb[seqid]->isexample)

				   {
344
		    		   //pcurrent->inexample++;
345 346 347 348 349 350 351 352
		    		   pcurrent->sumd+=distance;

		    		   if ((pcurrent->maxd==DMAX) || (distance > pcurrent->maxd))
			    		   pcurrent->maxd = distance;

			    	   if (distance < pcurrent->mind)
			    		   pcurrent->mind = distance;
		    	   }
353 354
		    	   //else
		    		 //  pcurrent->outexample++;
355

356 357 358 359
		    	   //for each pair we save current sequence id in the pair
		    	   //when we see this pair for the first time in currnet sequence
		    	   //because we want to increment inexample & outexample count
		    	   //only once for one sequence
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
				  if (pcurrent->curseqid != (seqid+1))
				   {
						if (seqdb[seqid]->isexample)
							pcurrent->inexample++;
		    	        else
		    		        pcurrent->outexample++;

						if (pcurrent->curseqid != 0)
							pcurrent->curseqid = seqid+1;
					}

					/*if ((pcurrent->outexample+pcurrent->inexample)==0)
					{
						fprintf(stderr,"pcurrent->outexample+pcurrent->inexample=0!\n");
						exit(0);
					}*/

		    	   if (pcurrent->curseqid == 0)//((pcurrent->outexample+pcurrent->inexample)==1)
378
		    	   {
379
					   pcurrent->curseqid = seqid+1;
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
					   paircount++;
					   pcurrent->pcr.ampslot=200;
					   pcurrent->pcr.ampcount=0;
					   pcurrent->pcr.amplifias = ECOMALLOC(sizeof(amplifia_t)*pcurrent->pcr.ampslot,
							                               "Cannot allocate amplifia table");
		    	   }
		    	   else
		    	   {
		    		   if (pcurrent->pcr.ampslot==pcurrent->pcr.ampcount)
		    		   {
		    			   pcurrent->pcr.ampslot+=200;
		    			   pcurrent->pcr.amplifias = ECOREALLOC(pcurrent->pcr.amplifias,
															    sizeof(amplifia_t)*pcurrent->pcr.ampslot,
		    			   							            "Cannot allocate amplifia table");
		    		   }
		    	   }

397 398
		    	   if (seqid==options->refseqid)
		    		   pcurrent->refsequence=seqid;
399 400 401
		    	   pcurrent->pcr.amplifias[pcurrent->pcr.ampcount].length=distance;
		    	   pcurrent->pcr.amplifias[pcurrent->pcr.ampcount].sequence=seqdb[seqid];
		    	   pcurrent->pcr.amplifias[pcurrent->pcr.ampcount].strand=strand;
402 403
		    	   pcurrent->pcr.amplifias[pcurrent->pcr.ampcount].begin=matches[i].position + options->primer_length;
		    	   pcurrent->pcr.amplifias[pcurrent->pcr.ampcount].end=  matches[j].position - 1;
404 405 406 407 408 409

		    	   if (strand)
		    	   	   pcurrent->pcr.amplifias[pcurrent->pcr.ampcount].amplifia=  seqdb[seqid]->SQ + matches[i].position + options->primer_length;
		    	   else
		    	   	   pcurrent->pcr.amplifias[pcurrent->pcr.ampcount].amplifia=  seqdb[seqid]->SQ + matches[j].position - 1 ;

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
		    	   
		    	   /*strncpy (prmr, seqdb[seqid]->SQ + matches[i].position, options->primer_length);
		    	   mtemp = nparam_CalcSelfTM (options->pnparm, prmr, options->primer_length) - 273.0;
		    	   if (mtemp < pcurrent->p1mintemp)
		    		   pcurrent->p1mintemp = mtemp;
		    	   //fprintf (stderr, "prmr1: %s\n", seqdb[seqid]->SQ);
		    	   strncpy (prmr, seqdb[seqid]->SQ + matches[j].position, options->primer_length);
		    	   mtemp = nparam_CalcSelfTM (options->pnparm, prmr, options->primer_length) - 273.0;
		    	   if (mtemp < pcurrent->p2mintemp)
		    		   pcurrent->p2mintemp = mtemp;
		    	   //fprintf (stderr, "prmr2: %s\n", prmr);

		    	   if (pcurrent->p1temp == 100)
		    		   pcurrent->p1temp = nparam_CalcSelfTM (options->pnparm, ecoUnhashWord(pcurrent->p1->word, options->primer_length), 0) - 273.0;
		    	   if (pcurrent->p2temp == 100)
		    		   pcurrent->p2temp = nparam_CalcSelfTM (options->pnparm, ecoUnhashWord(pcurrent->p2->word, options->primer_length), 0) - 273.0;
		    	   */
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
		    	   pcurrent->pcr.ampcount++;
//		    	   fprintf(stderr,"%c%c W1 : %s   direct : %c",
//		    			   "bG"[(int)pcurrent->p1->good],
//		    			   "bG"[(int)pcurrent->p2->good],
//		    			   ecoUnhashWord(pcurrent->p1->word, options->primer_length),
//		    			   "><"[(int)pcurrent->asdirect1]
//		    			   );
//
//		    	   fprintf(stderr,"   W2 : %s   direct : %c distance : %d (min/max/avg : %d/%d/%f) in/out: %d/%d %c (%d pairs)\n",
//		    			   ecoUnhashWord(pcurrent->p2->word, options->primer_length),
//		    			   "><"[(int)pcurrent->asdirect2],
//		    			   distance,
//		    			   pcurrent->mind,pcurrent->maxd,
//		    			   (pcurrent->inexample) ? (float)pcurrent->sumd/pcurrent->inexample:0.0,
//		    			   pcurrent->inexample,pcurrent->outexample,
//		    			   " N"[(pcurrent->outexample+pcurrent->inexample)==1],
//		    			   paircount
//
//		    			   );
//

		       }
		 }
	}

   pairs->count=paircount;

454
}