deps/kb_meme/meme-5.0.1/src/momo-motifx.c
/**
 * Recursive function. Creates and stores a motif using the motif-x
 * algorithm until no more are left.
 */
Recursive function. Creates and stores a motif using the motif-x
algorithm until no more are left.
Recursive function . Creates and stores a motif using the motif - x algorithm until no more are left .
void create_motifx_motif(ARRAYLST_T* phospho_seqs,
                         ARRAYLST_T* bg_seqs,
                         MOTIFX_STATUS_T** phospho_status,
                         MOTIFX_STATUS_T** bg_status,
                         MATRIX_T* phospho_count,
                         MATRIX_T* bg_count,
                         int* num_active,
                         int* num_bg_active,
                         char* modname,
                         MOD_INFO_T* mod_info,
                         MOMO_OPTIONS_T* options,
                         SUMMARY_T* summary) {
  int i;
  int j;
  const char* alph_letters = summary->alph_letters;
  char* pattern = mm_malloc(options->width + 1);
  for (i = 0; i < options->width; ++i) {
    pattern[i] = 'X';
  }
  pattern[options->width] = '\0';
  int* num_active_copy = mm_malloc(sizeof(int));
  *num_active_copy = *num_active;
  int* num_bg_active_copy = mm_malloc(sizeof(int));
  *num_bg_active_copy = *num_bg_active;
  double* motif_score = mm_malloc(sizeof(double));
  *motif_score = 0;
  MATRIX_T* result_count_matrix = add_to_pattern(pattern, phospho_seqs, bg_seqs, phospho_status, bg_status, num_active_copy, num_bg_active_copy, phospho_count, bg_count, motif_score, summary, options);
  bool found_pattern = false;
  if (*num_bg_active > 0 && *num_active > 0) {
    for (i = 0; i < options->width; ++i) {
      if (pattern[i] != 'X') {
	found_pattern = true;
      }
    }
  }
  if (found_pattern) {
    for (i = 0; i < options->width; i++) {
      for (j = 0; j < strlen(alph_letters); j++) {
        if ((int) get_matrix_cell(i, j, result_count_matrix) == *num_active_copy) {
          pattern[i] = alph_letters[j];
        }
      }
    }
    char* pattern_name = mm_malloc(strlen(pattern) + strlen(modname) + 3);
    pattern_name[0] = '\0';
    strncat(pattern_name, pattern, strlen(pattern)/2);
    strncat(pattern_name, "_", 1);
    strncat(pattern_name, modname, strlen(modname));
    strncat(pattern_name, "_", 1);
    strncat(pattern_name, pattern + strlen(pattern)/2 + 1, strlen(pattern)/2);
    normalize_rows(0.0, result_count_matrix);
    MOTIF_INFO_T* motifinfo = mm_malloc(sizeof(MOTIF_INFO_T));
    MOTIF_T* motif = allocate_motif(pattern_name, "", summary->alph, result_count_matrix, NULL);
    set_motif_nsites(motif, *num_active_copy);
    motifinfo->motif = motif;
    motifinfo->seqs = arraylst_create();
    motifinfo->score = *motif_score;
    motifinfo->fg_match = *num_active_copy;
    motifinfo->fg_size = *num_active;
    motifinfo->bg_match = *num_bg_active_copy;
    motifinfo->bg_size = *num_bg_active;
    for (i = 0; i < arraylst_size(phospho_seqs); ++i) {
      MOTIFX_STATUS_T status = (*phospho_status)[i];
      if (status == ACTIVE) {
        SEQ_T* active_sequence = (options->eliminate_repeat_width) ? hash_get_entry_value(arraylst_get(i, phospho_seqs)) : arraylst_get(i, phospho_seqs);
        arraylst_add(get_raw_sequence(active_sequence), motifinfo->seqs);
      }
    }
    arraylst_add(motifinfo, mod_info->motifinfos);
    delete_sequences(phospho_status, arraylst_size(phospho_seqs));
    delete_sequences(bg_status, arraylst_size(bg_seqs));
    *num_active = *num_active - *num_active_copy;
    *num_bg_active = *num_bg_active - *num_bg_active_copy;
    phospho_count = get_count_matrix(phospho_count, phospho_seqs, phospho_status, options, summary);
    bg_count = get_count_matrix(bg_count, bg_seqs, bg_status, options, summary);
    myfree(pattern);
    myfree(num_active_copy);
    myfree(num_bg_active_copy);
    myfree(motif_score);
    myfree(pattern_name);
    create_motifx_motif(phospho_seqs,
                        bg_seqs,
                        phospho_status,
                        bg_status,
                        phospho_count,
                        bg_count,
                        num_active,
                        num_bg_active,
                        modname,
                        mod_info,
                        options,
                        summary);
  }
  myfree(pattern);
  myfree(num_active_copy);
  myfree(num_bg_active_copy);
  myfree(motif_score);
}
void create_motifx_motif ( ARRAYLST_T * phospho_seqs , ARRAYLST_T * bg_seqs , MOTIFX_STATUS_T * * phospho_status , MOTIFX_STATUS_T * * bg_status , MATRIX_T * phospho_count , MATRIX_T * bg_count , int * num_active , int * num_bg_active , char * modname , MOD_INFO_T * mod_info , MOMO_OPTIONS_T * options , SUMMARY_T * summary ) { int i ; int j ; const char * alph_letters = summary -> alph_letters ; char * pattern = mm_malloc ( options -> width + 1 ) ; for ( i = 0 ; i < options -> width ; ++ i ) { pattern [ i ] = ' ' ; } pattern [ options -> width ] = ' \0 ' ; int * num_active_copy = mm_malloc ( sizeof ( int ) ) ; * num_active_copy = * num_active ; int * num_bg_active_copy = mm_malloc ( sizeof ( int ) ) ; * num_bg_active_copy = * num_bg_active ; double * motif_score = mm_malloc ( sizeof ( double ) ) ; * motif_score = 0 ; MATRIX_T * result_count_matrix = add_to_pattern ( pattern , phospho_seqs , bg_seqs , phospho_status , bg_status , num_active_copy , num_bg_active_copy , phospho_count , bg_count , motif_score , summary , options ) ; bool found_pattern = false ; if ( * num_bg_active > 0 && * num_active > 0 ) { for ( i = 0 ; i < options -> width ; ++ i ) { if ( pattern [ i ] != ' ' ) { found_pattern = true ; } } } if ( found_pattern ) { for ( i = 0 ; i < options -> width ; i ++ ) { for ( j = 0 ; j < strlen ( alph_letters ) ; j ++ ) { if ( ( int ) get_matrix_cell ( i , j , result_count_matrix ) == * num_active_copy ) { pattern [ i ] = alph_letters [ j ] ; } } } char * pattern_name = mm_malloc ( strlen ( pattern ) + strlen ( modname ) + 3 ) ; pattern_name [ 0 ] = ' \0 ' ; strncat ( pattern_name , pattern , strlen ( pattern ) / 2 ) ; strncat ( pattern_name , " " , 1 ) ; strncat ( pattern_name , modname , strlen ( modname ) ) ; strncat ( pattern_name , " " , 1 ) ; strncat ( pattern_name , pattern + strlen ( pattern ) / 2 + 1 , strlen ( pattern ) / 2 ) ; normalize_rows ( 0.0 , result_count_matrix ) ; MOTIF_INFO_T * motifinfo = mm_malloc ( sizeof ( MOTIF_INFO_T ) ) ; MOTIF_T * motif = allocate_motif ( pattern_name , " " , summary -> alph , result_count_matrix , NULL ) ; set_motif_nsites ( motif , * num_active_copy ) ; motifinfo -> motif = motif ; motifinfo -> seqs = arraylst_create ( ) ; motifinfo -> score = * motif_score ; motifinfo -> fg_match = * num_active_copy ; motifinfo -> fg_size = * num_active ; motifinfo -> bg_match = * num_bg_active_copy ; motifinfo -> bg_size = * num_bg_active ; for ( i = 0 ; i < arraylst_size ( phospho_seqs ) ; ++ i ) { MOTIFX_STATUS_T status = ( * phospho_status ) [ i ] ; if ( status == ACTIVE ) { SEQ_T * active_sequence = ( options -> eliminate_repeat_width ) ? hash_get_entry_value ( arraylst_get ( i , phospho_seqs ) ) : arraylst_get ( i , phospho_seqs ) ; arraylst_add ( get_raw_sequence ( active_sequence ) , motifinfo -> seqs ) ; } } arraylst_add ( motifinfo , mod_info -> motifinfos ) ; delete_sequences ( phospho_status , arraylst_size ( phospho_seqs ) ) ; delete_sequences ( bg_status , arraylst_size ( bg_seqs ) ) ; * num_active = * num_active - * num_active_copy ; * num_bg_active = * num_bg_active - * num_bg_active_copy ; phospho_count = get_count_matrix ( phospho_count , phospho_seqs , phospho_status , options , summary ) ; bg_count = get_count_matrix ( bg_count , bg_seqs , bg_status , options , summary ) ; myfree ( pattern ) ; myfree ( num_active_copy ) ; myfree ( num_bg_active_copy ) ; myfree ( motif_score ) ; myfree ( pattern_name ) ; create_motifx_motif ( phospho_seqs , bg_seqs , phospho_status , bg_status , phospho_count , bg_count , num_active , num_bg_active , modname , mod_info , options , summary ) ; } myfree ( pattern ) ; myfree ( num_active_copy ) ; myfree ( num_bg_active_copy ) ; myfree ( motif_score ) ; }
Recursive function.
Recursive function .
// Initialize pattern, sequence count, bg sequence count, and overall score for this motif. // Set the pattern, num active copy, num bg active copy, motif score, and get a count of the sequences // Quit if there are no more foreground or background sequences. // If any of the characters are not X, then we have found a pattern // If there is a pattern, store the pattern and call create_motifx_motif again. // fill out the rest of the pattern (e.g. if you have pattern ..ASAAA, and realize the actual pattern is A.ASAAA // create the pattern name // convert this count matrix into frequencies // Store this motif // delete the sequences from this motif. turn inactive into active. // update the count of number of actives // recalculate phospho count and bg count. // free up space // try to create another motif. // free up space