aboutsummaryrefslogtreecommitdiff
path: root/kmer_total_count.c
blob: 6f1c8eec1848bba8e07fe9fcedd7abf53b327dfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2013 Calvin Morrison
#include <errno.h>
#include <stdbool.h>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <getopt.h>

#include "kmer_utils.h"

template <typename array_type>
void count_sequence(const char *seq, const size_t seq_length, const unsigned int kmer, array_type *counts) {
	long long position;
	long long i;

	// loop through our seq to process each k-mer
	for(position = 0; position < (signed)(seq_length - kmer + 1); position++) {
		unsigned long long mer = 0;
		unsigned long long multiply = 1;

		// for each char in the k-mer check if it is an error char
		for(i = position + kmer - 1; i >= position; i--){
			if(seq[i] == 5) {
				position = i;
				goto next;
			}

			// multiply this char in the mer by the multiply
			// and bitshift the multiply for the next round
			mer += seq[i] * multiply;
			multiply = multiply << 2;
		}
		// bump up the mer value in the counts array
		inc(counts, mer);

		// skip count if error
		next: ;
	}
}

void help() {
	printf("usage: kmer_total_count -i input_file -k kmer [-c] [-n] [-l] ...\n\n"
				 "count mers in size k from a fasta file\n"
				 "\n"
				 "  --input    -i  input fasta file to count\n"
				 "  --kmer     -k  size of mers to count\n"
				 "  --compliment -c  count compliment of sequences\n"
				 "  --nonzero  -n  only print non-zero values\n"
				 "  --label    -l  print mer along with value\n"
				 "  --sparse   -s  force sparse table for any mer\n"
				 "\n"
				 "Report all bugs to mutantturkey@gmail.com\n"
				 "\n"
				 "Copyright 2014 Calvin Morrison, Drexel University.\n"
				 "\n"
				 "If you are using any dna-utils tool for a publication\n"
				 "please cite your usage:\n\n"
				 "dna-utils. Drexel University, Philadelphia USA, 2014;\n"
				 "software available at www.github.com/EESI/dna-utils/\n");
}


int main(int argc, char **argv) {

	char *fn = NULL;
	FILE *fh = NULL;

	unsigned int kmer = 0;

	bool nonzero = false;
	bool label = false;
	bool kmer_set = false;
	bool count_compliment = false;
	bool force_sparse = false;

	static struct option long_options[] = {
		{"input", required_argument, 0, 'i'},
		{"kmer",  required_argument, 0, 'k'},
		{"compliment", required_argument, 0, 'c'},
		{"nonzero", no_argument, 0, 'n'},
		{"label", no_argument, 0, 'l'},
		{"sparse", no_argument, 0, 's'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};

	while (1) {

		int option_index = 0;
		int c = 0;

		c = getopt_long (argc, argv, "i:k:cnslvh", long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
			case 'i':
				fn = optarg;
				break;
			case 'k':
				kmer = atoi(optarg);
				kmer_set = true;
				break;
			case 'c':
				count_compliment = true;
				break;
			case 'n':
				nonzero = true; 
				break;
			case 'l':
				label = true;
				break;
			case 's':
				force_sparse = true;
				break;
			case 'h':
				help();
				exit(EXIT_SUCCESS);
				break;
			case 'v':
				printf("dna-utils version " VERSION "\n");
				exit(EXIT_SUCCESS);
				break;
			default:
				break;
		}
	}
	if(argc == 1) {
		help();
		exit(EXIT_FAILURE);
	}
	if(fn == NULL) {
		fprintf(stderr, "no input file specified with -i, reading from stdin\n");
		fh = stdin;
	}
	else {
		fh = fopen(fn, "r");
		if(fh == NULL) {
			fprintf(stderr, "Could not open %s - %s\n", fn, strerror(errno));
			exit(EXIT_FAILURE);
		}
	}
	if(!kmer_set) {
		fprintf(stderr, "Error: kmer (-k) must be supplied\n");
		exit(EXIT_FAILURE);
	}
	if(kmer == 0) { 
		fprintf(stderr, "Error: invalid kmer - '%d'.\n", kmer);
		exit(EXIT_FAILURE);
	}

	if(kmer > 12 || force_sparse) {
		kmer_map *counts = NULL;
		kmer_map *res = get_kmer_counts_from_file(counts, fh, kmer, count_compliment);

		print_kmer(res, label, nonzero, kmer);
	}
	else {
		unsigned long long *counts = NULL;
		unsigned long long *res = get_kmer_counts_from_file(counts, fh, kmer, count_compliment);
		print_kmer(res, label, nonzero, kmer);
	}

	return EXIT_SUCCESS;
}