forked from OpenTreeOfLife/otcetera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mungenames.cpp
63 lines (54 loc) · 1.95 KB
/
mungenames.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
#include "otc/otcli.h"
#include <cstring>
using namespace otc;
typedef RootedTree<RTNodeNoData, RTreeNoData> Tree_t;
template<typename T>
bool cleanNamesAndWrite(OTCLI & otCLI, std::unique_ptr<T> tree);
bool hasProblematicCharacter(const std::string & name);
std::string generateCleanedName(const std::string & name);
static const char * problematicChars = ",():;[]{}'\t\n";
static const char replacementChar = ' ';
static bool logToStderr = true;
bool hasProblematicCharacter(const std::string & name) {
for (const auto & c : name) {
if (strchr(problematicChars, c) != nullptr) {
return true;
}
}
return false;
}
std::string generateCleanedName(const std::string & name) {
std::string x = name;
std::size_t pos = 0U;
for (const auto & c : name) {
if (strchr(problematicChars, c) != nullptr) {
x[pos] = replacementChar;
}
++pos;
}
if (logToStderr) {
std::cerr << "Replacing \"" << name << "\" with \"" << x << "\"\n";
}
return x;
}
template<typename T>
bool cleanNamesAndWrite(OTCLI & , std::unique_ptr<T> tree) {
for (auto nd : iter_node(*tree)) {
const auto name = nd->get_name();
if (hasProblematicCharacter(name)) {
nd->set_name(generateCleanedName(name));
}
}
write_tree_as_newick(std::cout, *tree);
return true;
}
int main(int argc, char *argv[]) {
OTCLI otCLI("otc-munge-names",
"takes a filepath to a newick file. Writes the tree to standard output after replacing each " \
"\ninstance of a problematic character in a name with space." \
"\nThe problematic characters are considered to be:\n" \
" , ( ) : ; [ ] { } ' <TAB> <NEWLINE>",
"some.tre");
std::function<bool (OTCLI &, std::unique_ptr<Tree_t>)> wnl = cleanNamesAndWrite<Tree_t>;
return tree_processing_main<Tree_t>(otCLI, argc, argv, wnl, nullptr, 1);
}