-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
155 lines (140 loc) · 3.78 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cinttypes>
#include <limits>
#include <memory>
#include "kmer_api.h"
#include "kmc_file.h"
using kmer_t = CKmerAPI;
class KMCFileWrapper
{
std::unique_ptr<CKMCFile> kmc_file;
size_t tot_kmers;
size_t cur_kmer_no{};
kmer_t cur;
size_t cur_count;
uint32_t k;
public:
KMCFileWrapper(KMCFileWrapper&&) = default;
KMCFileWrapper& operator=(KMCFileWrapper&&) = default;
KMCFileWrapper(const std::string& path)
{
kmc_file = std::make_unique<CKMCFile>();
if(!kmc_file->OpenForListing(path))
{
std::cerr << "Error: cannot open kmc database " << path << "\n";
exit(1);
}
if(kmc_file->IsKMC2() == true)
{
std::cerr << "Error: kmc database not sorted: " << path << "\n";
std::cerr << "Hint: KMC database may be sorted with kmc_tools transform <input> sort <output>\n";
exit(1);
}
CKMCFileInfo kmc_file_info;
kmc_file->Info(kmc_file_info);
k = kmc_file_info.kmer_length;
tot_kmers = kmc_file_info.total_kmers;
cur = kmer_t(k);
if(!Finished())
Next();
}
uint32_t GetK() const
{
return k;
}
bool Finished()
{
return cur_kmer_no >= tot_kmers;
}
const kmer_t& First() const
{
return cur;
}
size_t FirstCount() const
{
return cur_count;
}
void Next()
{
++cur_kmer_no;
if (!kmc_file->ReadNextKmer(cur, cur_count))
{
std::cerr << "Error: critical, this should not happen, details: " << __FILE__ << "(" << __LINE__ << ")\n";
exit(1);
}
}
~KMCFileWrapper() noexcept
{
if (kmc_file)
kmc_file->Close();
}
};
bool allKAreSame(const std::vector<KMCFileWrapper>& samples)
{
if (samples.empty())
return true;
uint32_t k = samples.front().GetK();
for (const auto& sample : samples)
if (k!= sample.GetK())
return false;
return true;
}
int main(int argc, char**argv)
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <file_with_sorted_kmc_dbs_per_line>\n";
return 1;
}
std::ifstream in(argv[1]);
if (!in)
{
std::cerr << "Error: cannot open file " << argv[1] << "\n";
return 1;
}
std::string path;
std::vector<KMCFileWrapper> samples;
while (std::getline(in, path))
samples.emplace_back(path);
if (!allKAreSame(samples))
{
std::cerr << "Error: each database should have the same k\n";
return 1;
}
std::string str_kmer;
while (true)
{
size_t min_id = std::numeric_limits<size_t>::max();
for (size_t i = 0 ; i < samples.size() ; ++i)
{
if (!samples[i].Finished())
{
if (min_id == std::numeric_limits<size_t>::max() || samples[i].First() < samples[min_id].First())
min_id = i;
}
}
if (min_id == std::numeric_limits<size_t>::max()) // no more k-mers
break;
//todo: print kmer as string, and tab
auto min_kmer = samples[min_id].First();
min_kmer.to_string(str_kmer);
std::cout << str_kmer;
for (auto& sample : samples)
{
size_t c;
if (sample.Finished() || !(sample.First() == min_kmer))
c = 0;
else
{
c = sample.FirstCount();
sample.Next();
}
std::cout << "\t" << c;
}
std::cout << "\n";
}
return 0;
}