Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
geewhiz.h
00001 /**************************************************************************
00002  * Copyright (c) 2006-2011 T. M. Murali                                   *
00003  *                                                                        *
00004  * This file is part of Biorithm.                                         *
00005  *                                                                        *
00006  * Biorithm is free software: you can redistribute it and/or modify       *
00007  * it under the terms of the GNU General Public License as published by   *
00008  * the Free Software Foundation, either version 3 of the License, or      *
00009  * (at your option) any later version.                                    *
00010  *                                                                        *
00011  * Biorithm is distributed in the hope that it will be useful,            *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00014  * GNU General Public License for more details.                           *
00015  *                                                                        *
00016  * You should have received a copy of the GNU General Public License      *
00017  * along with Biorithm.  If not, see <http://www.gnu.org/licenses/>.      *
00018  *                                                                        *
00019  **************************************************************************/
00020 
00027 #ifndef _GEEWHIZ_H
00028 #define _GEEWHIZ_H
00029 
00030 #include<iostream>
00031 #include<fstream>
00032 #include<vector>
00033 #include<string>
00034 #include<map>
00035 #include<sstream>
00036 #include<algorithm>
00037 
00038 #include "boost/lexical_cast.hpp"
00039 
00040 //#include "global.h"
00041 //#include "function.h"
00042 #include "graph.h"
00043 
00044 using namespace std;
00045 
00046 namespace Graphwhiz
00047 {
00048 
00049 //poor information hiding for classes, but really these are structs in
00050 //need of a constructor
00051 class MyColor
00052 {
00053 public:
00054   unsigned int R, G, B;
00055   string RGB, RGBHTML;
00056   string blank;
00058   MyColor()
00059     {
00060       R = G = B = 0;
00061       // see MyColor::generate() to see how to initialise these colours.
00062       RGB="00,00,00";
00063       // should be hex.
00064       RGBHTML = "000000";
00065     }
00070   MyColor(unsigned int _R, unsigned int _G, unsigned int _B)
00071     {
00072       R = min(_R, (unsigned int) 255);
00073       G = min(_G, (unsigned int) 255);
00074       B = min(_B, (unsigned int) 255);
00075       _fillIn();
00076     }
00077   
00083   static MyColor generate(string id);
00084   
00085   string printHTML() const
00086     {
00087       if ("" == RGBHTML)
00088         cerr << "\tThere is no colour value." << endl;
00089       return blank+"\"#"+RGBHTML+"\"";
00090     }
00091   
00092   string printRGB() const
00093     {
00094       return(RGB);
00095     }
00096 
00097 private:
00098 
00100   void _fillIn();
00101 
00102 };
00103 
00104 struct FunctionType
00105 {
00106 private:
00107   MyColor color;
00108   string url;
00109 
00110 public:
00111 
00113   FunctionType()
00114       : color(), url("")
00115     {}
00116 
00118   FunctionType(MyColor c, string u = "")
00119       : color(c), url(u)
00120     {}
00121   
00125   FunctionType(string id, string url = "")
00126       : color(MyColor::generate(id)), url("")
00127     {}
00128   
00129   string getColorHTML() const
00130     {
00131       return color.printHTML();
00132     }
00133   string getColorRGB() const
00134     {
00135       return color.printRGB();
00136     }
00137 
00138   string getURL() const
00139     {
00140       return url;
00141     }
00142   
00143   void setColor(MyColor &c)
00144     {
00145       color = c;
00146     }
00147   
00148   void setURL(string u)
00149     {
00150       url = u;
00151     }
00152 };
00153 
00154 
00159 struct NodeInfo
00160 {
00161 public:
00162   NodeInfo()
00163       : name(""), label(""), type(""), functions(), url(""), color(), colors(), image(""), weight(0)
00164     {}
00165   
00166   // the string used to ID a node.
00167   string name;
00168   // the string used to label a node in the picture output by the visualisation programme.
00169   //
00170   // it is useful to differentiate between names and labels because we
00171   // often draw networks from more than 1 species. orthologous genes
00172   // can have the same name in > 1 species but we want to draw them as
00173   // separate nodes, while still using the same label for them in the
00174   // picture.
00175   string label;
00176   string type;
00177   vector< FunctionType > functions;
00178   string url;
00179   // this will colour the node.
00180   MyColor color;
00181   // this will be a rows of colours below the node, each row made up
00182   // on the colours in each sub-vector.
00183   vector< vector< MyColor > > colors;
00184   string image;
00185   // sometimes a node may have a weight that i want to draw.
00186   MyNT weight;
00187 
00189   // void addColour(MyColor &c)
00190   //   {
00191   //     colors.push_back(c);
00192   //   }
00193   
00195   void addFunction(FunctionType &f)
00196     {
00197       functions.push_back(f);
00198     }
00199   
00200 };
00201 
00209 class NodeType
00210 {
00211 public:
00212   string shape;
00213   string style;
00214   MyColor color;
00215   bool url,img;
00216   NodeType()
00217     {
00218       url=img=false;
00219       shape="rectangle";
00220     }
00221 
00223   string getDescriptionString(NodeInfo &n);
00224 
00226   string getHTMLString(NodeInfo & n);
00227   
00228 };
00229 
00230 
00231 
00232 // function object for comparing edge weights. 
00233 // struct compareMyEdgeWeights : public binary_function< const myedge&, const myedge&, bool> 
00234 // {
00235 //   bool operator()(const myedge& __x, const myedge& __y) const
00236 //     {
00237 //       return(__x.weight < __y.weight);
00238 //     }
00239 // };
00240 
00241 
00242 class EdgeType
00243 {
00244 public:
00245   MyColor color;
00246   string style;
00247   string direction;
00248   string head;
00249   string tail;
00250   string length;
00251   
00252   EdgeType()
00253     {
00254       style="solid";
00255       direction="none";
00256       head="normal";
00257       tail="none";
00258       length="1.0";
00259     }
00260 
00262   string print(double width=1);
00263 };
00264 
00265 
00266 class Geewhiz
00267 {
00268 private:
00269   MyGraph _graph;
00270 
00271   // should the output file cluster nodes of the same type into
00272   // subgraphs? _isDirected must also be true for dot to be used and
00273   // for the subgraphs to show up.
00274   bool _clusterNodes;
00275   
00276   bool _isDirected;
00277 
00278   // should I print edge labels.
00279   bool _printEdgeLabels;
00280   
00281   string _undirectedLayoutProgramme;
00282   string _directedLayoutProgramme;
00283   
00284   
00285   map< string, EdgeType > _edgeTypeDefinitions;
00286   
00287   map< string, FunctionType > _functionTypeDefinitions;
00288   
00289   map< string, NodeInfo > _nodeInfo;
00290   
00291 
00292   map< string, NodeType > _nodeTypeDefinitions;
00293 
00294 public:
00295   Geewhiz()
00296       : _graph(), _clusterNodes(false), _isDirected(false), _printEdgeLabels(false),
00297         _undirectedLayoutProgramme("neato"), _directedLayoutProgramme("dot"),
00298         _edgeTypeDefinitions(),
00299         _functionTypeDefinitions(), _nodeInfo(), _nodeTypeDefinitions()
00300     {}
00301 
00303   void addFunctionTypeDefinition(string functionId, MyColor colour, string url = "")
00304     {
00305       addFunctionTypeDefinition(functionId, FunctionType(colour, url));
00306     }
00307 
00309   void addFunctionTypeDefinition(string id, const FunctionType &ftype)
00310     {
00311       _functionTypeDefinitions[id] = ftype;
00312     }
00313   
00314   
00315   
00317   void addNodeInfo(const NodeInfo &info)
00318     {
00319       static set< string > unknownNodeInfos;
00320       _nodeInfo[info.name] = info;
00321       if ((_nodeTypeDefinitions.end() == _nodeTypeDefinitions.find(info.type)) &&
00322           (unknownNodeInfos.end() != unknownNodeInfos.find(info.type)))
00323         // this is the first time i am seeing an known node info.
00324         {
00325           cerr << "Geewhiz::addNodeInfo(): unknown node type " << info.type
00326                << " for node " << info.name
00327                << ". Suppressing messages for other nodes with this type." << endl;
00328           unknownNodeInfos.insert(info.type);
00329         }
00330       
00331     }
00332 
00341   void setClusterNodes()
00342     {
00343       _clusterNodes = true;
00344     }
00345 
00346   bool getClusterNodes() const
00347     {
00348       return(_clusterNodes);
00349     }
00350 
00359   void setIsDirected()
00360     {
00361       _isDirected = true;
00362     }
00363 
00365   bool getIsDirected() const
00366     {
00367       return(_isDirected);
00368     }
00369 
00370 
00372   void getFunctionTypeDefinition(string id, FunctionType &type) const
00373     {
00374       map< string, FunctionType >::const_iterator ftdItr = _functionTypeDefinitions.find(id);
00375       if (_functionTypeDefinitions.end() != ftdItr)
00376         type = ftdItr->second;
00377       else
00378         {
00379           cerr << "\tNo function type definition exists for function \""
00380                << id << "\"" << endl;
00381           type = FunctionType("000000", "");
00382         }
00383     }
00384   
00386   bool getPrintEdgeLabels() const
00387     {
00388       return(_printEdgeLabels);
00389     }
00390 
00392   void setPrintEdgeLabels()
00393     {
00394       _printEdgeLabels = true;
00395     }
00396   
00398   void unsetPrintEdgeLabels()
00399     {
00400       _printEdgeLabels = false;
00401     }
00402   
00404   void setUndirectedLayoutProgramme(string prog) 
00405     {
00406       _undirectedLayoutProgramme = prog;
00407     }
00409   string getUndirectedLayoutProgramme()  const
00410     {
00411       return(_undirectedLayoutProgramme);
00412     }
00413   
00415   void setDirectedLayoutProgramme(string prog) 
00416     {
00417       _directedLayoutProgramme = prog;
00418     }
00420   string getDirectedLayoutProgramme()  const
00421     {
00422       return(_directedLayoutProgramme);
00423     }
00424   
00441   void layout(string outfile, const set< string > &formats, ostream &logStream, bool runGraphviz = true);
00442   
00444   void printDot(string outfile);
00445   
00447   void readEdges(string file)
00448     {
00449       _graph.read(file);
00450     }
00451 
00453   void readEdgeTypeDefinitions(string file);
00454 
00456   void readNodeInfo(string file);  
00457 //     {
00458 //       _graph.readNodeTypes(file);
00459 //     }
00460   
00462   void readNodeTypeDefinitions(string file);
00463 
00464 
00470   void readParameters(string filename);
00471 
00472 
00474 
00483   void setGraph(const MyGraph &graph)
00484     {
00485       _graph = graph;
00486     }
00487   
00488 };
00489 
00490 }; // end of namespace Geewhiz.
00491 
00492 #endif // _GEEWHIZ_H 
 All Classes Functions Variables Typedefs Friends