hmmer-3.3/src/p7_hmm.c
/* Function:  p7_hmm_SetConsensus()
 * Synopsis:  Set the consensus residue line of the HMM.
 *
 * Purpose:   Sets the consensus annotation line of the model <hmm>.
 *            
 *            Behavior differs, depending on whether this is a
 *            single-sequence model (i.e. phmmer) or a standard
 *            model of a multiple sequence alignment. If <sq> is
 *            non-<NULL> this is a single-sequence model and <sq> is
 *            the digital sequence it was built from. If <sq> is <NULL>
 *            this is a standard multiple-sequence model.
 *            
 *            In a standard model, the most likely (highest emission
 *            probability) residue is the consensus at each position.
 *            In a single-sequence model, the consensus is the
 *            sequence itself.
 *            
 *            In both cases, if the emission probability is $\geq$
 *            certain threshold, the residue is upper cased. The
 *            threshold is arbitrarily set to 0.9 for nucleic acid
 *            alphabets (<eslDNA>, <eslRNA>) and 0.5 for amino acid
 *            alphabets (<eslAMINO>) and all other alphabets.
 *            
 *            The special handling of single-sequence models avoids
 *            a counterintuitive case where the most likely residue is
 *            not the original residue. For example, under the
 *            BLOSUM62 matrix, given an observed M, the most likely
 *            aligned residue is an L, not an M. (Because L is so much
 *            more likely a priori than M.)
 *
 * Args:      hmm    - model with valid probability parameters mat[1..M][x]
 *            sq     - NULL if a standard model;
 *                     or the query sequence for a single-sequence model.
 *           
 * Returns:   <eslOK> on success. The <p7H_CONS> flag on the <hmm> is raised
 *            if it wasn't already. The <hmm->consensus> line is set.
 *
 * Throws:    <eslEMEM> on allocation error. The <p7H_CONS> is dropped, even
 *            if it was up to begin with, and the <hmm->consensus> is <NULL>,
 *            even if we had one to begin with.
 *
 * Xref:      SRE:J8/26.
 */
p7_hmm_SetConsensus()
Synopsis:  Set the consensus residue line of the HMM.

Sets the consensus annotation line of the model .

Behavior differs, depending on whether this is a
single-sequence model  or a standard
model of a multiple sequence alignment. If  is
non- this is a single-sequence model and  is
the digital sequence it was built from. If  is 
this is a standard multiple-sequence model.

In a standard model, the most likely (highest emission
probability) residue is the consensus at each position.
In a single-sequence model, the consensus is the
sequence itself.

In both cases, if the emission probability is $\geq$
certain threshold, the residue is upper cased. The
threshold is arbitrarily set to 0.9 for nucleic acid
alphabets (, ) and 0.5 for amino acid
alphabets () and all other alphabets.

The special handling of single-sequence models avoids
a counterintuitive case where the most likely residue is
not the original residue. For example, under the
BLOSUM62 matrix, given an observed M, the most likely
aligned residue is an L, not an M. (Because L is so much
more likely a priori than M.)

hmm    - model with valid probability parameters mat[1..M][x]
sq     - NULL if a standard model;
or the query sequence for a single-sequence model.

 on success. The  flag on the  is raised
if it wasn't already. The consensus> line is set.

 on allocation error. The  is dropped, even
if it was up to begin with, and the consensus> is ,
even if we had one to begin with.


p7_hmm_SetConsensus () Synopsis : Set the consensus residue line of the HMM . Sets the consensus annotation line of the model . Behavior differs depending on whether this is a single - sequence model or a standard model of a multiple sequence alignment . If is non - this is a single - sequence model and is the digital sequence it was built from . If is this is a standard multiple - sequence model . In a standard model the most likely ( highest emission probability ) residue is the consensus at each position . In a single - sequence model the consensus is the sequence itself . In both cases if the emission probability is $ \ geq$ certain threshold the residue is upper cased . The threshold is arbitrarily set to 0 . 9 for nucleic acid alphabets ( ) and 0 . 5 for amino acid alphabets () and all other alphabets . The special handling of single - sequence models avoids a counterintuitive case where the most likely residue is not the original residue . For example under the BLOSUM62 matrix given an observed M the most likely aligned residue is an L not an M . ( Because L is so much more likely a priori than M . ) hmm - model with valid probability parameters mat [ 1 .. M ] [ x ] sq - NULL if a standard model ; or the query sequence for a single - sequence model . on success . The flag on the is raised if it wasn ' t already . The consensus > line is set . on allocation error . The is dropped even if it was up to begin with and the consensus > is even if we had one to begin with .
int
p7_hmm_SetConsensus(P7_HMM *hmm, ESL_SQ *sq)
{
  int   k, x;
  float mthresh;
  int   status;
  if (! hmm->consensus) ESL_ALLOC(hmm->consensus, sizeof(char) * (hmm->M+2));
  if      (hmm->abc->type == eslAMINO) mthresh = 0.5;
  else if (hmm->abc->type == eslDNA)   mthresh = 0.9;
  else if (hmm->abc->type == eslRNA)   mthresh = 0.9;
  else                                 mthresh = 0.5;
  hmm->consensus[0] = ' ';
  for (k = 1; k <= hmm->M; k++) 
    {
      x = (sq ?  sq->dsq[k] : esl_vec_FArgMax(hmm->mat[k], hmm->abc->K));
      hmm->consensus[k] = ((hmm->mat[k][x] >= mthresh) ? toupper(hmm->abc->sym[x]) : tolower(hmm->abc->sym[x]));
    }
  hmm->consensus[hmm->M+1] = '\0';
  hmm->flags  |= p7H_CONS;	
  return eslOK;
 ERROR:
  if (hmm->consensus) free(hmm->consensus);
  hmm->consensus = NULL;
  hmm->flags    &= (~p7H_CONS);	
  return status;
}
int p7_hmm_SetConsensus ( P7_HMM * hmm , ESL_SQ * sq ) { int k , x ; float mthresh ; int status ; if ( ! hmm -> consensus ) ESL_ALLOC ( hmm -> consensus , sizeof ( char ) * ( hmm -> M + 2 ) ) ; if ( hmm -> abc -> type == eslAMINO ) mthresh = 0.5 ; else if ( hmm -> abc -> type == eslDNA ) mthresh = 0.9 ; else if ( hmm -> abc -> type == eslRNA ) mthresh = 0.9 ; else mthresh = 0.5 ; hmm -> consensus [ 0 ] = ' ' ; for ( k = 1 ; k <= hmm -> M ; k ++ ) { x = ( sq ? sq -> dsq [ k ] : esl_vec_FArgMax ( hmm -> mat [ k ] , hmm -> abc -> K ) ) ; hmm -> consensus [ k ] = ( ( hmm -> mat [ k ] [ x ] >= mthresh ) ? toupper ( hmm -> abc -> sym [ x ] ) : tolower ( hmm -> abc -> sym [ x ] ) ) ; } hmm -> consensus [ hmm -> M + 1 ] = ' \0 ' ; hmm -> flags |= p7H_CONS ; return eslOK ; ERROR : if ( hmm -> consensus ) free ( hmm -> consensus ) ; hmm -> consensus = NULL ; hmm -> flags &= ( ~ p7H_CONS ) ; return status ; }
Function:  p7_hmm_SetConsensus()
 Synopsis:  Set the consensus residue line of the HMM.
Function : p7_hmm_SetConsensus () Synopsis : Set the consensus residue line of the HMM .
/* allocation, if needed */ /* set our arbitrary threshold for upper/lower casing */