forked from magnusolavhelland/CSD-Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.cpp
257 lines (209 loc) · 6.59 KB
/
preprocessing.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
#include <string>
#include <vector>
#include <algorithm>
#include <iostream> // remove after debugging
#include <omp.h>
#include "preprocessing.h"
#include "utils.h"
#include "myStructs.h"
using namespace std;
void initializeGeneVectors(vector< vector<string> >& allIDs, vector< vector<Gene> >& allGenes, const int& threadCount)
{
for (unsigned int i=0; i<allIDs.size(); ++i)
{
vector<Gene> dummy;
allGenes.push_back(dummy);
(allGenes[i]).resize((allIDs[i]).size());
}
//Gene dummy;
//vector<Gene> vecDummy;
# pragma omp parallel for num_threads(threadCount)
for (unsigned int fi=0; fi<allIDs.size(); ++fi)
{
//allGenes.push_back(vecDummy);
//cout << endl << "\t\t\tInitializing allGenes vector " << i << endl;
for (unsigned int gi=0; gi<(allIDs[fi]).size(); ++gi)
{
//(allGenes[i]).push_back(dummy);
(allGenes[fi][gi]).geneID = allIDs[fi][gi];
(allGenes[fi][gi]).index = gi;
(allGenes[fi][gi]).sortedPos = -1;
(allGenes[fi][gi]).origin = fi;
//cout << endl << "\t\t\t\tgeneID = " << (allGenes[i][j]).geneID << endl;
//cout << "\t\t\t\tindex = " << (allGenes[i][j]).index << endl;
//cout << "\t\t\t\torigin = " << (allGenes[i][j]).origin << endl;
}
}
}
bool indicesInRange(vector< vector<Gene> >& allGenes, const vector<unsigned int>& indices)
{
//cout << "\t\tCall to indices in range...\n";
for (unsigned int i=0; i<allGenes.size(); ++i)
{
if (indices[i] < (allGenes[i]).size())
{
continue;
}
else
{
//cout << "\t\t\tReturn: FALSE\n";
return 0;
}
}
//cout << "\t\t\tReturn TRUE\n";
return 1;
}
bool allAreEqual(vector< vector<Gene> >& allGenes, const vector<unsigned int>& indices)
{
//cout << "\t\tCall to allAreEqual..\n";
for (unsigned int i=1; i<allGenes.size(); ++i)
{
if ((allGenes[i-1][indices[i-1]]).geneID == (allGenes[i][indices[i]]).geneID)
{
continue;
}
else
{
//cout << "\t\t\tReturn: FALSE\n";
return 0;
}
}
//cout << "\t\t\tReturn: TRUE\n";
return 1;
}
void incrementAllIndices(vector<unsigned int>& indices)
{
for (unsigned int i=0; i<indices.size(); ++i)
{
indices[i] += 1;
}
}
void filterAndSortGenes(vector< vector<string> >& allIDs, vector< vector<Gene> >& allGenes, vector< vector<Gene> >& filteredGenes, const int& threadCount)
{
//cout << "\t\tBeginning of filterAndSortGenes...\n";
// Special case: only one input file
// - add all genes to filtered genes and return
if (allIDs.size() == 1)
{
initializeGeneVectors(allIDs, filteredGenes, threadCount);
return;
}
// Initializing gene vectors
initializeGeneVectors(allIDs, allGenes, threadCount);
// cout << "After initialization of allGenes...\n";
vector<Gene> dummy;
for (unsigned int i=0; i<allIDs.size(); ++i) filteredGenes.push_back(dummy);
//cout << "\t\tInitializing gene vectors done...\n";
//cout << "\t\t\tSize of allIDs vector 1: " << (allIDs[0]).size() << endl;
//cout << "\t\t\tSize of allIDs vector 2: " << (allIDs[1]).size() << endl;
//cout << "\t\t\tSize of gene vector 1: " << (allGenes[0]).size() << endl;
//cout << "\t\t\tSize of gene vector 2: " << (allGenes[1]).size() << endl;
// Sorting genes
# pragma omp parallel for num_threads(threadCount)
for (unsigned int i=0; i<allGenes.size(); ++i)
{
/*
cout << "File " << i+1 << " before sorting by geneID:\n";
for (unsigned int j=0; j<(allGenes[i]).size(); ++j)
{
cout << (allGenes[i][j]).geneID << "\t" << (allGenes[i][j]).index << endl;
}
*/
//cout << "\t\tSorting genes iteration " << i+1 << endl;
//sort((allGenes[i]).begin(), (allGenes[i]).end(), geneCompare); NB! geneCompare have been modified
quickSortGenes(allGenes[i], 0, (allGenes[i]).size()-1, geneCompare);
//cout << "\t\tSorting genes iteration " << i+1 << " done...\n";
/*
cout << "File " << i+1 << " after sorting by geneID:\n";
for (unsigned int j=0; j<(allGenes[i]).size(); ++j)
{
cout << (allGenes[i][j]).geneID << "\t" << (allGenes[i][j]).index << "\t" << (allGenes[i][j]).sortedPos << endl;
}
*/
//for (unsigned int j=0; j<(allGenes[i]).size(); ++j)
//{
// cout << "\n\t\t\t\t" << (allGenes[i][j]).geneID;
//}
}
// SO FAR ALL GOOD
//cout << "\t\tSorting gene vectors done...\n";
// Filtering Genes
vector<unsigned int> indices(allGenes.size());
for (unsigned int i=0; i<allGenes.size(); ++i) indices[i] = 0;
//cout << "\tAfter indices initialization...\n";
unsigned int i=0;
Gene biggest;
//cout << "\t\tPreparing extraction of genes done...\n";
int j=0;
while (indicesInRange(allGenes, indices))
{
if (allAreEqual(allGenes, indices))
{
//cout << "\t\t\tPushing similar genes to vector...\n";
pushToFilteredGenes(allGenes, indices, filteredGenes);
for (unsigned int l=0; l<allIDs.size(); ++l)
{
(filteredGenes[l][j]).index = j;
(allGenes[l][indices[l]]).sortedPos = j;
}
++j;
//cout << "\t\t\t\tPushing done...\n";
//cout << "\t\t\tIncrementing all indices...\n";
incrementAllIndices(indices);
//cout << "\t\t\t\tIncrementation done...\n";
}
else
{
i = 0;
//cout << "\t\t\t Call to findBiggest()\n";
findBiggest(allGenes, indices, biggest);
while (!allAreEqual(allGenes, indices) && indicesInRange(allGenes,indices) && i<allGenes.size())
{
while (geneLessThan(allGenes[i][indices[i]], biggest) && indices[i]<(allGenes[i]).size())
{
indices[i] += 1;
}
if ((allGenes[i][indices[i]]).geneID == biggest.geneID)
{
i++;
continue;
}
else
{
biggest.geneID = (allGenes[i][indices[i]]).geneID;
i = 0;
continue;
}
}
}
}
/*
// Important step to maintain the wanted order of genes upon sorting them later
for (unsigned int i=0; i<filteredGenes.size(); ++i)
{
for (unsigned int j=0; j<(filteredGenes[i]).size(); ++j)
{
(filteredGenes[i][j]).index = j;
}
}
*/
}
void findBiggest(vector< vector<Gene> >& allGenes, const vector<unsigned int>& indices, Gene& biggest)
{
biggest.geneID = "";
for (unsigned int i=0; i<allGenes.size(); ++i)
{
if (geneLessThan(biggest, allGenes[i][indices[i]]))
{
biggest.geneID = (allGenes[i][indices[i]]).geneID;
}
}
}
void pushToFilteredGenes(vector< vector<Gene> >& allGenes, const vector<unsigned int>& indices, vector< vector<Gene> >& filteredGenes)
{
for (unsigned int i=0; i<allGenes.size(); ++i)
{
(filteredGenes[i]).push_back(allGenes[i][indices[i]]);
//(allGenes[i][indices[i]]).sortedPos = &((filteredGenes[i]).back());
}
}