-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstarttree.cpp
316 lines (278 loc) · 9.4 KB
/
starttree.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// 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 "starttree.h"
#include <iostream> //for std::cout
#include <sstream> //for std::stringstream
#include <utils/stringfunctions.h> //for contains
namespace StartTree {
#ifndef USE_BIONJ_2009
#define USE_BIONJ_2009 (1)
#endif
#if (USE_BIONJ_2009)
extern void addBioNJ2009TreeBuilders(Registry& f);
#endif
extern void addBioNJ2020TreeBuilders(Registry& f);
//extern void addStitchupTreeBuilders (Registry& f);
Registry::Registry() {
}
Registry::~Registry() {
for (auto it=mapOfTreeBuilders.begin();
it!=mapOfTreeBuilders.end(); ++it) {
delete it->second;
}
mapOfTreeBuilders.clear();
}
size_t Registry::getBuilderCount() {
return mapOfTreeBuilders.size();
}
Registry& Registry::getInstance() {
static Registry instance;
if (instance.getBuilderCount()==0) {
#if (USE_BIONJ_2009)
addBioNJ2009TreeBuilders(instance);
#endif
addBioNJ2020TreeBuilders(instance);
//addStitchupTreeBuilders(instance);
BuilderInterface *bench = new BenchmarkingTreeBuilder(instance, "BENCHMARK", "Benchmark");
instance.addBuilder(bench->getName(), bench);
}
return instance;
}
void Registry::addBuilder(const std::string& name, BuilderInterface* builder) {
auto found = mapOfTreeBuilders.find(name);
if ( found != mapOfTreeBuilders.end()) {
if (found->second != builder ) {
//If replacing an existing distance matrix phylogenetic inferefence
//implementation (owned by the registry), then the existing one
//needs to be deleted (otherwise, any memory allocated for it
//will be leaked).
delete found->second;
found->second = nullptr;
}
}
mapOfTreeBuilders [ name ] = builder;
}
BuilderInterface* Registry::getBuilder(const std::string& name) const {
auto found = mapOfTreeBuilders.find(name);
return ( found == mapOfTreeBuilders.end())
? nullptr
: found->second;
}
BuilderInterface* Registry::getBuilder(const char* name) const {
std::string s(name);
return getBuilder(s);
}
std::string Registry::getListOfTreeBuilders() const {
std::stringstream list;
for (auto it=mapOfTreeBuilders.begin();
it!=mapOfTreeBuilders.end(); ++it) {
auto builder = (*it).second;
list << builder->getName() << ": "
<< builder->getDescription() << "\n";
}
return list.str();
}
StrVector Registry::getVectorOfTreeBuilderNames(bool withDescriptions) const {
StrVector vector;
for (auto it=mapOfTreeBuilders.begin();
it!=mapOfTreeBuilders.end(); ++it) {
auto builder = (*it).second;
std::string name = builder->getName();
if (withDescriptions) {
name += ": ";
name += builder->getDescription();
}
vector.emplace_back(name);
}
return vector;
}
void Registry::advertiseTreeBuilder(BuilderInterface* builder) {
std::string name = builder->getName();
addBuilder( name, builder );
}
void Registry::setNameOfDefaultTreeBuilder(const char* name) {
nameOfDefaultTreeBuilder = name;
}
const std::string& Registry::getNameOfDefaultTreeBuilder() {
return getInstance().nameOfDefaultTreeBuilder;
}
BuilderInterface* Registry::getTreeBuilderByName(const std::string& name) {
return getInstance().getBuilder(name);
}
BuilderInterface* Registry::getDefaultTreeBuilder() const {
return getBuilder(nameOfDefaultTreeBuilder);
}
BuilderInterface* Registry::getTreeBuilderByName(const char* name) {
if (name!=nullptr && strlen(name)!=0) {
return getInstance().getBuilder(name);
}
else {
return getInstance().getDefaultTreeBuilder();
}
}
BenchmarkingTreeBuilder::BenchmarkingTreeBuilder(Registry& f, const char* nameToUse,
const char *descriptionToGive)
: name(nameToUse), description(descriptionToGive)
, isOutputToBeZipped(false), silent(false), precision(4)
, subtreeOnly(false) {
for (auto it=f.mapOfTreeBuilders.begin(); it!=f.mapOfTreeBuilders.end(); ++it) {
if (!it->second->getName().empty()) {
builders.push_back(it->second);
}
}
}
const std::string& BenchmarkingTreeBuilder::getName() const {
return name;
}
const std::string& BenchmarkingTreeBuilder::getDescription() {
return description;
}
bool BenchmarkingTreeBuilder::isBenchmark() const {
return true;
}
bool BenchmarkingTreeBuilder::constructTree
( const std::string& distanceMatrixFilePath
, const std::string& newickTreeFilePath) {
bool result = (!builders.empty());
for (auto it=builders.begin(); it!=builders.end(); ++it) {
(*it)->setIsRooted(isRooted);
(*it)->setZippedOutput(isOutputToBeZipped);
(*it)->setSubtreeOnly(subtreeOnly);
result &= (*it)->constructTree(distanceMatrixFilePath,
newickTreeFilePath);
}
return result;
}
bool BenchmarkingTreeBuilder::setZippedOutput(bool zipIt) {
isOutputToBeZipped = zipIt;
return true;
}
void BenchmarkingTreeBuilder::beSilent() {
silent = true;
}
bool BenchmarkingTreeBuilder::setAppendFile(bool /*appendIt*/) {
return false;
}
bool BenchmarkingTreeBuilder::setSubtreeOnly(bool /*appendIt*/) {
return false;
}
bool BenchmarkingTreeBuilder::setPrecision(int precisionToUse) {
precision = precisionToUse;
return true;
}
bool BenchmarkingTreeBuilder::setIsRooted(bool rootIt) {
isRooted = rootIt;
return true;
}
namespace {
std::string formatPositiveNumber(double n, intptr_t w) {
std::stringstream s;
s.precision( (w>=2) ? (w-2) : 0);
s << n;
std::string t = s.str();
if (contains(t,"e")) {
return t;
}
if (1.0 <= n ) {
if ( t.length() < (size_t)w ) {
return std::string(w-t.length(), ' ') + t;
} else {
return t;
}
}
if (t.length() == (size_t)w || w < 2 ) {
return t;
}
if (t.length() < (size_t)w ) {
return t + std::string(w-t.length(), '0');
}
bool carry = (t[w] >= '5');
if (carry) {
intptr_t j = w - 1;
for (; t[j] == '9'; --j ) {
t[j] = '0';
}
++t[j];
}
return t.substr(0,w);
}
}
bool BenchmarkingTreeBuilder::constructTreeStringInMemory
( const StrVector& sequenceNames
, const double* distanceMatrix
, std::string& output_string) {
return false;
}
bool BenchmarkingTreeBuilder::constructTreeInMemory
( const StrVector &sequenceNames
, const double *distanceMatrix
, const std::string & newickTreeFilePath) {
bool ok = false;
#ifdef _OPENMP
int maxThreads = omp_get_max_threads();
#endif
size_t maxNameLen = 0;
for (auto it=builders.begin(); it!=builders.end(); ++it) {
auto builder_name = (*it)->getName();
if ( maxNameLen < builder_name.length() ) {
maxNameLen = builder_name.length();
}
}
std::string padding(maxNameLen, '\x20');
std::cout.width(0);
for (auto it=builders.begin(); it!=builders.end(); ++it) {
auto builder_name = (*it)->getName();
(*it)->beSilent();
(*it)->setPrecision(precision);
#ifdef _OPENMP
omp_set_num_threads(1);
#endif
double startTime = getRealTime();
bool succeeded = (*it)->constructTreeInMemory(sequenceNames,
distanceMatrix,
newickTreeFilePath);
double elapsed = getRealTime() - startTime;
if (succeeded) {
ok = true;
std::cout << (builder_name + padding).substr(0, maxNameLen);
std::cout << "\t" << formatPositiveNumber(elapsed,7);
#ifdef _OPENMP
for (int t=2; t<=maxThreads; ++t) {
std::cout.flush();
omp_set_num_threads(t);
startTime = getRealTime();
ok &= (*it)->constructTreeInMemory(sequenceNames,
distanceMatrix,
newickTreeFilePath);
elapsed = getRealTime() - startTime;
std::cout << "\t" << formatPositiveNumber(elapsed,7);
}
#endif
std::cout << std::endl;
}
}
return ok;
}
bool BenchmarkingTreeBuilder::constructTreeAndAppendToStream
( const StrVector& sequenceNames
, const double* distanceMatrix
, std::iostream& newickTreeFilePath) {
return false;
}
};