diff options
author | Calvin Morrison <mutantturkey@gmail.com> | 2014-03-19 14:01:03 -0400 |
---|---|---|
committer | Calvin Morrison <mutantturkey@gmail.com> | 2014-03-19 14:01:03 -0400 |
commit | f0917e5c7d35275f810088b9f51536f36fed6969 (patch) | |
tree | 25befe379e596ab758bb606180c2dbc47967392a /src/c/quikr_functions.c | |
parent | 603a90c1d0d22acaeeac4c8fb6bdad730f4591f3 (diff) |
seperate function for getting a rare value
Diffstat (limited to 'src/c/quikr_functions.c')
-rw-r--r-- | src/c/quikr_functions.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/c/quikr_functions.c b/src/c/quikr_functions.c index 061d3b8..5bbe913 100644 --- a/src/c/quikr_functions.c +++ b/src/c/quikr_functions.c @@ -10,6 +10,7 @@ #include "kmer_utils.h" #include "quikr.h" + void check_malloc(void *ptr, char *error) { if (ptr == NULL) { if(error != NULL) { @@ -20,6 +21,44 @@ void check_malloc(void *ptr, char *error) { exit(EXIT_FAILURE); } } +static int double_cmp (const void * a, const void * b) { + return ( *(double*)a - *(double*)b ); +} + +void get_rare_value(double *count_matrix, unsigned long long width, double rare_percent, unsigned long long *ret_rare_value, unsigned long long *ret_rare_width) { + size_t y, x; + unsigned long long rare_width = 0; + double rare_value = 0; + + // allocate * sort a temporary matrix + double *sorted_count_matrix = malloc(width * sizeof(double)); + check_malloc(sorted_count_matrix, NULL); + + memcpy(sorted_count_matrix, count_matrix, width * sizeof(double)); + + qsort(sorted_count_matrix, width, sizeof(double), double_cmp); + + // get our "rare" counts + for(y = 0; y < width; y++) { + double percentage = 0; + + rare_value = sorted_count_matrix[y]; + rare_width = 0; + for(x = 0; x < width; x++) { + if(count_matrix[x] <= rare_value) { + rare_width++; + } + } + percentage = (double)rare_width / (double)width; + + if(percentage >= rare_percent) + break; + } + + free(sorted_count_matrix); + *ret_rare_width = rare_width; + *ret_rare_value = rare_value; +} void debug_arrays(double *count_matrix, struct matrix *sensing_matrix) { FILE *count_fh = fopen("count.mat", "w"); |