-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSIANNPredictionBatch2.c
526 lines (466 loc) · 19.2 KB
/
CSIANNPredictionBatch2.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
// gcc-o c CSIANNPrediction.c -lm
/*input:
testing.txt
Features.txt // file for the FDA parameters
partical_weights.txt // file for the TDNN parameters
output:
Prediction.txt the prediction result
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MaxHistNo 25 // permitted maximum number of histones would be MaxHistNo = 25
#define MaxSize MaxHistNo*2
#define WindowSize 10 //size of the window is 10, the window for enhancer prediction is 2kb, so 2000/Resolution = 10
#define PeakWindowSize 2500
#define Resolution 200
#define TimeofCutting 100 // TimeofCutting should be smaller than 39, otherwise you'll have to add some code
#define Center2Center 2500
#define OUTLIE 3 // the window would be predicted positive only when there're more than 3 items have value>threshold. If OUTLIE =-1 there's no filtering
#define FDRCUTOFF 0.05 //
//#define FootStep 0.02
#define FootStep 0.01
#define MaxEnhancerNo 100000
typedef struct {
char chrID[10];
int positionID;
float histvalues[MaxSize/2];
} struct_position;
#include "TDNNEva.h"
#include "randperm.h"
#include "randperm2.h"
#include "RandomPerPrediction3.h"
int main(int argc, char *argv[]){
int i,j,k,flag,q;
int no_column=0, no_row=0;
int PeakWindow = (int) PeakWindowSize / Resolution;
int offset;
double cutoff[TimeofCutting]={0};
int RandomFP[TimeofCutting]={0};
int predictedP[TimeofCutting];
double FDR[TimeofCutting];
char filename[200], fileout[200];
char buffer[300];
double weights[22];
struct_position *Position;
FILE *fp;
char *ptr;
float xs, wei;
float *means, *deviations;
double *features, *fdaresult, *output, awindow[WindowSize];
int *outputflag; // a flag to show whether a window is OK to be a prediction
double space;
int predictions[MaxEnhancerNo];
double maxx,randmaxx;
double minn,randminn;
double fdamean;
float *positiontemp; // for scheme 1 of generating randomlized data
double *randomfda, *randomoutput; // for randomlize the data
int *randomoutputflag; // a flag to show whether a window is OK to be a prediction
int x, randrow, randcolumn, *randindex;
int position;
float curdistance, mindistance;
int FDRindex;
// get input : testing file
if(argc != 3) {
printf("Input wrong! ./XXX testing.txt output\n");
exit(1);
}
strcpy(filename, argv[1] );
strcpy(fileout, argv[2] );
printf("reading testing file: %s, footstep = %e, no.cutting = %d \n", filename, FootStep, TimeofCutting);
/* Step 1:======================================= Read testing data from file ======================================*/
// printf("PLEASE INPUT THE FILE NAME FOR TESTING DATA: ");
// scanf("%s",filename);
fp = fopen(filename,"r");
if(fp == NULL) {perror(filename);}
while(!feof(fp) && (fgets(buffer,300,fp)!= NULL)){
if( (ptr = strstr(buffer, "ID") ) != NULL){
// handle the header of the training file to find the dimension the data
while(*ptr != '\0'){
if((*ptr == '\t') && (*(ptr+1) != '\0') &&(*(ptr+1) != '\n') ){ no_column++;}
ptr++;
}
}
else{ no_row++;
}
bzero(buffer,300);
}
fclose(fp);
no_column--; // the number of histions value columns
//printf("There are %d columns and %d rows in the Testing data. \n", no_column, no_row);
if(no_column -1> MaxSize){printf("\n The code is currently programmed to process data with no more than %d histones. Please change the value of MaxSize then try again!\n", MaxSize/2); return;}
Position = malloc(no_row * sizeof(struct_position));
space = no_row * sizeof(struct_position);
if(Position == NULL){printf("Allocate %lf MB for Position failed! \n", space/(1024*1024)); return;}
//else{printf("Allocate %lf MB for Position succeeded! \n", space /(1024 * 1024));}
else{printf("\nAllocate memory for input data succeeded!\nReading file %s...\n",filename);}
for(i=0;i<no_row;i++){
bzero(Position[i].chrID,10);
Position[i].positionID = 0;
for(j=0;j<no_column;j++)
Position[i].histvalues[j]=0;
}
fp = fopen(filename,"r");
if(fp == NULL) {perror(filename);}
i =0; bzero(buffer,300);
while(!feof(fp) && (fgets(buffer,300,fp)!= NULL) ){
if( (ptr = strstr(buffer, "ID") ) != NULL){ // i=0; // handle the header of the training file to find the dimension the data
}
else{
ptr = (char *)buffer;
j=0;
while(*ptr != '\t'){
Position[i].chrID[j] = *ptr;
ptr++;
j++;
}
Position[i].chrID[j] = '\0';
ptr++; //ptr pointing to positions now
while(*ptr != '\t'){
Position[i].positionID = Position[i].positionID *10 + *ptr -48;
ptr++;
}
ptr++; //ptr pointing to the histone values now
k =0; flag =1;
while(flag!= -1){
if(*ptr == '\0' || *ptr == '\n'){ flag = -1;} //'\0' and '\n'
else if(*ptr=='.'){
flag=0;
xs = 0;
wei = 0;
ptr++;
}
else if(*ptr == '\t'){
k++;
flag = 1;
ptr++;
}
else {
if(flag==1) {
Position[i].histvalues[k]= Position[i].histvalues[k]*10 + *ptr-48;
ptr++;
}
if(flag==0){
wei++;
xs=*ptr-48;
for(j=0;j<wei;j++)
xs = xs/10;
Position[i].histvalues[k]= Position[i].histvalues[k] + xs;
ptr++;
}
}
}
bzero(buffer,300);
i++;
}
}
fclose(fp); //*/
/* Step 2:======================================= Normalized the columns ======================================*/
// normalized the columns so that the columns have zero mean and 1 standard deviation
// d[i][j] = (d[i][j] - mean[j])/std[j]
means = malloc(no_column * sizeof(float));
if(means == NULL) {printf("Allocate %d bytes for means failed!", no_column*sizeof(float)); return;}
for(i=0;i<no_column;i++) { means[i] = 0;}
deviations = malloc(no_column * sizeof(float));
if(deviations == NULL) {printf("Allocate %d bytes for deviations failed!", no_column*sizeof(float)); return;}
for(i=0;i<no_column;i++) { deviations[i] = 0;}
for(i=0;i<no_column;i++){
for(j=0;j<no_row;j++){
means[i] = means[i]+Position[j].histvalues[i];
}
}
for(i=0;i<no_column;i++){
means[i] = means[i] / no_row;
}
for(i=0;i<no_column;i++){
for(j=0;j<no_row;j++){
deviations[i] = deviations[i] + (Position[j].histvalues[i] - means[i]) * (Position[j].histvalues[i] - means[i]);
}
}
for(i=0;i<no_column;i++){
deviations[i] = sqrt(deviations[i] / (no_row-1));
}
for(j=0;j<no_row;j++){
for(i=0;i<no_column;i++){
Position[j].histvalues[i] = (Position[j].histvalues[i] - means[i] )/ deviations[i];
}
}
free(deviations);
/* Step 3:======================================= FDA transfer ======================================*/
fp = fopen("Features.txt","r");
if(fp == NULL) {perror("Features.txt");return;}
features = malloc(no_column*sizeof(double));
if(features == NULL){printf("Allocate %d bytes for features failed!\n", no_column*sizeof(float)); return;}
for(i=0;i<no_column;i++)
fscanf(fp,"%lf",&features[i]);
fclose(fp);
fdaresult = malloc(no_row * sizeof(double));
if(fdaresult == NULL){printf("Allocate %d bytes for features failed!\n", no_row*sizeof(float)); return;}
for(i=0;i<no_row;i++)
fdaresult[i] = 0;
for(i=0;i<no_row;i++){
for (j=0;j<no_column;j++)
fdaresult[i] = fdaresult[i] + (double) Position[i].histvalues[j] * features[j];
}
printf("\nTesting Data FDA transfering finished!\n");
/* Step 4:============================ Calculate output of ANN model on testing data======================================*/
/*4-1 =============================== Reading weight setting for the ANN model ===============*/
fp = fopen("partical_weights.txt","r");
if(fp == NULL) {perror("partical_weights.txt");return;}
for(i=0;i<22;i++)
fscanf(fp,"%lf",&weights[i]);
fclose(fp);
/*4-2=========== calculate the output of ANN model of each 2kb windows with delay 1 and save the output in file ========*/
output = malloc( (no_row - WindowSize + 1) * sizeof (double));
if(output == NULL) {printf("Allocate space for output failed!\n"); return;}
for(i=0;i< no_row - WindowSize + 1;i++)
output[i] = 0;
outputflag = malloc((no_row - WindowSize +1) * sizeof(int));
if(outputflag == NULL) {printf("Allocate space for outputflag failed!\n");return;}
for(i=0;i<no_row - WindowSize +1;i++)
outputflag[i] = 1;
fdamean=0;
for(i=0;i<no_row;i++)
fdamean = fdamean + fdaresult[i];
fdamean = fdamean/no_row;
maxx = 0;
minn = 1;
for(i=0;i< no_row - WindowSize + 1;i++){
k=0;
for(j=0;j<WindowSize;j++){
awindow[j] = fdaresult[i+j];
if(awindow[j] > fdamean) k++;
}
output[i] = TDNNEva((double *)awindow, WindowSize, (double *) weights);
if(k<= OUTLIE)
outputflag[i] =0;
if(output[i] > maxx) maxx = output[i];
if(output[i]< minn) minn = output[i];
}
printf("\nWorking with the ANN model...\n");
fp=fopen("ANNoutput","w");
if(fp == NULL){perror("ANNoutput");}
offset = WindowSize/2;
for (i=0 ; i < no_row - WindowSize + 1; i++){
fprintf(fp,"%s\t%d\t%f\n", Position[i + offset].chrID, Position[i + offset].positionID, output[ i ]);
}
fclose(fp);
/* Step 5: ============================ Randomlize the data and calculate ANN output of randomlized data =====================*/
randomfda = malloc(no_row * sizeof(double));
if(randomfda == NULL) {printf("\nAllocate space for randomfda failed!"); return;}
for(i=0;i<no_row;i++)
randomfda[i]=0;
// generating randomlized data Scheme 2
positiontemp = malloc (no_row * sizeof (float));
for(i = 0;i<no_column;i++){
for(j=0;j<no_row;j++)
positiontemp[j] = Position[j].histvalues[i];
randperm2(positiontemp, no_row );
for(j=0;j<no_row;j++)
Position[j].histvalues[i] = positiontemp[j];
}
for(i=0;i<no_row;i++){ // random permuate all of the items in Position
for(j=0;j<no_column;j++){
randomfda[i] = randomfda[i] + (double) Position[i].histvalues[j] * features[j];
}
}
//
/* // generating randomlized data Scheme 2
randindex = malloc( no_row * no_column * sizeof(int));
if(randindex == NULL) {printf("\nAllocate space for randindex failed!\n"); return;}
randperm(randindex, no_row * no_column); // generate a random index for all of the items in Position
//printf("\nStarting permutation!");
for(i=0;i<no_row;i++){ // random permuate all of the items in Position
for(j=0;j<no_column;j++){
x = randindex[i*no_column + j];
randrow = (int) x / no_column;
randcolumn = x % no_column;
randomfda[i] = randomfda[i] + (double) Position[randrow].histvalues[randcolumn] * features[j];
}
}
//printf("\nRandomly permutation finished !");
*/
randomoutput = malloc( (no_row - WindowSize + 1) * sizeof (double));
if(randomoutput == NULL) {printf("Allocate space for randomoutput failed!\n");return;}
for(i=0;i< no_row - WindowSize + 1;i++)
randomoutput[i] = 0;
randomoutputflag = malloc((no_row - WindowSize +1) * sizeof(int));
if(randomoutputflag == NULL) {printf("Allocate space for randomoutputflag failed!\n");return;}
for(i=0;i<no_row - WindowSize +1;i++)
randomoutputflag[i] = 1;
randmaxx = 0;
randminn = 1;
for(i=0;i< no_row - WindowSize + 1;i++){
k=0;
for(j=0;j<WindowSize;j++){
awindow[j] = randomfda[i+j];
if(awindow[j] > fdamean) k++;
}
randomoutput[i] = TDNNEva((double *)awindow, WindowSize, (double *) weights);
if(k<=OUTLIE)
randomoutputflag[i] = 0;
if(randomoutput[i] > randmaxx) randmaxx = randomoutput[i];
if(randomoutput[i]< randminn) randminn = randomoutput[i];
}
free(randomfda);
/* Step 6 =========================== calculate prediction based on each possible cutoff =========================*/
printf("maxx = %.5f, minn = %.5f, footstep = %.5f \n", maxx, minn, FootStep);
// 6-1 ==== set possible cutoffs ===========================================================//
offset = WindowSize/2;
for(i=0;i<TimeofCutting;i++) {
// cutoff[i] = maxx - 0.00125 * (i+1);
// cutoff[i] = 0.5 - 0.025 * (i+1);
cutoff[i] = maxx - FootStep * (i+1);
// cutoff[i] = 0.5 - 0.01 * (i+1);
}
// cutoff[TimeofCutting -1 ] = 0.5;
// 6-2 ==== run the prediction using each cutoff ===========================================
for(q=0;q<TimeofCutting;q++){
//6-2-1 ======== find the peaks in each 25Kb windows ====================================
j=0;
if(no_row - WindowSize + 1 <= PeakWindow){ // when the searching space is no larger than one PeakWindow
maxx = 0;
for(i=0;i<no_row-WindowSize+1;i++){
if(output[i]> cutoff[q] && outputflag[i]){
if(output[i] > maxx){
maxx = output[i];
j=1;
}
}
}
}
else{
for (i=0;i<no_row - WindowSize +1; i++){
if(output[i]> cutoff[q] && outputflag[i]) {
flag =1;
k = 1;
if( i < PeakWindow / 2 ){ // the first a few positions
while(flag && (k<= PeakWindow/2)){
if(output[i] <= output[i+k]) flag =0;
k++;
}
k=1;
while(flag && k<=i){
if(output[i] < output[i-k]) flag =0;
k++;
}
}
else if(i > no_row-WindowSize+1 - PeakWindow/2){ // the last a few positions
while(flag && k<= PeakWindow/2){
if(output[i]<output[i-k]) flag =0;
k++;
}
k = no_row-WindowSize+1 -1;
while(flag && k>i){
if(output[i]<= output[k]) flag = 0;
k--;
}
}
else{ //We check [-PeakWindow/2 ~ +PeakWindow/2] positions (that's 12 positions in total when PeakWinwod = 12) to find a peak in the PeakWindow
while(flag && k<=PeakWindow/2){
if(output[i]<output[i-k] || output[i]<=output[i+k]) flag =0;
k++;
}
}
if(flag){ // if output[i] is the peak of its PeakWindow
if(j < MaxEnhancerNo)
j++;
i = i + PeakWindow/2; //move forward to the next position which is out of the current window, making the minimum distance bwteen two predictions to be 7*200=1400
}
}
}
}
predictedP[q] = j;
//6-2.2 ==== calculate FDR ================================================================
RandomFP[q] = RandomPerPrediction3(randomoutput, randomoutputflag, no_row, weights, cutoff[q]);
FDR[q] = (double) RandomFP[q]/predictedP[q];
printf("FDR[%d] = %.5f, at cutoff = %.5f , with %d predictions.\n", q + 1, FDR[q], cutoff[q], predictedP[q] );
}
/* Step 7 ======== Choose a cutoff when FDR is closest to 0.05 =================================== */
for(i=0;i<TimeofCutting;i++){
if(i==0){
mindistance = FDR[i] - FDRCUTOFF;
if(mindistance <0) mindistance = - mindistance;
FDRindex = i;
}
else {
curdistance = FDR[i] - FDRCUTOFF;
if(curdistance <0) curdistance = - curdistance;
if(curdistance < mindistance){
mindistance = curdistance;
FDRindex = i;
}
}
}
printf("\nCutoff was chosen when FDR = %f, with %d predictions. \n",FDR[FDRindex], predictedP[FDRindex]);
// FDRindex = 40;
// ------------------------------Set the cutoff and Save the Predictions when FDR is closest to 0.05 -----------------//
j=0;
if(no_row - WindowSize + 1 <= PeakWindow){ // when the searching space is no larger than one PeakWindow
maxx = 0;
for(i=0;i<no_row-WindowSize+1;i++){
if(output[i]> cutoff[FDRindex] && outputflag[i]){
if(output[i] > maxx){
maxx = output[i];
predictions[0] = i; // there's only one peak
j=1;
}
}
}
}
else{
for (i=0;i<no_row - WindowSize +1; i++){
if(output[i]> cutoff[FDRindex] && outputflag[i]) {
flag =1;
k = 1;
if( i < PeakWindow / 2 ){ // the first a few positions
while(flag && (k<= PeakWindow/2)){
if(output[i] <= output[i+k]) flag =0;
k++;
}
k=1;
while(flag && k<=i){
if(output[i] < output[i-k]) flag =0;
k++;
}
}
else if(i > no_row-WindowSize+1 - PeakWindow/2){ // the last a few positions
while(flag && k<= PeakWindow/2){
if(output[i]<output[i-k]) flag =0;
k++;
}
k = no_row-WindowSize+1 -1;
while(flag && k>i){
if(output[i]<= output[k]) flag = 0;
k--;
}
}
else{ //We check [-PeakWindow/2 ~ +PeakWindow/2] positions (that's 12 positions in total when PeakWinwod = 12) to find a peak in the PeakWindow
while(flag && k<=PeakWindow/2){
if(output[i]<output[i-k] || output[i]<=output[i+k]) flag =0;
k++;
}
}
if(flag){ // if output[i] is the peak of its PeakWindow
predictions[j]=i;
if(j < MaxEnhancerNo)
j++;
i = i + PeakWindow/2; //move forward to the next position which is out of the current window, making the minimum distance bwteen two predictions to be 7*200=1400
}
}
}
}
if (j== MaxEnhancerNo)
printf("There are more than %d predictions. Some predictions are disrecarded. Please increase cutoff.\n", MaxEnhancerNo);
j = predictedP[FDRindex];
fp=fopen(fileout,"w");
if(fp == NULL){perror(fileout);}
for (i=0;i<j;i++){
fprintf(fp,"%s\t%d\t%f\n", Position[predictions[i]+offset].chrID, Position[predictions[i]+offset].positionID, output[predictions[i]]);
// fprintf(fp,"%s\t%d\t%f\n", Position[predictions[i]+offset].chrID, Position[predictions[i]+offset].positionID, output[predictions[i]]/maxx ); // normalize ANN output by maxx value
}
fclose(fp);
printf("Prediction result is saving in %s!\n", fileout);
}