/************************************************************************* * MEME and DREME motifs can have very small E-values which are impossible * to represent using a double. Internally we represent such small evalues * as log evalues. When outputting we have to convert those log values * back into a string representation taking into account the possiblity * that the user specified an e-value of zero (which we actually recommended * in documentation for quite a while). * * This function writes the E-value to an expandable string buffer in scientific * notation. The buffer, expanded to fit the number if required, is returned. *************************************************************************/ char* log10_evalue_to_string2(double log10_ev, int prec, char **expandable_buffer, int *buffer_size, int offset) { char *expanded_buffer, *out; int extra; assert(*buffer_size >= 0); assert(offset >= 0); extra = 0; out = log10_evalue_to_string(log10_ev, prec, (*expandable_buffer)+offset, *buffer_size - offset, &extra); if (extra) { expanded_buffer = realloc(*expandable_buffer, sizeof(char) * (*buffer_size + extra)); if (expanded_buffer) { *expandable_buffer = expanded_buffer; *buffer_size += extra; } else { //memory allocation failure fprintf(stderr, "FATAL: log10_evalue_to_string2 - realloc failed to " "expand buffer by %d bytes.\n", extra); exit(1); } out = log10_evalue_to_string(log10_ev, prec, (*expandable_buffer)+offset, *buffer_size - offset, &extra); } assert(extra == 0); return out; }
/************************************************************************* * MEME and DREME motifs can have very small E-values which are impossible * to represent using a double. Internally we represent such small evalues * as log evalues. When outputting we have to convert those log values * back into a string representation taking into account the possiblity * that the user specified an e-value of zero (which we actually recommended * in documentation for quite a while). * * This function writes the E-value to an expandable string buffer in scientific * notation. The buffer, expanded to fit the number if required, is returned. *************************************************************************/ char* log10_evalue_to_string2(double log10_ev, int prec, char **expandable_buffer, int *buffer_size, int offset) { char *buffer, *expanded_buffer; int space, written, needed; assert(*buffer_size >= 0); assert(offset >= 0); // calculate the size of the buffer buffer = (*expandable_buffer)+offset; space = *buffer_size - offset; // attempt to write the E-value written = log10_evalue_to_string(log10_ev, prec, buffer, space); // check for success if (written >= space) { needed = *buffer_size + (written - space) + 1; expanded_buffer = realloc(*expandable_buffer, sizeof(char) * needed); if (expanded_buffer) { *expandable_buffer = expanded_buffer; *buffer_size = needed; } else { //memory allocation failure fprintf(stderr, "FATAL: log10_evalue_to_string2 - realloc failed to " "expand buffer by %d bytes.\n", (written - space) + 1); exit(1); } // calculate the new size of the buffer buffer = (*expandable_buffer)+offset; space = *buffer_size - offset; written = log10_evalue_to_string(log10_ev, prec, buffer, space); } return buffer; }