aboutsummaryrefslogtreecommitdiff
path: root/kmer_locations.c
blob: e118b9fedff1d6643b20f096fd424cab057eca40 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2013 Calvin Morrison
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include "kmer_utils.h"

void print_mer(unsigned long long mer, const long long pos, const bool labels, const bool reverse, unsigned int kmer, char *label) {
	if(labels) {
		char *kmer_str;
		bool free_ptr = false;
		if(label == NULL) {
			kmer_str = index_to_kmer(mer, kmer);
			free_ptr = true;
		}
		else {
			kmer_str = label;
		}

		if(reverse)
			printf("%s %llu c\n", kmer_str, pos);
		else
			printf("%s %llu\n", kmer_str, pos);
		if (free_ptr)
			free(kmer_str);
	}
	else {
		if(reverse)
			printf("%llu %llu c\n", mer, pos);
		else
			printf("%llu %llu\n", mer, pos);
	}
}

void print_sequence(const char *seq, // sequence
										const size_t seq_length,  // length of sequence
										const size_t global_pos,  // overall position in read
										const unsigned int kmer,  // kmer size
									  size_t *specific_mers, // specific mers  array
									  char   **specific_mers_labels, // specific mers  array
										size_t num_specific_mers,
										bool labels,  // print label instead of indicies
										bool reverse) // reverse points
{
	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
		long long pos;
		if(!reverse)
			pos = global_pos + position;
		else
			pos = global_pos + ((signed)seq_length - position);

		if(num_specific_mers != 0) {
			size_t j;
			for(j = 0; j < num_specific_mers; j++) {
				if(mer == specific_mers[j])
					print_mer(mer, pos, labels, reverse, kmer, specific_mers_labels[j]);
			}
		}
		else {
			print_mer(mer, pos, labels, reverse, kmer, NULL);
		}
		// skip count if error
		next: ;
	}
}

void help() {
	printf("usage: kmer_locations -i input_file -k kmer [-c] [-n] [-l] ...\n\n"
				 "print locations of 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 (position is not in the reverse. \n"
				 "  --label      -l  print mer along with location\n"
				 "  --mer-file   -m  a file containing a list of mers you are interested\n"
				 "                   in opening. this will enable output your results in\n"
				 "                   a sparse format \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;

	// for getdelim
	char *line = NULL;
	size_t len = 0;
	ssize_t read;

	unsigned int kmer = 0;

	// for specific mers
	char *mer_fn = NULL;
	size_t num_desired_indicies = 0;
	size_t *desired_indicies = NULL;
	char **labeled_mers = NULL;

	bool label = false;
	bool kmer_set = false;
	bool specific_mers = false;
	bool count_compliment = false;

	static struct option long_options[] = {
		{"input", required_argument, 0, 'i'},
		{"kmer",  required_argument, 0, 'k'},
		{"compliment", required_argument, 0, 'c'},
		{"label", no_argument, 0, 'l'},
		{"mer-file", no_argument, 0, 'm'},
		{"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:m:clvh", 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 'm':
				specific_mers = true;
				mer_fn = optarg;
				break;
			case 'l':
				label = 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(specific_mers) {
		size_t j; 
		size_t width = pow_four(kmer);
		desired_indicies = (size_t *) malloc((width) * sizeof(size_t));
		if(desired_indicies == NULL) 
			exit(EXIT_FAILURE);

		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"); 
			exit(EXIT_FAILURE);
		}

		labeled_mers = (char**) malloc(sizeof(char **) * num_desired_indicies);
		check_null_ptr(labeled_mers, NULL);
		for(j = 0; j < num_desired_indicies; j++) {
			labeled_mers[j] = index_to_kmer(desired_indicies[j], kmer);
		}
	}

	unsigned long long global_position = 0;
	while ((read = getdelim(&line, &len, '>', fh)) != -1) {
		size_t k = 0;

		// find our first \n, this should be the end of the header
		char *seq = strchr(line, '\n');	
		if(seq == NULL) 
			continue;


		// point to one past that.
		seq = seq + 1;

		// strip out all other newlines to handle multiline sequences
		size_t seq_length = strnstrip(seq, '\n', strlen(seq));

		for(k = 0; k < seq_length; k++) {
			seq[k] = alpha[(int)seq[k]];
		}
		
		print_sequence(seq, seq_length, global_position, kmer, desired_indicies, labeled_mers, num_desired_indicies, label, false);

		if(count_compliment) {
			for(k = 0; k < seq_length; k++) { 
				seq[k] = compliment[(int)seq[k]];
			}
			reverse_string(seq, seq_length);
			size_t rev_seq_length = seq_length;

			// chomp all errors off the beginning
			while(*seq == 5)  {
			 rev_seq_length--;
			 seq++;
			}

			print_sequence(seq, rev_seq_length, global_position, kmer, desired_indicies, labeled_mers, num_desired_indicies, label, true);
		}

		if(seq_length != 0) {
			seq_length--;
		}
		global_position += seq_length;
	}

	free(line);
	free(desired_indicies);
	free(labeled_mers);
	fclose(fh);

	return EXIT_SUCCESS;
	}