From 719c45d9ea49f33f087db899737ad5af4fc4414f Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Tue, 18 Feb 2014 12:42:11 -0500 Subject: working sparse implementation --- kmer_utils.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'kmer_utils.c') diff --git a/kmer_utils.c b/kmer_utils.c index 060e311..f2057ee 100644 --- a/kmer_utils.c +++ b/kmer_utils.c @@ -4,6 +4,7 @@ #include #include +#include "sparse.h" const unsigned char alpha[256] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, @@ -156,8 +157,8 @@ size_t strnstrip(char *s, int c, size_t len) { } -unsigned long long * get_kmer_counts_from_file(FILE *fh, const unsigned int kmer) { - +node * get_kmer_counts_from_file(FILE *fh, const unsigned int kmer) { + char *line = NULL; size_t len = 0; ssize_t read; @@ -166,15 +167,11 @@ unsigned long long * get_kmer_counts_from_file(FILE *fh, const unsigned int kmer long long position = 0; // width is 4^kmer - // there's a sneaky bitshift to avoid pow dependency - const unsigned long width = pow_four(kmer); + const unsigned long long width = pow_four(kmer); + + node *root = NULL; + - // malloc our return array - unsigned long long * counts = calloc((width+ 1), sizeof(unsigned long long)); - if(counts == NULL) { - fprintf(stderr, strerror(errno)); - exit(EXIT_FAILURE); - } while ((read = getdelim(&line, &len, '>', fh)) != -1) { size_t k; @@ -219,15 +216,24 @@ unsigned long long * get_kmer_counts_from_file(FILE *fh, const unsigned int kmer } // use this point to get mer of our loop next: - // bump up the mer value in the counts array - counts[mer]++; + { + // bump up the mer value in the counts array + node *tmp = search(&root, mer); + if(tmp) { + tmp->value++; + } + else { + insert(&root, mer); + } + } } } free(line); fclose(fh); - return counts; + return root; + } unsigned long long * get_kmer_counts_from_filename(const char *fn, const unsigned int kmer) { -- cgit v1.2.3