-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
339 lines (303 loc) · 8.97 KB
/
Util.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
//The part of the function in the code from adapted from CAVIAR developped by Hormozdiari, F et al.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
using namespace std;
long int fact(int n) {
if(n==0)
return 1;
return n* fact(n-1);
}
void copyConfigure(double *dest, double *src, int size) {
for(int i = 0; i < size; i++)
dest[i] = src[i];
}
double min(double a, double b) {
if(a>b)
return b;
else
return a;
}
long int nCr(int n, int r) {
long int result = 1;
for(int i = n; i > n-r; i--)
result *= i;
return result/fact(r);
}
void printVector(char * data, int size) {
for(int i = 0; i < size; i++)
printf("%c, ", data[i]);
}
void printVector(int * data, int size) {
for(int i = 0; i < size; i++)
printf("%d, ", (int)data[i]);
}
void printVector(double * data, int size) {
for(int i = 0; i < size; i++)
printf("%lf, ", data[i]);
}
void diffVector(double * data1, double * data2, int size, double * result) {
for(int i = 0; i < size; i++ )
result[i] = data1[i] - data2[i];
}
void sumVector(double * data1, double * data2, int size, double * result) {
for(int i = 0; i < size; i++ )
result[i] = data1[i] + data2[i];
}
double multVector(double * data1, double * data2, int size) {
double res = 0;
for(int i = 0; i < size; i++ )
res += data1[i] * data2[i];
return res;
}
void dotVector(double * data1, double * data2, int size, double * result) {
for(int i = 0; i < size; i++ )
result[i] = data1[i] * data2[i];
}
void multVectorMatrix(double *vector, double * matrix, int size, double * result) {
double total_row = 0;
for(int i = 0; i < size; i++) {
total_row = 0;
for(int j = 0; j < size; j++) {
total_row += vector[j] * matrix[i + j * size];
}
result[i]= total_row;
}
}
void importData(string fileName, double * vector) {
int index = 0;
double data = 0;
ifstream fin(fileName.c_str(), std::ifstream::in);
fin >> data;
while (fin.good()) {
vector[index] = data;
index++;
fin >> data;
}
fin.close();
}
void importData(string fileName, int * vector) {
int index = 0;
double data = 0;
ifstream fin(fileName.c_str(), std::ifstream::in);
while( fin.good() ){
fin >> data;
vector[index] = (int)data;
index++;
}
fin.close();
}
/*
The column index starts by 1 in this implemenation
*/
void importDataSecondColumn(string fileName, double * vector) {
int index = 0;
string line = "";
string dataS = "";
double data = 0.0;
ifstream fin(fileName.c_str(), std::ifstream::in);
while( getline(fin, line) ){
istringstream iss(line);
iss >> dataS;
iss >> data;
vector[index] = (double)data;
index++;
}
// cout << "reach=" << index << endl;
fin.close();
}
/*
The column index starts by 1 in this implemenation
*/
void importDataNthColumn(string fileName, double * vector, int colNum) {
int index = 0;
string line = "";
string dataS = "";
double data = 0.0;
ifstream fin(fileName.c_str(), std::ifstream::in);
while( getline(fin, line) ){
istringstream iss(line);
iss >> dataS;
for(int i = 0; i < colNum-1;i++)
iss >> data;
vector[index] = (double)data;
index++;
}
// cout << "reach=" << index << endl;
fin.close();
}
void importDataFirstColumn(string fileName, double * list) {
// cout<<"Come into to extract variants names"<<endl;
int index = 0;
double data = 0.0;
string line = "";
ifstream fin(fileName.c_str(), std::ifstream::in);
// cout<<"I am safe in extracting variants"<<endl;
while( getline(fin, line) ){
// cout<<"Come into"<<endl;
// cout<<"line is: "<<line<<endl;
// istringstream iss(line);
istringstream iss;
// string test_b="Biao Zeng is the best";
// istringstream iss_test;
// cout<<"Tested string is: "<<test_b<<endl;
// iss_test.str(test_b);
// cout<<"It was changed"<<endl;
iss.str(line);
// cout<<"Still safe 1"<<endl;
// cout<<"Come into"<<endl;
iss >> data;
// cout<<"Still safe 2"<<endl;
list[index] = (double) data ;
// cout<<"Still safe 3"<<endl;
index++;
}
double mean=0;
for(int i=0;i<index;i++)
{
mean+=list[i];
}
mean/=index;
for(int i=0;i<index;i++)
{
list[i]-=mean;
}
// double mean=0;
// for(int i=0;i<index;i++)
// {
// mean+=list[index];
// }
// mean=mean/index;
// for(int i=0;i<index;i++)
// {
// list[index]-=mean;
// }
cout << "FINISH" << endl;
fin.close();
}
void fileSize(string fileName, int & size) {
// cout<<"Come into to extract variant number"<<endl;
size = 0;
double data = 0;
ifstream fin(fileName.c_str(), std::ifstream::in);
if( fin.good() ){
fin >> data;
// cout<<" "<<data;
size++;
}
// cout<<endl;
// cout<<"size is: "<<size<<endl;
fin.close();
}
string convertInt(int number) {
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
void resetVector(char *data, int size){
for(int i = 0; i < size; i++)
data[i] = '0';
}
void resetVector(int * data, int size) {
for(int i = 0; i < size; i++)
data[i] = 0;
}
void resetVector(double * data, int size) {
for(int i = 0; i < size; i++)
data[i] = 0;
}
void exportVector2File(string fileName, char * data, int size) {
ofstream outfile(fileName.c_str(), ios::out );
for (int i = 0; i < size; i++)
outfile << data[i] << " ";
outfile << endl;
outfile.close();
}
void exportVector2File(string fileName, double * data, int size) {
ofstream outfile(fileName.c_str(), ios::out );
for (int i = 0; i < size; i++)
outfile << data[i] << " ";
outfile << endl;
outfile.close();
}
void exportVector2File(string fileName, int * data, int size) {
ofstream outfile(fileName.c_str(), ios::out );
for (int i = 0; i < size; i++)
outfile << data[i] << " ";
outfile << endl;
outfile.close();
}
void export2File(string fileName, int data) {
ofstream outfile(fileName.c_str(), ios::out );
outfile << data << endl;
outfile.close();
}
int snp2Gene(int * G, int snpId, int snpCount, int geneCount) {
for(int i = 0; i < geneCount; i++) {
if(G[snpId*geneCount + i] == 1)
return i;
}
return -1;
}
void setIdentitymatrix(int * G, int snpCount, int geneCount) {
for(int i = 0; i < snpCount; i++) {
for(int j = 0; j < geneCount; j++) {
G[i*geneCount + j] = 0;
}
G[i*geneCount + (i/(snpCount/geneCount))] = 1;
}
for(int i = 0; i < snpCount; i++) {
for(int j = 0; j < geneCount; j++) {
printf("%d ", G[i*geneCount+j]);
}
printf("\n");
}
}
void makeSigmaPositiveSemiDefinite(double * sigma, int size) {
int gsl_tmp = 0;
double matDet = 0;
double addDiag = 0;
bool positive = false;
// cout<<"Come into the code of makeSigmaPositiveSemiDefinite"<<endl;
// cout<<"Value of size is: "<<size<<endl;
//gsl_set_error_handler_off();
gsl_matrix * tmpResultMatrix = gsl_matrix_calloc (size, size);
// cout<<"Come into the code of makeSigmaPositiveSemiDefinite"<<endl;
gsl_permutation *p = gsl_permutation_alloc(size);
// cout<<"Come into the code of makeSigmaPositiveSemiDefinite"<<endl;
do{
for(int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if(i==j)
gsl_matrix_set(tmpResultMatrix,i,j,sigma[i*size+j]+addDiag);
else
gsl_matrix_set(tmpResultMatrix,i,j,sigma[i*size+j]);
}
}
gsl_linalg_LU_decomp(tmpResultMatrix, p, &gsl_tmp);
matDet = gsl_linalg_LU_det(tmpResultMatrix,gsl_tmp);
// cout << matDet << "\t" << addDiag << endl;
if(matDet > 0 )
positive = true;
else {
// cout << "add" << endl;
addDiag+=0.1;
}
} while(!positive);
// cout<<"Come into the code of makeSigmaPositiveSemiDefinite"<<endl;
for(int i = 0; i < size*size; i++){
if(i%(size+1) == 0)
sigma[i] = sigma[i] + addDiag;
}
// cout<<"Come into the code of makeSigmaPositiveSemiDefinite"<<endl;
}