-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
131 lines (110 loc) · 3.67 KB
/
main.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
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <vector>
#include "FastaReader.h"
#include "KmerGenerator.h"
#include "KmerStats.h"
#include "KmerHash.h"
void printUsage() {
std::cout << "Usage:\n"
<< " tinyckg count -i <input_fasta_file> -k <kmer_length> [-C] -o <output_file>\n"
<< " tinyckg dump <input_ckg_file> -o <output_csv_file>\n";
}
void countKmers(const std::string& fastaFile, int kmerLength, const std::string& outputFile, bool canonical) {
FastaReader fastaReader(fastaFile);
fastaReader.read();
KmerGenerator kmerGenerator(fastaReader.getSequence(), kmerLength, canonical);
kmerGenerator.generateKmers();
kmerGenerator.saveKmersToFile(outputFile);
}
void dumpKmers(const std::string& ckgFile, const std::string& outputFile) {
std::ifstream infile(ckgFile, std::ios::binary);
if (!infile.is_open()) {
throw std::runtime_error("Unable to open input .ckg file");
}
int kmerSize;
int kmerCount;
infile.read(reinterpret_cast<char*>(&kmerSize), sizeof(kmerSize));
infile.read(reinterpret_cast<char*>(&kmerCount), sizeof(kmerCount));
std::map<std::string, int> kmerCounts;
for (int i = 0; i < kmerCount; ++i) {
unsigned long hash;
int count;
infile.read(reinterpret_cast<char*>(&hash), sizeof(hash));
infile.read(reinterpret_cast<char*>(&count), sizeof(count));
std::string kmer = KmerHash::reverseHash(hash, kmerSize);
kmerCounts[kmer] = count;
}
infile.close();
std::ofstream outfile(outputFile);
if (!outfile.is_open()) {
throw std::runtime_error("Unable to open output CSV file");
}
for (const auto& pair : kmerCounts) {
outfile << pair.first << "\t" << pair.second << "\n";
}
outfile.close();
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printUsage();
return EXIT_FAILURE;
}
std::string command = argv[1];
if (command == "count") {
std::string fastaFile;
std::string outputFile = "output.ckg";
int kmerLength = 5;
bool canonical = false;
int opt;
while ((opt = getopt(argc - 1, argv + 1, "i:k:o:C")) != -1) {
switch (opt) {
case 'i':
fastaFile = optarg;
break;
case 'k':
kmerLength = std::stoi(optarg);
break;
case 'o':
outputFile = optarg;
break;
case 'C':
canonical = true;
break;
default:
printUsage();
return EXIT_FAILURE;
}
}
if (fastaFile.empty() || kmerLength <= 0) {
printUsage();
return EXIT_FAILURE;
}
std::cout << "Counting " << kmerLength << "-mers in " << fastaFile << ", canonical: " << canonical << std::endl;
countKmers(fastaFile, kmerLength, outputFile, canonical);
} else if (command == "dump") {
if (argc < 4) {
printUsage();
return EXIT_FAILURE;
}
std::string ckgFile = argv[2];
std::string outputFile = "output.csv";
int opt;
while ((opt = getopt(argc - 2, argv + 2, "o:")) != -1) {
switch (opt) {
case 'o':
outputFile = optarg;
break;
default:
printUsage();
return EXIT_FAILURE;
}
}
dumpKmers(ckgFile, outputFile);
} else {
printUsage();
return EXIT_FAILURE;
}
return 0;
}