deps/kb_meme/meme-5.0.1/src/build-hmm.c
/*************************************************************************
 * Build a star topology HMM.
 *************************************************************************/
Build a star topology HMM.
Build a star topology HMM .
void build_star_hmm
  (ARRAY_T*  background,
   int       spacer_states, 
   MOTIF_T*  motifs,
   int       nmotifs,
   bool fim,
   MHMM_T**  the_hmm)
{
  ALPH_T*   alph;
  int       motif_states; 
  int       num_spacers;  
  int       num_states;   
  int       i_motif;      
  int       i_position;   
  int       i_state = 0;  
  alph = get_motif_alph(motif_at(motifs, 0));
  for (motif_states = 0, i_motif = 0; i_motif < nmotifs; i_motif++)
    motif_states += get_motif_length(motif_at(motifs, i_motif));
  num_spacers = 1;
  num_states = motif_states + (num_spacers * spacer_states) + 2;
  *the_hmm = allocate_mhmm(alph, num_states);
  (*the_hmm)->type = STAR_HMM;
  (*the_hmm)->num_motifs = nmotifs;
  (*the_hmm)->num_states = num_states;
  (*the_hmm)->num_spacers = 1;
  (*the_hmm)->spacer_states = spacer_states;
  copy_array(background, (*the_hmm)->background);
  build_star_state(
    alph,
    START_STATE,
		i_state,
		0, 
		NULL,
		0, 
		NON_MOTIF_INDEX,
		NON_MOTIF_POSITION,
		nmotifs,
		spacer_states,
		motifs,
		&((*the_hmm)->states[i_state])
  );
  i_state++;
  for (i_position = 0; i_position < spacer_states; i_position++) {
    build_star_state(
      alph,
      SPACER_STATE, 
		  i_state, 
		  DEFAULT_SPACER_LENGTH,
		  background,
		  SPACER_NUMSITES,
		  NON_MOTIF_INDEX,
		  i_position,
		  nmotifs,
		  spacer_states,
		  motifs,
		  &((*the_hmm)->states[i_state])
    );
    i_state++;
  }
  for (i_motif = 0; i_motif < nmotifs; i_motif++) {
    MOTIF_T *this_motif = motif_at(motifs, i_motif);
    assert(get_motif_length(this_motif) > 1);
    i_position = 0;
    build_star_state(
      alph,
      START_MOTIF_STATE, 
		  i_state,
		  0, 
		  get_matrix_row(i_position, get_motif_freqs(this_motif)),
		  get_motif_nsites(this_motif),
		  i_motif,
		  i_position,
		  nmotifs,
		  spacer_states,
		  motifs,
		  &((*the_hmm)->states[i_state])
    );
    i_state++;
    for (i_position = 1; i_position < get_motif_length(this_motif) - 1; i_position++) {
      build_star_state(
        alph,
        MID_MOTIF_STATE, 
		    i_state,
		    0,  
		    get_matrix_row(i_position, get_motif_freqs(this_motif)),
		    get_motif_nsites(this_motif),
		    i_motif,
		    i_position, 
		    nmotifs,
		    spacer_states,
		    motifs,
		    &((*the_hmm)->states[i_state])
      );
      i_state++;
    }
    build_star_state(
      alph,
      END_MOTIF_STATE, 
		  i_state,
		  0, 
		  get_matrix_row(i_position, get_motif_freqs(this_motif)),
		  get_motif_nsites(this_motif),
		  i_motif,
		  i_position,
		  nmotifs,
		  spacer_states,
		  motifs,
		  &((*the_hmm)->states[i_state])
    );
    i_state++;
  }
  build_star_state(
    alph,
    END_STATE, 
		i_state,
		0, 
		NULL, 
		0, 
		NON_MOTIF_INDEX,
		NON_MOTIF_POSITION,
		nmotifs,
		spacer_states,
		motifs,
		&((*the_hmm)->states[i_state])
  );
  i_state++;
  if (fim) {
    convert_to_fims(*the_hmm);
  }
  build_transition_matrix(*the_hmm);
}
void build_star_hmm ( ARRAY_T * background , int spacer_states , MOTIF_T * motifs , int nmotifs , bool fim , MHMM_T * * the_hmm ) { ALPH_T * alph ; int motif_states ; int num_spacers ; int num_states ; int i_motif ; int i_position ; int i_state = 0 ; alph = get_motif_alph ( motif_at ( motifs , 0 ) ) ; for ( motif_states = 0 , i_motif = 0 ; i_motif < nmotifs ; i_motif ++ ) motif_states += get_motif_length ( motif_at ( motifs , i_motif ) ) ; num_spacers = 1 ; num_states = motif_states + ( num_spacers * spacer_states ) + 2 ; * the_hmm = allocate_mhmm ( alph , num_states ) ; ( * the_hmm ) -> type = STAR_HMM ; ( * the_hmm ) -> num_motifs = nmotifs ; ( * the_hmm ) -> num_states = num_states ; ( * the_hmm ) -> num_spacers = 1 ; ( * the_hmm ) -> spacer_states = spacer_states ; copy_array ( background , ( * the_hmm ) -> background ) ; build_star_state ( alph , START_STATE , i_state , 0 , NULL , 0 , NON_MOTIF_INDEX , NON_MOTIF_POSITION , nmotifs , spacer_states , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; for ( i_position = 0 ; i_position < spacer_states ; i_position ++ ) { build_star_state ( alph , SPACER_STATE , i_state , DEFAULT_SPACER_LENGTH , background , SPACER_NUMSITES , NON_MOTIF_INDEX , i_position , nmotifs , spacer_states , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; } for ( i_motif = 0 ; i_motif < nmotifs ; i_motif ++ ) { MOTIF_T * this_motif = motif_at ( motifs , i_motif ) ; assert ( get_motif_length ( this_motif ) > 1 ) ; i_position = 0 ; build_star_state ( alph , START_MOTIF_STATE , i_state , 0 , get_matrix_row ( i_position , get_motif_freqs ( this_motif ) ) , get_motif_nsites ( this_motif ) , i_motif , i_position , nmotifs , spacer_states , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; for ( i_position = 1 ; i_position < get_motif_length ( this_motif ) - 1 ; i_position ++ ) { build_star_state ( alph , MID_MOTIF_STATE , i_state , 0 , get_matrix_row ( i_position , get_motif_freqs ( this_motif ) ) , get_motif_nsites ( this_motif ) , i_motif , i_position , nmotifs , spacer_states , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; } build_star_state ( alph , END_MOTIF_STATE , i_state , 0 , get_matrix_row ( i_position , get_motif_freqs ( this_motif ) ) , get_motif_nsites ( this_motif ) , i_motif , i_position , nmotifs , spacer_states , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; } build_star_state ( alph , END_STATE , i_state , 0 , NULL , 0 , NON_MOTIF_INDEX , NON_MOTIF_POSITION , nmotifs , spacer_states , motifs , & ( ( * the_hmm ) -> states [ i_state ] ) ) ; i_state ++ ; if ( fim ) { convert_to_fims ( * the_hmm ) ; } build_transition_matrix ( * the_hmm ) ; }
Build a star topology HMM.
Build a star topology 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 within the current motif or spacer. */ /* Index of the current state. */ /* Count the width of the motifs. */ // Only 1 spacer. /* Total states = motifs + spacer_states + begin/end */ /* fprintf(stderr, "motif_states=%d num_spacers=%d num_states=%d\n",
	  motif_states, num_spacers, num_states); */ /* Allocate the model. */ /* Record that this is a star 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 // Number of sites. // Build the spacer state (state 0).  Allow multi-state spacers. /* Build the motif states. */ // Expected spacer length. // Expected spacer length.  // Expected spacer length. /* Build the end state. */ // Expected spacer length. // Emissions // Number of sites. /* Convert spacers to FIMs if requested. */ /* Fill in the transition matrix. */