deps/kb_meme/meme-5.0.1/src/build-hmm.c
/*************************************************************************
 * Build a completely connected HMM.
 *************************************************************************/
Build a completely connected HMM.
Build a completely connected HMM .
void build_complete_hmm
  (ARRAY_T* background,
   int spacer_states, 
   MOTIF_T *motifs,
   int nmotifs,
   MATRIX_T *transp_freq,
   MATRIX_T *spacer_ave,
   bool fim,
   MHMM_T **the_hmm)
{
  ALPH_T*   alph;
  int motif_states; 
  int num_spacers;  
  int num_states;   
  int i_motif;      
  int j_motif;      
  int i_position;   
  int i_state = 0;  
  assert(nmotifs > 0);
  alph = get_motif_alph(motifs);
  for (motif_states = 0, i_motif = 0; i_motif < nmotifs; i_motif++)
    motif_states += get_motif_length(motif_at(motifs, i_motif));
  num_spacers = nmotifs * 2;
  num_spacers += nmotifs * nmotifs;
  num_states = motif_states + (num_spacers * spacer_states) + 2;
  *the_hmm = allocate_mhmm(alph, num_states);
  (*the_hmm)->type = COMPLETE_HMM;
  (*the_hmm)->num_motifs = nmotifs;
  (*the_hmm)->num_states = num_states;
  (*the_hmm)->num_spacers = ((nmotifs + 1) * (nmotifs + 1)) - 1;
  (*the_hmm)->spacer_states = spacer_states;
  copy_array(background, (*the_hmm)->background);
  build_complete_state(
      START_STATE, 
      i_state,
      alph,
      0, 
      NULL, 
      0, 
      NON_MOTIF_INDEX,
      NON_MOTIF_POSITION,
      nmotifs,
      0, 
      0, 
      transp_freq,
      spacer_states,
      num_spacers,
      motifs,
      &((*the_hmm)->states[i_state]));
  i_state++;
  int from_motif_state, to_motif_state;
  for (i_motif = 0; i_motif <= nmotifs; i_motif++) {
    for (j_motif = 1; j_motif <= nmotifs+1; j_motif++) {
      if ((i_motif == 0) && (j_motif == nmotifs+1))
        continue;
      for (i_position = 0; i_position < spacer_states; i_position++, i_state++) {
        build_complete_state(
            SPACER_STATE, 
            i_state, 
            alph,
            get_matrix_cell(i_motif, j_motif, spacer_ave),
            background,
            SPACER_NUMSITES,
            NON_MOTIF_INDEX,
            i_position,
            nmotifs,
            i_motif,
            j_motif,
            transp_freq,
            spacer_states,
            num_spacers,
            motifs,
            &((*the_hmm)->states[i_state]));
      }
    }
  }
  for (i_motif = 0; i_motif < nmotifs; i_motif++) {
    MOTIF_T *this_motif = motif_at(motifs, i_motif);
    STATE_T state;
    for (i_position = 0; i_position < get_motif_length(this_motif); i_position++, i_state++) {
      if (i_position == 0) {
        state = START_MOTIF_STATE;
      } else if (i_position == (get_motif_length(this_motif) - 1)) {
        state = END_MOTIF_STATE;
      } else {
        state = MID_MOTIF_STATE;
      }
      build_complete_state(
          MID_MOTIF_STATE, 
          i_state,
          alph,
          0, 
          get_matrix_row(i_position, get_motif_freqs(this_motif)),
          get_motif_nsites(this_motif),
          i_motif,
          i_position, 
          nmotifs,
          0, 
          0, 
          transp_freq,
          spacer_states,
          num_spacers,
          motifs,
          &((*the_hmm)->states[i_state]));
    }
  }
  build_complete_state(
      END_STATE, 
      i_state,
      alph,
      0, 
      NULL, 
      0, 
      NON_MOTIF_INDEX,
      NON_MOTIF_POSITION,
      nmotifs,
      0, 
      0, 
      transp_freq,
      spacer_states,
      num_spacers,
      motifs,
      &((*the_hmm)->states[i_state]));
  i_state++;
  if (fim) {
    convert_to_fims(*the_hmm);
  }
  build_transition_matrix(*the_hmm);
}
void build_complete_hmm ( ARRAY_T * background , int spacer_states , MOTIF_T * motifs , int nmotifs , MATRIX_T * transp_freq , MATRIX_T * spacer_ave , bool fim , MHMM_T * * the_hmm ) { ALPH_T * alph ; int motif_states ; int num_spacers ; int num_states ; int i_motif ; int j_motif ; int i_position ; int i_state = 0 ; assert ( nmotifs > 0 ) ; alph = get_motif_alph ( motifs ) ; for ( motif_states = 0 , i_motif = 0 ; i_motif < nmotifs ; i_motif ++ ) motif_states += get_motif_length ( motif_at ( motifs , i_motif ) ) ; num_spacers = nmotifs * 2 ; num_spacers += nmotifs * nmotifs ; num_states = motif_states + ( num_spacers * spacer_states ) + 2 ; * the_hmm = allocate_mhmm ( alph , num_states ) ; ( * the_hmm ) -> type = COMPLETE_HMM ; ( * the_hmm ) -> num_motifs = nmotifs ; ( * the_hmm ) -> num_states = num_states ; ( * the_hmm ) -> num_spacers = ( ( nmotifs + 1 ) * ( nmotifs + 1 ) ) - 1 ; ( * the_hmm ) -> spacer_states = spacer_states ; copy_array ( background , ( * the_hmm ) -> background ) ; build_complete_state ( START_STATE , i_state , alph , 0 , NULL , 0 , NON_MOTIF_INDEX , NON_MOTIF_POSITION , nmotifs , 0 , 0 , transp_freq , spacer_states , num_spacers , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; int from_motif_state , to_motif_state ; for ( i_motif = 0 ; i_motif <= nmotifs ; i_motif ++ ) { for ( j_motif = 1 ; j_motif <= nmotifs + 1 ; j_motif ++ ) { if ( ( i_motif == 0 ) && ( j_motif == nmotifs + 1 ) ) continue ; for ( i_position = 0 ; i_position < spacer_states ; i_position ++ , i_state ++ ) { build_complete_state ( SPACER_STATE , i_state , alph , get_matrix_cell ( i_motif , j_motif , spacer_ave ) , background , SPACER_NUMSITES , NON_MOTIF_INDEX , i_position , nmotifs , i_motif , j_motif , transp_freq , spacer_states , num_spacers , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; } } } for ( i_motif = 0 ; i_motif < nmotifs ; i_motif ++ ) { MOTIF_T * this_motif = motif_at ( motifs , i_motif ) ; STATE_T state ; for ( i_position = 0 ; i_position < get_motif_length ( this_motif ) ; i_position ++ , i_state ++ ) { if ( i_position == 0 ) { state = START_MOTIF_STATE ; } else if ( i_position == ( get_motif_length ( this_motif ) - 1 ) ) { state = END_MOTIF_STATE ; } else { state = MID_MOTIF_STATE ; } build_complete_state ( MID_MOTIF_STATE , i_state , alph , 0 , get_matrix_row ( i_position , get_motif_freqs ( this_motif ) ) , get_motif_nsites ( this_motif ) , i_motif , i_position , nmotifs , 0 , 0 , transp_freq , spacer_states , num_spacers , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; } } build_complete_state ( END_STATE , i_state , alph , 0 , NULL , 0 , NON_MOTIF_INDEX , NON_MOTIF_POSITION , nmotifs , 0 , 0 , transp_freq , spacer_states , num_spacers , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; if ( fim ) { convert_to_fims ( * the_hmm ) ; } build_transition_matrix ( * the_hmm ) ; }
Build a completely connected HMM.
Build a completely connected HMM .
// Total length of the motifs. // Total number of spacer states. // Total number of states in the model. // Index of the current "from" motif. // Index of the current "to" motif. // Index within the current motif or spacer. // Index of the current state. // get the alphabet from the first motif // Count the width of the motifs. // Count the spacer states adjacent to begin and end. // Add the spacer states between motifs. // Total states = motifs + spacer_states + begin/end // Allocate the model. // Record that this is a completely connected model. // Record the number of motifs in the model. // Record the number of states in the model. // Put the background distribution into the model. // Build the begin state. // expected length // Emissions. // Number of sites. // previous motif // next motif // Build the spacer states. No transitions from the end state. // No transitions to the start state. // No transitions from start to end. // Allow multi-state spacers. // Build the motif states. // Expected spacer length.  // Previous motif index. // Next motif index. // Build the end state. // Expected spacer length. // Emissions // Number of sites. // Previous motif index. // Next motif index. // Convert spacers to FIMs if requested. // Fill in the transition matrix.