#include <ncbi.h>
#include <gishlib.h>
#include "blastapp.h"

void
evals_x(hlp, tlen, dblen)
	HitListPtr	hlp;
	unsigned long	tlen;
	unsigned long	dblen;
{
	register HSPPtr	hp, minhp;
	register double	p;
	Score_t	highscore = SCORE_MIN, scoreray[3];
	register int	frame;
	int		sgn;
	double	x;
	seq_data	*sdp;

	scoreray[0] = scoreray[1] = scoreray[2] = 0;
	for (hp=minhp= hlp->hp; hp != NULL; hp = hp->next) {
		frame = hp->frame;

		sgn = SIGN(frame) + 1;
		scoreray[sgn] += hp->score;

		if (frame > 0)
			--frame;
		else
			frame = 2 - frame;
		sdp = &sd[frame];
		if (hp->score > highscore)
			highscore = hp->score;
		x = stoe(hp->score, sdp->Lambda, sdp->K, sdp->H, tlen, sdp->efflen);
		p = etopoissonp(hp->n, x);
		p = gapdecay(hp->n, p, gapdecayrate);

		/*
		Calculate the expected no. of occurrences using length of the database
		sequence versus the total length of the database
		*/
		hp->expect = (p * dblen)/tlen;
		hp->P_val = -1.;

		/* Remember which HSP had the smallest Poisson probability */
		if (hp->expect < minhp->expect)
			minhp = hp;
	}
	hlp->signifhsp = minhp;
	hlp->highscore = highscore;
	hlp->totalscore = MAX(scoreray[0], MAX(scoreray[1], scoreray[2]));
}


void
consist_evals_x(hlp, tlen, dblen)
	HitListPtr	hlp;
	unsigned long	tlen;
	unsigned long	dblen;
{
	register HSPPtr	hp, minhp;
	register double	p;
	Score_t	highscore = SCORE_MIN, scoreray[3];
	register int	frame;
	int		sgn;
	double	x;
	seq_data	*sdp;

	scoreray[0] = scoreray[1] = scoreray[2] = 0;
	for (hp=minhp= hlp->hp; hp != NULL; hp = hp->next) {
		frame = hp->frame;

		sgn = SIGN(frame) + 1;
		scoreray[sgn] += hp->score;

		if (frame > 0)
			--frame;
		else
			frame = 2 - frame;
		sdp = &sd[frame];
		if (hp->score > highscore)
			highscore = hp->score;
		x = stoe(hp->score, sdp->Lambda, sdp->K, sdp->H, tlen, sdp->efflen);
		p = etoconsistp(hp->n, x);
		p = gapdecay(hp->n, p, gapdecayrate);

		/*
		Calculate the expected no. of occurrences using length of the database
		sequence versus the total length of the database
		*/
		hp->expect = (p * dblen)/tlen;
		hp->P_val = -1.;

		/* Remember which HSP had the smallest Poisson probability */
		if (hp->expect < minhp->expect)
			minhp = hp;
	}
	hlp->signifhsp = minhp;
	hlp->highscore = highscore;
	hlp->totalscore = MAX(scoreray[0], MAX(scoreray[1], scoreray[2]));
}
