Repository navigation
Expand file tree
/
Copy pathDataMethods.cpp
More file actions
executable file
·397 lines (322 loc) · 10.6 KB
/
Copy pathDataMethods.cpp
File metadata and controls
executable file
·397 lines (322 loc) · 10.6 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
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
// This file contains graph theoretical subroutines for FastIndep, a C++ program
// for finding maximal independent sets given an input matrix and a threshold.
// Methods with "rand" in the title are randomized greedy algorithms
// This program may be freely distributed or modified, provided these
// comment lines are retained. In particular, there are no restrictions
// on non-commercial use. If this code (either in the original form or
// modified) is used in academic publications, a citation would be appreciated.
// Please read the accompanying ReadMe.txt file for instructions on how to compile
// and run the program.
// Author: Joseph Abraham May 2013
#include <iostream>
#include <fstream>
#include <ostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include <limits>
#include "./headers.h"
using namespace std;
void Data::FindSeparatedVec(vector<unsigned>& ReturnVec)
{
vector<unsigned> interVec;
vector<unsigned>::iterator endit,vecit;
vector<unsigned> candVec,tempVec,next_vec;
unsigned i;
unsigned nextSNP = 0;
unsigned startSNP = 0;
// initialization
candVec.clear();
ReturnVec.clear();
tempVec.clear();
coveredVec.clear();
interVec.clear();
// Finding a good starting SNP
if(data_rand){
startSNP = FindstartSNP_rand();
} else{
startSNP = FindstartSNP();
}
if (startSNP == 0){
std::cout << startSNP << " Bad Return " << endl;
std::cout.flush();
throw exception();
}
tempVec.push_back(startSNP);
candVec = SNP_vector[startSNP -1]->seps;
// experimental code, remove singletons at start
if(singletons.size() > 0){
interVec.resize(candVec.size() );
endit = set_difference(candVec.begin(), candVec.end(), singletons.begin(), \
singletons.end(), interVec.begin() );
candVec.resize(unsigned(endit -interVec.begin()));
copy(interVec.begin(), endit, candVec.begin() );
sort(candVec.begin(), candVec.end() );
}
AugmentcoveredVec(startSNP);
if(singletons.size() > 0){
copy(singletons.begin(), singletons.end(), back_inserter(coveredVec) );
sort(coveredVec.begin(), coveredVec.end() );
copy(singletons.begin(), singletons.end(), back_inserter(tempVec) );
}
if(candVec.size() == 0){
if(singletons.size() == 0){
std::cout << "Should not happen ... Check entries in parameters.txt !" << endl;
std::cout << "Started from " << startSNP << endl;
std::cout.flush();
throw exception();
}else{
ReturnVec.resize(tempVec.size() );
copy(tempVec.begin(), tempVec.end(),ReturnVec.begin() );
return;
}
}
// Main cycle of program
do {
if(data_rand){
nextSNP = FindnextSNP_rand(candVec);
}else{
nextSNP = FindnextSNP(candVec);
}
if(nextSNP == 0){
ReturnVec.resize(tempVec.size() );
copy(tempVec.begin(),tempVec.end(),ReturnVec.begin() );
return;
}
next_vec = SNP_vector[nextSNP -1]->seps;
sort(next_vec.begin(), next_vec.end() );
sort(tempVec.begin(), tempVec.end() );
interVec.resize(tempVec.size() );
endit = set_intersection(next_vec.begin(), next_vec.end(),
tempVec.begin(), tempVec.end(), interVec.begin() );
if(unsigned(endit -interVec.begin() ) != tempVec.size() ){
for(i = 0;i != tempVec.size();i++){
vecit = find(next_vec.begin(), next_vec.end(), tempVec[i]);
if(vecit == next_vec.end() ) {
std::cout << " Inconsistent SNP Added " << endl;
std::cout << SNP_vector[nextSNP-1]->SNPname \
<< " and " << SNP_vector[tempVec[i]-1]->SNPname << " correlated " << endl;
std::cout.flush();
std::cout << " Program Terminating " << endl;
throw exception();
}
}
}
AugmentcoveredVec(nextSNP);
tempVec.push_back(nextSNP);
candVec.erase(find(candVec.begin(),candVec.end(), nextSNP));
sort(candVec.begin(), candVec.end() );
sort(next_vec.begin(), next_vec.end() );
interVec.resize(candVec.size() );
endit = set_intersection(candVec.begin(),candVec.end(), next_vec.begin(), next_vec.end(),
interVec.begin() );
if(unsigned(endit -interVec.begin())== 0){
ReturnVec.resize(tempVec.size() );
copy(tempVec.begin(), tempVec.end(),ReturnVec.begin() );
return;
}
candVec.resize(unsigned(endit -interVec.begin()));
copy(interVec.begin(), endit, candVec.begin() );
}while(tempVec.size() < SNP_vector.size()); // dummy condition
return;
}
unsigned Data::FindstartSNP()
{
// Greedy Heuristic to find starting SNP
unsigned nextSNP = 0;
unsigned i;
vector<unsigned>::iterator vecit;
bool is_connector,is_singleton;
double num_choices,largest_so_far;
largest_so_far = 0;
num_choices = 0;
for(i=0;i != SNP_vector.size();i++){
is_connector = false;
if(all_connectors.size() > 0) {
vecit = find(all_connectors.begin(), all_connectors.end(), (i+1) );
if(vecit != all_connectors.end() ) is_connector = true;
}
if(is_connector) continue;// do not include connectors among possible starts
is_singleton = false;
if(singletons.size() > 0){
vecit = find(singletons.begin(), singletons.end(), (i+1) );
if(vecit != singletons.end() ) is_singleton = true;
}
if(is_singleton) continue; // do not include singletons among possible starts
num_choices = SNP_vector[i]->seps.size();
if(num_choices > largest_so_far){
largest_so_far = num_choices;
nextSNP = (i+1);
}
}
return nextSNP;
}
unsigned Data::FindnextSNP(const vector<unsigned>& CandVec)
{
// Greedy Heuristic to find next SNP
unsigned nextSNP = 0;
unsigned i;
double num_choices,largest_so_far;
largest_so_far = 0;
num_choices = 0;
for(i=0;i != CandVec.size();i++){
num_choices = SNP_vector[CandVec[i] -1]->seps.size();
if(num_choices > largest_so_far){
largest_so_far = num_choices;
nextSNP = CandVec[i];
}
}
if(nextSNP == 0) {
std::cout << " Possible Bug " << endl;
std::cout.flush();
throw exception();
}
return nextSNP;
}
unsigned Data::FindnextSNP_rand(const vector<unsigned>& candVec)
{
unsigned nextSNP = 0;
unsigned i;
double num_choices = 0;
double total_num = 0;
double incr;
vector<std::pair<unsigned,double> > neighvec;
neighvec.resize(candVec.size() );
for(i=0; i != candVec.size(); i++){
num_choices = SNP_vector[candVec[i] -1]->seps.size();
incr = (double)num_choices;
std::pair<unsigned,double> thispair(candVec[i],incr);
neighvec[i] =thispair;
total_num += incr;
}
if (total_num == 0) {
{
std::cout << " Possible Bug " << endl;
std::cout.flush();
throw exception();
}
}
for(i =0 ; i != neighvec.size(); i++){
neighvec[i].second = (neighvec[i].second/total_num);
}
double previous = 0;
double u = ran_obj.rand();
for(i = 0; i != neighvec.size(); i++){
if(u < (neighvec[i].second + previous)) {nextSNP = neighvec[i].first;}
else{previous += neighvec[i].second;}
if (nextSNP != 0) break;
}
return nextSNP;
}
unsigned Data::FindstartSNP_rand()
{
unsigned nextSNP = 0;
unsigned i;
double num_choices = 0;
double total_num = 0;
vector<std::pair<unsigned,double> > neighvec;
vector<unsigned>::iterator vecit;
bool is_connector;
bool is_singleton;
for(i=0; i != SNP_vector.size(); i++){
double incr = numeric_limits<double>::quiet_NaN();
is_connector = false;
if(all_connectors.size() > 0){
vecit = find(all_connectors.begin(), all_connectors.end(), (i+1) );
if(vecit != all_connectors.end() ) is_connector = true;
}
if(is_connector) continue; // do not include connectors among possible starts
is_singleton = false;
if(singletons.size() > 0){
vecit = find(singletons.begin(), singletons.end(), (i+1) );
if(vecit != singletons.end() ) is_singleton = true;
}
if(is_singleton) continue; // do not include singletons among possible starts
num_choices = SNP_vector[i]->seps.size();
if(num_choices <= 0){
std::cout << " Entries for element " << (i+1)
<< " dubious ... program terminating " << endl;
std::cout.flush();
throw exception();
}
incr = pow((num_choices*1.0),1.0);
std::pair<unsigned,double> thispair((i+1),incr);
neighvec.push_back(thispair);
total_num += incr;
}
if(total_num == 0) {
std::cout << " Total sum of weights vanishes ... error ... program terminating " << endl;
std::cout.flush();
throw exception();
}
for(i =0 ; i != neighvec.size(); i++){
neighvec[i].second = (neighvec[i].second/total_num);
}
double previous = 0;
double u = ran_obj.rand();
for(i = 0; i != neighvec.size(); i++){
if(u < (neighvec[i].second + previous)) {nextSNP = neighvec[i].first;}
else{previous += neighvec[i].second;}
if (nextSNP != 0) break;
}
if(nextSNP == 0){
std::cout << " Possible Problems with biased coin toss ... program terminating " << endl;
std::cout.flush();
throw exception();
}
return nextSNP;
}
// Used to check for dominating property
void Data::AugmentcoveredVec(const unsigned nextSNP){
vector<unsigned> diffVec;
diffVec.resize(allIds.size(),0);
// set<unsigned> newSet;
// newSet.clear();
vector<unsigned> newVec;
newVec.clear();
vector<unsigned>::iterator endit,endit1;
vector<unsigned> next_vec = SNP_vector[nextSNP -1]->seps;
endit = set_difference(allIds.begin(),allIds.end(),\
next_vec.begin(),next_vec.end(), diffVec.begin() );
if(unsigned(endit -diffVec.begin() ) != unsigned(allIds.size() -next_vec.size()) ){
std::cout << " Size mismatch with cover of " << nextSNP
<< " " << next_vec.size() << " " << unsigned(endit -diffVec.begin() ) << endl;
std::cout.flush();
throw exception();
}
if(coveredVec.size() == 0){
coveredVec.resize(unsigned(endit -diffVec.begin()) );
copy(diffVec.begin(),endit, coveredVec.begin() );
}else{
newVec.resize(coveredVec.size() + unsigned(endit -diffVec.begin() ) );
endit1 = set_union(coveredVec.begin(), coveredVec.end(), diffVec.begin(), \
endit,newVec.begin() );
coveredVec.resize(unsigned(endit1 -newVec.begin()) );
copy(newVec.begin(),endit1,coveredVec.begin() );
}
sort(coveredVec.begin(), coveredVec.end());
return;
}
void Data::cleanup()
{
allIds.clear();
singletons.clear();
all_connectors.clear();
coveredVec.clear();
for(unsigned i = 0; i != SNP_vector.size();i++){
SNP_vector[i]->seps.clear();
SNP_vector[i]->SNPname.clear();
delete SNP_vector[i];
}
SNP_vector.clear();
NameIdMap.clear();
return;
}
void Data::initialize_rand_gen(){
ran_obj = MTRand();
ran_obj.seed(longseed);
}