-
Notifications
You must be signed in to change notification settings - Fork 3
/
ecopd.cpp
1400 lines (1277 loc) · 45.2 KB
/
ecopd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
/*
* ecopd.cpp
*
* Created on: Oct 30, 2013
* Author: Olga
*/
#include <sstream>
#include "ecopd.h"
#include "split.h"
#include "pdnetwork.h"
#include "mtreeset.h"
#include "ecopdmtreeset.h"
#include "graph.h"
ECOpd::ECOpd(const char *userTreeFile, bool &is_rooted) : MTree(userTreeFile,is_rooted) {}
ECOpd::ECOpd() :MTree(){}
ECOpd::~ECOpd(){}
void ECOpd::initializeEcoPD(Params ¶ms){
}
void ECOpd::readInitialTaxa(const char *infile){
ifstream in;
//cout<<"Reading taxa to be included in the final optimal subset from file: "<<infile<< endl;
try {
in.exceptions(ios::failbit | ios::badbit);
in.open(infile);
in.exceptions(ios::badbit);
readInitialTaxa(in);
in.close();
} catch (const char* str) {
outError(str);
} catch (ios::failure) {
outError(ERR_READ_INPUT, infile);
}
}
void ECOpd::readInitialTaxa(istream &in){
string name;
while(!in.eof()){
in>>name;
initialTaxa.push_back(name);
}
if(initialTaxa.size() != 0){
initialTaxa.erase(initialTaxa.end());
}
}
bool ECOpd::OUT_tree(int i){
bool check=false;
for(int j=0;j<OUTtreeTaxa.size();j++)
if(OUTtreeTaxa[j]==i){
check=true;
//cout<<"Taxon "<<i<<" is not present in the tree."<<endl;
}
return(check);
}
void ECOpd::readDAG(const char* infile) {
ifstream in;
//cout<<endl<<"-----------------------------------------------------"<<endl;
if(weighted)
cout<<"Reading Diet Composition matrix from file: "<<infile<<endl;
else
cout<<"Reading Food Web matrix from file: "<<infile<<endl;
try {
in.exceptions(ios::failbit | ios::badbit);
in.open(infile);
in.exceptions(ios::badbit);
readDAG(in);
in.close();
} catch (const char* str) {
outError(str);
} catch (ios::failure) {
outError(ERR_READ_INPUT, infile);
}
}
void ECOpd::readDAG(istream &in) {
int i=0,j=0;
/* ---------------------------------------------------------------------------------------------------------
* Reading Diet Composition matrix from the input file
* ---------------------------------------------------------------------------------------------------------*/
if(!(in>>SpeciesNUM)) throw "The first line must contain the number of species in this Food Web!!";
string str_rest, speciesName;
getline(in, str_rest);
if(rooted)
SpeciesNUM++;
vector<double*> MM;
for(i=0;i<SpeciesNUM;i++){
MM.push_back(new double [SpeciesNUM]);
}
nvar = (TaxaNUM > SpeciesNUM) ? TaxaNUM : SpeciesNUM;
for(i=0;i<nvar;i++){
DAG.push_back(new double [nvar]);
for(j=0; j<nvar; j++){
DAG[i][j] = 0.0;
}
}
i = 0;
j = 0;
if(rooted){
while(i != SpeciesNUM-1){
if(!(in >> speciesName)) throw "Each line should start with a species name!";
dagNames.push_back(speciesName);
j = 0;
while(j != SpeciesNUM-1){
if(!(in >> MM[i][j])) throw "Could not read matrix entry! For each species make sure there are as many entries as the number of species specified in the file. Only square matrices are accepted.";
if(MM[i][j] < 0) throw "The Food Web matrix should not contain negative values.Use either 0, 1 or a positive number to indicate the portion of diet.";
j++;
}
MM[i][SpeciesNUM-1] = 0;
i++;
}
for(j=0; j<SpeciesNUM; j++)
MM[SpeciesNUM-1][j] = 0;
dagNames.push_back("_root");
} else {
while(i != SpeciesNUM){
if(!(in >> speciesName)) throw "Each line should start with a species name!";
dagNames.push_back(speciesName);
j = 0;
while(j != SpeciesNUM){
if(!(in >> MM[i][j])) throw "Could not read matrix entry! For each species make sure there are as many entries as the number of species specified in the file. Only square matrices are accepted.";
if(MM[i][j] < 0) throw "The Food Web matrix should not contain negative values.Use either 0, 1 or a positive number to indicate the portion of diet.";
j++;
}
i++;
}
}
/* ---------------------------------------------------------------------------------------------------------
* Input data
* ---------------------------------------------------------------------------------------------------------*/
if(verbose_mode == VB_MAX){
cout<<endl<<"Food web is defined by the following matrix"<<endl;
for(i=0;i<SpeciesNUM;i++) {
cout<<dagNames[i]<<"\t";
for(j=0;j<SpeciesNUM;j++)
cout<<MM[i][j]<<"\t";
cout<<endl;
}
// Species in the food web and their ids
for(i=0; i<SpeciesNUM;i++)
cout<<"["<<i<<"] "<<dagNames[i]<<endl;
}
/* ---------------------------------------------------------------------------------------------------------
* Processing the input data
* ---------------------------------------------------------------------------------------------------------*/
//Ignoring cannibalism -------------------------------------------------------------------------
int cannibals=0;
for(i=0;i<SpeciesNUM;i++)
if(MM[i][i]!=0){
cannibals++;
if(weighted){
if(cannibals == 1){
cout<<"------------------------------------------"<<endl;
cout<<" Cannibal species link weight "<<endl;
cout<<"------------------------------------------"<<endl;
}
cout.width(30);
cout<<left<<dagNames[i];
cout<<" | "<<MM[i][i]<<endl;
}else{
if(cannibals == 1){
cout<<"-----------------------------"<<endl;
cout<<" Cannibal species "<<endl;
cout<<"-----------------------------"<<endl;
}
cout<<dagNames[i]<<endl;
}
MM[i][i]=0;
}
if(cannibals!=0){
cout<<endl<<"Deleted "<<cannibals;
if(cannibals == 1)
cout<<" cannibalistic link."<<endl;
else
cout<<" cannibalistic links."<<endl;
}
//Check whether the graph is acyclic or not
Graph g(SpeciesNUM);
for(i=0; i<SpeciesNUM; i++)
for(j=0; j<SpeciesNUM; j++)
if(MM[i][j]>0)
g.addEdge(i,j);
if(g.isCyclic()){
if(cannibals != 0)
cout<<endl<<"ERROR: Even after deleting cannibalistic links, there are still some cycles present."<<endl;
else
cout<<endl<<"ERROR: ";
cout<<"Cyclic food webs are not supported. Delete the links which cause cycles and run again."<<endl;
cout<<"SOLUTION:"<<endl;
cout<<"Detect species in the cycle and choose one link to be deleted in order to break the cycle."<<endl;
cout<<"One possibility is to delete the link with least weight. This can be done by setting the corresponding value in the matrix to 0."<<endl;
exit(0);
}
// The number of links -------------------------------------------------------------------------
linksNUM = 0;
for(i = 0; i<SpeciesNUM; i++)
for(j = 0; j<SpeciesNUM; j++)
if(MM[i][j]>0)
linksNUM++;
//Rescaling the diet if necessary --------------------------------------------------------------
if(weighted){
int dietReScaled = 0;
vector<double> colsum;
//cout<<"Food web is weighted."<<endl;
for(j=0;j<SpeciesNUM;j++){
colsum.push_back(0);
for(i=0;i<SpeciesNUM;i++)
colsum[j]=colsum[j]+MM[i][j];
if(colsum[j]!=1 && colsum[j]!=0){
dietReScaled++;
//cout<<" WARNING: rescaled diet composition of species "<<j<<". Column sum = "<<colsum[j]<<endl;
for(i=0;i<SpeciesNUM;i++)
MM[i][j]=MM[i][j]/colsum[j];
}
colsum[j]=0;
//for(i=0;i<SpeciesNUM;i++)
// colsum[j]=colsum[j]+MM[i][j];
//cout<<j<<" Column sum = "<<colsum[j]<<endl;
}
cout<<"Rescaled diet composition of "<<dietReScaled<<" species."<<endl;
}else{
for(i=0; i<SpeciesNUM; i++)
for(j=0; j<SpeciesNUM; j++)
if( MM[i][j] > 0)
MM[i][j] = 1;
//cout<<"Since the -eco option was chosen, the entries of Food Web matrix will be converted to 0/1 [not prey / prey]. You can use -ecoW option to account for the Diet Composition."<<endl;
}
// Technical: in case of rooted trees, we check which species are basal ones, i.e. for which check = 0, and set them to "feed on" root M[i,j] = 1
if(rooted){
vector<double> check;
for(j=0;j<SpeciesNUM-1;j++){
check.push_back(0);
for(i=0;i<SpeciesNUM-1;i++)
check[j]=check[j]+MM[i][j];
if(check[j]==0)
MM[SpeciesNUM-1][j]=1;
}
}
//Detecting which species are not present in either FoodWeb or Tree/SplitNetwork-----------------
detectMissingSpecies();
//Check whether all the species from initialTaxa set are actually present on Tree/SplitSys or in Food Web
checkInitialTaxa();
// Synchronization of species in Tree/SplitSys and species in FoodWeb ---------------------------
synchronizeSpecies();
for(i=0; i<SpeciesNUM; i++){
for(j=0; j<SpeciesNUM; j++){
DAG[phylo_order[i]][phylo_order[j]]=MM[i][j];
}
}
for(i=SpeciesNUM-1;i>=0;i--)
delete[] MM[i];
if(verbose_mode == VB_MAX){
// Print info about synchronization
cout<<endl<<"Synchronization:"<<endl;
cout<<"PhyloInfo id | FoodWeb id, name"<<endl;
for(i=0; i<SpeciesNUM; i++){
cout<<"["<<phylo_order[i]<<"] | ["<<i<<"] "<<dagNames[i]<<endl;
}
cout<<"PhyloInfo id | name"<<endl;
for(i=0; i<TaxaNUM;i++){
cout<<"["<<i<<"] "<<findNodeID(i)->name<<endl;
}
// Input data after processing: cannibalism, rescaling, reordering
cout<<endl<<"Food web is defined by the following matrix"<<endl;
for(i=0;i<nvar;i++) {
if(findFoodWebID(i) != -1)
cout<<dagNames[findFoodWebID(i)]<<"\t";
else
cout<<"\t\t";
for(j=0;j<nvar;j++)
cout<<DAG[i][j]<<"\t";
cout<<endl;
}
}
/* ---------------------------------------------------------------------------------------------------------
* Filling out taxaDAG vector: node corresponds to taxa, neighbors to preys, length (node-neighbor) to weight
* ---------------------------------------------------------------------------------------------------------*/
vector<int> vec2;//the value of vec[j] is the height of the species in the DAG
taxaDAG.resize(nvar,NULL);
for(j=0;j<nvar;j++){
taxaDAG[j] = newNode(j,j);
//cout<<"taxonDAG[j="<<j+1<<"]->id="<<taxaDAG[j]->id<<endl;
}
for(j=0;j<nvar;j++){
for(i=0;i<nvar;i++)
if(DAG[i][j]>0){
//cout<<"cheking matrix"<<i<<j<<endl;
taxaDAG[j]->addNeighbor(taxaDAG[i], DAG[i][j], taxaDAG[i]->id);
//cout<<"neighbors[i="<<taxaDAG[j]->degree()-1<<"]->id="<<taxaDAG[j]->neighbors[taxaDAG[j]->degree()-1]->node->id<<endl;
}
//cout<<endl;
}
/* ---------------------------------------------------------------------------------------------------------
* Defining levels in the Food Web based on the longest food chain of predators
* ---------------------------------------------------------------------------------------------------------*/
for(j=0;j<nvar;j++){
levelDAG.push_back(0);
if(taxaDAG[j]->degree()>0)
vec2.push_back(1);
else
vec2.push_back(0);
// if(taxaDAG[j]->degree()>0){
// cout<<"Children of taxonDAG[j="<<j<<"]->id="<<taxaDAG[j]->id<<":"<<endl;
// for(i=0;i<taxaDAG[j]->degree();i++)
// cout<<"taxaDAG["<<j<<"]->neighbors["<<i<<"]->node->id "<<taxaDAG[j]->neighbors[i]->node->id<<endl;
// //cout<<"id of the child "<<i<<" node id "<<taxaDAG[j]->neighbors[i]->node->id+1<<" "<<endl;
// //cout<<" neighbors[i="<<i<<"]->id="<<taxaDAG[j]->neighbors[i]->node->id<<endl;
// cout<<endl;
//
// }
}
// for(j=0;j<nvar;j++)
// cout<<j<<" "<<levelDAG[j]<<" "<<vec2[j]<<endl;
int eq=0,step=0;
//cout<<"Starting while..."<<endl;
while(eq!=1){
eq=1;
step++;
// if(step==1 or step==2 or step==3)
// cout<<"-------STEP "<<step<<"-------"<<endl<<"j v1 v2"<<endl;
for(j=0;j<nvar;j++){
if(levelDAG[j]!=vec2[j])
eq=0;
// if(step==1 or step==2 or step==3)
// cout<<j<<" "<<levelDAG[j]<<" "<<vec2[j]<<endl;
levelDAG[j]=vec2[j];
}
for(j=0;j<nvar;j++){
if(taxaDAG[j]->degree()>0){
//cout<<"taxaDAG["<<j<<"]->neighbors[0]->node->id "<<taxaDAG[j]->neighbors[0]->node->id<<endl;
vec2[j]=vec2[taxaDAG[j]->neighbors[0]->node->id]+1;
for(i=1;i<taxaDAG[j]->degree();i++)
if(vec2[taxaDAG[j]->neighbors[i]->node->id]>=vec2[j])
vec2[j]=vec2[taxaDAG[j]->neighbors[i]->node->id]+1;
}
}
}
// For each predator the level corresponds to its longest food chain----------------------------
if(verbose_mode == VB_MAX){
cout<<"For each species its longest chain according to a food web"<<endl;
for(j=0;j<nvar;j++)
//if(findFoodWebID(j) != -1)
// cout<<dagNames[findFoodWebID(j)]<<"\t| "<<levelDAG[j]<<endl;
//else
cout<<*names[j]<<"\t| "<<levelDAG[j]<<endl;
}
//cout<<"Species - level"<<endl;
//ofstream levelF;
//levelF.open("Level",ios::app);
//for(j=0;j<SpeciesNUM;j++)
// levelF<<j+1<<" "<<levelDAG[j]<<endl;
// for(i=0;i<tree.leafNum;i++)
// myfile<<"taxon id: "<<taxaTree[i]->id<<" | taxon name: "<<taxaTree[i]->name<<endl;
// myfile<<"root id: "<<root->id<<" | root name: "<<root->name<<endl;
// // myfile.close();
// The maximum level is the longest food chain of the food web ---------------------------------
// int maxlevel;
// maxlevel=0;
// for(i=0;i<SpeciesNUM;i++)
// if(maxlevel<levelDAG[i])
// maxlevel=levelDAG[i];
// Decrease SpeciesNUM since you do not need to include the root to the Species anymore---------
if(rooted)
SpeciesNUM--;
}
/* =========================================================================================================
* ROOTED TREES
* =========================================================================================================*/
void ECOpd::printECOlpRooted(const char* fileOUT,ECOpd &tree){
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(fileOUT);
int m,i,j;
//int step=0,step_all=0;
//cout<<"# of species to conserve:"<<nspecies<<endl;
int nspecies=k;
nspecies++; //you have to include also one place for the root
//----------------------------------------------------------------------------------------------------------------
// Dealing with d levels
//----------------------------------------------------------------------------------------------------------------
// max d level-------------------------------------------------
int maxlevel;
maxlevel=levelDAG[0]; //i=0;
// cout<<"DAG levels:"<<endl;
// cout<<"LevelDAG[0]:"<<levelDAG[0]<<endl;
for(i=1;i<nvar;i++){
// //cout<<"LevelDAG["<<i<<"]:"<<levelDAG[i]<<endl;
if(maxlevel<levelDAG[i])
maxlevel=levelDAG[i];
}
//cout<<"max DAG level:"<<maxlevel+1<<endl;
//# of species at each d level---------------------------------
// int hit=0;
// hvec.resize(maxlevel+1,0);
// while(hit<=maxlevel){
// for(i=0;i<nvar;i++)
// if(levelDAG[i]==hit) hvec[hit]++;
// hit++;
// }
/*cout<<"# of species at each level"<<endl;
for(i=0;i<hvec.size();i++)
cout<<hvec[i]<<" ";
cout<<endl;
*/
/****************************************************************************************************************
* Integer Programming formulation
****************************************************************************************************************/
//----------------------------------------------------------------------------------------------------------------
// Printing objective function
//----------------------------------------------------------------------------------------------------------------
out<<"Maximize"<<endl;
tree.getBranchOrdered(nodes1,nodes2);
for(i=0;i<tree.branchNum;i++){
nodes1[i]->findNeighbor(nodes2[i])->id=i;
nodes2[i]->findNeighbor(nodes1[i])->id=i;
if(i<tree.branchNum-1)
out<<nodes1[i]->findNeighbor(nodes2[i])->length<<" "<<"y"<<i<<" + ";
else
out<<nodes1[i]->findNeighbor(nodes2[i])->length<<" "<<"y"<<i<<endl;
}
//----------------------------------------------------------------------------------------------------------------
// Printing constraints
//----------------------------------------------------------------------------------------------------------------
out<<"Subject To"<<endl;
//----------------------------------------------------------------------------------------------------------------
// 1. constraint: species present in the set
if(initialTaxa.size()!=0)
for(m=0;m<initialTaxa.size();m++)
out<<"x"<<findSpeciesIDname(&initialTaxa[m])<<" = 1"<<endl;
//----------------------------------------------------------------------------------------------------------------
// 2. constraint: the sum of all species is <= k
for(i=0;i<nvar-1;i++)
out<<"x"<<i<<" + ";
out<<"x"<<nvar-1<<" <= "<<nspecies<<endl;
//----------------------------------------------------------------------------------------------------------------
// 3. constraint: the sum of leaves in the DAG is >= to 1
// SpeciesNUM++;
// int nleafDAG=0,nleaf=0;
// for(i=0;i<SpeciesNUM;i++)
// if(levelDAG[i]==0)
// nleafDAG++;
// for(j=0;j<SpeciesNUM;j++){
// if(taxaDAG[j]->degree()==0){
// nleaf++;
// if(nleaf<nleafDAG)
// out<<"x"<<taxaDAG[j]->id<<" + ";
// else
// out<<"x"<<taxaDAG[j]->id<<" >= 1"<<endl;
// }
// }
//----------------------------------------------------------------------------------------------------------------
// 4. constraints: SURVIVAL CONSTRAINT
if(weighted){
//weighted food web: sum of weights is greater than a given threshold--------------------------------
for(j=0;j<nvar;j++)
if(taxaDAG[j]->degree()>0){//the ones that have children in the DAG
for(i=0;i<taxaDAG[j]->degree();i++){
if(i<taxaDAG[j]->degree()-1){
out<<taxaDAG[j]->neighbors[i]->length<<" x"<<taxaDAG[j]->neighbors[i]->node->id<<" + ";
} else {
out<<taxaDAG[j]->neighbors[i]->length<<" x"<<taxaDAG[j]->neighbors[i]->node->id<<" - "<<T<<" x"<<taxaDAG[j]->id<<" >= 0"<<endl;
}
}
}
}else{
//for each predator the sum of children in the DAG is >= to its value-------------------------------
for(j=0;j<nvar;j++)
if(taxaDAG[j]->degree()>0){//the ones that have children in the DAG
for(i=0;i<taxaDAG[j]->degree();i++){
if(i<taxaDAG[j]->degree()-1){
out<<"x"<<taxaDAG[j]->neighbors[i]->node->id<<" + ";
} else {
out<<"x"<<taxaDAG[j]->neighbors[i]->node->id<<" - x"<<taxaDAG[j]->id<<" >= 0"<<endl;
}
}
}
}
//----------------------------------------------------------------------------------------------------------------
// 5. constraints for edges in the PhyloTree
//cout<<"root "<<tree.root->id<<endl;
vector<int> taxaBelow;
for(i=0;i<tree.branchNum;i++)
//constraints: SUM{Xv in T(e)}(Xv)>=Ye -----------------------------------------------
if((nodes1[i]->isLeaf()) && (nodes1[i]!=root))
out<<"x"<<nodes1[i]->id<<" - y"<<nodes1[i]->findNeighbor(nodes2[i])->id<<" >= 0"<<endl;
else {
tree.getTaxaID(taxaBelow,nodes2[i],nodes1[i]);
for(j=0;j<taxaBelow.size();j++)
if(j<taxaBelow.size()-1)
out<<"x"<<taxaBelow[j]<<" + ";
else
out<<"x"<<taxaBelow[j];
taxaBelow.clear();
out<<" - y"<<nodes1[i]->findNeighbor(nodes2[i])->id<<" >= 0"<<endl;
}
//----------------------------------------------------------------------------------------------------------------
// Printing bounds for variables
//----------------------------------------------------------------------------------------------------------------
out<<"Bounds"<<endl;
for(j=0;j<nvar;j++)
out<<"0 <= x"<<taxaDAG[j]->id<<" <= 1"<<endl;
for(i=0;i<tree.branchNum;i++)
out<<"0 <= y"<<i<<" <= 1"<<endl;
//----------------------------------------------------------------------------------------------------------------
// Printing variables (For IP model)
//----------------------------------------------------------------------------------------------------------------
out<<"Generals"<<endl;
for(j=0;j<nvar;j++)
out<<"x"<<taxaDAG[j]->id<<" ";
for(i=0;i<tree.branchNum;i++)
out<<"y"<<i<<" ";
//----------------------------------------------------------------------------------------------------------------
out<<endl<<"End"<<endl;
out.close();
}
/* =========================================================================================================
* UNROOTED TREES and d-levels
* =========================================================================================================*/
void ECOpd::printECOlpUnrooted(const char* fileOUT,ECOpd &tree){
ofstream myfile;
string str_out = fileOUT;
string str_out_1,str_out_2;
//myfile.open(fileOUT);
int i,m,j,step=0,step_all=0;
int nspecies=k;
//---------------------------------------------Dealing with d levels---------------------------------------------
{
//--------------------------------------------------max d level--------------------------------------------------
int maxlevel;
maxlevel=levelDAG[0];
// cout<<"DAG levels:"<<endl;
// cout<<"LevelDAG[0]:"<<levelDAG[0]<<endl;
for(i=1;i<TaxaNUM;i++){
// cout<<"LevelDAG["<<i<<"]:"<<levelDAG[i]<<endl;
if(maxlevel<levelDAG[i])
maxlevel=levelDAG[i];
}
// cout<<"max DAG level:"<<maxlevel+1<<endl;
//-----------------------------------------------------end-------------------------------------------------------
//---------------------------------------generating first vector of d levels-------------------------------------
generateFirstMultinorm(dvec, nspecies-1, maxlevel+1); //nspecies-1 - we are saving at least 1 place at d[0] level
//maxlevel+1 - as we start counting levels from 0 one
// cout<<"vector of d levels:"<<endl;
// dvec[0]++;
// for(i=0;i<dvec.size();i++)
// cout<<dvec[i]<<" ";
// cout<<endl;
// dvec[0]--;
//-----------------------------------------------------end-------------------------------------------------------
//------------------------------------------# of species at each d level-----------------------------------------
int hit=0;
hvec.resize(maxlevel+1,0);
while(hit<=maxlevel){
for(i=0;i<TaxaNUM;i++)
if(levelDAG[i]==hit) hvec[hit]++;
hit++;
}
/*cout<<"# of species at each level"<<endl;
for(i=0;i<hvec.size();i++)
cout<<hvec[i]<<" ";
cout<<endl;
*/
//-----------------------------------------------------end-------------------------------------------------------
}
//-----------------------------------------END:Dealing with d levels---------------------------------------------
int DAGlevels=1,check_print=1;
//--------------------------------------Printing all cases for different d levels--------------------------------
while(DAGlevels==1) {
step_all++;
//cout<<endl<<"STEP ALL:"<<step_all<<endl;
// cout<<"vector of d levels:"<<endl;
dvec[0]++;
// for(i=0;i<dvec.size();i++)
// cout<<dvec[i]<<" ";
// cout<<endl;
//--------------------------------------CHECKPOINT:is vector d good or we should ignore it?----------------------
{
//Print only if d[i] is <= than the # of species on this level, otherwise it's a waste of places for conservation
check_print=1;
for(i=0;i<hvec.size();i++){
//cout<<"dvec["<<i<<"]="<<dvec[i]<<" hvec["<<i<<"]="<<hvec[i]<<endl;
if(dvec[i]>hvec[i])
check_print=0;
}
//cout<<"CHECKPOINT="<<check_print<<endl<<endl;
check_print=1;//IGNORE checkpoint: when only one model for each run is needed, used together with DAGlevels=0; (below)
if(check_print==1){
step++;
//cout<<"Vector d is SUITABLE, step="<<step<<endl;
str_out_1 = convertIntToString(step);
str_out_2 = str_out + str_out_1 + ".lp";
//myfile.open(str_out_2.c_str());
//str_out_2 = str_out + "lp"; //IGNORED d levels: only one model for each run
str_out_2 = str_out;
myfile.open(str_out_2.c_str());
/**----------------------------------------------Printing objective function---------------------------------------*/
{
myfile<<"Maximize"<<endl;
tree.getBranchOrdered(nodes1,nodes2);
{
for(i=0;i<tree.branchNum;i++){
nodes1[i]->findNeighbor(nodes2[i])->id=i;
nodes2[i]->findNeighbor(nodes1[i])->id=i;
if(i<tree.branchNum-1)
myfile<<nodes1[i]->findNeighbor(nodes2[i])->length<<" "<<"y"<<i<<" + ";
else
myfile<<nodes1[i]->findNeighbor(nodes2[i])->length<<" "<<"y"<<i<<endl;
}
}
//IDEA: objective**********************************************************************
{
// double lambda_sum=0;
// for(i=0;i<tree.branchNum;i++){
// nodes1[i]->findNeighbor(nodes2[i])->id=i;
// nodes2[i]->findNeighbor(nodes1[i])->id=i;
// lambda_sum = lambda_sum + nodes1[i]->findNeighbor(nodes2[i])->length;
// if(i<tree.branchNum-1)
// myfile<<nodes1[i]->findNeighbor(nodes2[i])->length<<" "<<"y"<<i<<" + ";
// else
// myfile<<nodes1[i]->findNeighbor(nodes2[i])->length<<" "<<"y"<<i;
// }
//
// for(j=0;j<tree.leafNum;j++)
// if(taxaDAG[j]->degree()>0)
// myfile<<" - "<<lambda_sum<<" z"<<j;
// myfile<<endl;
}
//*************************************************************************************
}
/**--------------------------------------------------Printing constraints------------------------------------------*/
{
myfile<<"Subject To"<<endl;
int c=0;
/**species present in the set-----------------------------------------------*/
if(initialTaxa.size()!=0)
for(m=0;m<initialTaxa.size();m++)
myfile<<"x"<<findSpeciesIDname(&initialTaxa[m])<<" = 1"<<endl;
/**the sum of all species is <= k-------------------------------------------------------------------*/
{
NodeVector taxaTree;
tree.getTaxa(taxaTree);
myfile<<"c"<<c<<": ";
c++;
for(i=0;i<nvar-1;i++)
myfile<<"x"<<i<<" + ";
myfile<<"x"<<nvar-1<<" <= "<<nspecies<<endl;
}
/**the sum of leaves in the DAG is >= to 1----------------------------------------------------------*/
{ //it might be incorrect, so check it out before using...
// myfile<<"c"<<c<<": ";
// c++;
// int nleafDAG=0,nleaf=0;
// for(i=0;i<TaxaNUM;i++)
// if(levelDAG[i]==0)
// nleafDAG++;
// for(j=0;j<TaxaNUM;j++){
// if(taxaDAG[j]->degree()==0){
// nleaf++;
// if(nleaf<nleafDAG)
// myfile<<"x"<<taxaDAG[j]->id<<" + ";
// else
// myfile<<"x"<<taxaDAG[j]->id<<" >= 1"<<endl;
// }
// }
}
//constraints: SURVIVAL CONSTRAINT
if(weighted){//weighted food web: sum of weights is greater than a given threshold--------------------------------
for(j=0;j<nvar;j++){
if(taxaDAG[j]->degree()>0){//the ones that have children in the DAG
for(i=0;i<taxaDAG[j]->degree();i++)
if(i<taxaDAG[j]->degree()-1)
myfile<<taxaDAG[j]->neighbors[i]->length<<" x"<<taxaDAG[j]->neighbors[i]->node->id<<" + ";
else
myfile<<taxaDAG[j]->neighbors[i]->length<<" x"<<taxaDAG[j]->neighbors[i]->node->id<<" - "<<T<<" x"<<taxaDAG[j]->id<<" >= 0"<<endl;
}
}
}
else {//for each predator the sum of children in the DAG is >= to its value-----------------------------
for(j=0;j<TaxaNUM;j++){
if(taxaDAG[j]->degree()>0){//the ones that have children in the DAG
myfile<<"c"<<c<<": ";
c++;
for(i=0;i<taxaDAG[j]->degree();i++)
if(i<taxaDAG[j]->degree()-1)
myfile<<"x"<<taxaDAG[j]->neighbors[i]->node->id<<" + ";
else
myfile<<"x"<<taxaDAG[j]->neighbors[i]->node->id<<" - x"<<taxaDAG[j]->id<<" >= 0"<<endl;
}
}
}
//IDEA:new variables Z'tas*************************************************************
{
// for(j=0;j<tree.leafNum;j++){
// if(taxaDAG[j]->degree()>0){
// myfile<<"c"<<c<<": ";
// c++;
// myfile<<"z"<<j<<" - x"<<j<<" >= 0"<<endl;
// for(i=0;i<taxaDAG[j]->degree();i++){
// myfile<<"c"<<c<<": ";
// c++;
// myfile<<"z"<<j<<" - x"<<taxaDAG[j]->neighbors[i]->node->id<<" >= 0"<<endl;
// }
// }
// }
}
//*************************************************************************************
//d levels----------------------------------------------------------------------------------------
{
// int hit=0;
// int h=0;
// int maxlevel=dvec.size()-1;
// while(hit<=maxlevel){
// myfile<<"c"<<c<<": ";
// c++;
// h=0;
// for(i=0;i<SpeciesNUM;i++)
// if(levelDAG[i]==hit){
// h++;
// if(h<hvec[hit])
// myfile<<"x"<<i<<" + ";
// else {
// //myfile<<"x"<<i<<" = "<<dvec[hit];
// myfile<<"x"<<i<<" - d"<<hit<<" = 0";
// if(step==1)
// cout<<"dvec["<<hit<<"]="<<dvec[hit]<<endl;
// }
// }
// myfile<<endl;
// hit++;
// }
// myfile<<"c"<<c<<": ";
// c++;
// for(i=0;i<=maxlevel;i++){
// if(i<maxlevel)
// myfile<<"d"<<i<<" + ";
// else
// myfile<<"d"<<i<<" = "<<nspecies<<endl;
// }
}
//for edges in the PhyloTree--------------------------------------------------------------------------
{
vector<int> taxaBelow;
for(i=0;i<tree.branchNum;i++){
//constraints: SUM{Xv in T(e)}(Xv)>=Ye -----------------------------------------------
myfile<<"c"<<c<<": ";
c++;
tree.getTaxaID(taxaBelow,nodes2[i],nodes1[i]);
for(j=0;j<taxaBelow.size();j++)
if(j<taxaBelow.size()-1)
myfile<<"x"<<taxaBelow[j]<<" + ";
else
myfile<<"x"<<taxaBelow[j];
taxaBelow.clear();
myfile<<" - y"<<nodes1[i]->findNeighbor(nodes2[i])->id<<" >= 0"<<endl;
//constraints: SUM{Xv not in T(e)}(Xv)>=Ye -------------------------------------------
myfile<<"c"<<c<<": ";
c++;
tree.getTaxaID(taxaBelow,nodes1[i],nodes2[i]);
for(j=0;j<taxaBelow.size();j++)
if(j<taxaBelow.size()-1)
myfile<<"x"<<taxaBelow[j]<<" + ";
else
myfile<<"x"<<taxaBelow[j];
taxaBelow.clear();
myfile<<" - y"<<nodes1[i]->findNeighbor(nodes2[i])->id<<" >= 0"<<endl;
}
}//end printing constraints for edges (PhyloTree)
}//-------------------------------------END printing constraints ALL--------------------------------------
//int maxlevel=dvec.size()-1;
//---------------------------------------------Printing bounds for variables--------------------------------------
{
myfile<<"Bounds"<<endl;
for(j=0;j<nvar;j++)
myfile<<"0 <= x"<<taxaDAG[j]->id<<" <= 1"<<endl;
for(i=0;i<tree.branchNum;i++)
myfile<<"0 <= y"<<i<<" <= 1"<<endl;
}
//------------------------------------------Printing variables (For IP model)---------------------------------
{
myfile<<"Generals"<<endl;
for(j=0;j<nvar;j++)
myfile<<"x"<<taxaDAG[j]->id<<" ";
for(i=0;i<tree.branchNum;i++)
myfile<<"y"<<i<<" ";
// myfile<<"Generals"<<endl;
// int maxlevel=dvec.size()-1;
// for(j=0;j<=maxlevel;j++)
// myfile<<"d"<<j<<" ";
// if(fractVAR.size()!=0)
// for(i=0;i<fractVAR.size();i++)
// myfile<<fractVAR[i]<<" ";
myfile<<endl;
}
myfile<<"End"<<endl;
myfile.close();
}//checkpoint "if" ends here
}
//----------------------------------------------CHECKPOINT ENDs here----------------------------------------------
//---------------------------------------generating next vector of d levels--------------------------------------
dvec[0]--;
if(generateNextMultinorm(dvec))
DAGlevels=1;
else
DAGlevels=0;
//-----------------------------------------------------end-------------------------------------------------------
DAGlevels=0;//IGNORE d levels: only one model to solve for each run
}
//----------------------------------END:Printing all cases for different d levels--------------------------------
//cout<<"ALL STEPS:"<<step_all<<endl;
//cout<<"STEPs:"<<step<<endl;
}
/* =========================================================================================================
* SPLIT systems
* =========================================================================================================*/
void ECOpd::printInfDAG (const char* fileOUT,PDNetwork &splitsys, Params ¶ms) {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(fileOUT,ios::app);
int i,j,nspecies=k;
int maxlevel;
maxlevel=levelDAG[0];
for(i=1;i<TaxaNUM;i++)
if(maxlevel<levelDAG[i])
maxlevel=levelDAG[i];
int hit=0;
hvec.resize(maxlevel+1,0);
while(hit<=maxlevel){
for(i=0;i<TaxaNUM;i++)
if(levelDAG[i]==hit) hvec[hit]++;
hit++;
}
//Constraints----------------------------------------------------------------------
//species present in the set-----------------------------------------------
if(initialTaxa.size()!=0)
for(i=0;i<initialTaxa.size();i++)
out<<"x"<<findSpeciesIDname(&initialTaxa[i])<<" = 1"<<endl;
//the sum of all species is <= k-------------------------------------------
for(i=0;i<nvar-1;i++)
out<<"x"<<i<<" + ";
out<<"x"<<nvar-1<<" <= "<<nspecies<<endl;
//the sum of leaves in the DAG is >= to 1----------------------------------
int nleafDAG=0,nleaf=0;
for(i=0;i<nvar;i++)
if(levelDAG[i]==0)
nleafDAG++;
for(j=0;j<nvar;j++){
if(taxaDAG[j]->degree()==0){
nleaf++;
if(nleaf<nleafDAG)
out<<"x"<<taxaDAG[j]->id<<" + ";
else
out<<"x"<<taxaDAG[j]->id<<" >= 1"<<endl;
}
}
//SURVIVAL CONSTRAINT
if(weighted){
//constraint: Weighted food web. Sum of weights is greater than a given threshold--------------------------------
for(j=0;j<nvar;j++){
if(taxaDAG[j]->degree()>0){//the ones that have children in the DAG
for(i=0;i<taxaDAG[j]->degree();i++)
if(i<taxaDAG[j]->degree()-1)
out<<taxaDAG[j]->neighbors[i]->length<<" x"<<taxaDAG[j]->neighbors[i]->node->id<<" + ";
else
out<<taxaDAG[j]->neighbors[i]->length<<" x"<<taxaDAG[j]->neighbors[i]->node->id<<" - "<<T<<" x"<<taxaDAG[j]->id<<" >= 0"<<endl;
}
}
} else {
//for each predator the sum of children in the DAG is >= to its value-------
for(j=0;j<nvar;j++)
if(taxaDAG[j]->degree()>0){//the ones that have children in the DAG
for(i=0;i<taxaDAG[j]->degree();i++){
if(i<taxaDAG[j]->degree()-1){
out<<"x"<<taxaDAG[j]->neighbors[i]->node->id<<" + ";
} else {
out<<"x"<<taxaDAG[j]->neighbors[i]->node->id<<" - x"<<taxaDAG[j]->id<<" >= 0"<<endl;
}
}
}
}
//Bounds-----------------------------------------------------------------------------
Split included_tax(TaxaNUM);
IntVector::iterator it2;
// for (it2 = splitsys.initialset.begin(); it2 != splitsys.initialset.end(); it2++)
// included_tax.addTaxon(*it2);
vector<int> y_value;
y_value.resize(splitsys.getNSplits(), -1);
//splitsys.checkYValue(nspecies, y_value);
splitsys.lpVariableBound(out, params, included_tax, y_value);
//Generals for IP or MIP--------------------------------------------------------------
out<<"Generals"<<endl;
for(i=0;i<nvar;i++)
out<<"x"<<i<<" ";
for(i=0;i<splitsys.getNSplits();i++)
out<<"y"<<i<<" ";
// for(j=0;j<=maxlevel;j++)
// out<<"d"<<j<<" ";
/* if(fractVAR.size()!=0)
for(i=0;i<fractVAR.size();i++)
out<<fractVAR[i]<<" ";*/
out<<endl;
out<<"End"<<endl;
out.close();
// ofstream out1;
// out1.exceptions(ios::failbit | ios::badbit);
// out1.open("variablesNUM.data",ios::app);
// out1<<this->k<<" "<<nvar<<" "<<splitsys.getNSplits()<<endl;//" "<<maxlevel+1<<endl;
// out1.close();
}
//Fractional stuff-----------------------------------------------------------------
void ECOpd::readREC(const char* infile) {
ifstream in;
cout<<endl<<"-----------------------------------------------------"<<endl;
cout<<"Reading file with fractional variables from "<<infile<<endl;