From 7b69badc405e4a3c7572c1428da27be891b306d4 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Mon, 11 Nov 2013 13:30:12 -0500 Subject: index to kmerfunction --- kmer_utils.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'kmer_utils.c') diff --git a/kmer_utils.c b/kmer_utils.c index 0f5f6d6..2a739d6 100644 --- a/kmer_utils.c +++ b/kmer_utils.c @@ -18,6 +18,8 @@ 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, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}; +const char reverse_alpha[4] = { 'A', 'C', 'G', 'T' }; + inline unsigned long long pow_four(unsigned long long x) { return (unsigned long long)1 << (x * 2); } @@ -46,6 +48,36 @@ inline unsigned long num_to_index(const char *str, const int kmer, const long er return out; } +// convert an index back into a kmer string +char *index_to_kmer(unsigned long long index, long kmer) { + + int num_array[64]; + char *ret = malloc(64); + if(ret == NULL) + exit(EXIT_FAILURE); + + int i = 0; + int j = 0; + + while (index != 0) { + num_array[i] = index % 4; + index /= 4; + i++; + } + + ret[i] = '\0'; + i--; + + for(j = 0; j < (kmer - i -1); j++) + ret[j] = 'A'; + + for(; i>=0; i--) { + ret[i+j] = reverse_alpha[num_array[i]]; + } + + return ret; +} + // Strip out any character 'c' from char array 's' into a destination dest (you // need to allocate that) and copy only len characters. char *strnstrip(const char *s, char *dest, int c, unsigned long long len) { -- cgit v1.2.3