-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClusterFlags.cpp
454 lines (370 loc) · 8.98 KB
/
ClusterFlags.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "ClusterFlags.h"
#include "Matrix.h"
#include <stdexcept>
using namespace std;
using namespace std::tr1;
// Initialize the clusterflags
// n_timepoints: number of time points in this study
// samples: a vector indicating the number of samples at each time point
// will throw exceptions if samples does not contain enough items
ClusterFlags::ClusterFlags()
{
}
ClusterFlags::ClusterFlags(long n_timepoints, const std::vector<unsigned long> &samples)
: data(vector<vector<long> >(n_timepoints))
{
for(long i=0; i < n_timepoints; i++) {
data[i].resize(samples.at(i));
}
}
ClusterFlags::ClusterFlags(const std::vector<std::vector<long> > &one)
{
data = one;
}
ClusterFlags::~ClusterFlags(void)
{
}
long ClusterFlags::operator()(long row, long col) const {
return getFlag(row, col);
}
long & ClusterFlags::operator()(long row, long col) {
return data.at(row).at(col);
}
// Reload =
ClusterFlags &ClusterFlags::operator=(const ClusterFlags &clu)
{
data = clu.data;
return *this;
}
long ClusterFlags::rows() const {
return data.size();
}
long ClusterFlags::cols(int i) const {
if(data.size() == 0) {
return 0;
}
return data.at(i).size();
}
// Fill all the flags with the value
// Note that cluster index begins with 0
// Therefore the default value of -1 means flag not set
void ClusterFlags::fillFlags(long flag) {
for(vector<vector<long> >::iterator itor = data.begin(); itor != data.end(); itor++) {
for(vector<long>::iterator itor_time = itor->begin(); itor_time != itor->end(); itor_time++) {
*itor_time = flag;
}
}
}
// Get the flag for a specific time/sample
// notice that both time and sample is 0-based
// will throw out_of_bounds if so
long ClusterFlags::getFlag(long time, long sample) const {
return data.at(time).at(sample);
}
// Set the flag for a specific time/sample
// will throw out_of_bounds if so
void ClusterFlags::setFlag(long time, long sample, long flag) {
data.at(time).at(sample) = flag;
}
// Get the earliest time point for a specific flag (Real time point).
// If not found, return -1
long ClusterFlags::getEarliestTime(long flag) const {
for(long i = 0; i < data.size(); i++) {
for(long j = 0; j < data.at(i).size(); j++) {
if(data.at(i).at(j) == flag) {
return i+1;
}
}
}
return -1;
}
// Get the number of cluster flags in the set
// note that this is 1 larger than the largest number
long ClusterFlags::getMaxFlag() const {
long max = -1;
for(vector<vector<long> >::const_iterator itor = data.begin(); itor != data.end(); itor++) {
for(vector<long>::const_iterator itor_time = itor->begin(); itor_time != itor->end(); itor_time++) {
if(*itor_time > max) {
max = *itor_time;
}
}
}
return max;
}
std::vector<std::vector<long> > ClusterFlags::getTimeSampleByCluster(long flag) const {
vector<vector<long> > result;
for(vector<vector<long> >::const_iterator itor = data.begin(); itor != data.end(); itor++) {
vector<long> current_vec;
for(long i = 0; i < itor->size(); i++) {
if(itor->at(i) == flag) {
current_vec.push_back(i);
}
}
result.push_back(current_vec);
}
return result;
}
// If this 'flag' is not exist in clusterflags, a 0 matrix will return.
vector<vector<long> > ClusterFlags::onezerocluster(long flag) const
{
vector<vector<long> > result;
for(vector<vector<long> >::const_iterator itor = data.begin(); itor != data.end(); itor++) {
vector<long> current_vec;
for(long i = 0; i < itor->size(); i++) {
if(itor->at(i) == flag) {
current_vec.push_back(1);
}
else {
current_vec.push_back(0);
}
}
result.push_back(current_vec);
}
return result;
}
ClusterFlags ClusterFlags::sortz(const ClusterFlags &one, long n_timepoints, vector<long> NT){
ClusterFlags two;
return two;
}
void ClusterFlags::reflag()
{
vector<long> x;
vector<long> one;
vector<long> two;
long i,j, l=0, k=1;
long length = 0;
for(i=0; i<data.size(); i++)
for(j=0; j<data.at(i).size(); j++)
one.push_back(data.at(i).at(j));
x.push_back(one.at(0));
two.push_back(k);
for(i=1; i<one.size();i++)
{
if(one.at(i)==one.at(i-1)){
two.push_back(two.at(i-1));
} else {
while(l < k)
{
if(one.at(i)==x.at(l))
break;
l++;
}
if(l < k)
two.push_back(l+1);
else {
k++;
x.push_back(one.at(i));
two.push_back(k);
}
l = 0;
}
}
for(i=0; i<data.size(); i++){
for(j=0; j<data.at(i).size(); j++)
data.at(i).at(j) = two.at(length + j);
length += data.at(i).size();
}
}
//void reflag(std::vector<long> &one)
//{
// std::vector<long> x;
// std::vector<long> two;
// int i,j=0,k=1;
// x.push_back(one.at(0));
// two.push_back(k);
// for(i=1; i<one.size();i++)
// {
// if(one.at(i)==one.at(i-1)){
//
// two.push_back(two.at(i-1));
//
// } else {
//
// while(j < k)
// {
// if(one.at(i)==x.at(j))
// break;
// j++;
// }
// if(j < k)
// two.push_back(j+1);
// else {
// k++;
// x.push_back(one.at(i));
// two.push_back(k);
//
// }
// j = 0;
//
// }
// }
// one = two;
//}
// Summarize the data number for some cluster 'flag'.
// With a large flag, k should just be equal to 0, it is not need to output an error information.
long ClusterFlags::flagnum(long flag) const
{
long k = 0;
long i,j;
if((flag <= (this->getMaxFlag()))&&(flag>0))
{
for(i=0; i< data.size(); i++){
for(j=0; j<data.at(i).size(); j++){
if(data.at(i).at(j)==flag)
k++;
}
}
}
//else {
//
////cout<<"Flag is out of range!!!"<<endl;
//throw(exception("Flag is out of range!!!"));
//}
return k;
}
// Return the number of cluster indicator for some clusternumber(flag) in time point ntime.
// ntime is 0-based time point!
long ClusterFlags::tflagnum(long flag, long ntime) const{
long k=0;
long i;
if((flag <= (this->getMaxFlag()))&&(flag>0)&&(ntime<data.size()))
{
for(i=0; i< data.at(ntime).size(); i++){
if(data.at(ntime).at(i)==flag)
k++;
}
}
/*else {
throw(exception("Flag is out of range!!!"));
}*/
//cout<<data.size()<<endl;
return k;
}
// Useless!!!!!!
// Get the clusternumber in time stage 'ntime'.
// Will return the maximum cluster number even if some small cluster number missed.
std::vector<long> ClusterFlags::getTflag(long ntime)const{
//long clusternum = this->getMaxFlag();
std::vector<long> one(ntime);
one.at(0) = 1;
long i,j;
long mid = 1;
if(ntime<data.size()){
for(i=1; i<ntime; i++){
mid = data.at(i).at(0);
for(j=0; j<(data.at(i).size()-1); j++){
if(data.at(i).at(j+1)>data.at(i).at(j))
mid = data.at(i).at(j+1);
}
one.at(i) = mid;
if(one.at(i)<one.at(i-1)){
one.at(i) = one.at(i-1);
}
}
} else{
std::cout<<"ntime is out of range."<<std::endl;
}
return one;
}
// Get different cluster indicators in time "ntime"
std::vector<long> ClusterFlags::getTindicator(long ntime)const{
long n, i, j;
std::vector<long> one;
std::vector<long> two;
bool init = false;
if(ntime<data.size()){
n = data.at(ntime).size();
one.push_back(data.at(ntime).at(0));
for(i=1; i<n; i++){
for(j=0;j<one.size(); j++){
if(data.at(ntime).at(i)==one.at(j)){
init = true;
break;
}
}
if(!init){
one.push_back(data.at(ntime).at(i));
}
init = false;
}
}else
{
std::cout<<"ntime is out of range."<<std::endl;
}
return one;
}
// Justify whether flagx appears in the set of ntime cluster indicators.
bool ClusterFlags::flaginT(long flagx, long ntime) const{
long i;
bool init = false;
long n = data.at(ntime).size();
for(i=0; i<n; i++){
if(flagx == data.at(ntime).at(i)){
init = true;
break;
}
}
return init;
}
// Get different cluster indicators from time 1 to time "ntime"
std::vector<long> ClusterFlags::getTtoTindicator(long ntime)const{
long n, i, j, k;
std::vector<long> one;
bool init = false;
if(ntime<data.size()){
one.push_back(data.at(0).at(0));
for(k=0; k<=ntime; k++){
n = data.at(k).size();
for(i=1; i<n; i++){
for(j=0;j<one.size(); j++){
if(data.at(k).at(i)==one.at(j)){
init = true;
break;
}
}
if(!init){
one.push_back(data.at(k).at(i));
}
init = false;
}
}
}else
{
std::cout<<"ntime is out of range."<<std::endl;
}
return one;
}
// Return nonempty cluster number
long ClusterFlags::nonemptynum()const{
long n, i, j, k, s=1;
long m = data.size();
std::vector<long> one;
bool init = false;
one.push_back(data.at(0).at(0));
for(k=0; k<m; k++){
n = data.at(k).size();
for(i=0; i<n; i++){
for(j=0;j<one.size(); j++){
if(data.at(k).at(i)==one.at(j)){
init = true;
break;
}
}
if(!init){
one.push_back(data.at(k).at(i));
s++;
}
init = false;
}
}
return s;
}
std::ostream & operator<<(std::ostream &os, const ClusterFlags &clust) {
for(long i = 0; i < clust.rows(); i++) {
for(long j = 0; j < clust.cols(i); j++) {
os << clust(i,j) << '\t';
}
os << endl;
}
return os;
}