-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmer2pca.c
597 lines (434 loc) · 12.5 KB
/
kmer2pca.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<math.h>
#include<pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef __AVX__
#include <immintrin.h>
#endif
void dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*);
typedef struct {
size_t n_features;
size_t n_samples;
} data_shape;
typedef struct {
double* eigenvalues;
double* eigenvectors;
} eigen_space;
typedef struct {
double *data;
double *means;
data_shape *shape;
double* matrix;
size_t* incrementor;
size_t* dimensions;
eigen_space* space;
} thread_handle;
pthread_mutex_t lock;
data_shape shape_from_input_file(int infile) {
data_shape s;
unsigned char current_character;
off_t size;
size_t i;
size_t chunk = 4096;
size_t n_chunks;
size_t rest_size;
s.n_features = 0;
s.n_samples = 0;
size = lseek(infile, 0, SEEK_END);
lseek(infile, 0, SEEK_SET);
char* f_buffer = (char*)malloc(sizeof(char)*size);
n_chunks = size/chunk;
rest_size = size%chunk;
for(i=0;i<n_chunks;i++) {
if (chunk != read(infile, (f_buffer+(i*chunk)), chunk)) {
printf("Warning could not load file into memory\n");
_exit(1);
}
}
if (rest_size != read(infile, (f_buffer+(n_chunks*chunk)), rest_size)) {
printf("Warning could not load file into memory\n");
_exit(1);
}
i = 0;
while( f_buffer[i] != '\n') {
if ( f_buffer[i] == '\t') s.n_features++;
i++;
}
while( i < size ) {
if ( f_buffer[i] == '\n' ) s.n_samples++;
i++;
}
free(f_buffer);
return(s);
}
double eigenvector_test(eigen_space ei, data_shape s, int vi, int vj ) {
size_t i, j;
double sum = 0;
for( i = 0; i < s.n_features; i++) {
sum +=
ei.eigenvectors[vi*s.n_features+i]*ei.eigenvectors[vj*s.n_features+i];
}
printf("%lf\n", sum);
return(sum);
}
eigen_space eigen_from_corr(double* corr, data_shape s) {
eigen_space ei;
char JOBZ = 'V';
char ULPO = 'U';
int N = (int)s.n_features;
double*A = (double*)malloc(sizeof(double)*s.n_features*s.n_features);
int LDA = (int)s.n_features;
double*W = (double*)malloc(sizeof(double)*s.n_features);
double*WORK;
int LWORK;
int INFO;
memcpy(A, corr, sizeof(double)*s.n_features*s.n_features);
LWORK = 3*N;
WORK = (double*)malloc(sizeof(double)*LWORK);
dsyev_(&JOBZ, &ULPO, &N, A, &LDA, W, WORK, &LWORK, &INFO);
/* if (INFO) {
printf("Error in diagonalization routine INFO=%ls \n", &INFO);
_exit(1);
}
*/
ei.eigenvectors = A;
ei.eigenvalues = W;
free(WORK);
return(ei);
}
void* obtain_projections_thread_handler(void* handle) {
size_t i,j,k;
thread_handle* th=(thread_handle*)handle;
size_t dimensions = th->dimensions[0];
size_t n_samples = th->shape->n_samples;
size_t n_features = th->shape->n_features;
double* projections = th->matrix;
double* data = th->data;
size_t position;
eigen_space ei = th->space[0];
thread_continuition:
pthread_mutex_lock(&lock);
j = th->incrementor[0];
th->incrementor[0]++;
pthread_mutex_unlock(&lock);
if(j < n_samples) {
for(i = 0; i < dimensions; i++) {
position = j*dimensions+i;
projections[position] = 0;
for( k = 0 ; k < n_features; k++ ) {
projections[position]+=
data[j*n_features+k]
*ei.eigenvectors[(n_features-1-i)*n_features+k];
}
}
goto thread_continuition;
}
}
double* obtain_projections(size_t dimensions, double* data,
eigen_space ei, data_shape s, size_t n_threads) {
size_t incrementor = 0;
size_t i,j,k;
int position;
double* projections = (double*)malloc(sizeof(double)*dimensions*s.n_samples);
pthread_t* threads = (pthread_t*)malloc(sizeof(pthread_t)*n_threads);
thread_handle th;
th.shape = &s;
th.dimensions = &dimensions;
th.incrementor = &incrementor;
th.matrix = projections;
th.data = data;
th.space = &ei;
pthread_mutex_init(&lock, NULL);
for(i=0; i<n_threads;i++) {
pthread_create(threads+i, NULL,
obtain_projections_thread_handler,
&th);
}
for(i=0;i<n_threads;i++) {
pthread_join(threads[i],NULL);
}
free(threads);
return(projections);
}
void val_vec_test(double *m, eigen_space ei, data_shape s) {
size_t i,j,k;
double a, b;
for(k=0 ; k < s.n_features; k++) {
printf("vec_val_test pair %lu \n", k);
for(i =0 ; i< s.n_features; i++) {
a = 0;
for(j = 0; j< s.n_features; j++) {
a += m[i*s.n_features+j]*ei.eigenvectors[k*s.n_features+j];
}
b = ei.eigenvalues[k]*ei.eigenvectors[k*s.n_features+i];
printf("vec_val_test: a = %lf, b = %lf \n", a, b);
}
}
}
void print_projections(FILE* f, double* projections,
size_t dimensions, data_shape s) {
size_t i,j;
for(j = 0; j < s.n_samples; j++) {
for(i = 0; i < dimensions-1; i++) {
fprintf(f,"%lf\t", projections[j*dimensions+i]);
}
fprintf(f,"%lf\n", projections[j*dimensions+(dimensions-1)]);
}
}
void print_eigenvalues(FILE* f, data_shape s, eigen_space ei) {
size_t i;
for(i = 0; i < s.n_features; i++) {
fprintf(f,"%lf\n",ei.eigenvalues[i]);
}
}
void data_from_file(FILE* infile,double* in_data, data_shape s) {
size_t i,j;
char buffer[1024];
rewind(infile);
for(i=0;i<s.n_samples;i++) {
fscanf(infile,"%s", buffer);
for(j=0;j<s.n_features;j++) {
fscanf(infile,"%lf", in_data+(i*s.n_features+j));
}
}
}
double* create_normalized_data_from_data(double* coefficients, double* indata,
data_shape s) {
size_t i,j;
double coefficient;
double current_data;
size_t position;
double* normalized_data =
(double*)malloc(sizeof(double)*s.n_features*s.n_samples);
if(!normalized_data) {
printf("Memory allocation for normalized_data failed! \n");
_exit(1);
}
for(i = 0; i < s.n_features; i++) {
coefficient = 0;
for(j= 0; j < s.n_samples; j++) {
current_data = indata[j*s.n_features+i];
current_data *= current_data;
coefficient += current_data;
}
coefficient = 1./sqrt(coefficient);
if (coefficients != NULL) coefficients[i] = coefficient;
for(j= 0; j < s.n_samples; j++) {
position = j*s.n_features+i;
normalized_data[position] = indata[position]*coefficient;
}
}
return(normalized_data);
}
double* feature_correlation_matrix(double* normalized_data, data_shape s) {
size_t i,j,k;
double sum;
double* c_matrix = (double*)malloc(sizeof(double)*s.n_features*s.n_features);
for(i = 0; i < s.n_features; i++ ) {
for(j = 0; j <= i; j++) {
sum = 0;
for(k = 0; k < s.n_samples; k++) {
sum +=
normalized_data[k*s.n_features+j]*normalized_data[k*s.n_features+i];
}
c_matrix[i*s.n_features+j] = c_matrix[j*s.n_features+i] = sum;
}
}
return(c_matrix);
}
void* feature_covariance_matrix_thread_handler(void* handle) {
size_t i,j,k, k_four;
thread_handle* th = (thread_handle*)handle;
size_t n_samples = th->shape->n_samples;
size_t n_features = th->shape->n_features;
double sum;
double* data = th->data;
double* means = th->means;
double* c_matrix = th->matrix;
thread_continuition:
pthread_mutex_lock(&lock);
i=th->incrementor[0];
th->incrementor[0]++;
pthread_mutex_unlock(&lock);
#ifdef __AVX__
__m256d c = _mm256_set1_pd(0.);
__m256d y;
__m256d t;
__m256d input_v;
__m256d sum_v = _mm256_set1_pd(0.);
__m256d input;
__m256d means_i_v;
__m256d means_j_v;
__m256d data_i_v;
__m256d data_j_v;
double data_i[4] __attribute__((aligned(32)));
double data_j[4] __attribute__((aligned(32)));
double sum_buffer[4] __attribute__((aligned(32)));
#else
double c;
double y;
double t;
#endif
if(i<n_features) {
#ifdef __AVX__
means_i_v = _mm256_set1_pd(means[i]);
#endif
for(j = 0; j <= i; j++) {
sum = 0;
#ifdef __AVX__
c = _mm256_set1_pd(0.);
sum_v = _mm256_set1_pd(0.);
means_j_v = _mm256_set1_pd(means[j]);
for(k = 0; k < n_samples/4; k++) {
k_four = 4*k;
data_j[0] = data[ k_four *n_features+j];
data_j[1] = data[(k_four+1)*n_features+j];
data_j[2] = data[(k_four+2)*n_features+j];
data_j[3] = data[(k_four+3)*n_features+j];
data_i[0] = data[ k_four *n_features+i];
data_i[1] = data[(k_four+1)*n_features+i];
data_i[2] = data[(k_four+2)*n_features+i];
data_i[3] = data[(k_four+3)*n_features+i];
data_j_v = _mm256_load_pd(data_j);
data_i_v = _mm256_load_pd(data_i);
input = _mm256_sub_pd(data_j_v,means_j_v);
input = _mm256_mul_pd(input,_mm256_sub_pd(data_i_v,means_i_v));
y = _mm256_sub_pd(input,c);
t = _mm256_add_pd(sum_v,y);
c = _mm256_sub_pd(_mm256_sub_pd(t,sum_v),y);
sum_v = t;
/*sum +=
(data[k*n_features+j]-means[j])*(data[k*n_features+i]-means[i]);*/
}
_mm256_store_pd(sum_buffer,sum_v);
for(k = 0;k<4;k++) {
sum+=sum_buffer[k];
}
// scalar padding without kahan as not needed for small sums
if(!n_samples%4 && n_samples > 0) {
for(k = n_samples-(n_samples%4);k< n_samples;k++) {
sum+=
(data[k*n_features+j]-means[j])*(data[k*n_features+i]-means[i]);
}
}
#else
c = 0.0;
for(k = 0;k< n_samples;k++) {
y = (data[k*n_features+j]-means[j])*(data[k*n_features+i]-means[i]);
y = y - c;
t = sum + y;
c = ( t - sum ) - y;
sum = t;
}
#endif
c_matrix[i*n_features+j] = c_matrix[j*n_features+i] = sum/n_samples;
}
goto thread_continuition;
}
}
double* feature_covariance_matrix(double* data, double* means, data_shape s,
size_t n_threads) {
size_t i,j,k;
size_t incrementor=0;
pthread_t* threads = (pthread_t*)malloc(sizeof(pthread_t)*n_threads);
double sum;
double* c_matrix = (double*)malloc(sizeof(double)*s.n_features*s.n_features);
thread_handle th;
th.data = data;
th.means = means;
th.shape = &s;
th.matrix = c_matrix;
th.incrementor = &incrementor;
pthread_mutex_init(&lock,NULL);
for(i=0;i<n_threads;i++) {
pthread_create(threads+i, NULL,
feature_covariance_matrix_thread_handler,
&th);
}
for(i=0;i<n_threads;i++) {
pthread_join(threads[i], NULL);
}
free(threads);
pthread_mutex_destroy(&lock);
return(c_matrix);
}
double* means_from_data(double* in_data, data_shape s) {
size_t i, j;
double sum;
double* means = (double*)malloc(sizeof(double)*s.n_features);
for( i = 0 ; i < s.n_features; i++ ) {
sum = 0;
for( j = 0 ; j < s.n_samples; j++ ) {
sum += in_data[j*s.n_features+i];
}
means[i] = sum;
means[i] /= (double)s.n_samples;
}
return(means);
}
int main(int argc, char** argv){
int infile = open(argv[1], O_RDONLY);
FILE* proj_file = fopen(argv[2], "w");
FILE* eig_file = fopen(argv[3], "w");
FILE* infilep;
double* in_data;
double* normalized_data;
double* corr_matrix;
double* projections;
double* corr;
double* means;
data_shape shape;
eigen_space ei;
size_t dimensions;
size_t n_threads;
if(argc < 5) {
printf("Arguments are: \n"
" [FILE-in ] kmer file to apply PCA on \n"
" [FILE-out] file to write kmers projected onto"
"principal components to\n"
" [FILE-out] Eigenvalue Spectrum of the PCA \n"
" [int] dimensions - how many principal components to project"
"onto \n"
" [int] number of threads to use for this computation\n");
return(1);
}
sscanf(argv[4],"%lu",&dimensions);
sscanf(argv[5],"%lu",&n_threads);
if(!infile) {
printf("Failed to open input file! \n");
_exit(1);
}
shape = shape_from_input_file(infile);
in_data = (double*)malloc(sizeof(double)*shape.n_features*shape.n_samples);
infilep = fdopen(infile, "r");
data_from_file(infilep,in_data,shape);
printf("Read in data complete: shape: features %lu samples %lu\n",
shape.n_features, shape.n_samples);
//normalized_data = create_normalized_data_from_data(NULL, in_data, shape);
means = means_from_data(in_data, shape);
printf("Means complete \n");
corr = feature_covariance_matrix(in_data, means, shape, n_threads);
free(means);
//corr = feature_correlation_matrix(normalized_data, shape);
printf("Covariance complete\n");
ei = eigen_from_corr(corr, shape);
free(corr);
printf("Eigenspace obtained\n");
//val_vec_test(corr, ei, shape);
projections = obtain_projections(dimensions, in_data,
ei, shape, n_threads);
free(in_data);
print_eigenvalues(eig_file, shape, ei);
free(ei.eigenvalues);
free(ei.eigenvectors);
print_projections(proj_file, projections, dimensions, shape);
free(projections);
fclose(infilep);
fclose(proj_file);
fclose(eig_file);
}