-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdecenttree.cpp
127 lines (121 loc) · 4.73 KB
/
decenttree.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
//
// DecentTree.cpp
//
// Copyright (C) 2020, James Barbetti.
//
// LICENSE:
//* This program is free software; you can redistribute it and/or modify
//* it under the terms of the GNU General Public License as published by
//* the Free Software Foundation; either version 2 of the License, or
//* (at your option) any later version.
//*
//* This program is distributed in the hope that it will be useful,
//* but WITHOUT ANY WARRANTY; without even the implied warranty of
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//* GNU General Public License for more details.
//*
//* You should have received a copy of the GNU General Public License
//* along with this program; if not, write to the
//* Free Software Foundation, Inc.,
//* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include <string> //for std::string
#include <iostream> //for std::cout
#include "progress.h" //for progress_display::setProgressDisplay()
#include "starttree.h" //for StartTree::Factory
#include "operatingsystem.h" //for getOSName
#define PROBLEM(x) if (1) problems = problems + x + ".\n"; else 0
namespace {
bool endsWith(const std::string s, const char* suffix) {
auto suffixLen = strlen(suffix);
if (s.length() < suffixLen) {
return false;
}
return s.substr(s.length()-suffixLen, suffixLen) == suffix;
}
};
void showBanner() {
std::cout << "\nDecentTree for " << getOSName() << "\n";
std::cout << "Based on algorithms (UPGMA, NJ, BIONJ) proposed by Sokal & Michener [1958], Saitou & Nei [1987], Gascuel [2009]\n";
std::cout << "Incorporating (in NJ-R and BIONJ-R) techniques proposed by Simonson, Mailund, and Pedersen [2011]\n";
std::cout << "Developed by Olivier Gascuel [2009], Hoa Sien Cuong [2009], James Barbetti [2020]\n";
std::cout << "(To suppress this banner pass -no-banner)\n";
}
void showUsage() {
std::cout << "\nUsage: DecentTree -in [mldist] -out [newick] -t [algorithm] (-gz) (-no-banner)\n";
std::cout << "[mldist] is the path of a distance matrix file (which may be in .gz format)\n";
std::cout << "[newick] is the path to write the newick tree file to (if it ends in .gz it will be compressed)\n";
std::cout << "[algorithm] is one of the following, supported, distance matrix algorithms:\n";
std::cout << StartTree::Factory::getInstance().getListOfTreeBuilders();
}
int main(int argc, char* argv[]) {
std::string problems;
progress_display::setProgressDisplay(true); //Displaying progress bars
std::string algorithmName = StartTree::Factory::getNameOfDefaultTreeBuilder();
std::string inputFilePath;
std::string outputFilePath;
bool isOutputZipped = false;
bool isBannerSuppressed = false;
for (int argNum=1; argNum<argc; ++argNum) {
std::string arg = argv[argNum];
std::string nextArg = (argNum+1<argc) ? argv[argNum+1] : "";
if (arg=="-in") {
inputFilePath = nextArg;
++argNum;
}
else if (arg=="-t") {
if (START_TREE_RECOGNIZED(nextArg)) {
algorithmName = nextArg;
} else {
PROBLEM("Algorithm name " + nextArg + " not recognized");
PROBLEM("Recognized distance matrix algorithms are:");
PROBLEM(StartTree::Factory::getInstance().getListOfTreeBuilders());
}
++argNum;
}
else if (arg=="-out") {
outputFilePath = nextArg;
++argNum;
}
else if (arg=="-gz") {
isOutputZipped = true;
}
else if (arg=="-no-banner") {
isBannerSuppressed = true;
}
else {
PROBLEM("Unrecognized command-line argument, " + arg);
break;
}
}
if (argc==1) {
if (!isBannerSuppressed) {
showBanner();
}
showUsage();
return 0;
}
if (inputFilePath.empty()) {
PROBLEM("Input (mldist) file should be specified via -in [filepath.mldist]");
}
if (outputFilePath.empty()) {
PROBLEM("Ouptut (newick format) filepath should be specified via -out [filepath.newick]");
}
else if (inputFilePath==outputFilePath) {
PROBLEM("Input file and output file paths are the same (" + inputFilePath + ")");
}
if (!problems.empty()) {
std::cerr << problems;
return 1;
}
if (!isBannerSuppressed) {
showBanner();
}
StartTree::BuilderInterface* algorithm = StartTree::Factory::getTreeBuilderByName(algorithmName);
algorithm->setZippedOutput(isOutputZipped || endsWith(outputFilePath,".gz"));
if (!algorithm->constructTree(inputFilePath, outputFilePath)) {
std::cerr << "Tree construction failed.";
return 1;
}
return 0;
}