deps/kb_meme/meme-5.0.1/src/tomtom.c
/**************************************************************************
 * Computes the optimal offset and score corresponding to the smallest
 * pvalue.
 **************************************************************************/
Computes the optimal offset and score corresponding to the smallest
pvalue.
Computes the optimal offset and score corresponding to the smallest pvalue .
static void compare_motifs (
    MATRIX_T *scores, int query_len, int target_len, int target_offset,
    MATRIX_T *reference_matrix, MATRIX_T *pv_lookup_matrix,
    bool internal, int min_overlap, bool complete_scores,
    double scores_scale, double scores_offset, int *configurations,
    int *optimal_offset, double *optimal_pvalue, int *optimal_overlap) {
  int start, end, mo, configuration;
  start = 1;
  end = query_len + target_len - 1;
  if (internal) {
    if (query_len < target_len) {
      start = query_len; end = target_len;
    } else {
      start = target_len; end = query_len;
    }
  } else if (min_overlap > 1) {
    mo = min_overlap;
    if (query_len < mo) mo = query_len;
    if (target_len < mo) mo = target_len;
    start = mo;
    end = query_len + target_len - mo;
  }
  *configurations = end - start + 1;
  *optimal_pvalue = BIG;
  *optimal_overlap = -1;
  *optimal_offset = 0; 
  for (configuration = start; configuration <= end; configuration++) {
    double pvalue, temp_score;
    int overlap, i_start, i_stop, pv_index;
    temp_score = compute_overlap_score(scores, query_len, target_len,
      target_offset, configuration, &i_start, &i_stop);
    overlap = i_stop - i_start + 1;
    assert(overlap > 0);
    if (complete_scores) {
      pvalue = - rint(temp_score  + overlap * scores_offset * scores_scale);
    } else {
      pv_index = (int)get_matrix_cell(i_start, i_stop, reference_matrix);
      pvalue = get_matrix_cell(pv_index, (int)temp_score, pv_lookup_matrix);
    }
    if (pvalue == *optimal_pvalue) {
      if (*optimal_overlap < overlap) {
        *optimal_offset = configuration - query_len;
        *optimal_pvalue = pvalue;
        *optimal_overlap = overlap;
      }
    } else {
      if (pvalue < *optimal_pvalue) {
        *optimal_offset = configuration - query_len;
        *optimal_pvalue = pvalue;
        *optimal_overlap = overlap;
      }
    }
  }
  if (complete_scores) *optimal_pvalue = -(*optimal_pvalue);
}
static void compare_motifs ( MATRIX_T * scores , int query_len , int target_len , int target_offset , MATRIX_T * reference_matrix , MATRIX_T * pv_lookup_matrix , bool internal , int min_overlap , bool complete_scores , double scores_scale , double scores_offset , int * configurations , int * optimal_offset , double * optimal_pvalue , int * optimal_overlap ) { int start , end , mo , configuration ; start = 1 ; end = query_len + target_len - 1 ; if ( internal ) { if ( query_len < target_len ) { start = query_len ; end = target_len ; } else { start = target_len ; end = query_len ; } } else if ( min_overlap > 1 ) { mo = min_overlap ; if ( query_len < mo ) mo = query_len ; if ( target_len < mo ) mo = target_len ; start = mo ; end = query_len + target_len - mo ; } * configurations = end - start + 1 ; * optimal_pvalue = BIG ; * optimal_overlap = -1 ; * optimal_offset = 0 ; for ( configuration = start ; configuration <= end ; configuration ++ ) { double pvalue , temp_score ; int overlap , i_start , i_stop , pv_index ; temp_score = compute_overlap_score ( scores , query_len , target_len , target_offset , configuration , & i_start , & i_stop ) ; overlap = i_stop - i_start + 1 ; assert ( overlap > 0 ) ; if ( complete_scores ) { pvalue = - rint ( temp_score + overlap * scores_offset * scores_scale ) ; } else { pv_index = ( int ) get_matrix_cell ( i_start , i_stop , reference_matrix ) ; pvalue = get_matrix_cell ( pv_index , ( int ) temp_score , pv_lookup_matrix ) ; } if ( pvalue == * optimal_pvalue ) { if ( * optimal_overlap < overlap ) { * optimal_offset = configuration - query_len ; * optimal_pvalue = pvalue ; * optimal_overlap = overlap ; } } else { if ( pvalue < * optimal_pvalue ) { * optimal_offset = configuration - query_len ; * optimal_pvalue = pvalue ; * optimal_overlap = overlap ; } } } if ( complete_scores ) * optimal_pvalue = - ( * optimal_pvalue ) ; }
Computes the optimal offset and score corresponding to the smallest
 pvalue.
Computes the optimal offset and score corresponding to the smallest pvalue .
// determine acceptable overlaps // motifs must be fully overlapping // Override min-overlap if query/target is smaller than min-overlap // Slide one profile over another // stop compiler complaining // Compute the score for the current configuration // Compute the pvalue of the score // UK: undo the offset when (quantile-) shifted scores are used,  // also pvalue is not really probability in this case // JIJ: I assume the negative is to make it so the smaller scores are the // better ones. This seems to be the case as it is reversed upon return... // I had originally assumed that the // "temp_score + overlap * scores_offset * scores_scale" was meant to // reverse the effect of scaling the matrix to go between 0 and bins but // the formula doesn't seem right for that. I would have expected: // scores_scale * temp_score + overlap * scores_offset // I wonder if it was a mistake? // Another posibility is that this is part of the method used to take into // account non-overlapping columns, as before calculating p-values // in calc_shifted_scores_pvalues_per_query it does: // "temp_score - query_len * scores_offset * scores_scale" // as query_len is always going to be larger than overlap this results // in subracting away "scores_offset * scores_scale" for every // non-overlapping query column. However the odd thing about that theory // is that the full correction isn't applied here. // Keep the configuration with the lowest pvalue (or -score). // If pvalue is the same, keep the largest overlap  // undo the negation trick used to make the best scores the smallest