-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionalClass.C
275 lines (192 loc) · 5.3 KB
/
FunctionalClass.C
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
/****************************************************************
FunctionalClass.C
william.majoros@duke.edu
This is open-source software, governed by the Gnu General Public License (GPL) version 3
(see www.opensource.org).
****************************************************************/
#include <iostream>
#include "FunctionalClass.H"
#include "BOOM/Exceptions.H"
using namespace std;
using namespace BOOM;
// static members:
Vector<String> FunctionalClass::names;
Vector<char> FunctionalClass::labels;
Map<String,int> FunctionalClass::nameToID;
Vector<RateMatrix*> FunctionalClass::matrices;
Vector<Phylogeny*> FunctionalClass::phylogenies;
const FunctionalClass FunctionalClass::NO_CLASS(-1);
FunctionalClass FunctionalClass::bgFC(-1);
//Vector<bool> FunctionalClass::background;
Vector<ForegroundBackground> FunctionalClass::foregroundBackground;
Array1D<int> FunctionalClass::labelToClassID(256);
Vector<Array1D<double> > FunctionalClass::eqFreqs;
Vector<FunctionalElementType> FunctionalClass::elementTypes;
Vector<Strand> FunctionalClass::strands;
void FunctionalClass::resetAll()
{
names.clear();
labels.clear();
nameToID.clear();
Vector<RateMatrix*>::iterator rmCur=matrices.begin(), rmEnd=matrices.end();
for(; rmCur!=rmEnd ; ++rmCur) delete *rmCur;
matrices.clear();
//Vector<Phylogeny*>::iterator pCur=phylogenies.begin(),
// pEnd=phylogenies.end();
//for(; pCur!=pEnd ; ++pCur) {cout<<"ra6.1 "<<*pCur<<endl;delete *pCur;}
phylogenies.clear();
foregroundBackground.clear();
eqFreqs.clear();
elementTypes.clear();
}
FunctionalClass::FunctionalClass()
: classID(-1)
{
// ctor
}
FunctionalClass::FunctionalClass(int classID)
: classID(classID)
{
// ctor
}
bool FunctionalClass::isValid() const
{
return classID>=0 && fg_or_bg()!=FB_NEITHER;
}
FunctionalClass::FunctionalClass(const FunctionalClass &other)
: classID(other.classID)
{
// copy ctor
}
const String &FunctionalClass::getName() const
{
return names[classID];
}
char FunctionalClass::getLabel() const
{
return labels[classID];
}
RateMatrix *FunctionalClass::getMatrix() const
{
return matrices[classID];
}
Phylogeny *FunctionalClass::getPhylogeny() const
{
return phylogenies[classID];
}
bool FunctionalClass::isBackground() const
{
return foregroundBackground[classID]==BACKGROUND;
}
FunctionalClass FunctionalClass::registerClass(const String &className,
char label,
FunctionalElementType T,
ForegroundBackground fb,
RateMatrix *Q,
Phylogeny *tree)
{
int id=names.size();
nameToID[className]=id;
names.push_back(className);
elementTypes.push_back(T);
labels.push_back(label);
labelToClassID[label]=id;
matrices.push_back(Q);
phylogenies.push_back(tree);
foregroundBackground.push_back(fb);
strands.push_back(PLUS_STRAND);
if(fb==BACKGROUND) bgFC=FunctionalClass(id);
// Get equilibrium frequencies
Array1D<double> eq;
if(Q) {
SubstitutionMatrix *Pt=Q->instantiate(1.0);
Pt->getEqFreqs(eq);
delete Pt;
}
eqFreqs.push_back(eq);
return FunctionalClass(id);
}
FunctionalElementType FunctionalClass::getElementType() const
{
return elementTypes[classID];
}
FunctionalClass FunctionalClass::classFromLabel(char label)
{
return labelToClassID[label];
}
int FunctionalClass::getClassID() const
{
return classID;
}
int FunctionalClass::numClasses()
{
return names.size();
}
FunctionalClass FunctionalClass::getClassByName(const String &className)
{
if(nameToID.isDefined(className))
return FunctionalClass(nameToID[className]);
throw className+" undefined in FunctionClass::getClassByName()";
}
ostream &operator<<(ostream &os,const FunctionalClass &c)
{
if(c.getClassID()>=0) os<<c.getName();
else os<<"NO_CLASS";
return os;
}
FunctionalClass FunctionalClass::getBackground()
{
return bgFC;
}
ForegroundBackground FunctionalClass::fg_or_bg() const
{
return foregroundBackground[classID];
}
GainLossType FunctionalClass::classifyGainLoss(FunctionalClass parentFC,
FunctionalClass childFC)
{
ForegroundBackground childFB=childFC.fg_or_bg();
switch(parentFC.fg_or_bg()) {
case FOREGROUND:
switch(childFB) {
case FOREGROUND: return GLT_RETENTION;
case BACKGROUND: return GLT_LOSS;
}
break;
case BACKGROUND:
switch(childFB) {
case FOREGROUND: return GLT_GAIN;
case BACKGROUND: return GLT_VOID;
}
break;
}
return GLT_VOID; // ### 7/28/09
}
Array1D<double> &FunctionalClass::getEqFreqs()
{
return eqFreqs[classID];
}
FunctionalClass pickForeground(FunctionalClass a,
FunctionalClass b)
{
if(!a.isBackground()) return a;
if(!b.isBackground()) return b;
return FunctionalClass::getBackground();
}
bool FunctionalClass::beginsElement()
{
if(!isValid()) return false;
FunctionalElementType fet=getElementType();
if(!fet.isValid()) return false;
Vector<FunctionalClass> &classes=fet.getClasses();
return classes.size()>0 && classes[0]==*this;
}
bool FunctionalClass::endsElement()
{
if(!isValid()) return false;
FunctionalElementType fet=getElementType();
if(!fet.isValid()) return false;
Vector<FunctionalClass> &classes=getElementType().getClasses();
int n=classes.size();
return n>0 && classes[n-1]==*this;
}