| 1 |
#include <iostream> |
| 2 |
#include <vector> |
| 3 |
#include <sstream> |
| 4 |
#include <fstream> |
| 5 |
#include <memory> |
| 6 |
#include <algorithm> |
| 7 |
#include <unordered_map> |
| 8 |
|
| 9 |
#include <zlib.h> |
| 10 |
using namespace std; |
| 11 |
|
| 12 |
struct Params |
| 13 |
{ |
| 14 |
vector<string> inputPaths; |
| 15 |
string outputPath; |
| 16 |
uint64_t kmerLen = 25; |
| 17 |
uint64_t cutoffMin = 2; |
| 18 |
uint64_t cutoffMax = 1000000000; |
| 19 |
uint64_t countMax = 255; |
| 20 |
bool canonical = true; |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
class Counter |
| 25 |
{ |
| 26 |
const Params& params; |
| 27 |
uint64_t nSeqs{}; |
| 28 |
|
| 29 |
class LineReader |
| 30 |
{ |
| 31 |
gzFile file; |
| 32 |
static constexpr uint64_t buff_capacity = 1ull << 24; |
| 33 |
uint64_t buff_pos = 0; |
| 34 |
uint64_t buff_size = 0; |
| 35 |
std::unique_ptr<uint8_t[]> buff; |
| 36 |
|
| 37 |
bool read_char(uint8_t& c) |
| 38 |
{ |
| 39 |
if(buff_pos == buff_size) |
| 40 |
{ |
| 41 |
buff_pos = 0; |
| 42 |
buff_size = gzfread(buff.get(), 1, buff_capacity, file); |
| 43 |
} |
| 44 |
if(buff_size == 0) |
| 45 |
return false; |
| 46 |
c = buff[buff_pos++]; |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
public: |
| 51 |
LineReader(gzFile file): |
| 52 |
file(file), |
| 53 |
buff(make_unique_for_overwrite<uint8_t[]>(buff_capacity)) |
| 54 |
{ |
| 55 |
|
| 56 |
} |
| 57 |
pair<bool, string> NextLine() |
| 58 |
{ |
| 59 |
string line; |
| 60 |
uint8_t c; |
| 61 |
bool any = false; |
| 62 |
while (read_char(c)) |
| 63 |
{ |
| 64 |
if(c == '\r') |
| 65 |
continue; |
| 66 |
any = true; |
| 67 |
if (c != '\n') |
| 68 |
line.push_back((char)c); |
| 69 |
else |
| 70 |
break; |
| 71 |
} |
| 72 |
return make_pair(any, line); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
class SeqReader |
| 77 |
{ |
| 78 |
LineReader& lineReader; |
| 79 |
bool is = false; |
| 80 |
string prevLine; |
| 81 |
|
| 82 |
string nextSeqFastq() |
| 83 |
{ |
| 84 |
auto [is2, seq] = lineReader.NextLine(); |
| 85 |
auto [is3, qual_header] = lineReader.NextLine(); |
| 86 |
auto [is4, qual] = lineReader.NextLine(); |
| 87 |
|
| 88 |
if (!is2 || !is3 || !is4) |
| 89 |
{ |
| 90 |
cerr << "Error reading file " << __FILE__ << "\t" << __LINE__ << "\n"; |
| 91 |
exit(1); |
| 92 |
} |
| 93 |
|
| 94 |
std::tie(is, prevLine) = lineReader.NextLine(); |
| 95 |
|
| 96 |
return seq; |
| 97 |
} |
| 98 |
|
| 99 |
string nextSeqFasta() |
| 100 |
{ |
| 101 |
string seq; |
| 102 |
while (true) |
| 103 |
{ |
| 104 |
std::tie(is, prevLine) = lineReader.NextLine(); |
| 105 |
if(!is) |
| 106 |
return seq; |
| 107 |
|
| 108 |
if (prevLine.size() == 0) |
| 109 |
return seq; |
| 110 |
|
| 111 |
if (prevLine[0] == '>') |
| 112 |
return seq; |
| 113 |
|
| 114 |
seq += prevLine; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public: |
| 119 |
SeqReader(LineReader& lineReader) : |
| 120 |
lineReader(lineReader) |
| 121 |
{ |
| 122 |
std::tie(is, prevLine) = lineReader.NextLine(); |
| 123 |
} |
| 124 |
pair<bool, string> NextSeq() |
| 125 |
{ |
| 126 |
if (!is) |
| 127 |
return make_pair(false, ""); |
| 128 |
|
| 129 |
if (prevLine.size() == 0) |
| 130 |
{ |
| 131 |
cerr << "Error: something is wrong with this file " << __FILE__ << "\t" << __LINE__ << "\n"; |
| 132 |
exit(1); |
| 133 |
} |
| 134 |
if (prevLine[0] == '@') |
| 135 |
return make_pair(true, nextSeqFastq()); |
| 136 |
if (prevLine[0] == '>') |
| 137 |
return make_pair(true, nextSeqFasta()); |
| 138 |
cerr << "Error: unsupported file format. I don't know how to interpret line: " << prevLine << "\n"; |
| 139 |
cerr << __FILE__ << "\t" << __LINE__ << "\n"; |
| 140 |
exit(1); |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
string get_rev_compl(const std::string& kmer) |
| 145 |
{ |
| 146 |
string res; |
| 147 |
res.reserve(kmer.size()); |
| 148 |
for (auto it = kmer.crbegin() ; it != kmer.crend() ; ++it) |
| 149 |
{ |
| 150 |
switch (*it) |
| 151 |
{ |
| 152 |
case 'A': res.push_back('T'); break; |
| 153 |
case 'C': res.push_back('G'); break; |
| 154 |
case 'G': res.push_back('C'); break; |
| 155 |
case 'T': res.push_back('A'); break; |
| 156 |
default: |
| 157 |
cerr << "Error: wrong symbol in k-mer: " << kmer << "\n"; |
| 158 |
exit(1); |
| 159 |
} |
| 160 |
} |
| 161 |
return res; |
| 162 |
} |
| 163 |
void canonicalize(string& kmer) |
| 164 |
{ |
| 165 |
auto rev = get_rev_compl(kmer); |
| 166 |
if(rev < kmer) |
| 167 |
kmer = rev; |
| 168 |
} |
| 169 |
|
| 170 |
std::unordered_map<string, uint64_t> m; |
| 171 |
|
| 172 |
void processFile(const std::string& fname) |
| 173 |
{ |
| 174 |
cerr << "Processing file " << fname << "\n"; |
| 175 |
auto gzfile = gzopen(fname.c_str(), "r"); |
| 176 |
if(!gzfile) |
| 177 |
{ |
| 178 |
cerr << "Error: cannot open file " << fname << "\n"; |
| 179 |
exit(1); |
| 180 |
} |
| 181 |
LineReader lineReader(gzfile); |
| 182 |
SeqReader seqReader(lineReader); |
| 183 |
|
| 184 |
for (auto [is, seq] = seqReader.NextSeq(); is; tie(is, seq) = seqReader.NextSeq()) |
| 185 |
{ |
| 186 |
//if (nSeqs % 100000 == 0) |
| 187 |
// cout << nSeqs << "\n"; |
| 188 |
++nSeqs; |
| 189 |
|
| 190 |
if (seq.size() < params.kmerLen) |
| 191 |
continue; |
| 192 |
//cout << "seq:\n" << seq << "\n"; |
| 193 |
|
| 194 |
|
| 195 |
transform(seq.begin(), seq.end(), seq.begin(), ::toupper); |
| 196 |
uint64_t endPos = seq.size() - params.kmerLen + 1; |
| 197 |
for (uint64_t pos = 0; pos < endPos ; ++pos) |
| 198 |
{ |
| 199 |
auto kmer = seq.substr(pos, params.kmerLen); |
| 200 |
auto er_symb = kmer.find_first_not_of("ACGT"); |
| 201 |
if (er_symb != std::string::npos) |
| 202 |
{ |
| 203 |
pos += er_symb; |
| 204 |
continue; |
| 205 |
} |
| 206 |
if (params.canonical) |
| 207 |
canonicalize(kmer); |
| 208 |
++m[kmer]; |
| 209 |
} |
| 210 |
} |
| 211 |
gzclose(gzfile); |
| 212 |
} |
| 213 |
|
| 214 |
public: |
| 215 |
Counter(const Params& params): |
| 216 |
params(params) |
| 217 |
{ |
| 218 |
for (const auto& fname : params.inputPaths) |
| 219 |
processFile(fname); |
| 220 |
|
| 221 |
vector<pair<string, uint64_t>> kmers; |
| 222 |
cerr << "Take k-mers from unordered map to vector..."; |
| 223 |
uint64_t nCutoffMin{}, nCutoffMax{}; |
| 224 |
uint64_t tot_kmers{}; |
| 225 |
for (const auto& [kmer, count] : m) |
| 226 |
{ |
| 227 |
tot_kmers += count; |
| 228 |
if (count < params.cutoffMin) |
| 229 |
++nCutoffMin; |
| 230 |
else if(count > params.cutoffMax) |
| 231 |
++nCutoffMax; |
| 232 |
else |
| 233 |
kmers.emplace_back(kmer, count > params.countMax ? params.countMax : count); |
| 234 |
} |
| 235 |
cerr << "\n"; |
| 236 |
|
| 237 |
ofstream stats_file(params.outputPath + ".stats"); |
| 238 |
if(!stats_file) |
| 239 |
{ |
| 240 |
cerr << "Error: cannot open file " << params.outputPath + ".stats" << "\n"; |
| 241 |
exit(1); |
| 242 |
} |
| 243 |
stats_file << "Total unique k-mers below min cutoff:\t" << nCutoffMin << "\n"; |
| 244 |
stats_file << "Total unique k-mers above max cutoff:\t" << nCutoffMax << "\n"; |
| 245 |
stats_file << "Total unique k-mers:\t" << m.size() << "\n"; |
| 246 |
stats_file << "Total unique counted k-mers:\t" << kmers.size() << "\n"; |
| 247 |
stats_file << "Total k-mers:\t" << tot_kmers << "\n"; |
| 248 |
stats_file << "Total sequences:\t" << nSeqs << "\n"; |
| 249 |
|
| 250 |
cerr << "Sorting k-mers..."; |
| 251 |
sort(kmers.begin(), kmers.end()); |
| 252 |
cerr << "\n"; |
| 253 |
|
| 254 |
ofstream out(params.outputPath); |
| 255 |
if(!out) |
| 256 |
{ |
| 257 |
cerr << "Error: cannot open file " << params.outputPath << "\n"; |
| 258 |
exit(1); |
| 259 |
} |
| 260 |
|
| 261 |
for (const auto& [kmer, count] : kmers) |
| 262 |
out << kmer << "\t" << count << "\n"; |
| 263 |
|
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
}; |
| 268 |
|
| 269 |
template<typename T> |
| 270 |
void ParseToType(const std::string& parseFrom, T& parseTo) |
| 271 |
{ |
| 272 |
istringstream s(parseFrom); |
| 273 |
if(!(s >> parseTo)) |
| 274 |
{ |
| 275 |
std::cerr << "Error: cannot parse " << parseFrom << "\n"; |
| 276 |
exit(1); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
Params parse_params(int argc, char**argv) |
| 281 |
{ |
| 282 |
if(argc < 3) |
| 283 |
{ |
| 284 |
cerr << "Usage: " << argv[0] << " [-k<k>] [-ci<ci>] [-cx<cx>] [-cs<cs>] [-b] [@]<inputFile(s)> <outputFile>\n"; |
| 285 |
exit(1); |
| 286 |
} |
| 287 |
Params p; |
| 288 |
for (int i = 1 ; i < argc - 2; ++i) |
| 289 |
{ |
| 290 |
string param = argv[i]; |
| 291 |
if (param == "-k") |
| 292 |
ParseToType(argv[++i], p.kmerLen); |
| 293 |
else if (param == "-ci") |
| 294 |
ParseToType(argv[++i], p.cutoffMin); |
| 295 |
else if (param == "-cx") |
| 296 |
ParseToType(argv[++i], p.cutoffMax); |
| 297 |
else if (param == "-cs") |
| 298 |
ParseToType(argv[++i], p.countMax); |
| 299 |
else if (param == "-b") |
| 300 |
p.canonical = false; |
| 301 |
else |
| 302 |
{ |
| 303 |
cerr << "Error: unknown parameter: " << param << "\n"; |
| 304 |
exit(1); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
p.outputPath = argv[argc-1]; |
| 309 |
string inputPath = argv[argc-2]; |
| 310 |
if (inputPath[0] == '@') |
| 311 |
{ |
| 312 |
ifstream in(inputPath); |
| 313 |
if(!in) |
| 314 |
{ |
| 315 |
cerr << "Error: cannot open file " << inputPath <<"\n"; |
| 316 |
exit(1); |
| 317 |
} |
| 318 |
string path; |
| 319 |
while (getline(in, path)) |
| 320 |
p.inputPaths.push_back(path); |
| 321 |
} |
| 322 |
else |
| 323 |
p.inputPaths.push_back(inputPath); |
| 324 |
|
| 325 |
|
| 326 |
return p; |
| 327 |
} |
| 328 |
|
| 329 |
int main(int argc, char**argv) |
| 330 |
{ |
| 331 |
auto params = parse_params(argc, argv); |
| 332 |
Counter counter(params); |
| 333 |
} |