deps/kb_meme/meme-5.0.1/src/build-hmm.c
/*************************************************************************
 * Find the index, in a complete MHMM, of a spacer state, given the
 * indices of the motifs between which the spacer appears.
 *
 * In a complete MHMM, spacers appear at the beginning of the model,
 * starting at state 1 (state 0 is the start state).  The first n
 * spacers are those between the start state and each of the n motifs.
 * Next, there are n+1 spacers, corresponding to those between motif 1
 * motif and every motif plus the end state.
 *
 * The spacers can thus be thought of as indexed according to a
 * matrix, where the row is the motif from which the spacer comes, and
 * the column is the motif where it's going.
 *
 * Here's an example of such a matrix for the case of 3 motifs:
 *
 *    S  1  2  3  E
 *  S x  1  2  3  x
 *  1 x  4  5  6  7
 *  2 x  8  9 10 11
 *  3 x 12 13 14 15
 *  E x  x  x  x  x
 *
 * More generally, the spacers are indexed as follows:
 * 
 *  Index  From  To
 * ------------------
 *    1     0     1    Spacers from the start state to each motif.
 *    2     0     2
 *   ...   ...   ...
 *    n     0     n
 * ------------------
 *   n+1    1     1    Spacer from the first motif to every other motif,
 *   n+2    1     2    including the end state.
 *   ...   ...   ...
 *  2n+1    1    n+1
 * ------------------
 *  2n+2    2     1    Spacers from the second motif.
 *  2n+3    2     2
 *   ...   ...   ...
 *  3n+2    2    n+1
 * ------------------
 *    .     .     .
 *    .     .     .
 *    .     .     .     
 * ------------------
 *  n*n+n   n     1    Spacers from the last motif.
 * n*n+n+1  n     2
 *   ...   ...   ...
 * n*n+2n   n    n+1
 * ------------------
 *
 * This picture is further complicated by the fact that every spacer
 * can contain multiple states.  Thus, every 'n' above should be
 * replaced by a 'pn', where p is the number of states per spacer.
 *
 * RETURN: Index of the beginning or ending of the requested spacer.
 *************************************************************************/
Find the index, in a complete MHMM, of a spacer state, given the
indices of the motifs between which the spacer appears.

In a complete MHMM, spacers appear at the beginning of the model,
starting at state 1 (state 0 is the start state).  The first n
spacers are those between the start state and each of the n motifs.
Next, there are n+1 spacers, corresponding to those between motif 1
motif and every motif plus the end state.

The spacers can thus be thought of as indexed according to a
matrix, where the row is the motif from which the spacer comes, and
the column is the motif where it's going.

Here's an example of such a matrix for the case of 3 motifs.



More generally, the spacers are indexed as follows.

Index  From  To

1     0     1    Spacers from the start state to each motif.

n+1    1     1    Spacer from the first motif to every other motif,
n+2    1     2    including the end state.

2n+2    2     1    Spacers from the second motif.





This picture is further complicated by the fact that every spacer
can contain multiple states.  Thus, every 'n' above should be
replaced by a 'pn', where p is the number of states per spacer.

Index of the beginning or ending of the requested spacer.
Find the index in a complete MHMM of a spacer state given the indices of the motifs between which the spacer appears . In a complete MHMM spacers appear at the beginning of the model starting at state 1 ( state 0 is the start state ) . The first n spacers are those between the start state and each of the n motifs . Next there are n + 1 spacers corresponding to those between motif 1 motif and every motif plus the end state . The spacers can thus be thought of as indexed according to a matrix where the row is the motif from which the spacer comes and the column is the motif where it ' s going . Here ' s an example of such a matrix for the case of 3 motifs . More generally the spacers are indexed as follows . Index From To 1 0 1 Spacers from the start state to each motif . n + 1 1 1 Spacer from the first motif to every other motif n + 2 1 2 including the end state . 2n + 2 2 1 Spacers from the second motif . This picture is further complicated by the fact that every spacer can contain multiple states . Thus every ' n ' above should be replaced by a ' pn ' where p is the number of states per spacer . Index of the beginning or ending of the requested spacer .
static int spacer_index
  (const int from_motif,      
   const int to_motif,        
   const bool end_state, 
   const int nmotifs,         
   const int spacer_states)   
{
  int return_value;
  if (to_motif == 0) {
    die("Requested spacer leading to the start state.\n");
  } else if (from_motif == nmotifs + 1) {
    die("Requested spacer leading from the end state.\n");
  }
  assert(to_motif > 0 && to_motif <= (nmotifs + 1));
  assert(from_motif >= 0 && from_motif <= nmotifs);
  return_value = 1;
  return_value += from_motif * spacer_states * (nmotifs+1);
  return_value += (to_motif - 1) * spacer_states;
  if (from_motif > 0)
    return_value -= spacer_states;
  if (end_state)
    return_value += spacer_states - 1;
  return(return_value);
}
static int spacer_index ( const int from_motif , const int to_motif , const bool end_state , const int nmotifs , const int spacer_states ) { int return_value ; if ( to_motif == 0 ) { die ( " \n " ) ; } else if ( from_motif == nmotifs + 1 ) { die ( " \n " ) ; } assert ( to_motif > 0 && to_motif <= ( nmotifs + 1 ) ) ; assert ( from_motif >= 0 && from_motif <= nmotifs ) ; return_value = 1 ; return_value += from_motif * spacer_states * ( nmotifs + 1 ) ; return_value += ( to_motif - 1 ) * spacer_states ; if ( from_motif > 0 ) return_value -= spacer_states ; if ( end_state ) return_value += spacer_states - 1 ; return ( return_value ) ; }
Find the index, in a complete MHMM, of a spacer state, given the
 indices of the motifs between which the spacer appears.
Find the index in a complete MHMM of a spacer state given the indices of the motifs between which the spacer appears .
/* Index of motif preceding this spacer
				 (0=start,n+1=end). */ /* Index of motif following this spacer
			         (0=start,n+1=end). */ /* Is this an end state? */ /* Total number of motifs in the model. */ /* Number of states per spacer in the model. */ /* Make sure we're not trying something illegal. */ /* The spacer indices start at 1. */ /* Add in the lengths of complete rows. */ /* Add the final row. */ /* The first row of the matrix is missing one value, so subtract it. */ /* If the end of the spacer was requested, add. */