diff options
-rwxr-xr-x | kmer_continuous_count | bin | 0 -> 11952 bytes | |||
-rw-r--r-- | kmer_continuous_count.c | 2 | ||||
-rw-r--r-- | kmer_counts_per_sequence.c | 5 | ||||
-rw-r--r-- | kmer_utils.c | 50 | ||||
-rw-r--r-- | kmer_utils.h | 2 |
5 files changed, 26 insertions, 33 deletions
diff --git a/kmer_continuous_count b/kmer_continuous_count Binary files differnew file mode 100755 index 0000000..c0d050b --- /dev/null +++ b/kmer_continuous_count diff --git a/kmer_continuous_count.c b/kmer_continuous_count.c index 5ace9a5..a7c35f0 100644 --- a/kmer_continuous_count.c +++ b/kmer_continuous_count.c @@ -11,7 +11,7 @@ void help() { - printf("usage: kmer_total_count -i input_file -k kmer [-n] [-l] ...\n\n" + printf("usage: kmer_continuous_count -i input_file -k kmer [-n] [-l] ...\n\n" "count mers in size k from a fasta file, but do so continuously\n" "\n" " --input -i input fasta file to count\n" diff --git a/kmer_counts_per_sequence.c b/kmer_counts_per_sequence.c index d5b1927..7e0e119 100644 --- a/kmer_counts_per_sequence.c +++ b/kmer_counts_per_sequence.c @@ -130,10 +130,7 @@ int main(int argc, char **argv) { if(specific_mers) { desired_indicies = malloc((width) * sizeof(size_t)); - if(desired_indicies == NULL) { - fprintf(stderr, "%s\n", strerror(errno)); - exit(EXIT_FAILURE); - } + check_null_ptr(desired_indicies, NULL); num_desired_indicies = load_specific_mers_from_file(mer_fn, kmer, width, desired_indicies); if(num_desired_indicies == 0) { fprintf(stderr, "Error: no mers loaded from file\n"); diff --git a/kmer_utils.c b/kmer_utils.c index 79c9180..b8d5691 100644 --- a/kmer_utils.c +++ b/kmer_utils.c @@ -23,6 +23,18 @@ unsigned long long pow_four(unsigned long long x) { return (unsigned long long)1 << (x * 2); } +void check_null_ptr(void *ptr, const char *error) { + if (ptr == NULL) { + if(error != NULL) { + fprintf(stderr, "Error: %s - %s\n", error, strerror(errno)); + } + else { + fprintf(stderr, "Error: %s\n", strerror(errno)); + } + exit(EXIT_FAILURE); + } +} + // convert a string of k-mer size base-4 values into a // base-10 index unsigned long num_to_index(const char *str, const int kmer, const long error_pos, long long *current_position) { @@ -51,10 +63,7 @@ unsigned long long load_specific_mers_from_file(char *fn, unsigned int kmer, siz char line[64]; fh = fopen(fn, "r"); - if(fh == NULL) { - fprintf(stderr, "Error opening %s - %s\n", fn, strerror(errno)); - exit(EXIT_FAILURE); - } + check_null_ptr(fh, fn); while (fgets(line, sizeof(line), fh) != NULL) { size_t i; @@ -99,10 +108,8 @@ char *index_to_kmer(unsigned long long index, long kmer) { size_t j = 0; char *num_array = calloc(kmer, sizeof(char)); char *ret = calloc(kmer + 1, sizeof(char)); - if(ret == NULL || num_array == NULL) { - fprintf(stderr, "%s\n", strerror(errno)); - exit(EXIT_FAILURE); - } + check_null_ptr(num_array, NULL); + check_null_ptr(ret, NULL); // this is the core of the conversion. modulus 4 for base 4 conversion @@ -164,25 +171,22 @@ unsigned long long * get_kmer_counts_from_file(FILE *fh, const unsigned int kmer size_t len = 0; ssize_t read; - long long i = 0; - long long position = 0; - // width is 4^kmer // there's a sneaky bitshift to avoid pow dependency const unsigned long width = pow_four(kmer); // malloc our return array unsigned long long * counts = calloc((width+ 1), sizeof(unsigned long long)); - if(counts == NULL) { - fprintf(stderr, "%s\n", strerror(errno)); - exit(EXIT_FAILURE); - } + check_null_ptr(counts, NULL); while ((read = getdelim(&line, &len, '>', fh)) != -1) { size_t k; char *seq; size_t seq_length; + long long i = 0; + long long position = 0; + // find our first \n, this should be the end of the header seq = strchr(line, '\n'); if(seq == NULL) @@ -235,10 +239,7 @@ unsigned long long * get_kmer_counts_from_file(FILE *fh, const unsigned int kmer unsigned long long * get_kmer_counts_from_filename(const char *fn, const unsigned int kmer) { FILE *fh = fopen(fn, "r"); - if(fh == NULL) { - fprintf(stderr, "Could not open %s - %s\n", fn, strerror(errno)); - return NULL; - } + check_null_ptr(fh, fn); return get_kmer_counts_from_file(fh, kmer); } @@ -256,10 +257,7 @@ unsigned long long * get_continuous_kmer_counts_from_file(FILE *fh, const unsign // malloc our return array unsigned long long * counts = calloc((width+ 1), sizeof(unsigned long long)); - if(counts == NULL) { - fprintf(stderr, "%s\n", strerror(errno)); - exit(EXIT_FAILURE); - } + check_null_ptr(counts, NULL); size_t cpy_size = kmer - 1; @@ -327,7 +325,6 @@ unsigned long long * get_continuous_kmer_counts_from_file(FILE *fh, const unsign } - free(end_of_previous); free(line); fclose(fh); @@ -337,10 +334,7 @@ unsigned long long * get_continuous_kmer_counts_from_file(FILE *fh, const unsign unsigned long long * get_continuous_kmer_counts_from_filename(const char *fn, const unsigned int kmer) { FILE *fh = fopen(fn, "r"); - if(fh == NULL) { - fprintf(stderr, "Could not open %s - %s\n", fn, strerror(errno)); - return NULL; - } + check_null_ptr(fh, fn); return get_continuous_kmer_counts_from_file(fh, kmer); } diff --git a/kmer_utils.h b/kmer_utils.h index ae48136..4bc732a 100644 --- a/kmer_utils.h +++ b/kmer_utils.h @@ -6,6 +6,7 @@ char *index_to_kmer(unsigned long long index, long kmer); // Utility functions size_t strnstrip(char *s, int c, size_t len); unsigned long long pow_four(unsigned long long x); +void check_null_ptr(void *ptr, const char *error); // Variables const unsigned char alpha[256]; @@ -18,3 +19,4 @@ unsigned long long * get_kmer_counts_from_file(FILE *fh, const int kmer); unsigned long long * get_continuous_kmer_counts_from_filename(const char *fn, const unsigned int kmer); unsigned long long * get_continuous_kmer_counts_from_file(FILE *fh, const unsigned int kmer); + |