Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
reconciliation-algorithms.h
00001 /**************************************************************************
00002  * Copyright (c) 2009-2012 T. M. Murali                                   *
00003  * Copyright (c) 2009-2012 Christopher L. Poirel                          *
00004  * Copyright (c) 2010 Jacqueline Addesa                                   *
00005  *                                                                        *
00006  * This file is part of Bioverse.                                         *
00007  *                                                                        *
00008  * Bioverse is free software: you can redistribute it and/or modify       *
00009  * it under the terms of the GNU General Public License as published by   *
00010  * the Free Software Foundation, either version 3 of the License, or      *
00011  * (at your option) any later version.                                    *
00012  *                                                                        *
00013  * Bioverse is distributed in the hope that it will be useful,            *
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00016  * GNU General Public License for more details.                           *
00017  *                                                                        *
00018  * You should have received a copy of the GNU General Public License      *
00019  * along with Bioverse.  If not, see <http://www.gnu.org/licenses/>.      *
00020  *                                                                        *
00021  **************************************************************************/
00022 
00023 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <fstream>
00035 #include <math.h>
00036 
00037 #include "graph.h"   // for graphs
00038 #include "point.h"   // for MyPointSet
00039 
00040 #include "boost/algorithm/string.hpp" //for string split
00041 
00042 using std::cout;
00043 using std::endl;
00044 
00045 void readMultipleNetworks(string file, vector< MyGraph > & graphs);
00046 
00047 
00057 class AbstractSingleReconciliation
00058 {
00059 protected:
00061   vector< MyGraph > _graphs;
00062 
00064   MyGraph _currGraphA;
00065 
00067   MyPointSet _weights;
00068 
00069   // reconciliation parameters
00071   double _q;
00073   unsigned int _MAX_ITERS;
00075   double _EPSILON;
00076 
00078   unsigned int _currSampleA;
00079 
00081   string _outputPrefix;
00082 
00083 
00085   map< MyNodeId, double >  _initialNodeWeightsA;
00087   map< MyNodeId, double >  _currNodeWeightsA;
00089   map< MyNodeId, double >  _prevNodeWeightsA;
00090 
00091 
00102   virtual void _initialiseNodeWeights(map < MyNodeId, double > & initial,
00103                                       map < MyNodeId, double > & current,
00104                                       map < MyNodeId, double > & previous,
00105                                       MyGraph & graph,
00106                                       const MyPointSet & weights,
00107                                       unsigned int sample,
00108                                       bool normalize=true);
00109 
00117   virtual void _initialiseNodeWeights(map < MyNodeId, double > & initial,
00118                                       map < MyNodeId, double > & current,
00119                                       map < MyNodeId, double > & previous,
00120                                       MyGraph & graph);
00121 
00129   virtual void _initialiseNodeWeights(map < MyNodeId, double > & initial,
00130                                       map < MyNodeId, double > & current,
00131                                       map < MyNodeId, double > & previous,
00132                                       MyGraph & graph,
00133                                       double val);
00134 
00135 
00137   virtual void _runAlgorithmOnceA();
00138 
00143   virtual bool _checkFinished(unsigned int iters, double maxDiff);
00144 
00145   // not const MyNode& because of problems with MyNode::ConstIncidentEdgeIterator
00147   virtual void _updateNodeWeightA(MyNode node) = 0;
00148 
00150   virtual void _printNodeWeights(ofstream & ostr);
00151 
00153   virtual void _runForWeights();
00155   virtual void _runForGraphs();
00156 
00158   virtual pair< double, double > _computeEnergy() { pair< double, double > dummy (0,0); return dummy;  }
00159 
00160 public:
00161 
00170   AbstractSingleReconciliation(MyGraph &graph, MyPointSet &weights,
00171       double q, double maxIters, double epsilon, string outputPrefix)
00172       : _graphs(), _currGraphA(graph), _weights(weights), _q(q), _MAX_ITERS(maxIters), _EPSILON(epsilon),
00173         _currSampleA(), _outputPrefix(outputPrefix),
00174         _initialNodeWeightsA(), _currNodeWeightsA(), _prevNodeWeightsA()
00175     {
00176       if (!_weights._isFlipped())
00177         _weights.flip();
00178     }
00179 
00187   AbstractSingleReconciliation(vector< MyGraph > & graphs,
00188       double q, double maxIters, double epsilon, string outputPrefix)
00189       : _graphs(graphs), _currGraphA(), _weights(), _q(q), _MAX_ITERS(maxIters), _EPSILON(epsilon),
00190         _currSampleA(), _outputPrefix(outputPrefix),
00191         _initialNodeWeightsA(), _currNodeWeightsA(), _prevNodeWeightsA()
00192     {}
00193 
00195   virtual ~AbstractSingleReconciliation()
00196     {}
00197 
00199   virtual string getName() { return "AbstractSingleReconciliation"; }
00200 
00202   virtual void run();
00203 
00207   void setCurrGraphA(MyGraph & g) { _currGraphA.clear(); _currGraphA = g; }
00208 
00212   void setCurrSampleA(unsigned int n) { _currSampleA = n; }
00213 };
00214 
00215 
00222 class PageRank : public AbstractSingleReconciliation
00223 {
00224 protected:
00225 
00227   virtual void _updateNodeWeightA(MyNode node);
00228 
00230   pair< double, double > _computeEnergy();
00231 
00232 public:
00233 
00242   PageRank(MyGraph &graph, MyPointSet &weights,
00243       double q, double maxIters, double epsilon, string output)
00244       : AbstractSingleReconciliation(graph, weights, q, maxIters, epsilon, output)
00245     {}
00246 
00254   PageRank(vector< MyGraph > & graphs,
00255       double q, double maxIters, double epsilon, string outputPrefix)
00256       : AbstractSingleReconciliation(graphs, q, maxIters, epsilon, outputPrefix)
00257     {}
00258 
00260   virtual ~PageRank()
00261     {}
00262 
00264   virtual string getName() { return "PageRank"; }
00265 };
00266 
00267 
00274 class Vanilla : public AbstractSingleReconciliation
00275 {
00276 protected:
00277 
00279   virtual void _updateNodeWeightA(MyNode node);
00280 
00282   virtual pair< double, double > _computeEnergy();
00283 
00284 public:
00285 
00294   Vanilla(MyGraph &graph, MyPointSet &weights,
00295       double q, double maxIters, double epsilon, string output)
00296       : AbstractSingleReconciliation(graph, weights, q, maxIters, epsilon, output)
00297     {}
00298 
00306   Vanilla(vector< MyGraph > & graphs,
00307       double q, double maxIters, double epsilon, string outputPrefix)
00308       : AbstractSingleReconciliation(graphs, q, maxIters, epsilon, outputPrefix)
00309     {}
00310 
00312   virtual ~Vanilla()
00313     {}
00314 
00316   virtual string getName() { return "Vanilla"; }
00317 };
00318 
00319 
00326 class GeneMANIA : public AbstractSingleReconciliation
00327 {
00328 protected:
00329 
00331   virtual void _updateNodeWeightA(MyNode node);
00332 
00334   virtual pair< double, double > _computeEnergy();
00335 
00336 public:
00337 
00346   GeneMANIA(MyGraph &graph, MyPointSet &weights,
00347       double q, double maxIters, double epsilon, string output)
00348     : AbstractSingleReconciliation(graph, weights, q, maxIters, epsilon, output)
00349     {}
00350 
00358   GeneMANIA(vector< MyGraph > & graphs,
00359       double q, double maxIters, double epsilon, string outputPrefix)
00360       : AbstractSingleReconciliation(graphs, q, maxIters, epsilon, outputPrefix)
00361     {}
00362 
00364   virtual ~GeneMANIA()
00365     {}
00366 
00368   virtual string getName() { return "GeneMANIA"; }
00369 };
00370 
00371 
00378 class SinkSource : public AbstractSingleReconciliation
00379 {
00380 protected:
00381 
00382   double _LAMBDA;
00383 
00390   virtual void _runAlgorithmOnceA();
00391 
00393   virtual void _updateNodeWeightA(MyNode node);
00394 
00395 public:
00396 
00406   SinkSource(MyGraph &graph, MyPointSet &weights,
00407       double q, double maxIters, double epsilon, double lambda, string output)
00408       : AbstractSingleReconciliation(graph, weights, q, maxIters, epsilon, output),
00409         _LAMBDA(lambda)
00410     {}
00411 
00420   SinkSource(vector< MyGraph > & graphs,
00421       double q, double maxIters, double epsilon, double lambda, string outputPrefix)
00422       : AbstractSingleReconciliation(graphs, q, maxIters, epsilon, outputPrefix),
00423         _LAMBDA(lambda)
00424     {}
00425 
00427   virtual ~SinkSource()
00428     {}
00429 
00431   virtual string getName() { return "SinkSource"; }
00432 
00433 };
00434 
00435 
00442 class HeatKernel : public AbstractSingleReconciliation
00443 {
00444 protected:
00445 
00447   double _t;
00448 
00450   virtual void _updateNodeWeightA(MyNode node);
00451 
00455   virtual bool _checkFinished(unsigned int iters, double maxDiff);
00456 
00457 public:
00458 
00466   HeatKernel(MyGraph &graph, MyPointSet &weights,
00467       double q, double maxIters, string outputPrefix)
00468     : AbstractSingleReconciliation(graph, weights, q, maxIters, 1, outputPrefix)
00469     {
00470       if (_q==1)
00471         _t = 0;
00472       else
00473         _t = -log(q)/log(2);
00474     }
00475 
00482   HeatKernel(vector< MyGraph > & graphs,
00483       double q, double maxIters, string outputPrefix)
00484       : AbstractSingleReconciliation(graphs, q, maxIters, 1, outputPrefix),
00485         _t(-log(q)/log(2))
00486     {}
00487 
00489   virtual ~HeatKernel()
00490     {}
00491 
00493   virtual string getName() { return "HeatKernel"; }
00494 };
00495 
00496 
00497 
00498 
00499 #if 0 // use preprocessor to ignore unfinished multi-walk work.
00500 
00504 class AbstractDoubleReconciliation : public AbstractSingleReconciliation
00505 {
00506 protected:
00507 
00508   MyGraph _currGraphB;
00509 
00510   unsigned int _currSampleB;
00511 
00512   map< MyNodeId, double > _initialNodeWeightsB, _currNodeWeightsB, _prevNodeWeightsB;
00513 
00514   virtual void _runAlgorithmOnceB();
00515 
00516   virtual void _updateNodeWeightB(MyNode node) = 0;
00517 
00518   virtual void _runForWeights();
00519   virtual void _runForGraphs();
00520 
00521   virtual void _printNodeWeights(ofstream & ostr);
00522 
00523   void _initialiseNodeWeightsAW(map < MyNodeId, double > & initial,
00524                                 map < MyNodeId, double > & current,
00525                                 map < MyNodeId, double > & previous,
00526                                 MyGraph & graph,
00527                                 const MyPointSet & weights,
00528                                 unsigned int sample);
00529 
00530 public:
00531 
00532   AbstractDoubleReconciliation(MyGraph &graph, MyPointSet &weights,
00533       double q, double maxIters, double epsilon, string outputPrefix)
00534       : AbstractSingleReconciliation(graph, weights, q, maxIters, epsilon, outputPrefix),
00535         _currGraphB(), _currSampleB(),
00536         _initialNodeWeightsB(), _currNodeWeightsB(), _prevNodeWeightsB()
00537     {}
00538 
00539   AbstractDoubleReconciliation(vector< MyGraph > & graphs,
00540       double q, double maxIters, double epsilon, string outputPrefix)
00541       : AbstractSingleReconciliation(graphs, q, maxIters, epsilon, outputPrefix),
00542         _currGraphB(), _currSampleB(),
00543         _initialNodeWeightsB(), _currNodeWeightsB(), _prevNodeWeightsB()
00544     {}
00545 
00546   virtual ~AbstractDoubleReconciliation()
00547     {}
00548 
00549   virtual string getName() { return "AbstractDoubleReconciliation"; }
00550 
00551 };
00552 
00553 
00557 class AbsorbWalkVisit : public AbstractDoubleReconciliation
00558 {
00559 protected:
00560 
00561   double _r;
00562 
00563   double _nodeSum;
00564 
00565   virtual void _runAlgorithmOnceB();
00566 
00567   virtual void _updateNodeSum();
00568 
00569   virtual void _updateNodeWeightA(MyNode node);
00570 
00571   virtual void _updateNodeWeightB(MyNode node);
00572 
00573   virtual void _printNodeWeights(ofstream & ostr);
00574 
00575 
00576 public:
00577 
00578   AbsorbWalkVisit(MyGraph &graph, MyPointSet &weights,
00579       double q, double r, double maxIters, double epsilon, string outputPrefix)
00580       : AbstractDoubleReconciliation(graph, weights, q, maxIters, epsilon, outputPrefix),
00581         _r(r), _nodeSum(0)
00582     {}
00583 
00584   AbsorbWalkVisit(vector< MyGraph > &graphs, double q, double r, double maxIters, double epsilon, string outputPrefix)
00585       : AbstractDoubleReconciliation(graphs, q, maxIters, epsilon, outputPrefix),
00586         _r(r), _nodeSum(0)
00587     {}
00588 
00589   virtual ~AbsorbWalkVisit()
00590     {}
00591 
00592   virtual string getName() { return "AbsorbWalkVisit"; }
00593 
00594   virtual void run();
00595 
00596   virtual void printTransitionMatrix();
00597 
00598 };
00599 
00600 
00601 
00605 class AbsorbWalkLife : public AbsorbWalkVisit
00606 {
00607 protected:
00608 
00609   virtual void _updateNodeSum();
00610 
00611   virtual void _updateNodeWeightB(MyNode node);
00612 
00613 public:
00614 
00615   AbsorbWalkLife(MyGraph &graph, MyPointSet &weights,
00616       double q, double r, double maxIters, double epsilon, string outputPrefix)
00617       : AbsorbWalkVisit(graph, weights, q, r, maxIters, epsilon, outputPrefix)
00618     {}
00619 
00620   virtual ~AbsorbWalkLife()
00621     {}
00622 
00623   virtual string getName() { return "AbsorbWalkLife"; }
00624 };
00625 
00626 
00627 
00631 class MultiAbsorbWalkVisit : public AbstractDoubleReconciliation
00632 {
00633 protected:
00634 
00635   double _nodeSum;
00636 
00637   set< MyNodeId > _absorbers;
00638 
00639   map< MyNodeId, double > _nwPrevNodeWeightsA, _nwCurrNodeWeightsA;
00640 
00641   void _runNWOnceA();
00642 
00643   virtual void _runForWeights();
00644 
00645   virtual void _runAlgorithmOnceA();
00646 
00647   virtual void _updateNodeSum();
00648 
00649   virtual void _updateNodeWeightA(MyNode v);
00650 
00651   virtual void _updateNodeWeightB(MyNode v){}
00652 
00653   void _initialiseNodeWeights(map < MyNodeId, double > & initialA,
00654                               map < MyNodeId, double > & currentA,
00655                               map < MyNodeId, double > & previousA,
00656                               map < MyNodeId, double > & initialB,
00657                               MyGraph & graph, const MyPointSet & weights,
00658                               unsigned int sampleA, unsigned int sampleB);
00659 
00660   virtual void _printNodeWeights(ofstream & ostr);
00661 
00662 //  void _modifyAbsorbingEdges();
00663 //  void _unmodifyAbsorbingEdges();
00664 
00665 public:
00666 
00667   MultiAbsorbWalkVisit(MyGraph &graph, MyPointSet &weights,
00668       double q, double r, double maxIters, double epsilon, string outputPrefix)
00669       : AbstractDoubleReconciliation(graph, weights, q, maxIters, epsilon, outputPrefix),
00670         _nodeSum(0)
00671     {}
00672 
00673   virtual ~MultiAbsorbWalkVisit()
00674     {}
00675 
00676   virtual void run();
00677 
00678   virtual string getName() { return "MultiAbsorbWalkVisit"; }
00679 };
00680 
00681 #endif // end of preprocessor if statement to ignore multi-walks
 All Classes Functions Variables Typedefs Friends