-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxonSet.cpp
More file actions
91 lines (77 loc) · 1.99 KB
/
TaxonSet.cpp
File metadata and controls
91 lines (77 loc) · 1.99 KB
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
#include <iostream>
#include <cstdlib>
using namespace std;
#include "BiologicalSequences.h"
#include "TaxonSet.h"
#include "Tree.h"
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// TaxonSet
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
TaxonSet::TaxonSet(const string* names, int ntaxa) {
Ntaxa = ntaxa;
taxlist = new string[ntaxa];
for (int i=0; i<ntaxa; i++) {
if (taxmap[names[i]]) {
cerr << "found several taxa with same name : " << names[i] << '\n';
exit(1);
}
taxlist[i] = names[i];
taxmap[names[i]] = i+1;
}
}
TaxonSet::~TaxonSet() {
delete taxlist;
}
TaxonSet::TaxonSet(const Tree* tree, const Link* subgroup) {
Ntaxa = tree->GetSize(subgroup);
taxlist = new string[Ntaxa];
if (!subgroup) {
subgroup = tree->GetRoot();
}
int i = 0;
RecursiveGetSubSet(subgroup,i);
}
void TaxonSet::RecursiveGetSubSet(const Link* from, int& i) {
if (from->isLeaf()) {
taxlist[i] = from->GetNode()->GetName();
taxmap[from->GetNode()->GetName()] = i+1;
i++;
}
else {
for (const Link* link=from->Next(); link!=from; link=link->Next()) {
RecursiveGetSubSet(link->Out(),i);
}
}
}
void TaxonSet::ToStream(ostream& os) {
os << Ntaxa << '\n';
for (int i=0; i<Ntaxa; i++) {
os << taxlist[i] << '\n';
}
}
int TaxonSet::GetTaxonIndexWithIncompleteName(string taxname) const {
int found = -1;
for (int i=0; i<Ntaxa; i++) {
if (taxlist[i].substr(0,taxname.length()) == taxname) {
if (found != -1) {
cerr << "error : taxon found twice : " << taxname << '\n';
exit(1);
}
found = i;
}
}
if (found == -1) {
for (int i=0; i<Ntaxa; i++) {
if (taxname.substr(0,taxlist[i].length()) == taxlist[i]) {
if (found != -1) {
cerr << "error : taxon found twice : " << taxname << '\n';
exit(1);
}
found = i;
}
}
}
return found;
}