aproxpattern.c 6.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * aproxpattern.c
 *
 *  Created on: 20 nov. 2008
 *      Author: coissac
 */


#include  "ecoprimer.h"
#include  "apat.h"
#include  <math.h>

static uint8_t encoder[] = {0,                                            // A
		                   4,                                           // b
		                   1,                                            // C
	       	               4,4,4,                                     // d, e, f
		                   2,                                            // G
		                   4,4,4,4,4,4,4,4,4,4,4,4,          // h,i,j,k,l,m,n,o,p,q,r,s
		                   3,3,                                           // T,U
		                   4,4,4,4,4};                              // v,w,x,y,z


ppattern_t buildPatternFromWord(word_t word, uint32_t patlen)
{
	static pattern_t       pattern;
	uint32_t i;

    for (i = 0 ; i < ALPHA_LEN ; i++)
       pattern[i] = 0x0;

    for (i=0;i < patlen; i++)
	{
		pattern[word & 3LLU] |= 1 << i;
		word>>=2;
	}

	return pattern;

}


42 43
#ifdef IS_UPPER
#undef IS_UPPER
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#endif

/* -------------------------------------------- */
/* encode sequence                              */
/* IS_UPPER is slightly faster than isupper     */
/* -------------------------------------------- */

#define IS_UPPER(c) (((c) >= 'A') && ((c) <= 'Z'))

void encodeSequence(ecoseq_t *seq)
{
     int   i;
     uint8_t *data;
     char  *cseq;

     data = (uint8_t*)(seq->SQ);
     cseq = seq->SQ;

     for (i=0;i<seq->SQ_length;i++,data++,cseq++)
     {
64
         *data = encoder[(IS_UPPER(*cseq) ? *cseq : 'Z') - 'A'];
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
     }
}

pprimercount_t lookforAproxPrimer(pecodnadb_t database, uint32_t seqdbsize,uint32_t exampleCount,
		                     pwordcount_t words,poptions_t options)
{
	pprimer_t  data;
	pprimercount_t primers;
	ppattern_t pattern;
	patternParam_t params;
	uint32_t   i;
	uint32_t   w;
	uint32_t   j;
	Stacki     positions;
	uint32_t   count=1;
	uint32_t   goodPrimers=0;

	uint32_t  inSequenceQuorum;
	uint32_t  outSequenceQuorum;
	bool_t    conserved = TRUE;
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99
	//poslist_t ttt;


	inSequenceQuorum = (uint32_t)floor((float)exampleCount * options->sensitivity_quorum);
	outSequenceQuorum = (uint32_t)floor((float)(seqdbsize-exampleCount) * options->false_positive_quorum);

	fprintf(stderr,"  Primers should be at least present in %d/%d example sequences\n",inSequenceQuorum,exampleCount);
	fprintf(stderr,"  Primers should not be present in more than %d/%d counterexample sequences\n",outSequenceQuorum,(seqdbsize-exampleCount));

	data = ECOMALLOC(words->size * sizeof(primer_t),
			         "Cannot allocate memory for fuzzy matching results");

	params.circular = options->circular;
	params.maxerr   = options->error_max;
100 101
//	params.omask    = (1 << options->strict_three_prime) -1;
	params.omask    = 0;
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 151 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
	params.patlen   = options->primer_length;

	positions.val=NULL;

	for (i=0,w=0; i < words->size; i++)
	{
		data[w].word=WORD(words->words[i]);
		data[w].inexample = 0;
		data[w].outexample= 0;
		count = 1;

		if (conserved)
		{
			data[w].directCount=ECOMALLOC(seqdbsize * sizeof(uint32_t),
                                      "Cannot allocate memory for primer position");
			data[w].directPos = ECOMALLOC(seqdbsize * sizeof(poslist_t),
				                      "Cannot allocate memory for primer position");
			data[w].reverseCount=ECOMALLOC(seqdbsize * sizeof(uint32_t),
	                                      "Cannot allocate memory for primer position");
			data[w].reversePos = ECOMALLOC(seqdbsize * sizeof(poslist_t),
					                       "Cannot allocate memory for primer position");
		}

		pattern = buildPatternFromWord(data[w].word,options->primer_length);
		positions.val=NULL;

		for (j=0; j < seqdbsize && (count < 2 || !options->no_multi_match); j++)
		{
			positions.cursor=0;
			positions.top =0;
			if (!positions.val)
			{
				positions.size=1;
				positions.val = ECOMALLOC(sizeof(uint32_t),
                                      "Cannot allocate memory for primer position");
			}


			count = ManberAll(database[j],pattern,&params,&positions);
			data[w].directCount[j]=count;


			if (count>1)
			{
				data[w].directPos[j].pointer = (uint32_t*)positions.val;
				positions.val=NULL;
			}
			else
			{
				data[w].directPos[j].pointer=NULL;
				if (count==1)
					data[w].directPos[j].value = (uint32_t)*(positions.val);
			}


		}

		pattern = buildPatternFromWord(ecoComplementWord(data[w].word,options->primer_length),
				                       options->primer_length);

		for (j=0; j < seqdbsize && (count < 2 || !options->no_multi_match); j++)
		{
			positions.cursor=0;
			positions.top =0;
			if (!positions.val)
			{
				positions.size=1;
				positions.val = ECOMALLOC(sizeof(uint32_t),
                                      "Cannot allocate memory for primer position");
			}

			count = ManberAll(database[j],pattern,&params,&positions);
			data[w].reverseCount[j]=count;

			if (count>1)
			{
				data[w].reversePos[j].pointer = (uint32_t*)positions.val;
				positions.val=NULL;
			}
			else
			{
				data[w].reversePos[j].pointer=NULL;
				if (count==1)
					data[w].reversePos[j].value = (uint32_t)*(positions.val);
			}

			if (database[j]->isexample)
			{
				data[w].inexample+=(data[w].directCount[j] || data[w].reverseCount[j])? 1:0;
			}
			else
			{
				data[w].outexample+=(data[w].directCount[j] || data[w].reverseCount[j])? 1:0;

			}

            count+=data[w].directCount[j];
		}

		data[w].good = data[w].inexample >= inSequenceQuorum && data[w].outexample <= outSequenceQuorum;
		goodPrimers+=data[w].good? 1:0;

		fprintf(stderr,"Primers %5d/%d analyzed  => sequence : %s in %d example and %d counterexample sequences       \r",
				i+1,words->size,ecoUnhashWord(data[w].word,options->primer_length),
				data[w].inexample,data[w].outexample);


		conserved=data[w].inexample >= inSequenceQuorum;
		conserved=conserved && (count < 2 || !options->no_multi_match);

		if (conserved)
			w++;
	}

	if (positions.val)
		ECOFREE(positions.val,"Free stack position pointer");

	if (!conserved)
	{
		ECOFREE(data[w].directCount,"Free direct count table");
		ECOFREE(data[w].directPos,"Free direct count table");
		ECOFREE(data[w].reverseCount,"Free direct count table");
		ECOFREE(data[w].reversePos,"Free direct count table");
	}

	fprintf(stderr,"\n\nOn %d analyzed primers %d respect quorum conditions\n",words->size,goodPrimers);
	fprintf(stderr,"Conserved primers for further analysis : %d/%d\n",w,words->size);

	primers = ECOMALLOC(sizeof(primercount_t),"Cannot allocate memory for primer table");
	primers->primers=ECOREALLOC(data,
			                    w * sizeof(primer_t),
								"Cannot reallocate memory for fuzzy matching results");
	primers->size=w;

	return primers;
}