-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunNetwork.cpp
More file actions
1023 lines (892 loc) · 32.2 KB
/
Copy pathrunNetwork.cpp
File metadata and controls
1023 lines (892 loc) · 32.2 KB
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 <iostream>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <sstream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <getopt.h>
#include <dirent.h>
#include <omp.h>
using namespace std;
static string VERSION = "1.8";
static int DEBUG = 0;
//Integer to Identifier mapping
vector<string> int_id;
map<string, unsigned int> id_int;
//pValues index synced to identifier mapping
vector<double> int_pValue;
//keeps record of which p-Values have been accessed
vector<int> visited;
//Holds bin with highest index
unsigned int maxSize = 0;
//Holds max index of proteins in analysis
unsigned int proteinsInAnalysis;
//Holds number of proteins in network ==> NMB will be run for
unsigned int proteinsUnderConsideration;
//Bins
vector<vector<unsigned> > bin_prot;
vector<vector<unsigned> > prot_bin;
vector<unsigned> prot_con;
//Protein protein network
vector<vector<unsigned> > prot_prot;
//Random networks
vector<vector<vector<unsigned> > > randomNetworks;
//Keep genes to run NMB for
map<string, int> gene_compute;
void usage(){
cout << "runNetwork, Version: " << VERSION << endl << endl;
cout << "Usage:" << endl <<
"runNetwork " << "[arguments]" << endl << endl <<
"Required arguments:" << endl <<
" -i | --iterations number of iterations" << endl <<
" -j | --job jobnumber for partitioning input set (>0)" << endl <<
" -J | --jobs total number of jobs" << endl <<
" -n | --network tab-delimited file for interaction network, has to be symetric" << endl <<
" -N | --name jobname, defines output filename" << endl <<
" -p | --pvalues file to read p-values, 1st column is identifier" << endl <<
"Optional arguments:" << endl <<
" -b | --binaryNetworkFile flag for indicating binary network ('short' fields). Requires mapping file" << endl <<
" -c | --correctInflation sets correction factor for genomic inflation" << endl <<
" -C | --calculateOnlyPhi set to only calculate Phi and exit" << endl <<
" -d | --printDistributions print distributions of phi-values" << endl <<
" -D | --networkDirectory directory for getting the random networks" << endl <<
" -e | --estimatePerformance just prints out runtime for NMB calculation, exluding loading/initialization" << endl <<
" -f | --flipped inverted analysis, look for genes with non-mutated neighborhood" << endl <<
" -g | --ignoreInputNodes set to include interactors when resampling" << endl <<
" -G | --ignoreCentralNode set to include query node when resampling" << endl <<
" -h | --help print this message" << endl <<
" -l | --list file with list of genes for which to compute NMB" << endl <<
" -m | --minimumPvalues sets the minimum neighbours with p-value < 1 for a node to be considered" << endl <<
" -M | --mappingFile mapping file for p-Value IDs -> network IDs" << endl <<
" -o | --outputDirectory sets output directory" << endl <<
" -P | --pValColumn column index to take the p-value (counting starts at 0 ;) ) [default=3]" << endl <<
" -r | --ignoreConnectivity set to ignore connectivity when permuting" << endl <<
" -R | --randomize read in the p-values, return randomized and quit" << endl <<
" -s | --separator set separator for network file ('\t' is default, ',' if a .csv file is provided)" << endl <<
" -t | --threads set number of parallel processes run (default = 1), needs to be compiled using -fopenmp" << endl <<
" -v | --version print version number" << endl <<
" -V | --verbose extensive output" << endl <<
" -I | --included list genes with significant p-Values not found in the network (and their p-Values)" << endl <<
cout << endl << "Note: Genes with no assigned p-Value will be removed from the network" << endl << endl;
}
int myrandom (int i) {return rand()%i; }
long double calculateChiSquare(vector<unsigned>* nodes){
double score = 0;
for(int i=0;i<nodes->size();i++){
score += log(int_pValue[(*nodes)[i]]);
if(DEBUG) cerr << (*nodes)[i] << "\t" << int_id[(*nodes)[i]] << "\t" << int_pValue[(*nodes)[i]] << endl;
}
if(score != 0){score *= -2;}
return score;
}
long double calculateChiSquareFromValues(vector<double>* values){
double score = 0;
for(int i=0;i<values->size();i++){
score += log((*values)[i]);
}
if(score != 0){score *= -2;}
return score;
}
unsigned int getNodesFromBin(unsigned int binIndex, unsigned int nodeCounter, vector<unsigned int>* newNodes, vector<int>* selectedNodes, unsigned int* binsProtsSelected){
if(bin_prot[binIndex].size() == 0){return 0;}
unsigned int selectedNodesCounter = 0;
unsigned int selectedBinMax = bin_prot[binIndex].size();
unsigned int minSelectForPermute = 10;
if(binsProtsSelected[binIndex] < 1){return 0;}
//TODO: CHECK!!!!!
//if(nodeCounter<minSelectForPermute){
unsigned int randomIndex = rand()%selectedBinMax;
while((*selectedNodes)[bin_prot[binIndex][randomIndex]]){
randomIndex = rand()%selectedBinMax;
}
unsigned int prot = bin_prot[binIndex][randomIndex];
newNodes->push_back(prot);
(*selectedNodes)[prot] = true;
selectedNodesCounter++;
//save bin status
for(unsigned int k=0;k<prot_bin[prot].size();k++){binsProtsSelected[prot_bin[prot][k]]--;}
//}else{
// random_shuffle(bin_prot[binIndex].begin(), bin_prot[binIndex].end(), myrandom);
//
// for(unsigned int j=0;j<nodeCounter && j<selectedBinMax;j++){
// unsigned int prot = bin_prot[binIndex][j];
// if(!(*selectedNodes)[prot]){
// newNodes->push_back(prot);
// (*selectedNodes)[prot] = true;
// selectedNodesCounter++;
// //save bin status
// for(unsigned int k=0;k<prot_bin[prot].size();k++){binsProtsSelected[prot_bin[prot][k]]--;}
// }
// }
//}
return selectedNodesCounter;
}
//void getRandomNodes(vector<unsigned int>* nodes, vector<unsigned int>* newNodes, bool ignoreInputNodes){
void getRandomNodes(int index, vector<unsigned int>* newNodes, bool ignoreInputNodes, bool ignoreCentralNode){
vector<int> selectedNodes(proteinsInAnalysis);
if(DEBUG) cerr << "Index: " << index << endl;
unsigned int binsProtsSelected[maxSize];
unsigned int binsToSelectFrom[maxSize];
for(int i=0;i<maxSize;i++){
binsToSelectFrom[i] = 0;
binsProtsSelected[i] = bin_prot[i].size();
}
for(int i=0;i<proteinsInAnalysis;i++){selectedNodes[i] = false;}
if(ignoreInputNodes && !(prot_prot[index].size() > proteinsUnderConsideration/2.0)){
for(unsigned int j=0;j<prot_prot[index].size();j++){
unsigned int prot = prot_prot[index][j];
selectedNodes[prot] = true;
for(unsigned int k=0;k<prot_bin[prot].size();k++){
binsProtsSelected[prot_bin[prot][k]]--;
}
}
}
if(ignoreCentralNode){
selectedNodes[index] = true;
for(unsigned int k=0;k<prot_bin[index].size();k++){
binsProtsSelected[prot_bin[index][k]]--;
}
}
//create vector of bins to select from
for(unsigned int i=0;i<prot_prot[index].size();i++){
unsigned int size = prot_con[prot_prot[index][i]];
binsToSelectFrom[size]++;
if(DEBUG) cerr << "BinToSelectFrom: " << size << "\t->\t" << binsToSelectFrom[size] << endl;
}
//select proteins from corresponding bins
for(unsigned int i=0;i<maxSize;i++){
if(binsToSelectFrom[i] > 0){
unsigned int selectedNodesCounter = 0;
unsigned int binCounter = 1;
while(selectedNodesCounter < binsToSelectFrom[i]){
int dir = binCounter % 2;
if(dir != 1){dir = binCounter/2;}else{dir = -1*((binCounter-1)/2);}
binCounter++;
int newBin = i+dir;
if(newBin >= maxSize){continue;}
if(DEBUG) cerr << "GetNodeFrom: " << newBin << endl;
selectedNodesCounter += getNodesFromBin(newBin, binsToSelectFrom[i]-selectedNodesCounter, newNodes, &selectedNodes, binsProtsSelected);
}
}
}
}
vector<string> getFilesFromDirectory(string path = ".") {
DIR* dir;
dirent* pdir;
vector<string> files;
dir = opendir(path.c_str());
while ( (pdir = readdir(dir)) ) {
//skip hidden files and special directories
if(pdir->d_name[0] == '.'){continue;}
//take only the _InWeb3_HUGO_sym files
files.push_back(pdir->d_name);
}
return files;
}
int loadNetwork(string networkFile, vector<vector<unsigned> >* prot_prot_, char separator_='\t', int verbose=0, int mappingFile=0, int binaryNetworkFile=0, int symetric=1){
visited.resize(25000);
string line_;
string type = networkFile.substr(networkFile.size()-4, networkFile.size()-1);
ifstream IN_;
// From now on by definition: mapping file = binary
if(binaryNetworkFile){
if (verbose) cout << "Loading PPI data using mapping file [binary]..." << endl;
IN_.open(networkFile.c_str(), ios::binary);
}else{
if (verbose) cout << "Loading PPI data..." << endl;
IN_.open(networkFile.c_str());
}
if(!IN_){
cerr << "Network file not found: " << endl << networkFile.c_str() << endl;
return 0;
}
// From now on by definition: mapping file = binary
bool csv = false;
if(strncmp(type.c_str(), ".csv", 4) == 0) csv = true;
if(binaryNetworkFile){
IN_.seekg (0, IN_.end);
long length = (long)IN_.tellg();
IN_.seekg (0, IN_.beg);
if (verbose) cerr << "Filesize: " << length << endl;
short int1, int2;
while((long)IN_.tellg() != length){
IN_.read((char*)&int1, 2);
IN_.read((char*)&int2, 2);
if(int_pValue[int1] == 0){continue;}
if(int_pValue[int2] == 0){continue;}
//Remove self interactions
if (int1 == int2) {continue;}
if(int1 >= prot_prot_->size()){
prot_prot_->resize(int1+1);
}
(*prot_prot_)[int1].push_back(int2);
}
if (verbose) cerr << "Done reading" << endl;
}else{
while(getline(IN_, line_)){
vector<string> tokens;
if(line_[0] == '#'){continue;}
istringstream iss(line_);
string token;
if(csv){
while(getline(iss, token, ',')){
if(token[0] == '"') token = token.substr(1, token.size()-2);
if(strncmp(token.c_str(), "gene1", 5) == 0){continue;}
tokens.push_back(token);
}
}else{
while(getline(iss, token, separator_)){
tokens.push_back(token);
}
}
short int1, int2;
if(!mappingFile){
if(id_int.find(tokens[0]) == id_int.end()){
int_pValue.push_back(1);
int_id.push_back(tokens[0]);
id_int.insert(pair<string, unsigned int>(tokens[0], int_id.size()-1));
}
if(id_int.find(tokens[1]) == id_int.end()){
int_pValue.push_back(1);
int_id.push_back(tokens[1]);
id_int.insert(pair<string, unsigned int>(tokens[1], int_id.size()-1));
}
int1 = id_int[tokens[0]];
int2 = id_int[tokens[1]];
}else{
int1 = atoi(tokens[0].c_str());
int2 = atoi(tokens[1].c_str());
}
//Remove self interactions
if (int1 == int2) {continue;}
if(int1 >= visited.size()){
visited.resize(int1+1);
}
if(int2 >= visited.size()){
visited.resize(int2+1);
}
//mark genes as visited
visited[int1] = 1;
visited[int2] = 1;
if(int1 >= prot_prot_->size()){
prot_prot_->resize(int1+1);
}
(*prot_prot_)[int1].push_back(int2);
}
}
IN_.close();
//if not symetric
//just fill up networks
unsigned i = 0;
unsigned sym = 1;
while((*prot_prot_)[i].size()<1){ i++; }
for(unsigned j=0;j<(*prot_prot_)[i].size();j++){
if(find((*prot_prot_)[(*prot_prot_)[i][j]].begin(), (*prot_prot_)[(*prot_prot_)[i][j]].end(), i)==(*prot_prot_)[(*prot_prot_)[i][j]].end()){ sym=0; }
}
if(!sym){
// foreach prot_prot_, search and fill up if not present
for(unsigned i=0;i<(*prot_prot_).size();i++){
for(unsigned j=0;j<(*prot_prot_)[i].size();j++){
unsigned newId = (*prot_prot_)[i][j];
if(newId >= prot_prot_->size()){
prot_prot_->resize(newId+1);
(*prot_prot_)[newId].push_back(i);
}else if(find((*prot_prot_)[newId].begin(), (*prot_prot_)[newId].end(), i)==(*prot_prot_)[newId].end()){
(*prot_prot_)[newId].push_back(i);
}
}
}
}
//Check if we have a pValue for the key
//TODO: Set pValue to 1
for(unsigned i=0;i<(*prot_prot_).size();i++){
}
return 1;
}
int loadMappingFile(string mapping, vector<string>* int_id_, map<string, unsigned int>* id_int_){
ifstream in_mapping(mapping.c_str());
if(!in_mapping){
cerr << "Mapping file not found: " << endl << mapping.c_str() << endl;
return 0;
}
unsigned i = 0;
string line;
while(getline(in_mapping, line)){
vector<string> tokens;
if(line[0] == '#'){continue;}
istringstream iss(line);
string token;
while(getline(iss, token, '\t')){
tokens.push_back(token);
}
//token[0]: pVal ID
//token[1]: InWeb IDs
unsigned int newInt = (unsigned int)atoi(tokens[1].c_str());
if(newInt >= int_id_->size()){
int_id_->resize(newInt+1);
}
(*int_id_)[newInt] = tokens[0];
id_int_->insert(pair<string, unsigned int>(tokens[0], newInt));
}
return 1;
}
long double calculatePfromPhis(double chi_, long unsigned int iterations_, map<long double, unsigned int>* phis_, int highChiIsGood_){
long posCounter_ = 0;
if(highChiIsGood_ == 1){
for (map<long double, unsigned int>::iterator itr=(*phis_).begin(); itr!=(*phis_).end(); itr++){
if(chi_>itr->first){posCounter_+=itr->second;}
}
}else{
for (map<long double, unsigned int>::iterator itr=(*phis_).begin(); itr!=(*phis_).end(); itr++){
if(chi_<itr->first){posCounter_+=itr->second;}
}
}
long double p_ = 1-posCounter_/(iterations_+1.0);
return p_;
}
int main (int argc, char **argv) {
srand ( time(NULL) ); //initialize the random seed
clock_t t1,t2;
t1=clock();
int ignoreInputNodes = 1;
int ignoreCentralNode = 1;
int highChiIsGood = 1;
unsigned int job = 1;
unsigned int jobs = 1;
unsigned int pValColumn = 3;
long unsigned int iterations = 10000;
bool verbose = 0;
bool included = 0;
bool version = 0;
bool help = 0;
bool flipped = 0;
bool randomize = 0;
bool ignoreConnectivity = 0;
int minimumPvalues = 1;
bool loadRandomNetworks = 0;
bool onlyChi = 0;
bool mappingFile = 0;
bool estimatePerformance = 0;
bool printPhis = 0;
bool limit = 0;
bool binaryNetworkFile = false;
int nthreads = 1;
#if defined(_OPENMP)
nthreads = omp_get_thread_num();
#endif
float lambda = 1;
string pValueData, ppiData, jobName, limitList;
string OUTDIR = "./";
string NETWORKDIR = ".";
string mapping = "";
char separator = '\t';
const struct option longopts[] =
{
{"binaryNetworkFile", required_argument, 0, 'b'},
{"correctInflation", required_argument, 0, 'c'},
{"onlyChi", no_argument, 0, 'C'},
{"printDistributions", no_argument, 0, 'd'},
{"networkDirectory", required_argument, 0, 'D'},
{"estimatePerformance",no_argument, 0, 'e'},
{"flipped", no_argument, 0, 'f'},
{"ignoreInputNodes", no_argument, 0, 'g'},
{"ignoreCentralNode", no_argument, 0, 'G'},
{"help", no_argument, 0, 'h'},
{"iterations", required_argument, 0, 'i'},
{"included", no_argument, 0, 'I'},
{"job", required_argument, 0, 'j'},
{"jobs", required_argument, 0, 'J'},
{"list", required_argument, 0, 'l'},
{"minimumPvalues", required_argument, 0, 'm'},
{"mappingFile", required_argument, 0, 'M'},
{"network", required_argument, 0, 'n'},
{"name", required_argument, 0, 'N'},
{"outputDirectory", required_argument, 0, 'o'},
{"pvalues", required_argument, 0, 'p'},
{"pValColumn", required_argument, 0, 'P'},
{"ignoreConnectivity", no_argument, 0, 'r'},
{"randomize", no_argument, 0, 'R'},
{"separator", required_argument, 0, 's'},
{"nthreads", required_argument, 0, 't'},
{"version", no_argument, 0, 'v'},
{"verbose", no_argument, 0, 'V'},
{0,0,0,0}
};
opterr=1;
int c;
//while ((c = getopt_long(argc, argv, "vhVfRgGrCdeIi:j:J:p:P:n:N:o:m:c:D:M:", longopts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "bc:CdD:efgGhi:I:j:J:l:m:M:n:N:o:p:P:rRs:t:vV", longopts, NULL)) != -1) {
switch (c) {
case 'b':
binaryNetworkFile = true;
break;
case 'c':
lambda = atof(optarg);
break;
case 'C':
onlyChi = 1;
break;
case 'd':
printPhis = 1;
break;
case 'D':
NETWORKDIR = optarg;
loadRandomNetworks = 1;
break;
case 'e':
estimatePerformance = 1;
break;
case 'f':
flipped = 1;
highChiIsGood = (flipped==1) ? 0 : 1;
break;
case 'g':
ignoreInputNodes = 0;
break;
case 'G':
ignoreCentralNode = 0;
break;
case 'h':
help = 1;
usage();
return 0;
case 'i':
iterations = atoi(optarg);
break;
case 'I':
included = 1;
break;
case 'j':
job = atoi(optarg);
break;
case 'J':
jobs = atoi(optarg);
break;
case 'l':
limit = 1;
limitList = optarg;
break;
case 'm':
minimumPvalues = atoi(optarg);
break;
case 'M':
mapping = optarg;
mappingFile = 1;
break;
case 'n':
ppiData = optarg;
break;
case 'N':
jobName = optarg;
break;
case 'o':
OUTDIR = optarg;
break;
case 'p':
pValueData = optarg;
break;
case 'P':
pValColumn = atoi(optarg);
break;
case 'r':
ignoreConnectivity = 1;
break;
case 'R':
randomize = 1;
break;
case 's':
if(optarg[0]=='\\' && optarg[1]=='t') {
separator = '\t';
break;
}else{
separator = *optarg;
break;
}
case 't':
nthreads = atoi(optarg);
#if !defined(_OPENMP)
if(nthreads > 1) cerr << "Warning: More than one thread requested, but compiled without openMP" << endl;
nthreads = 1;
#endif
break;
case 'v':
cerr << "runNetwork, Version: " << VERSION << endl;
return 0;;
case 'V':
verbose = 1;
cerr << "Verbose mode, printing a lot..." << endl;
break;
case ':':
break;
case '?':
break;
default:
abort ();
}
}
if(!job){
cerr << "ERROR: 'job' parameter missing" << endl << endl;
usage();
return 0;
}
if(!jobs){
cerr << "ERROR: 'jobs' parameter missing" << endl << endl;
usage();
return 0;
}
job--;
if(pValueData.size() < 1){
cerr << "ERROR: no p-Value data available" << endl;
usage();
return 0;
}
if(ppiData.size() < 1){
cerr << "ERROR: no network data available" << endl;
usage();
return 0;
}
if(jobName.size() < 1){
cerr << "ERROR: 'jobName' parameter missing" << endl;
usage();
return 0;
}
//load mapping file
// int_id
// id_int
if(mappingFile){
if (verbose) cerr << "Loading mapping data..." << endl;
if(!loadMappingFile(mapping, &int_id, &id_int)){
return 0;
}
int_pValue.resize(int_id.size());
for(int i=0;i<int_pValue.size();i++){int_pValue[i]=1;};
}
//Load list of genes for compute (skip computation for genes NOT on this list)
if(limit){
if (verbose) cerr << "Loading genes to compute for..." << endl;
ifstream in_limit(limitList.c_str());
if(!in_limit){
cerr << "Limit file not found: " << endl << limitList.c_str() << endl;
return 0;
}
string line;
while(getline(in_limit, line)){
gene_compute[line] = 1;
}
}
//Load p-Values
ifstream in(pValueData.c_str());
if(!in){
cerr << "ERROR: p-Value file not found:" << endl << pValueData.c_str() << endl;
return 0;
}
//creates id_int mapping
if (verbose) cerr << "Loading p-Values data..." << endl;
unsigned i = 0;
string line;
while(getline(in, line)){
vector<string> tokens;
if(line[0] == '#'){continue;}
istringstream iss(line);
string token;
while(getline(iss, token, '\t')){
tokens.push_back(token);
}
if (pValColumn >= tokens.size()) {
cerr << "ERROR: invalid column reference entered" << endl;
return 0;
}
//if token casts inaccurately to 0 (meaning an invalid string input was found), skip
if (((double)atof(tokens[pValColumn].c_str()) == 0) && (tokens[pValColumn].compare("0") != 0)){continue;}
if(mappingFile){
if(id_int.find(tokens[0]) == id_int.end()) continue;
int_pValue[id_int[tokens[0]]] = (double)atof(tokens[pValColumn].c_str());
//if(tokens[0] == "ABCF1") cerr << id_int[tokens[0]] << "\t" << int_pValue[id_int[tokens[0]]] << endl;
}else{
int_pValue.push_back((double)atof(tokens[pValColumn].c_str()));
int_id.push_back(tokens[0]);
id_int.insert(pair<string, unsigned int>(tokens[0], i++)); // i gets incremented here!!!
}
}
in.close();
if (verbose) {
unsigned int count = 0;
for(unsigned i=0;i<=int_pValue.size();i++){
if(int_pValue[i] > 0) count++;
}
cout << count << " genes with p-values" << endl;
}
if(randomize == 1){
random_shuffle(int_pValue.begin(), int_pValue.end(), myrandom);
for(int i=0;i<int_pValue.size();i++){
cout << int_id[i] << "\tx\tx\t" << int_pValue[i] << endl;
}
return 1;
}
if(int_pValue.size() < 1){
cerr << "ERROR: no p-values loaded" << endl;
return 0;
}
//Load network as int -> array of interactors
if(!loadNetwork(ppiData, &prot_prot, separator, verbose, mappingFile, binaryNetworkFile)){
return 0;
}
proteinsInAnalysis = prot_prot.size();
if (verbose) cerr << "Proteins loaded: " << proteinsInAnalysis << endl;
prot_con.reserve(prot_prot.size());
for (int i=0; i<prot_prot.size(); i++) {
prot_con.push_back(prot_prot[i].size());
if(prot_prot[i].size() > 0) proteinsUnderConsideration++;
}
if (verbose) cerr << "Proteins in analysis: " << proteinsUnderConsideration << endl;
if(proteinsUnderConsideration < 1){
cerr << "ERROR: No proteins from network would be considered" << endl;
cerr << "No protein from the input has at least one neighbour with a p-Value?" << endl;
return 0;
}
if(!onlyChi){
// Put proteins into bins
for (int i=0; i<prot_prot.size(); i++) {
unsigned size = prot_con[i];
if(size > maxSize){
maxSize = size;
if(DEBUG) cerr << "MaxSize: " << maxSize << endl;
}
}
maxSize++;
if(DEBUG) cerr << "Done estimating protein degrees" << endl;
bin_prot.resize(maxSize+1);
prot_bin.resize(prot_prot.size()+1);
//Fill initial bins
for (int i=0; i<prot_prot.size(); i++) {
unsigned size = prot_prot[i].size();
if(size == 0){continue;}
bin_prot[size].push_back(i);
prot_bin[i].push_back(size);
}
if(DEBUG) cerr << "Done filling bins" << endl;
vector<vector<unsigned> > ORG_bin_prot(bin_prot);
//Now fill up to at least 20
for (int i=0; i<bin_prot.size(); i++) {
unsigned size = bin_prot[i].size();
if(size == 0 || size >= 20){continue;}
unsigned count = 2;
while(bin_prot[i].size() < 20){
int dir = count % 2;
if(dir != 1){dir = count/2;}else{dir = -1*((count-1)/2);}
int newBin = i+dir;
if (newBin <= ORG_bin_prot.size()) {
if(newBin < maxSize && newBin > 0 && ORG_bin_prot[newBin].size() > 0){ //data in bin
for(unsigned j=0;j<ORG_bin_prot[newBin].size();j++){ //foreach prot j in new bin
bool proteinInBin = false;
for(unsigned k=0;k<prot_bin[bin_prot[newBin][j]].size();k++){ //foreach bin k for protein
if(prot_bin[bin_prot[newBin][j]][k] == i){proteinInBin = true;}
}
if(!proteinInBin){
bin_prot[i].push_back(bin_prot[newBin][j]);
prot_bin[bin_prot[newBin][j]].push_back(i);
}
}
}
}
count++;
}
}
if(DEBUG) cerr << "Done completing bins" << endl;
}
//if a directory is specified, load random networks
if(loadRandomNetworks){
if(verbose) cerr << "Loading random networks from '" << NETWORKDIR << "'" << endl;
vector<string> files;
files = getFilesFromDirectory(NETWORKDIR);
randomNetworks.reserve(files.size());
for(unsigned i=0;i<files.size();i++){
ostringstream file;
file << NETWORKDIR << "/" << files[i];
loadNetwork(file.str(), &randomNetworks[i], verbose, mappingFile);
if(verbose) cerr << i << "/" << files.size() << NETWORKDIR << files[i] << endl;
}
}
t2=clock();
float diff (((float)t2-(float)t1)/CLOCKS_PER_SEC);
if(verbose) cerr << "Everything initialized, starting calculations... " << endl;
if(verbose) cerr << "Initialization took " << diff << "s" << endl;
/////////////////////////////////////////////////////////////////////////////
//
// Calculations start from here
//
/////////////////////////////////////////////////////////////////////////////
// Initialize timer for computation
t1=clock();
//get list of indexes to compute
int protCounter = 1;
vector<unsigned int> indexes;
indexes.reserve((int)prot_prot.size()/jobs+1);
for (int index=0; index<prot_prot.size(); index++) {
if(protCounter++%jobs != job){continue;}
//skip if no interactions known
if(prot_prot[index].size() <= 0){continue;}
//skip if not in gene_compute
if(limit){if(gene_compute.find(int_id[index]) == gene_compute.end()){continue;}}
indexes.push_back(index);
}
vector<string> resultLines;
//resultLines.reserve((int)prot_prot.size()/jobs+1);
resultLines.resize((int)indexes.size());
if(verbose){cerr << (int)indexes.size() << " genes to calculate" << endl;}
int COUNTER = 0;
#if defined(_OPENMP)
if(verbose){cerr << nthreads << " thread(s) started" << endl;}
clock_t clock_timer;
double wall_timer = omp_get_wtime();
#pragma omp parallel num_threads(nthreads)
{
#pragma omp for
#endif
for (int proteinLoopCounter=0; proteinLoopCounter<indexes.size(); proteinLoopCounter++) {
//unsigned int tid = omp_get_thread_num();
unsigned int index = indexes[proteinLoopCounter];
if (DEBUG){cerr << index << "\t" << int_id[index] << endl;}
map<long double, unsigned int> chis_count;
vector<long double> chis;
chis.reserve(iterations);
int actualIterations = 0;
unsigned int pCount = 0;
for(unsigned int i=0;i<prot_prot[index].size();i++){
if(int_pValue[prot_prot[index][i]] < 1){pCount++;}
}
double chi = calculateChiSquare(&prot_prot[index]);
if(onlyChi){ //calculate only Chi2
//nothing
}else if(pCount < minimumPvalues){ //minimum number of affected neighbors not matched
chi = 0; //also takes care if 0 neighbors are affected
chis_count[0] = iterations;
}else if(ignoreConnectivity == 0){ //replace taking network connectivity into consideration
for(int i=0;i<iterations;i++){
vector<unsigned int> newNodes;
newNodes.reserve(prot_prot[index].size());
getRandomNodes(index, &newNodes, ignoreInputNodes, ignoreCentralNode);
if (DEBUG) cerr << prot_prot[index].size() << " " << newNodes[0] << endl;
chis_count[calculateChiSquare(&newNodes)]++;
//estimate p value convergence
if(i%1000==1){
if(DEBUG) {cout << int_id[index] << "\t" << 1.0/i << "\t" << 1.0/(i*0.1) << "\t" << calculatePfromPhis(chi, i, &chis_count, highChiIsGood) << endl;};
if(calculatePfromPhis(chi, i, &chis_count, highChiIsGood) >= 1.0/(i*0.9)){
actualIterations=i;
break;
}
}
}
} else if(ignoreConnectivity == 1){ //just select random replacment nodes
//get list of values and remove central node or all incoming ones
vector<double> T_int_pValue;
vector<bool> selectedNodes(proteinsInAnalysis);
for(int i=0;i<proteinsInAnalysis;i++){selectedNodes[i] = false;}
if(ignoreCentralNode){selectedNodes[index] = true;}
if(ignoreInputNodes){
for(unsigned int j=0;j<prot_prot[index].size();j++){
selectedNodes[prot_prot[index][j]] = true;
}
}
for(unsigned int i=0;i<prot_prot.size();i++){
if(!((prot_prot[i].size()<=0) | selectedNodes[i])){
T_int_pValue.push_back(int_pValue[i]);
}
}
int T_int_pValue_max = T_int_pValue.size();
if(verbose){cerr << index << "\t" << T_int_pValue.size() << endl;}
//for i iterations: randomize list and pick X new values
vector<bool> selectedPs(T_int_pValue_max);
for(int i=0;i<iterations;i++){
vector<double> ps;
for(int j=0;j<T_int_pValue_max;j++){selectedPs[j] = false;}
for(unsigned int k=0;k<prot_prot[index].size();k++){
unsigned int randomIndex = rand()%T_int_pValue_max;
while(selectedPs[randomIndex]){
randomIndex = rand()%T_int_pValue_max;
}
ps.push_back(T_int_pValue[randomIndex]);
}
chis_count[calculateChiSquareFromValues(&ps)]++;
//estimate p value convergence
if(i%1000==1){
if(calculatePfromPhis(chi, i, &chis_count, highChiIsGood)>=1/(i*0.9)/i){
actualIterations = i;
break;
}
}
}
}
unsigned int posCounter = 0;
if(actualIterations==0){actualIterations=iterations;}
long double nmbScore = calculatePfromPhis(chi, actualIterations, &chis_count, highChiIsGood);
ostringstream oss;
oss << int_id[index] << "\t" << chi << "\t" << nmbScore;
//Add phi values for distribution plotting
if(printPhis==1){
vector<double> phis;
phis.reserve(iterations);
for (map<long double, unsigned int>::iterator itr=chis_count.begin(); itr!=chis_count.end(); itr++){
for(int i=0;i<itr->second;++i){
phis.push_back(itr->first);
}
}
stringstream ss;
for(int i=0; i<phis.size();++i){
if(i != 0) ss << ",";
ss << phis[i];
}
oss << "\t" << ss.str();
}
resultLines[proteinLoopCounter] = oss.str();
} //for index end
#if defined(_OPENMP)
} //omp end
#endif
if(verbose){cerr << "Done calculating all p-values" << endl;}
if(!estimatePerformance){
ostringstream filename;
job++;
filename << OUTDIR << "/" << jobName << "_j" << job << "_J" << jobs <<
"_i" << iterations << "_f" << flipped << "_g" << ignoreInputNodes << ignoreCentralNode <<
"_P" << pValColumn << "_r" << ignoreConnectivity << "_m" << minimumPvalues << ".txt";
ofstream myfile (filename.str().c_str());
if (myfile.is_open()){
for(int i=0;i<resultLines.size();i++){
myfile << resultLines[i] << endl;
}
t2=clock();
diff = (((float)t2-(float)t1)/CLOCKS_PER_SEC);
myfile << "#" << diff << endl;
}
myfile.close();
}
if(included){
cout << "Printing genes with significant p-Values not found in the network to file \"notIncluded.txt\"..." << endl;