Skip to content

Commit

Permalink
Removed unnecessary macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejSladky committed Aug 22, 2024
1 parent 11350b1 commit 0380d58
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 69 deletions.
3 changes: 3 additions & 0 deletions src/khash_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ KHASH_MAP_INIT_INT64(P64, size_t)
inline void kh_del_from_set(kh_S##type##_t *set, khint_t key) { \
kh_del_S##type(set, key); \
} \
inline void kh_destroy_set(kh_S##type##_t *set) { \
kh_destroy_S##type(set); \
} \
inline kh_P##type##_t *kh_init_map() { \
return kh_init_P##type(); \
} \
Expand Down
132 changes: 63 additions & 69 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,74 +53,68 @@ void Version() {
std::cerr << VERSION << std::endl;
}

// This macro cannot be avoided without bloating the code too much.
#define INIT_KMERCAMEL(type) \
int kmercamel##type(std::string path, int k, int d_max, std::ostream *of, bool complements, bool masks, \
std::string algorithm, bool optimize_memory, bool lower_bound) { \
kmer_dict##type##_t wrapper; \
kmer##type##_t kmer_type = 0; \
if (masks) { \
int ret = Optimize(wrapper, kmer_type, algorithm, path, *of, k, complements); \
if (ret) Help(); \
return ret; \
} \
\
/* Handle streaming algorithm separately. */ \
if (algorithm == "streaming") { \
WriteName(k, *of); \
Streaming(path, *of, k , complements); \
} \
/* Handle hash table based algorithms separately so that they consume less memory. */ \
else if (algorithm == "global" || algorithm == "local") { \
auto *kMers = kh_init_S##type(); \
ReadKMers(kMers, wrapper, kmer_type, path, k, complements); \
if (!kh_size(kMers)) { \
std::cerr << "Path '" << path << "' contains no k-mers." << std::endl; \
return Help(); \
} \
d_max = std::min(k - 1, d_max); \
if (!lower_bound) WriteName(k, *of); \
if (algorithm == "global") { \
auto kMerVec = kMersToVec(kMers, kmer_type); \
kh_destroy_S##type(kMers); \
/* Turn off the memory optimizations if optimize_memory is set to false. */ \
if(optimize_memory) PartialPreSort(kMerVec, k); \
else MEMORY_REDUCTION_FACTOR = 1; \
if (lower_bound) std::cout << LowerBoundLength(wrapper, kMerVec, k, complements); \
else Global(wrapper, kMerVec, *of, k, complements); \
} \
else Local(kMers, wrapper, kmer_type, *of, k, d_max, complements); \
} else { \
auto data = ReadFasta(path); \
if (data.empty()) { \
std::cerr << "Path '" << path << "' not to a fasta file." << std::endl; \
return Help(); \
} \
d_max = std::min(k - 1, d_max); \
\
auto kMers = ConstructKMers(data, k, complements); \
WriteName(k, *of); \
if (algorithm == "globalAC") { \
GlobalAC(kMers, *of, complements); \
} \
else if (algorithm == "localAC") { \
LocalAC(kMers, *of, k, d_max, complements); \
} \
else { \
std::cerr << "Algorithm '" << algorithm << "' not supported." << std::endl; \
return Help(); \
} \
} \
*of << std::endl; \
return 0; \
}
/// Run KmerCamel with the given parameters.
template <typename kmer_t, typename kh_wrapper_t>
int kmercamel(kh_wrapper_t wrapper, kmer_t kmer_type, std::string path, int k, int d_max, std::ostream *of, bool complements, bool masks,
std::string algorithm, bool optimize_memory, bool lower_bound) {
if (masks) {
int ret = Optimize(wrapper, kmer_type, algorithm, path, *of, k, complements);
if (ret) Help();
return ret;
}

INIT_KMERCAMEL(64)
INIT_KMERCAMEL(128)
INIT_KMERCAMEL(256)
/* Handle streaming algorithm separately. */
if (algorithm == "streaming") {
WriteName(k, *of);
Streaming(path, *of, k , complements);
}
/* Handle hash table based algorithms separately so that they consume less memory. */
else if (algorithm == "global" || algorithm == "local") {
auto *kMers = wrapper.kh_init_set();
ReadKMers(kMers, wrapper, kmer_type, path, k, complements);
if (!kh_size(kMers)) {
std::cerr << "Path '" << path << "' contains no k-mers." << std::endl;
return Help();
}
d_max = std::min(k - 1, d_max);
if (!lower_bound) WriteName(k, *of);
if (algorithm == "global") {
auto kMerVec = kMersToVec(kMers, kmer_type);
wrapper.kh_destroy_set(kMers);
/* Turn off the memory optimizations if optimize_memory is set to false. */
if(optimize_memory) PartialPreSort(kMerVec, k);
else MEMORY_REDUCTION_FACTOR = 1;
if (lower_bound) std::cout << LowerBoundLength(wrapper, kMerVec, k, complements);
else Global(wrapper, kMerVec, *of, k, complements);
}
else Local(kMers, wrapper, kmer_type, *of, k, d_max, complements);
} else {
auto data = ReadFasta(path);
if (data.empty()) {
std::cerr << "Path '" << path << "' not to a fasta file." << std::endl;
return Help();
}
d_max = std::min(k - 1, d_max);

auto kMers = ConstructKMers(data, k, complements);
WriteName(k, *of);
if (algorithm == "globalAC") {
GlobalAC(kMers, *of, complements);
}
else if (algorithm == "localAC") {
LocalAC(kMers, *of, k, d_max, complements);
}
else {
std::cerr << "Algorithm '" << algorithm << "' not supported." << std::endl;
return Help();
}
}
*of << std::endl;
return 0;
}

int main(int argc, char **argv) {
std::string path = "";
std::string path;
int k = 0;
int d_max = 5;
std::ofstream output;
Expand All @@ -142,7 +136,7 @@ int main(int argc, char **argv) {
while ((opt = getopt(argc, argv, "p:k:d:a:o:hcvml")) != -1) {
switch(opt) {
case 'p':
if (path != "") {
if (!path.empty()) {
std::cerr << "Error: parameter p set twice." << std::endl;
return Help();
}
Expand Down Expand Up @@ -218,10 +212,10 @@ int main(int argc, char **argv) {
return Help();
}
if (k < 32) {
return kmercamel64(path, k, d_max, of, complements, masks, algorithm, optimize_memory, lower_bound);
return kmercamel(kmer_dict64_t(), kmer64_t(0), path, k, d_max, of, complements, masks, algorithm, optimize_memory, lower_bound);
} else if (k < 64) {
return kmercamel128(path, k, d_max, of, complements, masks, algorithm, optimize_memory, lower_bound);
return kmercamel(kmer_dict128_t(), kmer128_t(0), path, k, d_max, of, complements, masks, algorithm, optimize_memory, lower_bound);
} else {
return kmercamel256(path, k, d_max, of, complements, masks, algorithm, optimize_memory, lower_bound);
return kmercamel(kmer_dict256_t(), kmer256_t(0), path, k, d_max, of, complements, masks, algorithm, optimize_memory, lower_bound);
}
}

0 comments on commit 0380d58

Please sign in to comment.