deps/kb_meme/meme-5.0.1/src/build-hmm.c
/*************************************************************************
 * Set up one state in a linear HMM, given the appropriate data.
 *************************************************************************/
Set up one state in a linear HMM, given the appropriate data.
Set up one state in a linear HMM given the appropriate data .
static void build_linear_state
  (ALPH_T*  alph,           
   STATE_T  state_type,     
   int      i_state,        
   int      expected_length,
   ARRAY_T* freqs,          
   double   num_sites,      
   int      i_motif,        
   int      i_position,     
   MOTIF_T*  motif,         
   MHMM_STATE_T * a_state)  
{
  if (verbosity >= NORMAL_VERBOSE) {
    switch (state_type) {
    case START_STATE :
      fprintf(stderr, "Building HMM: 0 ");
      break;
    case SPACER_STATE :
    case END_MOTIF_STATE :
      fprintf(stderr, "%d ", i_state);
      break;
    case START_MOTIF_STATE :
    case MID_MOTIF_STATE :
      fprintf(stderr, "%d-", i_state);
      break;
    case END_STATE :
      fprintf(stderr, "%d\n", i_state);
      break;
    case INVALID_STATE :
      die("Invalid state.\n");
    }
  }
  a_state->type = state_type;
  if (state_type == START_MOTIF_STATE ||
      state_type == MID_MOTIF_STATE ||
      state_type == END_MOTIF_STATE) {
    a_state->w_motif = get_motif_length(motif);
  } else {
    a_state->w_motif = 1;
  }
  a_state->emit = allocate_array(alph_size_full(alph));
  a_state->emit_odds = allocate_array(alph_size_full(alph));
  if (state_type == START_STATE || state_type == END_STATE) { 
    int i_alph;
    for (i_alph = 0; i_alph < alph_size_full(alph); i_alph++) {
      set_array_item(i_alph, 1.0, a_state->emit);
    }
  }
  else {
    copy_array(freqs, a_state->emit);
  }
  a_state->num_sites = num_sites;
  a_state->i_motif = i_motif;
  if ((state_type == START_STATE) ||
      (state_type == END_STATE) ||
      (state_type == SPACER_STATE)) {
    strcpy(a_state->motif_id, NON_MOTIF_ID);
    a_state->id_char = NON_MOTIF_ID_CHAR;
  } else { 
    strcpy(a_state->motif_id, get_full_motif_id(motif));
    a_state->id_char = get_motif_id_char(i_position, motif);
  }
  a_state->i_position = i_position;
  switch (state_type) {
  case START_STATE :
    a_state->ntrans_in = 0;
    a_state->itrans_in = NULL;
    a_state->trans_in = NULL;
    break;
  case START_MOTIF_STATE :
  case END_STATE :
    a_state->ntrans_in = 2;
    a_state->itrans_in = (int *)mm_malloc(sizeof(int) * 2);
    a_state->itrans_in[0] = i_state - 2;
    a_state->itrans_in[1] = i_state - 1;
    a_state->trans_in = allocate_array(2);
    set_array_item(0, 1.0 - self_trans(expected_length), a_state->trans_in);
    set_array_item(1, 1.0 - self_trans(expected_length), a_state->trans_in);
    break;
  case MID_MOTIF_STATE :
  case END_MOTIF_STATE :
    a_state->ntrans_in = 1;
    a_state->itrans_in = (int *)mm_malloc(sizeof(int));
    a_state->itrans_in[0] = i_state - 1;
    a_state->trans_in = allocate_array(1);
    set_array_item(0, 1.0, a_state->trans_in);
    break;
  case SPACER_STATE :
    a_state->ntrans_in = 2;
    a_state->itrans_in = (int *)mm_malloc(sizeof(int) * 2);
    a_state->itrans_in[0] = i_state - 1;
    a_state->itrans_in[1] = i_state;
    a_state->trans_in = allocate_array(2);
    set_array_item(0, 1.0 - self_trans(expected_length), a_state->trans_in);
    set_array_item(1, self_trans(expected_length), a_state->trans_in);
    break;
  default:
    die("Invalid state type.\n");
  }
  switch (state_type) {
  case START_STATE :
  case END_MOTIF_STATE :
    a_state->ntrans_out = 2;
    a_state->itrans_out = (int *)mm_malloc(sizeof(int) * 2);
    a_state->itrans_out[0] = i_state + 1;
    a_state->itrans_out[1] = i_state + 2;
    a_state->trans_out = allocate_array(2);
    set_array_item(0, self_trans(expected_length), a_state->trans_out);
    set_array_item(1, 1.0 - self_trans(expected_length), a_state->trans_out);
    break;
  case START_MOTIF_STATE :
  case MID_MOTIF_STATE :
    a_state->ntrans_out = 1;
    a_state->itrans_out = (int *)mm_malloc(sizeof(int));
    a_state->itrans_out[0] = i_state + 1;
    a_state->trans_out = allocate_array(1);
    set_array_item(0, 1.0, a_state->trans_out);
    break;
  case SPACER_STATE :
    a_state->ntrans_out = 2;
    a_state->itrans_out = (int *)mm_malloc(sizeof(int) * 2);
    a_state->itrans_out[0] = i_state;
    a_state->itrans_out[1] = i_state + 1;
    a_state->trans_out = allocate_array(2);
    set_array_item(0, self_trans(expected_length), a_state->trans_out);
    set_array_item(1, 1.0 - self_trans(expected_length), a_state->trans_out);
    break;
  case END_STATE :
    a_state->ntrans_out = 0;
    a_state->itrans_out = NULL;
    a_state->trans_out = NULL;
    break;
  default:
    die("Invalid state type.\n");
  }
}
static void build_linear_state ( ALPH_T * alph , STATE_T state_type , int i_state , int expected_length , ARRAY_T * freqs , double num_sites , int i_motif , int i_position , MOTIF_T * motif , MHMM_STATE_T * a_state ) { if ( verbosity >= NORMAL_VERBOSE ) { switch ( state_type ) { case START_STATE : fprintf ( stderr , " " ) ; break ; case SPACER_STATE : case END_MOTIF_STATE : fprintf ( stderr , " " , i_state ) ; break ; case START_MOTIF_STATE : case MID_MOTIF_STATE : fprintf ( stderr , " " , i_state ) ; break ; case END_STATE : fprintf ( stderr , " \n " , i_state ) ; break ; case INVALID_STATE : die ( " \n " ) ; } } a_state -> type = state_type ; if ( state_type == START_MOTIF_STATE || state_type == MID_MOTIF_STATE || state_type == END_MOTIF_STATE ) { a_state -> w_motif = get_motif_length ( motif ) ; } else { a_state -> w_motif = 1 ; } a_state -> emit = allocate_array ( alph_size_full ( alph ) ) ; a_state -> emit_odds = allocate_array ( alph_size_full ( alph ) ) ; if ( state_type == START_STATE || state_type == END_STATE ) { int i_alph ; for ( i_alph = 0 ; i_alph < alph_size_full ( alph ) ; i_alph ++ ) { set_array_item ( i_alph , 1.0 , a_state -> emit ) ; } } else { copy_array ( freqs , a_state -> emit ) ; } a_state -> num_sites = num_sites ; a_state -> i_motif = i_motif ; if ( ( state_type == START_STATE ) || ( state_type == END_STATE ) || ( state_type == SPACER_STATE ) ) { strcpy ( a_state -> motif_id , NON_MOTIF_ID ) ; a_state -> id_char = NON_MOTIF_ID_CHAR ; } else { strcpy ( a_state -> motif_id , get_full_motif_id ( motif ) ) ; a_state -> id_char = get_motif_id_char ( i_position , motif ) ; } a_state -> i_position = i_position ; switch ( state_type ) { case START_STATE : a_state -> ntrans_in = 0 ; a_state -> itrans_in = NULL ; a_state -> trans_in = NULL ; break ; case START_MOTIF_STATE : case END_STATE : a_state -> ntrans_in = 2 ; a_state -> itrans_in = ( int * ) mm_malloc ( sizeof ( int ) * 2 ) ; a_state -> itrans_in [ 0 ] = i_state - 2 ; a_state -> itrans_in [ 1 ] = i_state - 1 ; a_state -> trans_in = allocate_array ( 2 ) ; set_array_item ( 0 , 1.0 - self_trans ( expected_length ) , a_state -> trans_in ) ; set_array_item ( 1 , 1.0 - self_trans ( expected_length ) , a_state -> trans_in ) ; break ; case MID_MOTIF_STATE : case END_MOTIF_STATE : a_state -> ntrans_in = 1 ; a_state -> itrans_in = ( int * ) mm_malloc ( sizeof ( int ) ) ; a_state -> itrans_in [ 0 ] = i_state - 1 ; a_state -> trans_in = allocate_array ( 1 ) ; set_array_item ( 0 , 1.0 , a_state -> trans_in ) ; break ; case SPACER_STATE : a_state -> ntrans_in = 2 ; a_state -> itrans_in = ( int * ) mm_malloc ( sizeof ( int ) * 2 ) ; a_state -> itrans_in [ 0 ] = i_state - 1 ; a_state -> itrans_in [ 1 ] = i_state ; a_state -> trans_in = allocate_array ( 2 ) ; set_array_item ( 0 , 1.0 - self_trans ( expected_length ) , a_state -> trans_in ) ; set_array_item ( 1 , self_trans ( expected_length ) , a_state -> trans_in ) ; break ; default : die ( " \n " ) ; } switch ( state_type ) { case START_STATE : case END_MOTIF_STATE : a_state -> ntrans_out = 2 ; a_state -> itrans_out = ( int * ) mm_malloc ( sizeof ( int ) * 2 ) ; a_state -> itrans_out [ 0 ] = i_state + 1 ; a_state -> itrans_out [ 1 ] = i_state + 2 ; a_state -> trans_out = allocate_array ( 2 ) ; set_array_item ( 0 , self_trans ( expected_length ) , a_state -> trans_out ) ; set_array_item ( 1 , 1.0 - self_trans ( expected_length ) , a_state -> trans_out ) ; break ; case START_MOTIF_STATE : case MID_MOTIF_STATE : a_state -> ntrans_out = 1 ; a_state -> itrans_out = ( int * ) mm_malloc ( sizeof ( int ) ) ; a_state -> itrans_out [ 0 ] = i_state + 1 ; a_state -> trans_out = allocate_array ( 1 ) ; set_array_item ( 0 , 1.0 , a_state -> trans_out ) ; break ; case SPACER_STATE : a_state -> ntrans_out = 2 ; a_state -> itrans_out = ( int * ) mm_malloc ( sizeof ( int ) * 2 ) ; a_state -> itrans_out [ 0 ] = i_state ; a_state -> itrans_out [ 1 ] = i_state + 1 ; a_state -> trans_out = allocate_array ( 2 ) ; set_array_item ( 0 , self_trans ( expected_length ) , a_state -> trans_out ) ; set_array_item ( 1 , 1.0 - self_trans ( expected_length ) , a_state -> trans_out ) ; break ; case END_STATE : a_state -> ntrans_out = 0 ; a_state -> itrans_out = NULL ; a_state -> trans_out = NULL ; break ; default : die ( " \n " ) ; } }
Set up one state in a linear HMM, given the appropriate data.
Set up one state in a linear HMM given the appropriate data .
// alphabet // Type of state (START, SPACER,...) // The state index. // For spacers, expected length of output. // Emission probability distrib. // Number of sites for this emission. // Index of motif this state is in. // Position of this state within a motif or spacer. // Motif. // State to be filled in (pre-allocated). /* Record what type of state this is. */ // Record the motif width if this is a motif. /* Set up the emission distribution and a few other tidbits. */ /* Start and end don't have emissions. */ /* Record the motif index and ID. */ // a motif state /* First set up the transitions into this state. */ /* Then set up the transitions out of this state. */