-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarttree.cpp
163 lines (142 loc) · 5.25 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
// 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
namespace StartTree {
extern void addBioNJ2009TreeBuilders(Factory& f);
extern void addBioNJ2020TreeBuilders(Factory& f);
Factory::Factory() {
}
Factory::~Factory() {
for (auto it=mapOfTreeBuilders.begin(); it!=mapOfTreeBuilders.end(); ++it) {
delete it->second;
}
mapOfTreeBuilders.clear();
}
size_t Factory::getBuilderCount() {
return mapOfTreeBuilders.size();
}
Factory& Factory::getInstance() {
static Factory instance;
if (instance.getBuilderCount()==0) {
addBioNJ2009TreeBuilders(instance);
addBioNJ2020TreeBuilders(instance);
BuilderInterface *bench = new BenchmarkingTreeBuilder(instance, "BENCHMARK", "Benchmark");
instance.addBuilder(bench->getName(), bench);
}
return instance;
}
void Factory::addBuilder(const std::string& name, BuilderInterface* builder) {
mapOfTreeBuilders [ name ] = builder;
}
BuilderInterface* Factory::getBuilder(const std::string& name) {
auto found = mapOfTreeBuilders.find(name);
return ( found == mapOfTreeBuilders.end())
? nullptr
: found->second;
}
BuilderInterface* Factory::getBuilder(const char* name) {
std::string s(name);
return getBuilder(s);
}
std::string Factory::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();
}
void Factory::advertiseTreeBuilder(BuilderInterface* builder) {
std::string name = builder->getName();
addBuilder( name, builder );
}
void Factory::setNameOfDefaultTreeBuilder(const char* name) {
nameOfDefaultTreeBuilder = name;
}
const std::string& Factory::getNameOfDefaultTreeBuilder() {
return getInstance().nameOfDefaultTreeBuilder;
}
BuilderInterface* Factory::getTreeBuilderByName(const std::string& name) {
return getInstance().getBuilder(name);
}
BenchmarkingTreeBuilder::BenchmarkingTreeBuilder(Factory& f, const char* nameToUse, const char *descriptionToGive)
: name(nameToUse), description(descriptionToGive)
, isOutputToBeZipped(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() {
return name;
}
const std::string& BenchmarkingTreeBuilder::getDescription() {
return description;
}
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)->setZippedOutput(isOutputToBeZipped);
result &= (*it)->constructTree(distanceMatrixFilePath, newickTreeFilePath);
}
return result;
}
void BenchmarkingTreeBuilder::setZippedOutput(bool zipIt) {
isOutputToBeZipped = zipIt;
}
bool BenchmarkingTreeBuilder::constructTreeInMemory
( const std::vector<std::string> &sequenceNames
, double *distanceMatrix
, const std::string & newickTreeFilePath) {
bool ok = false;
#ifdef _OPENMP
int maxThreads = omp_get_max_threads();
#endif
for (auto it=builders.begin(); it!=builders.end(); ++it) {
double startTime = getRealTime();
#ifdef _OPENMP
omp_set_num_threads(1);
#endif
(*it)->beSilent();
bool succeeded = (*it)->constructTreeInMemory(sequenceNames, distanceMatrix, newickTreeFilePath);
double elapsed = getRealTime() - startTime;
if (succeeded) {
ok = true;
std::cout.precision(6);
std::cout << (*it)->getName() << " \t" << elapsed;
#ifdef _OPENMP
for (int t=2; t<=maxThreads; ++t) {
omp_set_num_threads(t);
startTime = getRealTime();
ok &= (*it)->constructTreeInMemory(sequenceNames, distanceMatrix, newickTreeFilePath);
elapsed = getRealTime() - startTime;
std::cout << "\t" << (elapsed);
}
#endif
std::cout << std::endl;
}
}
return true;
}
};