#include <ncbi.h>
#include <blast/blast.h>

/*	pcnt

  Determine the "n" parameter for Poisson statistics.  The determination
  performed here is without regard for "consistency" between the HSPs.
  For a "consistent" determination, see consistn_dynamic().

  The given list of HSPs is first sorted here in decreasing sort order
  by normalized-score (score X Lambda).  IF THE LIST WAS ALREADY SORTED
  BY SOME OTHER CRITERION, THAT ORDER IS LOST!
*/

void LIBCALL
pcnt(hp0)
	BLAST_HSPPtr	hp0;
{
	BLAST_HSPPtr	hp;
	unsigned	cnts[9];
	int		sgn, qsign, ssign;

	if (hp0->next == NULL) {
		hp0->n = 1;
		return;
	}

	LinkSort((VoidPtr PNTR)&hp0, offsetof(BLAST_HSP,next), BlastHSPCmpByNormScore);

	cnts[0] = cnts[1] = cnts[2] = cnts[3] = cnts[4]
		= cnts[5] = cnts[6] = cnts[7] = cnts[8] = 0;

	for (hp = hp0; hp != NULL; hp = hp->next) {
		qsign = SIGN(hp->q_seg.frame);
		ssign = SIGN(hp->s_seg.frame);
		sgn = (qsign + 1) * 3 + ssign + 1;
		hp->n = ++cnts[sgn];
	}
}
