Example #1
0
/* Function: CP9Logoddsify()
 *
 * Purpose:  Take an HMM with valid probabilities, and
 *           fill in the integer log-odds score section of the model.
 *
 *    Notes on log-odds scores (simplified from plan7.c):
 *         type of parameter       probability        score
 *         -----------------       -----------        ------
 *         any emission             p_x           log_2 p_x/null_x
 *         any transition           t_x           log_2 t_x
 *
 * Args:      hmm          - the hmm to calculate scores in.
 *
 * Return:    (void)
 *            hmm scores are filled in.
 */
void
CP9Logoddsify(CP9_t *hmm)
{
    /*printf("in CP9Logoddsify()\n");*/
    int k;			/* counter for model position */
    int x;			/* counter for symbols        */
    int *sc;
    int status;

    if (hmm->flags & CPLAN9_HASBITS) return;

    ESL_ALLOC(sc, hmm->abc->Kp * sizeof(int));

    /* Symbol emission scores
     */

    sc[hmm->abc->K]     = -INFTY; /* gap character */
    sc[hmm->abc->Kp-1]  = -INFTY; /* missing data character */
    sc[hmm->abc->Kp-2]  = -INFTY; /* non-residue data character */

    /* Insert emission scores, relies on sc[K, Kp-1] initialization to -inf above */
    for (k = 0; k <= hmm->M; k++) {
        for (x = 0; x < hmm->abc->K; x++)
            sc[x] = Prob2Score(hmm->ins[k][x], hmm->null[x]);
        esl_abc_IExpectScVec(hmm->abc, sc, hmm->null);
        for (x = 0; x < hmm->abc->Kp; x++) {
            hmm->isc[x][k] = sc[x];
        }
    }

    /* Match emission scores, relies on sc[K, Kp-1] initialization to -inf above */
    for (k = 1; k <= hmm->M; k++) {
        for (x = 0; x < hmm->abc->K; x++)
            sc[x] = Prob2Score(hmm->mat[k][x], hmm->null[x]);
        esl_abc_IExpectScVec(hmm->abc, sc, hmm->null);
        for (x = 0; x < hmm->abc->Kp; x++) {
            hmm->msc[x][k] = sc[x];
        }
    }

    for (k = 0; k <= hmm->M; k++)
    {
        hmm->tsc[CTMM][k] = Prob2Score(hmm->t[k][CTMM], 1.0);
        hmm->tsc[CTMI][k] = Prob2Score(hmm->t[k][CTMI], 1.0);
        hmm->tsc[CTMD][k] = Prob2Score(hmm->t[k][CTMD], 1.0);
        hmm->tsc[CTMEL][k] = Prob2Score(hmm->t[k][CTMEL], 1.0);
        hmm->tsc[CTIM][k] = Prob2Score(hmm->t[k][CTIM], 1.0);
        hmm->tsc[CTII][k] = Prob2Score(hmm->t[k][CTII], 1.0);
        hmm->tsc[CTID][k] = Prob2Score(hmm->t[k][CTID], 1.0);
        if(k != 0)
        {
            hmm->tsc[CTDM][k] = Prob2Score(hmm->t[k][CTDM], 1.0);
            hmm->tsc[CTDI][k] = Prob2Score(hmm->t[k][CTDI], 1.0);
            hmm->tsc[CTDD][k] = Prob2Score(hmm->t[k][CTDD], 1.0);
        }
        else
        {
            hmm->tsc[CTDM][k] = -INFTY;
            hmm->tsc[CTDD][k] = -INFTY; /*D_0 doesn't exist*/
            hmm->tsc[CTDI][k] = -INFTY;
        }
        if(k != 0)
        {
            hmm->bsc[k]   = Prob2Score(hmm->begin[k], 1.0);
            hmm->esc[k] = Prob2Score(hmm->end[k], 1.0);
        }
    }
    hmm->el_selfsc = Prob2Score(hmm->el_self, 1.0);

    /* Finally, fill the efficiently reordered transition scores for this HMM. */
    for (k = 0 ; k <= hmm->M; k++) {
        int *otsc_k = hmm->otsc + k*cp9O_NTRANS;
        otsc_k[cp9O_MM] = hmm->tsc[CTMM][k];
        otsc_k[cp9O_MI] = hmm->tsc[CTMI][k];
        otsc_k[cp9O_MD] = hmm->tsc[CTMD][k];
        otsc_k[cp9O_IM] = hmm->tsc[CTIM][k];
        otsc_k[cp9O_II] = hmm->tsc[CTII][k];
        otsc_k[cp9O_DM] = hmm->tsc[CTDM][k];
        otsc_k[cp9O_DD] = hmm->tsc[CTDD][k];
        otsc_k[cp9O_ID] = hmm->tsc[CTID][k];
        otsc_k[cp9O_DI] = hmm->tsc[CTDI][k];
        otsc_k[cp9O_BM] = hmm->bsc[k];
        otsc_k[cp9O_MEL]= hmm->tsc[CTMEL][k];
        otsc_k[cp9O_ME] = hmm->esc[k];
    }

    hmm->flags |= CPLAN9_HASBITS;	/* raise the log-odds ready flag */

    free(sc);

    return;

ERROR:
    cm_Fail("Memory allocation error.\n");
    return; /* never reached */
}
/* Function: CP9Logoddsify()
 * 
 * Purpose:  Take an HMM with valid probabilities, and
 *           fill in the integer log-odds score section of the model.
 *           
 *    Notes on log-odds scores (simplified from plan7.c):
 *         type of parameter       probability        score
 *         -----------------       -----------        ------
 *         any emission             p_x           log_2 p_x/null_x
 *         any transition           t_x           log_2 t_x
 *             
 * Args:      hmm          - the hmm to calculate scores in.
 *                  
 * Return:    (void)
 *            hmm scores are filled in.
 */  
void
CP9Logoddsify(CP9_t *hmm)
{
  int k;			/* counter for model position */
  int x;			/* counter for symbols        */
  int sc[MAXDEGEN];             /* 17, NEED TO INCREASE FOR BIGGER ALPHABETS! */

  if (hmm->flags & CPLAN9_HASBITS) return;

  /* Symbol emission scores
   */

  sc[hmm->abc->K]     = -INFTY; /* gap character */
  sc[hmm->abc->Kp-1]  = -INFTY; /* missing data character */

  /* Insert emission scores, relies on sc[K, Kp-1] initialization to -inf above */
  for (k = 0; k <= hmm->M; k++) {
    for (x = 0; x < hmm->abc->K; x++) 
      sc[x] = Prob2Score(hmm->ins[k][x], hmm->null[x]);
    esl_abc_IExpectScVec(hmm->abc, sc, hmm->null); 
    for (x = 0; x < hmm->abc->Kp; x++) {
      hmm->isc[x][k] = sc[x];
    }
  }

  /* Match emission scores, relies on sc[K, Kp-1] initialization to -inf above */
  for (k = 1; k <= hmm->M; k++) {
    for (x = 0; x < hmm->abc->K; x++) 
      sc[x] = Prob2Score(hmm->mat[k][x], hmm->null[x]);
    esl_abc_IExpectScVec(hmm->abc, sc, hmm->null); 
    for (x = 0; x < hmm->abc->Kp; x++) {
      hmm->msc[x][k] = sc[x];
    }
  }
  
  for (k = 0; k <= hmm->M; k++)
    {
      hmm->tsc[CTMM][k] = Prob2Score(hmm->t[k][CTMM], 1.0);
      hmm->tsc[CTMI][k] = Prob2Score(hmm->t[k][CTMI], 1.0);
      hmm->tsc[CTMD][k] = Prob2Score(hmm->t[k][CTMD], 1.0);
      hmm->tsc[CTMEL][k] = Prob2Score(hmm->t[k][CTMEL], 1.0);
      hmm->tsc[CTIM][k] = Prob2Score(hmm->t[k][CTIM], 1.0);
      hmm->tsc[CTII][k] = Prob2Score(hmm->t[k][CTII], 1.0);
      hmm->tsc[CTID][k] = Prob2Score(hmm->t[k][CTID], 1.0);
      if(k != 0)
	{
	  hmm->tsc[CTDM][k] = Prob2Score(hmm->t[k][CTDM], 1.0);
	  hmm->tsc[CTDI][k] = Prob2Score(hmm->t[k][CTDI], 1.0);
	  hmm->tsc[CTDD][k] = Prob2Score(hmm->t[k][CTDD], 1.0);
	}
      else
	{
	  hmm->tsc[CTDM][k] = -INFTY;
	  hmm->tsc[CTDD][k] = -INFTY; /*D_0 doesn't exist*/
	  hmm->tsc[CTDI][k] = -INFTY;
	}
      if(k != 0)
	{
	  hmm->bsc[k]   = Prob2Score(hmm->begin[k], 1.0);
	  //if(hmm->flags & CPLAN9_LOCAL_END) hmm->esc[k]   = 0;
	  //else hmm->esc[k]   = -INFTY;
	  hmm->esc[k] = Prob2Score(hmm->end[k], 1.0);
	}
    }
  hmm->el_selfsc = Prob2Score(hmm->el_self, 1.0);

  /* Finally, fill the efficiently reordered transition scores for this HMM. */
  for (k = 0 ; k <= hmm->M; k++) {
    int *otsc_k = hmm->otsc + k*cp9O_NTRANS;
    otsc_k[cp9O_MM] = hmm->tsc[CTMM][k];
    otsc_k[cp9O_MI] = hmm->tsc[CTMI][k];
    otsc_k[cp9O_MD] = hmm->tsc[CTMD][k];
    otsc_k[cp9O_IM] = hmm->tsc[CTIM][k];
    otsc_k[cp9O_II] = hmm->tsc[CTII][k];
    otsc_k[cp9O_DM] = hmm->tsc[CTDM][k];
    otsc_k[cp9O_DD] = hmm->tsc[CTDD][k];
    otsc_k[cp9O_ID] = hmm->tsc[CTID][k];
    otsc_k[cp9O_DI] = hmm->tsc[CTDI][k];
    otsc_k[cp9O_BM] = hmm->bsc[k];
    otsc_k[cp9O_MEL]= hmm->tsc[CTMEL][k];
    otsc_k[cp9O_ME] = hmm->esc[k];
  }

  hmm->flags |= CPLAN9_HASBITS;	/* raise the log-odds ready flag */
}