Repository navigation
Expand file tree
/
Copy pathGeneNetwork.cpp
More file actions
988 lines (819 loc) · 31.8 KB
/
Copy pathGeneNetwork.cpp
File metadata and controls
988 lines (819 loc) · 31.8 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
// $Id: GeneNetwork.cpp 29 2012-01-04 17:06:55Z jbao $
#include <cmath>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <gsl/gsl_errno.h>
#include "Exception.h"
#include "GeneNetwork.h"
#include "HillGene.h"
#include "RegulatoryModule.h"
#include "logging/logging.h"
using namespace ::logging;
LIBSBML_CPP_NAMESPACE_USE
struct IntCmp {
bool operator()(const std::pair<std::string,int>& lhs,
const std::pair<std::string,int>& rhs) {
return lhs.second < rhs.second;
}
};
// ============================================================================
// INITIALIZATION
/**
* Default constructor
*/
GeneNetwork::GeneNetwork() {
//x_ = NULL;
//y_ = NULL;
id_ = "grn";
}
// ----------------------------------------------------------------------------
GeneNetwork::GeneNetwork(const GeneNetwork& grn) {
x_ = grn.x_;
y_ = grn.y_;
nodes_ = grn.nodes_;
edges_ = grn.edges_;
id_ = grn.id_;
inDegrees = grn.inDegrees;
outDegrees = grn.outDegrees;
degrees = grn.degrees;
inDegreesVec = grn.inDegreesVec;
outDegreesVec = grn.outDegreesVec;
degreesVec = grn.degreesVec;
}
// ----------------------------------------------------------------------------
GeneNetwork& GeneNetwork::operator=(const GeneNetwork& rhs) {
x_ = rhs.x_;
y_ = rhs.y_;
nodes_ = rhs.nodes_;
edges_ = rhs.edges_;
id_ = rhs.id_;
inDegrees = rhs.inDegrees;
outDegrees = rhs.outDegrees;
degrees = rhs.degrees;
inDegreesVec = rhs.inDegreesVec;
outDegreesVec = rhs.outDegreesVec;
degreesVec = rhs.degreesVec;
return *this;
}
// ---------------------------------------------------------------------------
GeneNetwork::~GeneNetwork() {
//for (std::vector<HillGene>::iterator it = nodes_.begin(); it != nodes_.end(); ++it)
// delete *it;
//for (std::vector<Edge>::iterator it = edges_.begin(); it != edges_.end(); ++it)
// delete *it;
nodes_.clear();
edges_.clear();
}
// ---------------------------------------------------------------------------
void GeneNetwork::load(const char *filename, GeneNetwork::format f) {
switch (f) {
case TSV:
load_tsv(filename);
break;
case SBML:
load_sbml(filename);
break;
default:
std::cerr << "Wrong file format to load!" << std::endl;
exit(1);
}
}
// ---------------------------------------------------------------------------
void GeneNetwork::sanityCheck() {
int inSum = 0, outSum = 0;
for (int i = 0; i < getSize(); ++i) {
inSum += nodes_[i].getNumInputs();
outSum += nodes_[i].getNumOutputs();
}
assert(inSum == outSum);
}
// ---------------------------------------------------------------------------
void GeneNetwork::load_tsv(const char *filename) {
ifstream data_file(filename);
if (!data_file.is_open()) {
std::cerr << "Failed to open file " << filename << std::endl;
exit(1);
}
std::string line, entry;
// data matrix
while (getline(data_file, line)) {
std::istringstream ls(line);
getline(ls, entry, '\t');
std::string src_str = entry;
HillGene *src = new HillGene(entry);
//::logging::log::emit<Debug>() << entry.c_str() << "\t";
getline(ls, entry, '\t');
std::string tgt_str = entry;
HillGene *tgt = new HillGene(entry);
//::logging::log::emit<Debug>() << entry.c_str() << "\t";
//::logging::log::emit<Debug>() << entry.c_str() << ::logging::log::endl;
getline(ls, entry, '\t');
inDegrees[tgt_str]++;
outDegrees[src_str]++;
degrees[tgt_str]++;
degrees[src_str]++;
//::logging::log::emit<Debug>() << "sign = " << entry.c_str() <<
//::logging::log::endl;
Edge *e;
if (entry == tgt_str) { // only 2 columns
e = new Edge(src, tgt, "+-");
} else {
e = new Edge(src, tgt, entry);
}
edges_.push_back(*e);
if (std::find(nodes_.begin(),nodes_.end(),*src) == nodes_.end())
nodes_.push_back(*src);
if (std::find(nodes_.begin(),nodes_.end(),*tgt) == nodes_.end())
nodes_.push_back(*tgt);
delete src;
delete tgt;
delete e;
}
data_file.close();
x_ = Vec_DP(nodes_.size());
y_ = Vec_DP(nodes_.size());
inDegreesVec = std::vector< std::pair<std::string,int> >(inDegrees.begin(),
inDegrees.end());
sort(inDegreesVec.begin(), inDegreesVec.end(), IntCmp());
outDegreesVec = std::vector< std::pair<std::string,int> >(outDegrees.begin(),
outDegrees.end());
sort(outDegreesVec.begin(), outDegreesVec.end(), IntCmp());
degreesVec = std::vector< std::pair<std::string,int> >(degrees.begin(),
degrees.end());
sort(degreesVec.begin(), degreesVec.end(), IntCmp());
}
// ----------------------------------------------------------------------------
/**
* Load a gene network from an SBML file. Overrides Structure.load(). Format must
* be equal GeneNetwork.SBML. Note, the SBML file must be in the exact same format
* as the SBML files produced by writeSBML(). In particular, we assume that reactions are listed
* *ordered* as we do in writeSBML().
* @param filename URL to the file describing the network to load
* @param format File format (GML, DOT, etc.)
* @throws IOException
*/
void GeneNetwork::load_sbml(const char *filename) {
SBMLDocument* document;
SBMLReader reader;
document = reader.readSBML(filename);
unsigned int errors = document->getNumErrors();
if (errors > 0) {
std::cerr << "Failed to open file " << filename << std::endl;
exit(1);
}
Model *m = document->getModel();
// -----------------------------------------
// Set the network size and create the genes
// do not count the species _void_
int size = m->getNumSpecies() - 1;
ListOfSpecies *species = m->getListOfSpecies();
for (int g=0; g < size; g++) {
if (species->get(g)->getId() != "_void_") {
//HillGene hg = new HillGene(this);
//hg.setLabel(species.get(g).getId());
HillGene *n = new HillGene(species->get(g)->getId());
//n.setLabel(species->get(g)->getId());
nodes_.push_back(*n);
delete n;
}
}
x_ = Vec_DP(nodes_.size());
x_ = 0;
y_ = Vec_DP(nodes_.size());
y_ = 0;
//vector<string> parameterNames; // the names of the parameters
//vector<double> parameterValues; // the values of the parameters
std::map<std::string, double> params;
std::vector<std::string> inputNodes; // the indexes of the inputs
HillGene src, tgt;
Parameter *param;
// 2 loops for one gene: both synthesis and degradation reactions
// (we assume that reactions are listed *ordered* as we do in writeSBML())
//int counter = 0;
for (unsigned int i=0; i < m->getNumReactions(); i++) {
Reaction *re = m->getReaction(i);
std::string id = re->getId();
std::stringstream ss;
ss << i;
//::logging::log::emit<Debug>() << id.c_str() <<
// ::logging::log::endl;
tgt = nodes_.at(getIndexOfNode(getGeneReactantId(id)));
//tgt->setLabel(getGeneReactantId(*re));
//SpeciesReference *rt = re->getReactant(0);
//Node *tgt = new HillGene();
//tgt->setLabel(rt->getSpecies());
//ListOfSpeciesReferences *modifiers = re->getListOfModifiers();
for (unsigned int j=0; j < re->getNumModifiers(); j++) {
ModifierSpeciesReference *md = re->getModifier(j);
src = nodes_.at(getIndexOfNode(md->getSpecies()));
inputNodes.push_back(src.getLabel());
// set output genes
std::vector<std::string> outputs = src.getOutputGenes();
outputs.push_back(tgt.getLabel());
src.setOutputGenes(outputs);
// The edge type is unknown for now, it is initialized later
Edge *e = new Edge(&src, &tgt, "+-");
edges_.push_back(*e);
//delete src;
delete e;
}
KineticLaw *kl = re->getKineticLaw();
for(unsigned int j=0; j < kl->getNumParameters(); j++) {
param = kl->getParameter(j);
params[param->getId()] = param->getValue();
//char buf[256];
//sprintf(buf, "%s\t%f", param->getId().c_str(), param->getValue());
//::logging::log::emit<Info>() << buf << ::logging::log::endl;
}
//::logging::log::emit<Info>() << ::logging::log::dec << params.size() <<
// ::logging::log::endl;
// in the second iteration for this gene
if (i%2 == 1) {
// set parameters in gene
//tgt.initialization(params, inputNodes);
nodes_.at(getIndexOfNode(getGeneReactantId(id))).initialization(params, inputNodes);;
//char buf[256];
//sprintf(buf, "%f", params["k_1"]);
//::logging::log::emit<Info>() << buf << ::logging::log::endl;
inputNodes.clear(); // don't clear because the reference was copied to the gene
//parameterNames.clear(); // reset (they were not copied)
//parameterValues.clear();
params.clear();
}
//counter++;
}
//setEdgeTypesAccordingToDynamicalModel();
//signed_ = true;
//delete document;
//delete n;
//delete e;
}
// ----------------------------------------------------------------------------
void GeneNetwork::writeTSV(const char *filename) {
ofstream data_file(filename);
if (!data_file.is_open()) {
std::cerr << "Failed to open file " << filename << std::endl;
exit(1);
}
::logging::log::emit<Info>() << "Writing file " << filename <<
::logging::log::endl;
for (std::vector<Edge>::iterator it = edges_.begin(); it != edges_.end(); ++it) {
int idx = getIndexOfNode(it->getTarget().getLabel());
std::vector<std::string> inputGenes = nodes_[idx].getInputGenes();
std::vector<Edge::sign> inputSigns = nodes_[idx].getInputSigns();
std::vector<std::string>::const_iterator it_sign =
find(inputGenes.begin(),
inputGenes.end(), (*it).getSource().getLabel());
data_file << (*it).getSource().getLabel() << "\t" <<
(*it).getTarget().getLabel() << "\t" <<
(*it).signToString[inputSigns[it_sign-inputGenes.begin()]] <<
std::endl;
}
data_file.close();
}
// ----------------------------------------------------------------------------
/**
* Save the gene network to an SBML file. If the argument is null, use the network id.
* @param filename URL to the file describing the network to load
* @throws IOException
*/
void GeneNetwork::writeSBML(const char *filename) {
ofstream data_file(filename);
if (!data_file.is_open()) {
std::cerr << "Failed to open file " << filename << std::endl;
exit(1);
}
data_file.close();
::logging::log::emit<Info>() << "Writing file " << filename <<
::logging::log::endl;
SBMLDocument *sbmlDoc = new SBMLDocument(3, 1);
Model *model = sbmlDoc->createModel();
model->setId(id_);
//model.getNotes ().add (comment_); // save network description
int size = getSize();
Compartment *comp = model->createCompartment();
comp->setId("cell");
comp->setSize(1);
std::vector<Species*> all_sp;
Species *sp;
for (int s=0; s < size; s++) { // save gene as species
// species[s] = new Species(nodeIds_.get(s), nodeIds_.get(s));
sp = model->createSpecies();
sp->setCompartment("cell");
sp->setId((nodes_.at(s)).getLabel());
all_sp.push_back(sp);
//species[s].setInitialAmount(?); // maybe save the wild-type steady state?
//model.addSpecies(species[s]);
}
// create the void species
sp = model->createSpecies();
sp->setCompartment("cell");
sp->setId("_void_");
sp->setInitialAmount(0);
sp->setBoundaryCondition(true);
sp->setConstant(true);
all_sp.push_back(sp);
//model.addSpecies(species[size]);
// SET SYNTHESIS AND DEGRADATION REACTIONS FOR EVERY GENE
for (int i=0; i<size; i++) {
//::logging::log::emit<Info>() << ::logging::log::dec << i <<
//::logging::log::endl;
// the ID of gene i
// String currentGeneID = nodeIds_.get(i);
string currentGeneID = (nodes_.at(i)).getLabel();
// The modifiers (regulators) of gene i
std::vector<std::string> inputGenes = (nodes_.at(i)).getInputGenes();
// SYNTHESIS REACTION
std::string reactionId = currentGeneID + "_synthesis";
Reaction *reaction = model->createReaction();
KineticLaw *kineticLaw = reaction->createKineticLaw();
SpeciesReference *spr;
ModifierSpeciesReference *msr;
reaction->setId(reactionId);
reaction->setReversible (false);
spr = reaction->createReactant();
spr->setSpecies(sp->getId());
spr = reaction->createProduct();
spr->setSpecies((all_sp.at(i))->getId());
std::stringstream ss;
ss << inputGenes.size();
//::logging::log::emit<Debug>() << "node = " << nodes_.at(i).getLabel().c_str() << " #inputs = " << ss.str().c_str() << ::logging::log::endl;
for (unsigned int r=0; r<inputGenes.size(); r++) {// set gene modifiers
// reaction.addModifier(species[inputIndexes.get(r)]);
//log.log(Level.INFO, "i = " + size);
msr = reaction->createModifier();
msr->setSpecies((all_sp.at(getIndexOfNode(inputGenes.at(r))))->getId());
}
//std::vector<RegulatoryModule> modules = (nodes_.at(i)).getRegulatoryModules();
//log.log(Level.INFO, "size = " + modules.size());
std::map<std::string, double> *params = new std::map<std::string, double>();
(nodes_.at(i)).compileParameters(*params);
//char buf[256];
//sprintf(buf, "%f", nodes_.at(i).getDelta());
//::logging::log::emit<Info>() << buf << ::logging::log::endl;
//::logging::log::emit<Info>() << ::logging::log::dec << nodes_.at(i).getAlpha().size() <<
// ::logging::log::endl;
Parameter *para;
// save gene parameters (note, the first param is the degradation rate)
std::map<std::string, double>::iterator p = params->begin();
//p++;
for (; p!=params->end(); p++) {
//if (p == params->begin()) {
// p++;
// continue;
//}
//::logging::log::emit<Info>() << p->first.c_str() <<
// ::logging::log::endl;
if (p->first != "delta") {
para = kineticLaw->createParameter();
para->setId(p->first);
para->setValue(p->second);
}
}
reaction->setKineticLaw(kineticLaw);
model->addReaction(reaction);
// DEGRADATION REACTION
reaction = model->createReaction();
kineticLaw = reaction->createKineticLaw();
reactionId = currentGeneID + "_degradation";
reaction->setId(reactionId);
reaction->setReversible(false);
spr = reaction->createReactant();
spr->setSpecies((all_sp.at(i))->getId());
spr = reaction->createProduct();
spr->setSpecies(sp->getId());
para = kineticLaw->createParameter();
std::map<std::string,double>::iterator it = params->find("delta");
para->setId(it->first);
para->setValue(it->second);
reaction->setKineticLaw (kineticLaw);
model->addReaction (reaction);
}
// PRINT FILE
SBMLWriter sbmlWriter;
sbmlWriter.writeSBML(sbmlDoc, filename);
delete sbmlDoc;
}
// ----------------------------------------------------------------------------
/**
* Return gene id of a reaction.
* @param r Instance of Reaction
* @return Gene id of the reaction r
*/
std::string GeneNetwork::getGeneReactantId(std::string& id) {
// Example: G1_synthesis or G1_degradation -> id = "G1"
//StringTokenizer st = new StringTokenizer(r.getId(), "_");
//String id = st.nextToken();
// problem: if the id also has '_', the above code fails
//std::string id = r.getId();
std::string toReturn = id.substr(0, id.rfind("_"));
return toReturn;
}
// ----------------------------------------------------------------------------
int GeneNetwork::getIndexOfNode(std::string label) {
unsigned int i;
for (i = 0; i < nodes_.size(); ++i) {
if ((nodes_.at(i)).getLabel() == label)
break;
}
return i;
}
// ----------------------------------------------------------------------------
int GeneNetwork::getIndexOfNode(HillGene& gene) {
std::vector<HillGene>::iterator it = find(nodes_.begin(), nodes_.end(), gene);
if (it != nodes_.end())
return it - nodes_.begin();
else {
::logging::log::emit<Error>() << gene.getLabel().c_str() << " not in network!" << ::logging::log::endl;
throw Exception();
}
}
// ----------------------------------------------------------------------------
/** Get a string with all gene labels separated by tabs (often used as header when printing data) */
std::string GeneNetwork::getHeader(bool includeProteins) {
std::string header = "\"" + nodes_.at(0).getLabel() + "\"";
for (unsigned int i=1; i<nodes_.size(); i++)
header += "\t\"" + nodes_.at(i).getLabel() + "\"";
if (includeProteins)
for (unsigned int i=1; i<nodes_.size(); i++)
header += "\t\"" + nodes_.at(i).getLabel() + "_prot\"";
header += "\n";
return header;
}
// ----------------------------------------------------------------------------
/**
* Compute the rate of change of all state variables x (and y if translation is modelled).
* The result is returned concatenated into one array [dxdt dydt].
* @param xy Used to set the gene expressions x and protein concentrations y (if translation)
* @param dxydt Variations of x and y (output)
*/
void GeneNetwork::computeDxydt(Vec_DP& xy, Vec_DP& dxydt) {
bool modelTranslation = GnwSettings::Instance()->getModelTranslation();
//double[] dxydt = new double[xy.length];
int size = getSize();
for (int i=0; i<size; i++) {
x_[i] = xy[i];
std::stringstream s1,s2;
s1 << i;
s2 << x_[i];
//::logging::log::emit<Debug>() << "x" << s1.str().c_str() << " = " << s2.str().c_str() << ::logging::log::endl;
}
if (modelTranslation)
for (int i=0; i<size; i++)
y_[i] = xy[size+i];
else
y_ = x_;
// dxydt temporarily used to store the production rates of mRNA
computeMRnaProductionRates(dxydt);
for (int i=0; i < size; i++)
dxydt[i] = dxydt[i] - nodes_.at(i).computeMRnaDegradationRate(x_[i]);
if (modelTranslation)
for (int i=0; i<size; i++)
dxydt[size+i] = nodes_.at(i).getMaxTranslation()*x_[i] - nodes_.at(i).computeProteinDegradationRate(y_[i]);
}
// ---------------------------------------------------------------------------
void GeneNetwork::dynamics(Vec_I_DP& xy, Vec_O_DP& dxydt, GeneNetwork& grn) {
bool modelTranslation = GnwSettings::Instance()->getModelTranslation();
//double[] dxydt = new double[xy.length];
int size = grn.getSize();
for (int i=0; i<size; i++) {
grn.x_[i] = xy[i];
//std::stringstream s1,s2;
//s1 << i;
//s2 << x_[i];
//::logging::log::emit<Debug>() << "x" << s1.str().c_str() << " = " << s2.str().c_str() << ::logging::log::endl;
}
if (modelTranslation)
for (int i=0; i<size; i++)
grn.y_[i] = xy[size+i];
else
grn.y_ = grn.x_;
// dxydt temporarily used to store the production rates of mRNA
grn.computeMRnaProductionRates(dxydt);
for (int i=0; i < size; i++)
dxydt[i] = dxydt[i] - grn.nodes_.at(i).computeMRnaDegradationRate(grn.x_[i]);
if (modelTranslation)
for (int i=0; i<size; i++)
dxydt[size+i] = grn.nodes_.at(i).getMaxTranslation()*grn.x_[i] -
grn.nodes_.at(i).computeProteinDegradationRate(grn.y_[i]);
}
// ---------------------------------------------------------------------------
//int GeneNetwork::gene_network_dynamics(double, const double xy[], double dxydt[], void *param) {
//
// GeneNetwork *grn = (GeneNetwork*) param;
// bool modelTranslation = GnwSettings::Instance()->getModelTranslation();
// //double[] dxydt = new double[xy.length];
// int size = grn->getSize();
//
// for (int i=0; i<size; i++) {
// if (xy[i] >= 0)
// grn->x_[i] = xy[i];
// else
// grn->x_[i] = 0;
// std::stringstream s1,s2;
// s1 << i;
// s2 << grn->x_[i];
// //::logging::log::emit<Debug>() << "x" << s1.str().c_str() << " = " << s2.str().c_str() << ::logging::log::endl;
// }
//
// if (modelTranslation)
// for (int i=0; i<size; i++)
// grn->y_[i] = xy[size+i];
// else
// grn->y_ = grn->x_;
//
// // dxydt temporarily used to store the production rates of mRNA
// Vec_DP dxydt_v(size);
// grn->computeMRnaProductionRates(dxydt_v);
//
// for (int i=0; i < size; i++)
// dxydt_v[i] = dxydt_v[i] - grn->getNodes().at(i).computeMRnaDegradationRate(grn->x_[i]);
//
// for (int i=0; i < size; i++)
// dxydt[i] = dxydt_v[i];
//
// /*
// if (modelTranslation)
// for (int i=0; i<size; i++)
// dxydt[size+i] = nodes_.at(i).getMaxTranslation()*x_[i] - nodes_.at(i).computeProteinDegradationRate(y_[i]);
// */
// return GSL_SUCCESS;
//}
// ============================================================================
// PRIVATE FUNCTIONS
/**
* Compute the production rate for all the genes. This function is identical to
* the one below, except that the argument is passed as double[] instead of
* DoubleMatrix1d. We need both because the SDE implementation works with
* DoubleMatrix1d and the ODE implementation works with arrays.
* NOTE: x_ must be set before calling this function.
* @param productionRates Return the production rates of all the genes
*/
inline
void GeneNetwork::computeMRnaProductionRates(Vec_DP& productionRates) {
int size = getSize();
for (int i=0; i < size; i++) {
std::vector<std::string> inputGenes = nodes_.at(i).getInputGenes();
Vec_INT inputIndex(inputGenes.size());
std::stringstream ss, ssx;
//ss << nodes_.at(i).getRegulatoryModules().size();
ssx << x_[i];
//if (x_[i] < 0 || std::isinf(x_[i]))
// ::logging::log::emit<Debug>() << "node = " <<
// nodes_.at(i).getLabel().c_str() << " x = " <<
// ssx.str().c_str() << ::logging::log::endl;
if (GnwSettings::Instance()->getModelTranslation()) {
for (int j = 0; j < inputIndex.size(); ++j)
inputIndex[j] = getIndexOfNode(inputGenes.at(j));
productionRates[i] = nodes_.at(i).computeMRnaProductionRate(inputIndex, y_);
}
else {
for (int j = 0; j < inputIndex.size(); ++j)
inputIndex[j] = getIndexOfNode(inputGenes.at(j));
productionRates[i] = nodes_.at(i).computeMRnaProductionRate(inputIndex, x_);
}
}
}
// ---------------------------------------------------------------------------
/**
* Compute the production rate for all the genes. This function is identical to
* the one above, except that the argument is passed as DoubleMatrix1d instead of
* double[]. We need both because the SDE implementation works with
* DoubleMatrix1d and the ODE implementation works with arrays.
* NOTE: x_ must be set before calling this function.
* @param productionRates Return the production rates of all the genes
*/
void GeneNetwork::computeInputs(Vec_I_DP& x, Vec_O_DP& productionRates) {
int size = getSize();
for (int i=0; i < size; i++) {
//if (GnwSettings.getInstance().getModelTranslation())
// productionRates.set(i, ((Gene)nodes_.get(i)).computeMRnaProductionRate(i, y_));
//else
std::stringstream ss;
ss << x[i];
if (x[i] < 0)
::logging::log::emit<Debug>() << "node = " << nodes_.at(i).getLabel().c_str() << " x = " << ss.str().c_str() << ::logging::log::endl;
std::vector<std::string> inputGenes = nodes_.at(i).getInputGenes();
Vec_INT inputIndex(inputGenes.size());
for (int j = 0; j < inputIndex.size(); ++j)
inputIndex[j] = getIndexOfNode(inputGenes.at(j));
productionRates[i] = nodes_.at(i).computeMRnaProductionRate(inputIndex, x);
std::stringstream ss1,ss2,ss3;
ss1 << i;
ss2 << productionRates[i];
ss3 << x[i];
//::logging::log::emit<Debug>() << ss1.str().c_str() << " " << ss2.str().c_str() << " " << ss3.str().c_str() << ::logging::log::endl;
}
}
// ---------------------------------------------------------------------------
void GeneNetwork::setRank(const char *filename) {
ifstream data_file(filename);
if (!data_file.is_open()) {
::logging::log::emit<Error>() << "Failed to open file " << filename << ::logging::log::endl;
throw Exception();
}
std::string line, entry;
// ranked list
int counter = 1;
while (getline(data_file, line)) {
std::istringstream ls(line);
getline(ls, entry, '\n');
rank_.push_back(nodes_.at(getIndexOfNode(entry)));
counter++;
}
data_file.close();
}
// ---------------------------------------------------------------------------
std::vector<HillGene>& GeneNetwork::getRank() {
return rank_;
}
// ----------------------------------------------------------------------------
/**
* Return a list of all genes that perturbe the gene given in parameter.
* @param gene Gene perturbed
* @return Instance of all genes that perturbed the gene given in parameter.
*/
void GeneNetwork::getInputs(HillGene& gene, std::vector<std::string>& inputs) {
for (unsigned int i=0; i < edges_.size(); i++) {
if (edges_.at(i).getTarget().getLabel() == gene.getLabel()) {
std::string source = edges_.at(i).getSource().getLabel();
inputs.push_back(nodes_.at(getIndexOfNode(source)).getLabel());
}
}
}
// ----------------------------------------------------------------------------
/**
* Return a list of genes that are output target of the given gene.
* @param gene The gene of interest
* @param inputs Return value, list of genes that are output of the gene of
* interest
*/
void GeneNetwork::getOutputs(HillGene& gene, std::vector<std::string>& outputs) {
for (unsigned int i=0; i < edges_.size(); i++) {
if (edges_.at(i).getSource().getLabel() == gene.getLabel()) {
std::string target = edges_.at(i).getTarget().getLabel();
outputs.push_back(target);
}
}
}
// ----------------------------------------------------------------------------
void GeneNetwork::getInputSigns(HillGene& gene, std::vector<Edge::sign>& inputSigns) {
for (unsigned int i=0; i < edges_.size(); i++) {
if (edges_.at(i).getTarget().getLabel() == gene.getLabel()) {
//std::string source = edges_.at(i).getSource().getLabel();
inputSigns.push_back(edges_.at(i).getSign());
//::logging::log::emit<Debug>() <<
// edges_[i].signToString[edges_[i].getSign()].c_str() <<
// ::logging::log::endl;
}
}
}
// ----------------------------------------------------------------------------
/**
* Based on W_, determine for each gene which are its inputs
*/
void GeneNetwork::initializeInputWiring() {
int size = getSize();
for (int i=0; i < size; i++) {
std::vector<std::string> inputs;
getInputs(nodes_.at(i), inputs);
nodes_.at(i).setInputGenes(inputs);
std::vector<std::string> outputs;
getOutputs(nodes_.at(i), outputs);
nodes_.at(i).setOutputGenes(outputs);
std::vector<Edge::sign> inputSigns;
getInputSigns(nodes_.at(i), inputSigns);
nodes_.at(i).setInputSigns(inputSigns);
}
}
// ----------------------------------------------------------------------------
/**
* Random initialization of the dynamical model (based on the fixed topology defined by W_)
*/
void GeneNetwork::randomInitialization() {
::logging::log::emit<Info>() << "Random initialization ..." << ::logging::log::endl;
// set the inputs for the genes based on W_
initializeInputWiring();
int size = nodes_.size();
//HillGene gene;
for (int i=0; i < size; i++) {
nodes_.at(i).randomKineticInitialization();
}
std::stringstream ss1, ss2;
for (int i=0; i < size; i++) {
ss1 << i + 1;
ss2 << nodes_[i].getNumInputs();
//::logging::log::emit<Debug>() << ss1.str().c_str() << "\t" <<
// nodes_.at(i).getLabel().c_str() << "\t" << ss2.str().c_str()
// << ::logging::log::endl;
nodes_.at(i).randomStructureInitialization();
/*
std::stringstream ss;
if (nodes_.at(i).getRegulatoryModules().size() > 0) {
ss << nodes_.at(i).getLabel() << " " << nodes_.at(i).getRegulatoryModules().at(0).getNumInputs();
::logging::log::emit<Debug>() << ss.str().c_str() << ::logging::log::endl;
}
*/
//log.log(Level.INFO, gene.getLabel() + ": " + gene.toString());
}
// If the network was unsigned before the initialization, we have to set the types
// of the edges (enhancing / inhibiting) accordingly. If the network was signed,
// this just sets the same values that are already there.
//setEdgeTypesAccordingToDynamicalModel();
//signed_ = true;
}
// ----------------------------------------------------------------------------
/**
* Set the type (enhancing, inhibiting, dual, or unknown) of all edges according
* to the dynamically model.
*/
/*
void GeneNetwork::setEdgeTypesAccordingToDynamicalModel() {
for (unsigned int i=0; i<nodes_.size(); i++) {
nodes_.at(i).setInputEdgeTypesAccordingToDynamicalModel();
}
}
*/
// ---------------------------------------------------------------------------
void GeneNetwork::getEdge(HillGene& src, HillGene& tgt, Edge& e) {
for (unsigned int i = 0; i < edges_.size(); ++i) {
if (edges_.at(i).getSource() == src && edges_.at(i).getTarget() == tgt) {
e = edges_.at(i);
return;
}
}
::logging::log::emit<Info>() << "Edge " << src.getLabel().c_str() << "->" << tgt.getLabel().c_str() << " not found!" << ::logging::log::endl;
}
// ============================================================================
/**
* implement the pruning
*/
void GeneNetwork::prune(std::vector<HillGene>& genes) {
//log.log(Level.INFO, "edge_size = " + edges_.size());
std::vector<Edge> edge_clone;
for (unsigned int iNode = 0; iNode < genes.size(); iNode++) {
::logging::log::emit<Info>() << "Pruning " << genes.at(iNode).getLabel().c_str() << ::logging::log::endl;
//edge_clone = new ArrayList<Edge>();
for (unsigned int iEdge = 0; iEdge < edges_.size(); iEdge++) {
//log.log(Level.INFO, "edge " + Integer.toString(iEdge) + " " + edges_.get(iEdge).toString() + " " + genes.get(iNode));
if (edges_.at(iEdge).getSource() == genes.at(iNode) ||
edges_.at(iEdge).getTarget() == genes.at(iNode)) {
//edges_.erase(edges_.begin()+iEdge);
::logging::log::emit<Info>() << "\tPruning " << edges_.at(iEdge).getSource().getLabel().c_str() << " -> " << edges_.at(iEdge).getTarget().getLabel().c_str() << ::logging::log::endl;
nodes_.at(getIndexOfNode(edges_.at(iEdge).getTarget().getLabel())).pruneInput(nodes_.at(getIndexOfNode(edges_.at(iEdge).getSource().getLabel())));
//std::stringstream ss;
//ss << nodes_.at(getIndexOfNode(edges_.at(iEdge).getTarget().getLabel())).getRegulatoryModules().size();
//::logging::log::emit<Debug>() << nodes_.at(getIndexOfNode(edges_.at(iEdge).getTarget().getLabel())).getLabel().c_str() << " input size " << ss.str().c_str() << ::logging::log::endl;
} else {
edge_clone.push_back(edges_.at(iEdge));
}
}
edges_ = edge_clone;
}
edges_ = edge_clone;
//log.log(Level.INFO, "edge_size = " + edges_.size());
std::vector<HillGene> node_clone;
for (unsigned int iEdge = 0; iEdge < edges_.size(); iEdge++) {
if (find(node_clone.begin(),node_clone.end(),edges_.at(iEdge).getSource()) == node_clone.end())
node_clone.push_back(nodes_.at(getIndexOfNode(edges_.at(iEdge).getSource().getLabel())));
if (find(node_clone.begin(),node_clone.end(),edges_.at(iEdge).getTarget()) == node_clone.end())
node_clone.push_back(nodes_.at(getIndexOfNode(edges_.at(iEdge).getTarget().getLabel())));
}
nodes_ = node_clone;
//log.log(Level.INFO, "size = " + nodes_.size());
//std::stringstream ss;
//ss << nodes_.at(getIndexOfNode("G37")).getRegulatoryModules().size();
//::logging::log::emit<Debug>() << "G37 module size " << ss.str().c_str() << ::logging::log::endl;
//setSize(getSize());
initializeInputWiring();
// remove k,n,alpha
std::vector<RegulatoryModule> modules;
std::vector<HillGene> inputs;
//ArrayList<String> label;
HillGene src;
Vec_DP k, n;
for (unsigned int iNode = 0; iNode < nodes_.size(); iNode++) {
/*
modules = nodes_.at(iNode).getRegulatoryModules();
for (int iModule = 0; iModule < modules.size(); iModule++) {
k = modules.at(iModule).getK();
n = modules.at(iModule).getN();
inputs = modules.at(iModule).getInputs();
for (int iInput = 0; iInput < inputs.size(); iInput++) {
label = new ArrayList<String>();
label.add(inputs.get(iInput).getLabel());
src = nodes_.get(getNodeIndexesFromNodeLabels(label).get(0));
if (!containsEdge(getEdge(src, nodes_.get(iNode)))) {
n[iInput] = 0;
}
}
modules.get(iModule).setK(k);
}
*/
//nodes_.at(iNode).randomInitializationOfAlpha();
}
//return *this;
}