Skip to content

Commit f23115f

Browse files
James BarbettiJames Barbetti
James Barbetti
authored and
James Barbetti
committed
Additional DecentTree example showing how to plug in
vector classes that *aren't* from Agner Fog's VCL Vector Class library. 1. Moved example matrix string to example_matrix.cpp (example_matrix is declared in example_matrix.h). 2. Added fakevector.h, which declares MyFloatVector and MyBoolVector classes, and a version of select() that takes a fake boolean vector, and two fake float vectors, by reference select(a,b,c) returns a float vector, r such that r[i] = a[i] !=0 ? b[i] : c[i]. 3. Added differentvector.cpp, which builds an example that uses MyFloatVector and MyBoolVector. 4. examples/CMakeLists.txt hauls in more stuff (for progress bars, and builds differentvector)
1 parent 9ca4a69 commit f23115f

File tree

7 files changed

+125
-46
lines changed

7 files changed

+125
-46
lines changed

example/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
add_executable(hod_tree
2-
headeronlydecenttree.cpp)
2+
headeronlydecenttree.cpp example_matrix.cpp
3+
../utils/progress.cpp ../utils/progress.h
4+
../utils/operatingsystem.cpp ../utils/operatingsystem.h)
35

46
add_executable(vectordecenttree
5-
vectordecenttree.cpp ../utils/progress.cpp)
7+
vectordecenttree.cpp example_matrix.cpp ../utils/progress.cpp
8+
../utils/progress.cpp ../utils/progress.h
9+
../utils/operatingsystem.cpp ../utils/operatingsystem.h)
10+
11+
add_executable(differentvector
12+
differentvector.cpp example_matrix.cpp
13+
../utils/progress.cpp ../utils/progress.h
14+
../utils/operatingsystem.cpp ../utils/operatingsystem.h)

example/differentvector.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#define USE_PROGRESS_DISPLAY 1 //Even though there's not much point on an input this small.
2+
#define USE_VECTORCLASS_LIBRARY 1 //For an input this small, vectorisation doesn't matter.
3+
#define DECENT_TREE 1 //Tells shared headers that decenttree, rather than
4+
//iqtree2, is being built.
5+
6+
#include "example_matrix.h"
7+
#include "fakevector.h"
8+
#include <rapidnj.h>
9+
10+
typedef StartTree::VectorizedBoundingMatrix
11+
<double, MyFloatVector, MyBoolVector>
12+
MyVBM;
13+
14+
int main(int argc, char* argv[]) {
15+
16+
progress_display::setProgressDisplay(true);
17+
18+
MyVBM r;
19+
std::istringstream input_file(example_matrix);
20+
loadDistanceMatrixFromOpenFile(input_file, false, r);
21+
r.constructTree();
22+
23+
int precision = 6;
24+
std::string newickTreeFilePath = "STDOUT";
25+
r.writeTreeFile(precision, newickTreeFilePath);
26+
}

example/example_matrix.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const char* example_matrix = "17\n"
2+
"LngfishAu 0.0000000 0.4394262 0.4253346 0.4739334 0.6469595 0.8072464 0.7697307 0.7867055 0.7850803 0.6959920 0.6336127 0.6997504 0.6793033 0.7120045 0.7344446 0.7318557 0.6820929\n"
3+
"LngfishSA 0.4394262 0.0000000 0.3370420 0.6028511 0.7361390 0.8116547 0.7978951 0.7651678 0.6988087 0.7598439 0.7003997 0.7643007 0.7378452 0.7195253 0.8065221 0.7439359 0.6178323\n"
4+
"LngfishAf 0.4253346 0.3370420 0.0000000 0.5921847 0.7020407 0.8573580 0.9137406 0.8639154 0.7878422 0.8294442 0.7505365 0.7882964 0.7551778 0.7706974 0.8561868 0.7818997 0.6850401\n"
5+
"Frog 0.4739334 0.6028511 0.5921847 0.0000000 0.6268957 0.8150864 0.7705955 0.8145647 0.7573321 0.7166360 0.6626602 0.6762896 0.6809187 0.7005612 0.7136883 0.6974592 0.6449470\n"
6+
"Turtle 0.6469595 0.7361390 0.7020407 0.6268957 0.0000000 0.5416702 0.5841215 0.5399763 0.4849490 0.5861821 0.5383720 0.5458270 0.5612534 0.5632785 0.6088667 0.5946969 0.5778748\n"
7+
"Sphenodon 0.8072464 0.8116547 0.8573580 0.8150864 0.5416702 0.0000000 0.6723304 0.6249144 0.5897511 0.6946471 0.6787380 0.6565584 0.6772154 0.6833316 0.7476896 0.7240949 0.6518428\n"
8+
"Lizard 0.7697307 0.7978951 0.9137406 0.7705955 0.5841215 0.6723304 0.0000000 0.7160903 0.6480652 0.7386573 0.7042574 0.7513723 0.7675040 0.6849374 0.7926459 0.7473559 0.6709445\n"
9+
"Crocodile 0.7867055 0.7651678 0.8639154 0.8145647 0.5399763 0.6249144 0.7160903 0.0000000 0.4906657 0.7432287 0.7493887 0.7488493 0.8057593 0.8317762 0.8413625 0.7521970 0.7769550\n"
10+
"Bird 0.7850803 0.6988087 0.7878422 0.7573321 0.4849490 0.5897511 0.6480652 0.4906657 0.0000000 0.6755495 0.6691967 0.6860033 0.6905877 0.7565543 0.7504430 0.6842440 0.6705485\n"
11+
"Human 0.6959920 0.7598439 0.8294442 0.7166360 0.5861821 0.6946471 0.7386573 0.7432287 0.6755495 0.0000000 0.2750272 0.2814737 0.2961226 0.3590137 0.3757584 0.4477864 0.4220981\n"
12+
"Seal 0.6336127 0.7003997 0.7505365 0.6626602 0.5383720 0.6787380 0.7042574 0.7493887 0.6691967 0.2750272 0.0000000 0.1931946 0.2118470 0.2817599 0.3180795 0.3787176 0.3496224\n"
13+
"Cow 0.6997504 0.7643007 0.7882964 0.6762896 0.5458270 0.6565584 0.7513723 0.7488493 0.6860033 0.2814737 0.1931946 0.0000000 0.1754464 0.3199376 0.3320971 0.4013694 0.3449257\n"
14+
"Whale 0.6793033 0.7378452 0.7551778 0.6809187 0.5612534 0.6772154 0.7675040 0.8057593 0.6905877 0.2961226 0.2118470 0.1754464 0.0000000 0.3251485 0.3558385 0.3925798 0.3595924\n"
15+
"Mouse 0.7120045 0.7195253 0.7706974 0.7005612 0.5632785 0.6833316 0.6849374 0.8317762 0.7565543 0.3590137 0.2817599 0.3199376 0.3251485 0.0000000 0.1447365 0.4099219 0.3487804\n"
16+
"Rat 0.7344446 0.8065221 0.8561868 0.7136883 0.6088667 0.7476896 0.7926459 0.8413625 0.7504430 0.3757584 0.3180795 0.3320971 0.3558385 0.1447365 0.0000000 0.4419960 0.4050277\n"
17+
"Platypus 0.7318557 0.7439359 0.7818997 0.6974592 0.5946969 0.7240949 0.7473559 0.7521970 0.6842440 0.4477864 0.3787176 0.4013694 0.3925798 0.4099219 0.4419960 0.0000000 0.3202751\n"
18+
"Opossum 0.6820929 0.6178323 0.6850401 0.6449470 0.5778748 0.6518428 0.6709445 0.7769550 0.6705485 0.4220981 0.3496224 0.3449257 0.3595924 0.3487804 0.4050277 0.3202751 0.0000000\n";

example/example_matrix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern const char* example_matrix;

example/fakevector.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdint.h>
2+
3+
template<class T, int n> class FakeVector {
4+
public:
5+
T data[n]; //so that operator < can see it
6+
FakeVector() {
7+
for (int i=0; i<n; ++i) {
8+
data[i]=0;
9+
}
10+
}
11+
FakeVector(const FakeVector& r) = default;
12+
explicit FakeVector(T v) {
13+
for (int i=0; i<n; ++i) {
14+
data[i]=v;
15+
}
16+
}
17+
FakeVector& operator= (const FakeVector& rhs) = default;
18+
19+
int size() { return n; }
20+
FakeVector& load(const T* p) {
21+
for (int i=0; i<n; ++i) {
22+
data[i]=p[i];
23+
}
24+
return *this;
25+
}
26+
FakeVector& store(T* p) {
27+
for (int i=0; i<n; ++i) {
28+
p[i]=data[i];
29+
}
30+
return *this;
31+
}
32+
33+
FakeVector operator-(const FakeVector& rhs) {
34+
FakeVector result;
35+
for (int i=0; i<n; ++i) {
36+
result.data[i] = data[i] - rhs.data[i];
37+
}
38+
return result;
39+
}
40+
};
41+
42+
typedef FakeVector<double, 4> MyFloatVector;
43+
typedef FakeVector<uint64_t, 4> MyBoolVector;
44+
45+
template <class T, int s>
46+
FakeVector<uint64_t,s> operator<
47+
( const FakeVector<T,s>& left, const FakeVector<T,s>& right ) {
48+
FakeVector<uint64_t,s> result;
49+
for (int i=0; i<s; ++i) {
50+
result.data[i] = ( left.data[i] < right.data[i] ) ? -1 : 0;
51+
}
52+
return result;
53+
}
54+
55+
template <class T, int s, class B>
56+
FakeVector<T,s> select ( const FakeVector<B,s>& isLess,
57+
const FakeVector<T,s>& left,
58+
const FakeVector<T,s>& right ) {
59+
FakeVector<T,s> result;
60+
for (int i=0; i<s; ++i) {
61+
result.data[i] = (isLess.data[i]==-1)
62+
? left.data[i] : right.data[i];
63+
}
64+
return result;
65+
}

example/headeronlydecenttree.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,12 @@
1313
#include <distancematrix.h> //for loadDistanceMatrixFromOpenFile
1414
#include <rapidnj.h> //for RapidNJ
1515
#include <sstream> //for std::istringstream
16+
#include "example_matrix.h" //for example_matrix string
1617

1718
void example_1() {
1819
StartTree::RapidNJ r;
1920

20-
const char* input_string =
21-
"17\n"
22-
"LngfishAu 0.0000000 0.4394262 0.4253346 0.4739334 0.6469595 0.8072464 0.7697307 0.7867055 0.7850803 0.6959920 0.6336127 0.6997504 0.6793033 0.7120045 0.7344446 0.7318557 0.6820929\n"
23-
"LngfishSA 0.4394262 0.0000000 0.3370420 0.6028511 0.7361390 0.8116547 0.7978951 0.7651678 0.6988087 0.7598439 0.7003997 0.7643007 0.7378452 0.7195253 0.8065221 0.7439359 0.6178323\n"
24-
"LngfishAf 0.4253346 0.3370420 0.0000000 0.5921847 0.7020407 0.8573580 0.9137406 0.8639154 0.7878422 0.8294442 0.7505365 0.7882964 0.7551778 0.7706974 0.8561868 0.7818997 0.6850401\n"
25-
"Frog 0.4739334 0.6028511 0.5921847 0.0000000 0.6268957 0.8150864 0.7705955 0.8145647 0.7573321 0.7166360 0.6626602 0.6762896 0.6809187 0.7005612 0.7136883 0.6974592 0.6449470\n"
26-
"Turtle 0.6469595 0.7361390 0.7020407 0.6268957 0.0000000 0.5416702 0.5841215 0.5399763 0.4849490 0.5861821 0.5383720 0.5458270 0.5612534 0.5632785 0.6088667 0.5946969 0.5778748\n"
27-
"Sphenodon 0.8072464 0.8116547 0.8573580 0.8150864 0.5416702 0.0000000 0.6723304 0.6249144 0.5897511 0.6946471 0.6787380 0.6565584 0.6772154 0.6833316 0.7476896 0.7240949 0.6518428\n"
28-
"Lizard 0.7697307 0.7978951 0.9137406 0.7705955 0.5841215 0.6723304 0.0000000 0.7160903 0.6480652 0.7386573 0.7042574 0.7513723 0.7675040 0.6849374 0.7926459 0.7473559 0.6709445\n"
29-
"Crocodile 0.7867055 0.7651678 0.8639154 0.8145647 0.5399763 0.6249144 0.7160903 0.0000000 0.4906657 0.7432287 0.7493887 0.7488493 0.8057593 0.8317762 0.8413625 0.7521970 0.7769550\n"
30-
"Bird 0.7850803 0.6988087 0.7878422 0.7573321 0.4849490 0.5897511 0.6480652 0.4906657 0.0000000 0.6755495 0.6691967 0.6860033 0.6905877 0.7565543 0.7504430 0.6842440 0.6705485\n"
31-
"Human 0.6959920 0.7598439 0.8294442 0.7166360 0.5861821 0.6946471 0.7386573 0.7432287 0.6755495 0.0000000 0.2750272 0.2814737 0.2961226 0.3590137 0.3757584 0.4477864 0.4220981\n"
32-
"Seal 0.6336127 0.7003997 0.7505365 0.6626602 0.5383720 0.6787380 0.7042574 0.7493887 0.6691967 0.2750272 0.0000000 0.1931946 0.2118470 0.2817599 0.3180795 0.3787176 0.3496224\n"
33-
"Cow 0.6997504 0.7643007 0.7882964 0.6762896 0.5458270 0.6565584 0.7513723 0.7488493 0.6860033 0.2814737 0.1931946 0.0000000 0.1754464 0.3199376 0.3320971 0.4013694 0.3449257\n"
34-
"Whale 0.6793033 0.7378452 0.7551778 0.6809187 0.5612534 0.6772154 0.7675040 0.8057593 0.6905877 0.2961226 0.2118470 0.1754464 0.0000000 0.3251485 0.3558385 0.3925798 0.3595924\n"
35-
"Mouse 0.7120045 0.7195253 0.7706974 0.7005612 0.5632785 0.6833316 0.6849374 0.8317762 0.7565543 0.3590137 0.2817599 0.3199376 0.3251485 0.0000000 0.1447365 0.4099219 0.3487804\n"
36-
"Rat 0.7344446 0.8065221 0.8561868 0.7136883 0.6088667 0.7476896 0.7926459 0.8413625 0.7504430 0.3757584 0.3180795 0.3320971 0.3558385 0.1447365 0.0000000 0.4419960 0.4050277\n"
37-
"Platypus 0.7318557 0.7439359 0.7818997 0.6974592 0.5946969 0.7240949 0.7473559 0.7521970 0.6842440 0.4477864 0.3787176 0.4013694 0.3925798 0.4099219 0.4419960 0.0000000 0.3202751\n"
38-
"Opossum 0.6820929 0.6178323 0.6850401 0.6449470 0.5778748 0.6518428 0.6709445 0.7769550 0.6705485 0.4220981 0.3496224 0.3449257 0.3595924 0.3487804 0.4050277 0.3202751 0.0000000\n";
39-
40-
std::istringstream input_file(input_string);
41-
21+
std::istringstream input_file(example_matrix);
4222
loadDistanceMatrixFromOpenFile(input_file, false, r);
4323
r.constructTree();
4424

example/vectordecenttree.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <distancematrix.h> //for loadDistanceMatrixFromOpenFile
1616
#include <rapidnj.h> //for RapidNJ
1717
#include <sstream> //for std::istringstream
18+
#include "example_matrix.h" //for example_matrix string
1819

1920
template <class T> void testMatrix
2021
(const std::string &infile_name, bool silent, T& r) {
@@ -23,32 +24,11 @@ template <class T> void testMatrix
2324
}
2425

2526
void example_2() {
26-
27-
const char* input_string =
28-
"17\n"
29-
"LngfishAu 0.0000000 0.4394262 0.4253346 0.4739334 0.6469595 0.8072464 0.7697307 0.7867055 0.7850803 0.6959920 0.6336127 0.6997504 0.6793033 0.7120045 0.7344446 0.7318557 0.6820929\n"
30-
"LngfishSA 0.4394262 0.0000000 0.3370420 0.6028511 0.7361390 0.8116547 0.7978951 0.7651678 0.6988087 0.7598439 0.7003997 0.7643007 0.7378452 0.7195253 0.8065221 0.7439359 0.6178323\n"
31-
"LngfishAf 0.4253346 0.3370420 0.0000000 0.5921847 0.7020407 0.8573580 0.9137406 0.8639154 0.7878422 0.8294442 0.7505365 0.7882964 0.7551778 0.7706974 0.8561868 0.7818997 0.6850401\n"
32-
"Frog 0.4739334 0.6028511 0.5921847 0.0000000 0.6268957 0.8150864 0.7705955 0.8145647 0.7573321 0.7166360 0.6626602 0.6762896 0.6809187 0.7005612 0.7136883 0.6974592 0.6449470\n"
33-
"Turtle 0.6469595 0.7361390 0.7020407 0.6268957 0.0000000 0.5416702 0.5841215 0.5399763 0.4849490 0.5861821 0.5383720 0.5458270 0.5612534 0.5632785 0.6088667 0.5946969 0.5778748\n"
34-
"Sphenodon 0.8072464 0.8116547 0.8573580 0.8150864 0.5416702 0.0000000 0.6723304 0.6249144 0.5897511 0.6946471 0.6787380 0.6565584 0.6772154 0.6833316 0.7476896 0.7240949 0.6518428\n"
35-
"Lizard 0.7697307 0.7978951 0.9137406 0.7705955 0.5841215 0.6723304 0.0000000 0.7160903 0.6480652 0.7386573 0.7042574 0.7513723 0.7675040 0.6849374 0.7926459 0.7473559 0.6709445\n"
36-
"Crocodile 0.7867055 0.7651678 0.8639154 0.8145647 0.5399763 0.6249144 0.7160903 0.0000000 0.4906657 0.7432287 0.7493887 0.7488493 0.8057593 0.8317762 0.8413625 0.7521970 0.7769550\n"
37-
"Bird 0.7850803 0.6988087 0.7878422 0.7573321 0.4849490 0.5897511 0.6480652 0.4906657 0.0000000 0.6755495 0.6691967 0.6860033 0.6905877 0.7565543 0.7504430 0.6842440 0.6705485\n"
38-
"Human 0.6959920 0.7598439 0.8294442 0.7166360 0.5861821 0.6946471 0.7386573 0.7432287 0.6755495 0.0000000 0.2750272 0.2814737 0.2961226 0.3590137 0.3757584 0.4477864 0.4220981\n"
39-
"Seal 0.6336127 0.7003997 0.7505365 0.6626602 0.5383720 0.6787380 0.7042574 0.7493887 0.6691967 0.2750272 0.0000000 0.1931946 0.2118470 0.2817599 0.3180795 0.3787176 0.3496224\n"
40-
"Cow 0.6997504 0.7643007 0.7882964 0.6762896 0.5458270 0.6565584 0.7513723 0.7488493 0.6860033 0.2814737 0.1931946 0.0000000 0.1754464 0.3199376 0.3320971 0.4013694 0.3449257\n"
41-
"Whale 0.6793033 0.7378452 0.7551778 0.6809187 0.5612534 0.6772154 0.7675040 0.8057593 0.6905877 0.2961226 0.2118470 0.1754464 0.0000000 0.3251485 0.3558385 0.3925798 0.3595924\n"
42-
"Mouse 0.7120045 0.7195253 0.7706974 0.7005612 0.5632785 0.6833316 0.6849374 0.8317762 0.7565543 0.3590137 0.2817599 0.3199376 0.3251485 0.0000000 0.1447365 0.4099219 0.3487804\n"
43-
"Rat 0.7344446 0.8065221 0.8561868 0.7136883 0.6088667 0.7476896 0.7926459 0.8413625 0.7504430 0.3757584 0.3180795 0.3320971 0.3558385 0.1447365 0.0000000 0.4419960 0.4050277\n"
44-
"Platypus 0.7318557 0.7439359 0.7818997 0.6974592 0.5946969 0.7240949 0.7473559 0.7521970 0.6842440 0.4477864 0.3787176 0.4013694 0.3925798 0.4099219 0.4419960 0.0000000 0.3202751\n"
45-
"Opossum 0.6820929 0.6178323 0.6850401 0.6449470 0.5778748 0.6518428 0.6709445 0.7769550 0.6705485 0.4220981 0.3496224 0.3449257 0.3595924 0.3487804 0.4050277 0.3202751 0.0000000\n";
46-
4727
const std::string input_file("./example.dist");
4828

4929
std::ofstream dist;
5030
dist.open(input_file.c_str(), std::ios_base::out);
51-
dist << input_string;
31+
dist << example_matrix;
5232
dist.close();
5333

5434
StartTree::NJMatrix<NJFloat> r1;

0 commit comments

Comments
 (0)