deps/kb_meme/meme-5.0.1/src/dreme-sax.c
/*****************************************************************************
 * Reads frequency attributes into the pre-allocated freqs array.
 ****************************************************************************/
Reads frequency attributes into the pre-allocated freqs array.
Reads frequency attributes into the pre - allocated freqs array .
static void parse_freq_attrs(PS_T *ps, const char* tag, const xmlChar **attrs) {
  int i, ncore, seen, *idx;
  char *end_ptr;
  double value, sum;
  RBNODE_T *node;
  bool seen_bad;
  ncore = rbtree_size(ps->alph_ids);
  if (ps->freqs == NULL) ps->freqs = mm_malloc(sizeof(double) * ncore);
  for (i = 0; i < ncore; i++) ps->freqs[i] = -1;
  seen = 0;
  seen_bad = false;
  sum = 0.0;
  for (i = 0; attrs[i] != NULL; i += 2) {
    idx = (int*)rbtree_get(ps->alph_ids, attrs[i]);
    if (idx != NULL) {
      assert(*idx < ncore);
      if (ps->freqs[*idx] != -1) {
        dreme_attr_parse_error(ps, PARSE_ATTR_DUPLICATE, tag, (const char*)attrs[i], NULL);
        continue;
      }
      seen++;
      errno = 0; 
      value = strtod((const char*)attrs[i+1], &end_ptr);
      if (end_ptr == (const char*)attrs[i+1] || (errno && errno != ERANGE) || value < 0 || value > 1) {
        dreme_attr_parse_error(ps, PARSE_ATTR_BAD_VALUE, tag, (const char*)attrs[i], (const char*)attrs[i+1]);
        ps->freqs[*idx] = 0; 
        seen_bad = true;
        continue;
      }
      ps->freqs[*idx] = value;
      sum += value;
    }
  }
  if (seen < ncore) {
    for (node = rbtree_first(ps->alph_ids); node != NULL; node = rbtree_next(node)) {
      idx = (int*)rbtree_value(node);
      if (ps->freqs[*idx] == -1) {
        dreme_attr_parse_error(ps, PARSE_ATTR_MISSING, tag, (char*)rbtree_key(node), NULL);
      }
    }
  } else if (!seen_bad) {
    double delta = sum - 1;
    delta = (delta < 0 ? -delta : delta);
    if (delta > (0.001 * ncore)) {
      error(ps, "Probabilities of %s do not sum to 1, got %g .\n", tag, sum);
    }
  }
}
static void parse_freq_attrs ( PS_T * ps , const char * tag , const xmlChar * * attrs ) { int i , ncore , seen , * idx ; char * end_ptr ; double value , sum ; RBNODE_T * node ; bool seen_bad ; ncore = rbtree_size ( ps -> alph_ids ) ; if ( ps -> freqs == NULL ) ps -> freqs = mm_malloc ( sizeof ( double ) * ncore ) ; for ( i = 0 ; i < ncore ; i ++ ) ps -> freqs [ i ] = -1 ; seen = 0 ; seen_bad = false ; sum = 0.0 ; for ( i = 0 ; attrs [ i ] != NULL ; i += 2 ) { idx = ( int * ) rbtree_get ( ps -> alph_ids , attrs [ i ] ) ; if ( idx != NULL ) { assert ( * idx < ncore ) ; if ( ps -> freqs [ * idx ] != -1 ) { dreme_attr_parse_error ( ps , PARSE_ATTR_DUPLICATE , tag , ( const char * ) attrs [ i ] , NULL ) ; continue ; } seen ++ ; errno = 0 ; value = strtod ( ( const char * ) attrs [ i + 1 ] , & end_ptr ) ; if ( end_ptr == ( const char * ) attrs [ i + 1 ] || ( errno && errno != ERANGE ) || value < 0 || value > 1 ) { dreme_attr_parse_error ( ps , PARSE_ATTR_BAD_VALUE , tag , ( const char * ) attrs [ i ] , ( const char * ) attrs [ i + 1 ] ) ; ps -> freqs [ * idx ] = 0 ; seen_bad = true ; continue ; } ps -> freqs [ * idx ] = value ; sum += value ; } } if ( seen < ncore ) { for ( node = rbtree_first ( ps -> alph_ids ) ; node != NULL ; node = rbtree_next ( node ) ) { idx = ( int * ) rbtree_value ( node ) ; if ( ps -> freqs [ * idx ] == -1 ) { dreme_attr_parse_error ( ps , PARSE_ATTR_MISSING , tag , ( char * ) rbtree_key ( node ) , NULL ) ; } } } else if ( ! seen_bad ) { double delta = sum - 1 ; delta = ( delta < 0 ? - delta : delta ) ; if ( delta > ( 0.001 * ncore ) ) { error ( ps , " \n " , tag , sum ) ; } } }
Reads frequency attributes into the pre-allocated freqs array.
Reads frequency attributes into the pre - allocated freqs array .
// initilize the freqs array // reset freqs array; // iterate over attributes // reset because we're about to check it // allow out of range values, mainly because freqs can be very close to zero // mark frequence as seen, even though it's bad // check we got everthing // identify what we're missing // check the frequencies sum to 1 // dreme writes background probabilities to 3 decimal places so assuming  // the error on each is at maximum 0.001 then the total error for the  // sum must be less than or equal to 0.004