-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrocchio.c
1745 lines (1566 loc) · 64.1 KB
/
rocchio.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "rocchio.h"
void initlization(){
FEATURE_CONSIDER_TOTAL=0; EARLY_TERMINATE = false;
SORT_G = false; SORT_F = false;
HORIZONTAL = true; WEIGHTED = false;
SAMPLING = false; SAMPLING_OPT = false;
DELIMITER = "TRANSCOMMA"; output_dir = "./"; TOPK = 20;
DELTA = 0.05; TRASNFORM = true; PRINT = false; REST = true,HIST = false;
}
void parse_args(int argv, char *argc[]){
int ptr = 1;
string cur_arg;
string delim_str ="-delimiter";
string fconsider_str = "-Fconsider"; // consider only top 1D features
string earlyT_str = "-earlyT"; // early termination
string sortG_str = "-sortG"; // sort genes
string sortF_str = "-sortF"; // SORT FEATURES ACCORDING TO 1D score
string vertical_str = "-vertical"; //horizontal traversal or vertical traversal
string weighted_str = "-weighted"; // weighted on pos v.s. neg
string sampl_str = "-sampl"; // sampling
string sampl_opt_str = "-samplOpt"; //sort the candidates according to the lower bound
while (ptr != argv){
cur_arg = argc[ptr];
if (cur_arg.compare(delim_str) == 0){
ptr++;
DELIMITER = argc[ptr]; //-top1D 1000
} else
if (cur_arg.compare(fconsider_str) == 0){
ptr++;
cur_arg = argc[ptr];
FEATURE_CONSIDER_TOTAL = atol(cur_arg.c_str()); //-top1D 1000
cout << "FEATURE_CONSIDER_TOTAL =" << FEATURE_CONSIDER_TOTAL << endl;
} else
if (cur_arg.compare(earlyT_str) == 0){
EARLY_TERMINATE = true;
} else
if (cur_arg.compare(sortG_str) == 0){
SORT_G = true;
} else
if (cur_arg.compare(sortF_str) == 0){
SORT_F = true;
} else
if (cur_arg.compare(vertical_str) == 0){
HORIZONTAL = false;
} else
if (cur_arg.compare(weighted_str) == 0){
WEIGHTED = true;
} else
if(cur_arg.compare(sampl_str) == 0){
SAMPLING = true;
} else
if(cur_arg.compare(sampl_opt_str) == 0){
SAMPLING_OPT = true;
}else
if(cur_arg.compare("-matrixF") == 0){
ptr++;
MATRIXF = argc[ptr];
cout << "----- matrix " << MATRIXF << " ----" << endl;
}else
if(cur_arg.compare("-expF") == 0){
ptr++;
EXPF = argc[ptr];
}else
if(cur_arg.compare("-expF2") == 0){
ptr++;
EXPF2 = argc[ptr];
}else
if(cur_arg.compare("-topK") == 0){
ptr++;
TOPK = atoi(argc[ptr]);
}else
if(cur_arg.compare("-delta") == 0){
ptr++;
DELTA = atof(argc[ptr]);
}else
if(cur_arg.compare("-notransform") == 0){
TRASNFORM = false;
}else
if(cur_arg.compare("-print") == 0){
PRINT = true;
ptr++;
printF1 = atoi(argc[ptr]);
ptr++;
printF2 = atoi(argc[ptr]);
prints.push_back(make_pair(printF1,printF2));
ptr++;
buckets = atoi(argc[ptr]);
}else
if(cur_arg.compare("-hist") == 0){
HIST = true;
ptr++;
histSize = atoi(argc[ptr]);
}else
if(cur_arg.compare("-pos_neg") == 0){
REST = false;
} else
if (cur_arg.compare("-outDir") == 0) { // input the out directory
++ptr;
output_dir = argc[ptr];
}
ptr++;
}
}
void load_feature_gene_matrix_comma(){ // matrix fille, gene is the sample
clock_t t = clock();
ifstream fin(MATRIXF.c_str());
int rid =0; // row id
while(fin){
string s;
if(!getline(fin,s)) break;
istringstream ss(s);
vector<string> values;
vector<val_type > cur_f;
int cid =0; //column id
while(ss){
string value;
if(!getline(ss,value,',')) break; // the file's delimeter is ','
if(rid>0 && cid!=0) //fid = rid-1 and the first column is the feature's name
cur_f.push_back(atof(value.c_str())); //
else{
if(rid ==0 && cid!=0){ //the first line describes the genes' names
gene_id[value] = cid-1; // from string to id //value.substr(1,value.size()-2
//cout << cid-1 << " "<< value << " " << value.substr(1,value.size()-2)<<endl ;
}
else{ // rid>0 && cid =0
if(rid >0)//cout << rid-1 <<" " << value << endl;
featureName.push_back(value); //featureName[rid-1] = value
}
}
cid++;
}
if(rid>0){
feature cur(cur_f,0,0);
features.push_back(cur);
//matrix.push_back(cur_f); //matrix[f][g]
}
rid++;
}
GENE_NUM = features[0].vals.size(); FEATURE_NUM=features.size(); MATRIX_GENE_NUM=GENE_NUM;
//GENE_NUM = matrix[0].size(); FEATURE_NUM=matrix.size(); MATRIX_GENE_NUM=GENE_NUM;
fprintf(stderr,"=====total number of samples: %ld==%d; total number of features: %ld==%d======\n",gene_id.size(),GENE_NUM, featureName.size(),FEATURE_NUM);
t = clock() - t;
fprintf (stderr, "load_feature_gene_separatedBydelimeter: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
void load_gene_feature_matrix_comma(){ // matrix fille, gene is the sample
clock_t t = clock();
ifstream fin(MATRIXF.c_str());
int rid =0; // row id
while(fin){
string s;
if(!getline(fin,s)) break;
istringstream ss(s);
if(rid == 1) {
features.resize(featureName.size());
fprintf(stderr, "feature size = %ld \n", features.size());
}
int cid =0; //column id
while(ss){
string value;
if(!getline(ss,value,',')) break; // the file's delimeter is ','
if(rid > 0 && cid != 0) { //fid = rid-1 and the first column is the feature's name
features[cid - 1].vals.push_back(atof(value.c_str()));
}
else{
if(rid == 0 && cid != 0){ //the first line describes the feature's name
featureName.push_back(value);
}
else{ // rid>0 && cid =0
if(rid > 0) // && cid == 0
gene_id[value] = rid - 1;
}
}
cid++;
}
rid++;
}
GENE_NUM = features[0].vals.size(); FEATURE_NUM=features.size(); MATRIX_GENE_NUM=GENE_NUM;
//GENE_NUM = matrix[0].size(); FEATURE_NUM=matrix.size(); MATRIX_GENE_NUM=GENE_NUM;
fprintf(stderr,"=====total number of samples: %ld==%d; total number of features: %ld==%d======\n",gene_id.size(),GENE_NUM, featureName.size(),FEATURE_NUM);
t = clock() - t;
fprintf (stderr, "load_feature_gene_separatedBydelimeter: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
void load_feature_gene_matrix_space(){ // matrix fille, gene is the sample MSIGDB
clock_t t = clock();
ifstream fin(MATRIXF.c_str());
//ofstream fout("test.txt");
int rid =0, cid =0; // row id column id
string s;
if(getline(fin,s)){
istringstream ss(s);
string value;
while(ss >> value){
gene_id[value] = cid; // from string to id
cid++;
}
}
GENE_NUM = gene_id.size();
cout << "GENE_NUM=" <<GENE_NUM<<";cid="<<cid<<endl;
cid=0; rid++; // the second row
vector<val_type > cur_f;
while(fin>>s){
if(cid ==0){
featureName.push_back(s);
}
else
cur_f.push_back(atof(s.c_str()));
cid++;
if(cid > GENE_NUM){
cid =0;
rid++;
feature cur(cur_f,0,0);
features.push_back(cur);
cur_f.clear();
}
}
GENE_NUM = features[0].vals.size(); FEATURE_NUM=features.size(); MATRIX_GENE_NUM=GENE_NUM;
fprintf(stderr,"----total number of samples: %ld==%d; total number of features: %ld==%d----\n",gene_id.size(),GENE_NUM, featureName.size(),FEATURE_NUM);
t = clock() - t;
fprintf (stderr, "load_feature_gene_matrix_space: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
void load_sample_feature_matrix_space(){ // matrix fille, gene is the sample LINCS
clock_t t = clock();
ifstream fin(MATRIXF.c_str());
int rid =0, cid =0; // row id column id
string s;
if(getline(fin,s)){
istringstream ss(s);
string value;
while(ss>>value){
featureName.push_back(value);
cid++;
}
}
FEATURE_NUM = featureName.size();
cout << "FEATURE_NUM=" <<FEATURE_NUM<<";cid="<<cid<<endl;
features.resize(cid); // number of features
cid=0;rid++; // the second row
while(fin>>s){
if(cid ==0){
gene_id[s]=rid-1;
}
else{
features[cid-1].vals.push_back(atof(s.c_str()));
}
cid++;
if(cid > FEATURE_NUM){
cid =0;
rid++;
}
}
GENE_NUM = features[0].vals.size(); FEATURE_NUM=features.size(); MATRIX_GENE_NUM=GENE_NUM;
fprintf(stderr,"----total number of samples/genes: %ld==%d; total number of features: %ld==%d----\n",gene_id.size(),GENE_NUM, featureName.size(),FEATURE_NUM);
t = clock() - t;
fprintf (stderr, "load_feature_gene_matrix_space: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
void histogram_feature_sample_matrix_comma(){ // matrix fille, gene is the sample
clock_t t = clock();
ifstream fin(MATRIXF.c_str());
int rid =0; // row id
std::vector<val_type> vals;
while(fin){
string s;
if(!getline(fin,s)) break;
istringstream ss(s);
vector<string> values;
vector<val_type > cur_f;
int cid =0; //column id
while(ss){
string value;
if(!getline(ss,value,',')) break; // the file's delimeter is ','
if(rid>0 && cid!=0) //fid = rid-1 and the first column is the feature's name
vals.push_back(atof(value.c_str()));
else{
if(rid ==0 && cid!=0){ //the first line describes the genes' names
gene_id[value.substr(1,value.size()-2)] = cid-1; // from string to id
}
else{ // rid>0 && cid =0
if(rid >0)//cout << rid-1 <<" " << value << endl;
featureName.push_back(value); //featureName[rid-1] = value
}
}
cid++;
}
rid++;
}
sort(vals.begin(),vals.end());
string outfile_name = output_dir + "histgram_freq.txt";
FILE* fhist = fopen(outfile_name.c_str(),"w");
int cur = 0,step = vals.size()/histSize;
cout << "frequent: step size " << step <<endl;
for(int i=0; i<histSize; i++){
if(i == histSize-1){
fprintf(fhist, "%f \t %f \t\t %ld \n", vals[cur], vals[vals.size()-1], vals.size()-cur);
break;
}
fprintf(fhist, "%f \t %f \t\t %d \n", vals[cur], vals[cur+step], step);
cur += step;
}
fclose(fhist);
outfile_name = output_dir + "histgram_width.txt";
FILE* whist = fopen(outfile_name.c_str(),"w");
double val_step = (vals[vals.size()-1] - vals[0])/histSize;
cout << "width: step size " << val_step <<endl;
double cur_boundary = vals[0];
int cnt = 0, j=0;
for(int i=0; i<histSize; i++){
while(j < vals.size() && (vals[j] < cur_boundary + val_step || j == vals.size()-1))
{
cnt++; j++;
}
fprintf(whist, "%f \t %f \t\t %d \n", cur_boundary, cur_boundary + val_step, cnt);
cur_boundary += val_step;
cnt = 1;
}
fclose(fhist);
fprintf (stderr, "Histogram: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
void histogram_feature_sample_matrix_space(){ // matrix fille, gene is the sample
clock_t t = clock();
ifstream fin(MATRIXF.c_str());
int rid =0, cid =0; // row id column id
string s;
if(getline(fin,s)){
istringstream ss(s);
string value;
while(ss>>value){
gene_id[value] = cid; // from string to id
cid++;
}
}
GENE_NUM = gene_id.size();
cout << "GENE_NUM=" <<GENE_NUM<<";cid="<<cid<<endl;
std::vector<val_type> vals;
cid=0;rid++; // the second row
while(fin>>s){
if(cid ==0){
featureName.push_back(s);
}
else
vals.push_back(atof(s.c_str()));
cid++;
if(cid > GENE_NUM){
cid =0;
rid++;
}
}
sort(vals.begin(),vals.end());
string outfile_name = output_dir + "histgram_freq.txt";
FILE* fhist = fopen(outfile_name.c_str(),"w");
int cur = 0,step = vals.size()/histSize;
cout << "frequent: step size " << step <<endl;
for(int i=0; i<histSize; i++){
if(i == histSize-1){
fprintf(fhist, "%f \t %f \t\t %ld \n", vals[cur], vals[vals.size()-1], vals.size()-cur);
break;
}
fprintf(fhist, "%f \t %f \t\t %d \n", vals[cur], vals[cur+step], step);
cur += step;
}
fclose(fhist);
outfile_name = output_dir + "histgram_width.txt";
FILE* whist = fopen(outfile_name.c_str(),"w");
double val_step = (vals[vals.size()-1] - vals[0])/histSize;
cout << "width: step size " << val_step <<endl;
double cur_boundary = vals[0];
int cnt = 0, j=0;
for(int i=0; i<histSize; i++){
while(j < vals.size() && (vals[j] < cur_boundary + val_step || j == vals.size()-1))
{
cnt++; j++;
}
fprintf(whist, "%f \t %f \t\t %d \n", cur_boundary, cur_boundary + val_step, cnt);
cur_boundary += val_step;
cnt = 1;
}
fclose(fhist);
fprintf (stderr, "Histogram: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
void histogram_sample_feature_matrix_space(){ // matrix fille, gene is the sample
clock_t t = clock();
ifstream fin(MATRIXF.c_str());
int rid =0, cid =0; // row id column id
string s;
if(getline(fin,s)){
istringstream ss(s);
string value;
while(ss>>value){
cid++;
}
}
FEATURE_NUM = cid;
cout << "FEATURE_NUM=" <<FEATURE_NUM<<";cid="<<cid<<endl;
std::vector<val_type> vals;
cid=0; rid++; // the second row
while(fin >> s){
if(cid ==0){
gene_id[s] = rid-1;
}
else{
vals.push_back(atof(s.c_str()));
}
cid++;
if(cid > FEATURE_NUM){
cid =0;
rid++;
}
}
sort(vals.begin(),vals.end());
FILE* fhist = fopen("histgram_freq.txt","w");
int cur = 0,step = vals.size()/histSize;
cout << "frequent: step size " << step <<endl;
for(int i=0; i<histSize; i++){
if(i == histSize-1){
fprintf(fhist, "%f \t %f \t\t %ld \n", vals[cur], vals[vals.size()-1], vals.size()-cur);
break;
}
fprintf(fhist, "%f \t %f \t\t %d \n", vals[cur], vals[cur+step], step);
cur += step;
}
fclose(fhist);
string outfile_name = output_dir + "histgram_width.txt";
FILE* whist = fopen(outfile_name.c_str(),"w");
double val_step = (vals[vals.size()-1] - vals[0])/histSize;
cout << "width: step size " << val_step <<endl;
double cur_boundary = vals[0];
int cnt = 0;
for(int i=0; i<vals.size();i++){
if(i == vals.size()-1){
fprintf(whist, "%f \t %f \t\t %d \n", cur_boundary, vals[vals.size()-1], cnt);
break;
}
if(vals[i] < cur_boundary + val_step){
cnt++;
}else{
fprintf(whist, "%f \t %f \t\t %d \n", cur_boundary, cur_boundary + val_step, cnt);
cur_boundary += val_step;
cnt = 1;
}
}
fclose(fhist);
fprintf (stderr, "Histogram: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
/*void load_feature_gene_matrix_space(){ // matrix fille, gene is the sample
clock_t t = clock();
FEATURE_NUM = 21202; GENE_NUM=22209;
FILE * fin = fopen(MATRIXF.c_str(),"r");
matrix.resize(FEATURE_NUM);
char name[200];
for(int gid=0;gid<GENE_NUM;gid++) {
int x=fscanf(fin,"%s", name); // gene name
cout << gid << " " << name << endl;
string gene_name(name);
gene_id[gene_name] = gid; // gene id
}
for(int f=0; f<FEATURE_NUM; f++){
int x = fscanf(fin,"%s ", name);
featureName.push_back(name);
cout << f<< " " << name << endl;
val_type entry;
for(int g=0; g<GENE_NUM;g++){
x=fscanf(fin,"%lf ", &entry);
//if(f==0)
//fprintf(stderr,"=====%d,%lf======\n",g,entry);
matrix[f].push_back(entry);
}
}
fprintf(stderr,"----total number of samples: %ld==%d; total number of features: %ld==%d----\n",gene_id.size(),GENE_NUM, featureName.size(),FEATURE_NUM);
t = clock() - t;
fprintf (stderr, "load_feature_gene_matrix_space: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}*/
void load_exp_pos_neg(){ //not all samples are selected
fprintf(stderr,"=========loading pos, neg %s %s======\n",EXPF.c_str(),EXPF2.c_str());
clock_t t = clock();
FILE *fgene1 = fopen(EXPF.c_str(),"r");
FILE *fgene2 = fopen(EXPF2.c_str(),"r");
char name[200];
pos_num=0;
genes.resize(GENE_NUM, 0); //positive or negative
if(fgene1 != NULL){
while(fscanf(fgene1,"%s ", name)!=EOF){
string gene_name(name);
map<string, int>::iterator it = gene_id.find(gene_name);
if(it != gene_id.end()){
genes[it->second]=1;
pos_num++;
}
}
}
fprintf(stderr,"=====number of positive genes:%d;======\n",pos_num);
int neg_num =0;
if(fgene2 != NULL){
while(fscanf(fgene2,"%s ", name)!=EOF){
string gene_name(name);
map<string, int>::iterator it = gene_id.find(gene_name);
if(it != gene_id.end()){
genes[it->second]=-1;
neg_num++;
}
}
}
fprintf(stderr,"=====number of negative genes:%d;======\n",neg_num);
features.resize(FEATURE_NUM);
for(int f=0;f<FEATURE_NUM;f++){
features[f].fid = f;
vector<val_type > cur = features[f].vals;
std::vector<val_type > curF;
int gg = 0;
for(int g=0; g<GENE_NUM;g++){
if(genes[g]==1){//pos genes
//features[f].vals[gg] = cur[g];
curF.push_back(cur[g]);
gg++;
//features[f].vals.push_back(matrix[f][g]);
}
}
for(int g=0; g<GENE_NUM;g++){
if(genes[g]==-1){//neg genes
//features[f].vals[gg] = cur[g];
curF.push_back(cur[g]);
gg++;
//features[f].vals.push_back(matrix[f][g]);
}
}
features[f].vals = curF;
}
GENE_NUM = pos_num+ neg_num;
fprintf(stderr,"------total number of genes:%ld==%d-------\n",features[0].vals.size(), GENE_NUM);
fprintf(stderr,"------done with extracting submatrix for this experiment-------\n");
t = clock() - t;
fprintf (stderr, "load exp: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
/*
void load_exp_rest(){
clock_t t = clock();
FILE *fgene1 = fopen(EXPF.c_str(),"r");
FILE *fgene2 = fopen(EXPF2.c_str(),"r");
char name[200];
pos_num=0;
genes.resize(GENE_NUM, -1); //positive or negative
if(fgene1 != NULL){
while(fscanf(fgene1,"%s ", name)!=EOF){
string gene_name(name);
map<string, int>::iterator it = gene_id.find(gene_name);
if(it != gene_id.end()){
genes[it->second]=1;
pos_num++;
}
}
}
fprintf(stderr,"=====number of positive genes:%d;======\n",pos_num);
if(fgene2 != NULL){
while(fscanf(fgene2,"%s ", name)!=EOF){
string gene_name(name);
map<string, int>::iterator it = gene_id.find(gene_name);
if(it != gene_id.end()){
genes[it->second]=1;
pos_num++;
}
}
}
fprintf(stderr,"=====number of positive genes:%d;======\n",pos_num);
fprintf(stderr,"=====FEATURE_NUM:%d;GENE_NUM:%d======\n",FEATURE_NUM,GENE_NUM);
features.resize(FEATURE_NUM);
for(int f=0;f<FEATURE_NUM;f++){
features[f].fid = f;
for(int g=0; g<GENE_NUM;g++){
if(genes[g]==1){//pos genes
features[f].vals.push_back(matrix[f][g]);
}
}
for(int g=0; g<GENE_NUM;g++){
if(genes[g]==-1){//neg genes
features[f].vals.push_back(matrix[f][g]);
}
}
}
fprintf(stderr,"------total number of genes:%ld==%d-------\n",features[0].vals.size(), GENE_NUM);
fprintf(stderr,"------done with extracting submatrix for this experiment-------\n");
t = clock() - t;
fprintf (stderr, "load exp: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}*/
/// after load_exp_rest(), "features[f].vals" are ordered by positive samples and then negative samples;
void load_exp_rest(){
fprintf(stderr,"=========loading pos, neg %s %s======\n",EXPF.c_str(),EXPF2.c_str());
clock_t t = clock();
FILE *fgene1 = fopen(EXPF.c_str(),"r");
FILE *fgene2 = fopen(EXPF2.c_str(),"r");
char name[200];
pos_num=0;
genes.resize(GENE_NUM, -1); //positive or negative
if(fgene1 != NULL){
while(fscanf(fgene1,"%s ", name)!=EOF){
string gene_name(name);
map<string, int>::iterator it = gene_id.find(gene_name);
if(it != gene_id.end()){
genes[it->second]=1;
pos_num++;
}
}
}
fprintf(stderr,"=====number of positive genes:%d;======\n",pos_num);
if(fgene2 != NULL){
while(fscanf(fgene2,"%s ", name)!=EOF){
string gene_name(name);
map<string, int>::iterator it = gene_id.find(gene_name);
if(it != gene_id.end() && genes[it->second]!=1){
genes[it->second]=1;
pos_num++;
}
}
}
fprintf(stderr,"=====number of positive genes:%d;======\n",pos_num);
fprintf(stderr,"=====FEATURE_NUM:%d;GENE_NUM:%d======\n",FEATURE_NUM,GENE_NUM);
features.resize(FEATURE_NUM);
raw.resize(FEATURE_NUM);
for(int f=0;f<FEATURE_NUM;f++){
raw[f].resize(GENE_NUM);
features[f].fid = f;
vector<val_type > cur = features[f].vals;
int gg = 0;
for(int g=0; g<GENE_NUM;g++){
if(genes[g]==1){//pos genes
features[f].vals[gg] = cur[g];
raw[f][gg] = cur[g];
gg++;
}
}
for(int g=0; g<GENE_NUM;g++){
if(genes[g]==-1){//neg genes
features[f].vals[gg] = cur[g];
raw[f][gg] = cur[g];
gg++;
}
}
}
fprintf(stderr,"------total number of genes:%ld==%d-------\n",features[0].vals.size(), GENE_NUM);
fprintf(stderr,"------done with extracting submatrix for this experiment-------\n");
t = clock() - t;
fprintf (stderr, "load exp: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
/*transformation and get top features*/
void transformation(FILE *ftime){
clock_t t = clock();
ff.resize(FEATURE_NUM); // ff: sorted
cout << "pos_num="<<pos_num<<";GENE_NUM="<<GENE_NUM<<endl;
priority_queue<top_1d> top_1d_features;
for(int f=0; f<FEATURE_NUM; f++){
if(features[f].vals.size()!=GENE_NUM)
cout << "*******ERROR******"<<f<<" size:"<< features[f].vals.size() << endl;
vector<val_type > pos_genes(features[f].vals.begin(), features[f].vals.begin()+pos_num);
vector<val_type > neg_genes(features[f].vals.begin()+pos_num, features[f].vals.end());
std::nth_element(pos_genes.begin(), pos_genes.begin() + pos_genes.size()/2, pos_genes.end());
//std::cout << "The median is " << v[v.size()/2] << '\n';
//sort(pos_genes.begin(), pos_genes.end());
val_type median_p = pos_genes[pos_genes.size()/2]; //pos_genes[pos_genes.size()/2+1]; //x+
//sort(neg_genes.begin(), neg_genes.end());
std::nth_element(neg_genes.begin(), neg_genes.begin() + neg_genes.size()/2, neg_genes.end());
val_type median_n = neg_genes[neg_genes.size()/2]; //neg_genes[neg_genes.size()/2+1]; //x-
val_type w = median_p - median_n; //(x+ - x-)
val_type intercept = -(median_p*median_p-median_n*median_n)/2; //-(x+^2- x-^2)/2
int corP=0,corN=0; //correct positive genes & correct negative genes
for(int i=0; i<pos_num;i++){
val_type tmp=features[f].vals[i] * w + intercept; //CHANGE: int TO val_type
features[f].vals[i] = tmp;
if(features[f].vals[i]>0){
corP++;
}
}
for(int i=pos_num; i<GENE_NUM; i++){
val_type tmp= -(features[f].vals[i] * w + intercept); ////CHANGE: int TO val_type
features[f].vals[i] = tmp;
if(features[f].vals[i]>0){
corN++;
}
}
features[f].correct = corP*pos_weight+corN; //weighted!!!
top_1d cur_f(f,corP*pos_weight+corN,corP,corN); // can be omitted
top_1d_features.push(cur_f); // can be omitted
ff[f] = features[f];
}
fprintf(stderr,"------done with transformation-------\n");
t = clock() - t;
fprintf (stderr, "transformation: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
fprintf(ftime,"%f\t",((float)t)/CLOCKS_PER_SEC);
string outfile_name = output_dir + "FTOP1d.txt";
FILE *ftop1d = fopen(outfile_name.c_str(), "a");
//correfprintf(ftop1d,"fid\tcorrect\terror\tpos_correct\tneg_correct\n");
for(int i=0; i<1000; i++){
top_1d cur = top_1d_features.top();
fprintf(ftop1d,"%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",cur.fid, featureName[cur.fid].c_str(), cur.correct,pos_num*pos_weight+(GENE_NUM-pos_num)-cur.correct,cur.correct_p,cur.correct_n,pos_num-cur.correct_p,GENE_NUM-pos_num-cur.correct_n);
if(i<10)
fprintf(stderr,"%d correct in top-%d feature %d \n",cur.correct,i,cur.fid);
top_1d_features.pop();
}
int rank = 1000;
/*while(!top_1d_features.empty()){
top_1d cur = top_1d_features.top();
top_1d_features.pop();
if(cur.fid==15398)
fprintf(stderr,"%d correct in top-%d feature %d \n",cur.correct,rank,cur.fid);
rank++;
}*/
fclose(ftop1d);
}
void no_transformation_baseline(FILE* ftime){
clock_t t = clock();
top_2d best[TOPK];
for(int i=0;i<TOPK; i++)
best[i].miss=2*GENE_NUM;
/*each feature pair*/
long total_checked_g=0;
for(int f1=0;f1<FEATURE_NUM;f1++){
vector<val_type > pos_genes_f1(features[f1].vals.begin(), features[f1].vals.begin()+pos_num);
vector<val_type > neg_genes_f1(features[f1].vals.begin()+pos_num, features[f1].vals.end());
//median of positive
sort(pos_genes_f1.begin(), pos_genes_f1.end());
val_type median_p_f1 =pos_genes_f1[pos_genes_f1.size()/2+1]; //x+
sort(neg_genes_f1.begin(), neg_genes_f1.end());
val_type median_n_f1 =neg_genes_f1[neg_genes_f1.size()/2+1]; //x-
val_type w_f1 = median_p_f1 - median_n_f1; //(x+ - x-)
for(int f2=f1+1; f2<FEATURE_NUM;f2++){
vector<val_type > pos_genes_f2(features[f2].vals.begin(), features[f2].vals.begin()+pos_num);
vector<val_type > neg_genes_f2(features[f2].vals.begin()+pos_num, features[f2].vals.end());
//median of positive
sort(pos_genes_f2.begin(), pos_genes_f2.end());
val_type median_p_f2 =pos_genes_f2[pos_genes_f2.size()/2+1]; //x+
sort(neg_genes_f2.begin(), neg_genes_f2.end());
val_type median_n_f2 =neg_genes_f2[neg_genes_f2.size()/2+1]; //x-
val_type w_f2 = median_p_f2 - median_n_f2; //(y+ - y-)
val_type intercept = -(median_p_f1*median_p_f1-median_n_f1*median_n_f1)/2-(median_p_f2*median_p_f2-median_n_f2*median_n_f2)/2; //-(x+^2- x-^2)/2
//cout << "w_f1 =" << w_f1 <<"; w_f2="<<w_f2 <<";intercept="<<intercept<< endl;
int g=0, missed_p=0, missed_n=0,missed =0;
for(; g<pos_num; g++){
val_type result = w_f1*features[f1].vals[g]+w_f2*features[f2].vals[g]+intercept;
if(result <= 0){ // label is positive
missed_p++;
}
}
for(;g<GENE_NUM;g++){
val_type result = w_f1*features[f1].vals[g]+w_f2*features[f2].vals[g]+intercept;
if(result >= 0){ //!!! label is negative
missed_n++;
}
}
//cout << "missed_p = " << missed_p << ";missed_n = " << missed_n << endl;
missed=pos_weight*missed_p+missed_n;
if(missed < best[0].miss){
top_2d curFpair(f1,f2,missed,missed_p,missed_n);
best[0] = curFpair;
sort(best,best+TOPK);
}
total_checked_g+=g;
}
if(f1%1000==0)
cout << "f1=" << f1 << "; total_checked_g=" <<total_checked_g<<endl;
}
t = clock() - t;
int per_checked_g = 2*total_checked_g/(FEATURE_NUM-1)/FEATURE_NUM;
cout << "total_checked_g="<<total_checked_g<<"; per_checked_g="<< per_checked_g << endl;
fprintf (stderr, "******best feature pair (%d,%d:%d) (horizontal)*****\n",features[best[TOPK-1].f1].fid,features[best[TOPK-1].f2].fid,best[TOPK-1].miss);
fprintf (stderr, "no_transformation_baseline find best feature pair: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
fprintf(ftime,"%ld\t%d\t%f\n",total_checked_g,per_checked_g,((float)t)/CLOCKS_PER_SEC);
string outfile_name = output_dir + "FPs.txt";
FILE *no_transform_file = fopen(outfile_name.c_str(),"w");
for(int i=TOPK-1;i>=0;i--){
int f1_tmp= features[best[i].f1].fid, f2_tmp=features[best[i].f2].fid;
int f1 = min(f1_tmp,f2_tmp), f2 = max(f1_tmp,f2_tmp);
fprintf(no_transform_file,"%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n", f1,f2,featureName[f1].c_str(),featureName[f2].c_str(),pos_num*pos_weight+(GENE_NUM-pos_num)-best[i].miss, best[i].miss,pos_num-best[i].miss_p, GENE_NUM-pos_num-best[i].miss_n, best[i].miss_p,best[i].miss_n);
}
fclose(no_transform_file);
}
void sort_genes_weighted(FILE *ftime){
clock_t t = clock();
ff.clear();
ff.resize(FEATURE_NUM);
vector<topG > gene_correct_p,gene_correct_n; // use top_1d (id feature)-- fid->gid
for(int g=0;g<GENE_NUM;g++){
int g_corrects = 0;
for(int f=0;f<FEATURE_NUM;f++){
if(features[f].vals[g]>0)
g_corrects++;
}
topG tmp(g,g_corrects);
if(g<pos_num)
gene_correct_p.push_back(tmp);
else
gene_correct_n.push_back(tmp);
}
sort(gene_correct_p.begin(),gene_correct_p.end());
cout << gene_correct_p[0].correct << "---test--" <<gene_correct_p[gene_correct_p.size()/2].correct<< "---test--" <<gene_correct_p[gene_correct_p.size()-1].correct<< endl;
sort(gene_correct_n.begin(),gene_correct_n.end());
cout << gene_correct_n[0].correct << "---test--" <<gene_correct_n[gene_correct_n.size()/2].correct<< "---test--" <<gene_correct_n[gene_correct_n.size()-1].correct<< endl;
// FILE* testf = fopen("result/gene_reordering.txt", "w");
cout << "pos_num=" << pos_num << ";GENE_NUM=" <<GENE_NUM << endl;
for(int f=0; f<FEATURE_NUM;f++){
ff[f].correct = features[f].correct;
ff[f].fid = features[f].fid;
for(int g=0; g<pos_num;g++){
ff[f].vals.push_back(features[f].vals[gene_correct_p[g].gid]); //gene_correct[g].fid: gid of gth gene
// if(f==0)
// fprintf(testf,"%d \t %d \t %d\n",g, gene_correct_p[g].gid, gene_correct_p[g].correct);
}
for(int g=0; g<GENE_NUM-pos_num;g++){
ff[f].vals.push_back(features[f].vals[gene_correct_n[g].gid]); //gene_correct[g].fid: gid of gth gene
// if(f==0)
// fprintf(testf,"%d \t %d \t %d\n",g, gene_correct_n[g].gid, gene_correct_n[g].correct);
}
if(ff[f].vals.size() > GENE_NUM)
fprintf(stderr,"%d \t %ld \t %d\n",f, ff[f].vals.size(), GENE_NUM);
}
//fclose(testf);
t = clock() - t;
fprintf (stderr, "sort_genes_weighted : %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
fprintf(ftime,"%f\t",((float)t)/CLOCKS_PER_SEC);
}
void sort_genes(FILE *ftime){
clock_t t = clock();
ff.clear();
ff.resize(FEATURE_NUM);
vector<topG > gene_correct; // use top_1d (id feature)-- fid->gid
for(int g=0;g<GENE_NUM;g++){
int g_corrects = 0;
for(int f=0;f<FEATURE_NUM;f++){
if(features[f].vals[g]>0)
g_corrects++;
}
topG tmp(g,g_corrects);
gene_correct.push_back(tmp);
}
sort(gene_correct.begin(),gene_correct.end());
for(int f=0; f<FEATURE_NUM;f++){
ff[f].correct = features[f].correct;
ff[f].fid = features[f].fid;
for(int g=0; g<GENE_NUM;g++){
ff[f].vals.push_back(features[f].vals[gene_correct[g].gid]); //gene_correct[g].fid: gid of gth gene
}
if(ff[f].vals.size() > GENE_NUM)
fprintf(stderr,"***********%d \t %ld \t %d**********\n",f, ff[f].vals.size(), GENE_NUM);
}
t = clock() - t;
fprintf (stderr, "sort_genes : %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
fprintf(ftime,"%f\t",((float)t)/CLOCKS_PER_SEC);
}
void baseline(FILE *ftime){
clock_t t = clock();
top_2d best[TOPK];
for(int i=0;i<TOPK; i++)
best[i].miss=2*GENE_NUM;
long total_checked_g=0;
for(int f1=0;f1<FEATURE_CONSIDER;f1++){
for(int f2=f1+1; f2<FEATURE_NUM;f2++){
int g=0, missed_p=0, missed_n=0,missed =0;
for(; g<pos_num; g++){
if(ff[f1].vals[g]+ff[f2].vals[g] <= 0){
missed_p++;
}
}
for(;g<GENE_NUM;g++){
if(ff[f1].vals[g]+ff[f2].vals[g] <= 0){
missed_n++;
}
}
missed=pos_weight*missed_p+missed_n;
if(missed < best[0].miss){
top_2d curFpair(f1,f2,missed,missed_p,missed_n);
best[0] = curFpair;
sort(best,best+TOPK);
}
total_checked_g+=g;
}
}
t = clock() - t;
int per_checked_g = 2*total_checked_g/(2*FEATURE_NUM-FEATURE_CONSIDER-1)/(FEATURE_CONSIDER);
cout << "total_checked_g="<<total_checked_g<<"; per_checked_g="<< per_checked_g << endl;
fprintf (stderr, "******best feature pair (%d,%d:%d) among %d features considered(horizontal)*****\n",ff[best[TOPK-1].f1].fid,ff[best[TOPK-1].f2].fid,best[TOPK-1].miss, FEATURE_CONSIDER);
fprintf (stderr, "baseline find best feature pair: %ld clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
fprintf(ftime,"%ld\t%d\t%f\n",total_checked_g,per_checked_g,((float)t)/CLOCKS_PER_SEC);
string outfile_name = output_dir + "FPs.txt";
FILE *rocchio_file = fopen(outfile_name.c_str(),"w");
for(int i=TOPK-1;i>=0;i--){
int f1_tmp= ff[best[i].f1].fid, f2_tmp=ff[best[i].f2].fid;
int f1 = min(f1_tmp,f2_tmp), f2 = max(f1_tmp,f2_tmp);
fprintf(rocchio_file,"%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n", f1,f2,featureName[f1].c_str(),featureName[f2].c_str(),pos_num*pos_weight+(GENE_NUM-pos_num)-best[i].miss, best[i].miss,pos_num-best[i].miss_p, GENE_NUM-pos_num-best[i].miss_n, best[i].miss_p,best[i].miss_n);
}
fclose(rocchio_file);
}
void baseline_vertical(FILE *ftime){
clock_t t = clock();
top_2d best[TOPK];
for(int i=0;i<TOPK; i++)
best[i].miss=2*GENE_NUM;
long total_checked_g=0;
for(int f1=1;f1<FEATURE_CONSIDER;f1++){
for(int f2=0; f2<f1;f2++){
int g=0, missed_p=0, missed_n=0,missed =0;
for(; g<pos_num; g++){
if(ff[f1].vals[g]+ff[f2].vals[g] <= 0){
missed_p++;
}