This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.cpp
146 lines (119 loc) · 4.14 KB
/
count.cpp
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
#include <iostream>
#include <fstream>
#include <chrono>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <string.h>
using namespace std;
const string ALPHABET = "ACTG";
uint KMER_LEN;
bool COUNT_CANONICAL = false;
unordered_map <string, vector<uint>> COUNTED_KMERS;
uint NUM_FILES = 0;
char complement(const char nucleotide) {
switch (nucleotide) {
case 'A': return 'T';
case 'C': return 'G';
case 'T': return 'A';
case 'G': return 'C';
default: throw runtime_error((string)"Unhandled nucleotide: " + nucleotide);
}
}
void count_kmer(const string * kmer_p) {
if (COUNT_CANONICAL) {
string reverse_complement;
for (int i = KMER_LEN-1; i >= 0; i--) reverse_complement.push_back(complement((*kmer_p)[i]));
if (COUNTED_KMERS.find(reverse_complement) != COUNTED_KMERS.end()) {
vector<uint> * counts = &COUNTED_KMERS[reverse_complement];
while ((*counts).size() < NUM_FILES) (*counts).push_back(0);
(*counts)[NUM_FILES-1]++;
return;
}
}
vector<uint> * counts = &COUNTED_KMERS[*kmer_p];
while ((*counts).size() < NUM_FILES) (*counts).push_back(0);
(*counts)[NUM_FILES-1]++;
}
void output_kmer_counts() {
for (const auto &keyval : COUNTED_KMERS) {
cout << keyval.first;
for (uint i = 0; i < NUM_FILES; i ++) {
cout << "\t" << (i < keyval.second.size() ? keyval.second[i] : 0);
}
cout << endl;
}
}
void process_file(string filename) {
ifstream file;
file.open(filename);
if (!file.is_open()) throw runtime_error((string)"Cannot open file: " + filename);
NUM_FILES++;
cout << '\t' << filename;
string kmer_buffer[KMER_LEN];
uint max_buffer_index = 1;
char ch;
while ((ch = file.get()) != EOF) {
if (ch == '\n' || ch == '\r') {
// Skip
} else if (ALPHABET.find(ch) == string::npos) {
// Char is not part of nucleotide alphabet, break current kmers
if (ch == '>') file.ignore(UINT32_MAX, '\n');
max_buffer_index = 1;
for (uint i = 0; i < KMER_LEN; i++) kmer_buffer[i].clear();
} else {
for (uint i = 0; i < max_buffer_index; i++) {
kmer_buffer[i] += ch;
if (kmer_buffer[i].length() == KMER_LEN) {
count_kmer(&kmer_buffer[i]);
kmer_buffer[i].clear();
}
}
if (max_buffer_index < KMER_LEN) max_buffer_index++;
}
}
file.close();
}
int main(int argc, char* argv[]) {
auto usage = [&argv](string message){
cerr << message << endl;
fprintf(stderr, "Usage: %s -k kmer_len [-C] fileA [fileB ...]\n", argv[0]);
fprintf(stderr, " -k kmer_len : Kmer length as positive nonzero integer.\n");
fprintf(stderr, " -C : Count canonical kmers.\n");
return 1;
};
if (argc < 3) return usage("Not enough arguments!");
vector<string> files;
for (int i = 1; i < argc; i++) {
auto arg = [&argv, i](string arg){
return !strcmp(argv[i], arg.c_str());
};
if (arg("-k")) {
KMER_LEN = atoi(argv[++i]);
if (KMER_LEN < 1) {
return usage("Bad kmer length!");
}
continue;
}
if (arg("-C")) {
COUNT_CANONICAL = true;
continue;
}
files.push_back(argv[i]);
}
if (!KMER_LEN) return usage("No kmer length specified!");
for (uint i = 0; i < files.size(); i++) {
cerr << files[i] << endl;
cerr << "\tCounting K-mers...";
chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
process_file(files[i]);
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
cerr << time_span.count() << 's' << endl;
}
cout << '\n';
cerr << "Writing results...";
output_kmer_counts();
cerr << "DONE" << endl;
return 0;
}