Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
interval.h
00001 /**************************************************************************
00002  * Copyright (c) 2002-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 
00021 // Purpose: to define 1D intervals of various types
00022 
00023 #ifndef _INTERVAL_H
00024 #define _INTERVAL_H
00025 
00026 #include <map>
00027 #include <vector>
00028 
00029 #include "global.h"
00030 #include "binomial.h"
00031 #include "gaussian.h"
00032 
00033 
00034 
00035 // returns true if coord is in [low, high].
00036 inline bool contains(MyNT low, MyNT high, MyNT coord)
00037 {
00038   return((coord >= low) && (coord <= high));
00039 }
00040 
00041 
00042 // forward declaration.
00043 class MyClusterParams;
00044 class MyPointSet;
00045 
00046 // a struct that stores information about significant intervals.
00047 struct MyInterval
00048 {
00049 public:
00050   MyInterval()
00051       : start(), end(), width(), count(), pval()
00052     {}
00053   
00054   MyInterval(MyNT s, MyNT e)
00055       : start(s), end(e), width(e - s), count(), pval()
00056     {}
00057   
00058   MyInterval(MyNT s, MyNT e, MyNT w)
00059       : start(s), end(e), width(w), count(), pval()
00060     {}
00061   
00062   // constructor from a map. cannot use const map< > here since i use
00063   // map< >::operator[]. alternative is to use const and the find()
00064   // function at the risk of a seg fault.
00065   MyInterval(map< string, MyNT, ltstr >& init)
00066 //       : start(init["START"]), end(init["END"]), width(init["WIDTH"]),
00067 //         count(init["COUNT"]), pval(init["PVAL"])
00068       : start(init["start"]), end(init["end"]), width(init["width"]),
00069         count(init["count"]), pval(init["pval"])
00070     {}
00071 
00072   virtual ~MyInterval()
00073     {}
00074   // check if two intervals are identical. enough to check start and end.
00075   virtual bool operator==(const MyInterval& interval) const
00076     {
00077       // if both intervals are pointing to the same instance, they are equal. 
00078       if (&interval == this)
00079         return(true);
00080       if (start != interval.start)
00081         return(false);
00082       if (end != interval.end)
00083         return(false);
00084       return(true);
00085     }
00086 
00087   // check if two intervals are not identical. 
00088   virtual bool operator!=(const MyInterval& interval) const
00089     {
00090       return(!(*this == interval));
00091     }
00092   
00093   // gets and sets
00094   MyNT getStart() const
00095     {
00096       return(start);
00097     }
00098   void setStart(MyNT s)
00099     {
00100       start = s;
00101       setWidth();
00102     }
00103 
00104   MyNT getEnd() const
00105     {
00106       return(end);
00107     }
00108   void setEnd(MyNT e)
00109     {
00110       end = e;
00111       setWidth();
00112     }
00113   // more of an internal function to be called by setStart() and setEnd(). 
00114   void setWidth()
00115     {
00116       width = end - start;
00117     }
00118   
00119   
00120   virtual void print(ostream& ostr) const
00121     {
00122       ostr << "Interval is pval = " << pval << ", start = " << start
00123            << ", end = " << end << ", width = " << width << ", count = " << count
00124            << endl;
00125     }
00126 
00127   // check if *this contains in.
00128   virtual bool contains(const MyInterval& in) const
00129     {
00130       return(contains(in.getStart(), in.getEnd()));
00131     }
00132   
00133   // check if [start, end] contains p.
00134   virtual bool contains(MyNT p) const
00135     {
00136       return((start <= p) && (p <= end));
00137     }
00138   
00139   // checks if [start, end] contains [s, e]. needed for MyInterval::softContains().
00140   virtual bool contains(MyNT s, MyNT e) const
00141     {
00142       return((start <= s) && (e <= end));
00143     }
00144 
00145   // checks if [s, e] equals [start, end].
00146   virtual bool equals(MyNT s, MyNT e) const
00147     {
00148       return((start == s) && (e == end));
00149     }
00150 
00151   // checks what fraction of [s, e] is within [start, end]
00152   virtual MyNT softContains(MyNT s, MyNT e) const;
00153   
00154   
00155   MyNT start;
00156   MyNT end;
00157   MyNT width;
00158   MyNT count;
00159   double pval;
00160 };
00161 
00162 // an interval with its dimension
00163 struct MyDimensionInterval : public MyInterval
00164 {
00165   MyDimensionInterval()
00166       : MyInterval(), dimension()
00167     {}
00168   
00169   MyDimensionInterval(MyNT s, MyNT e, int d)
00170       : MyInterval(s, e), dimension(d)
00171     {}
00172   
00173   MyDimensionInterval(MyNT s, MyNT e, MyNT w, int d)
00174       : MyInterval(s, e, w), dimension(d)
00175     {}
00176 
00177   virtual ~MyDimensionInterval()
00178     {}
00179 
00180   // check if two intervals are identical. enough to check start and end.
00181   virtual bool operator==(const MyDimensionInterval& interval) const
00182     {
00183       // if both intervals are pointing to the same instance, they are equal. 
00184       if (&interval == this)
00185         return(true);
00186       if (dimension != interval.dimension)
00187         return(false);
00188       // call the parent class's operator.
00189       return((*this).MyInterval::operator==(interval));
00190     }
00191   
00192   unsigned int getDimension() const
00193     {
00194       return(dimension);
00195     }
00196 
00197   void setDimension(unsigned int d)
00198     {
00199       dimension = d;
00200     }
00201 
00202   bool isSignificant(const MyPointSet &points, const MyClusterParams &params);
00203   // compute the pvalue of this interval based on the assumption that
00204   // the points in this interval were generated by a uniform
00205   // distribution. not const because the function sets the count and
00206   // pval fields.
00207   double computePValueUniform(const MyPointSet &points);
00208   // compute the pvalue of this interval based on the assumption that
00209   // the points in this interval were generated by a gaussian
00210   // distribution. not const because the function sets the count and
00211   // pval fields.
00212   double computePValueGaussian(const MyPointSet &points);
00213   
00214   void _locate(vector< MyNT > &coords, int& middle, int& left, int& right);// ,
00215 //                MyNT *mean = NULL, MyNT *sigma = NULL);
00216 
00217 
00218   // the dimension this interval corresponds to. 
00219   unsigned int dimension;
00220 };
00221 
00222 
00223 // an interval with a p-value.
00224 struct MySignificantInterval : public MyInterval
00225 {};
00226 
00227 
00228 
00229 // a struct that stores information about a range of widths/intervals
00230 // with a given starting point.
00231 struct MyWidthRange
00232 {
00233 public:
00234   MyWidthRange()
00235       : start(), min_width(), max_width()
00236     {}
00237   
00238   MyWidthRange(MyNT s, MyNT min, MyNT max)
00239       : start(s), min_width(min), max_width(max)
00240     {}
00241   
00242   // constructor from a map. cannot use const map< > here since i use
00243   // map< >::operator[]. alternative is to use const and the find()
00244   // function at the risk of a seg fault.
00245   MyWidthRange(map< string, MyNT, ltstr >& init)
00246       : start(init["start"]), min_width(init["min-width"]),
00247         max_width(init["max-width"])
00248     {}
00249 
00250   virtual ~MyWidthRange()
00251     {}
00252 
00253   // check if two intervals are identical. enough to check start and end.
00254   virtual bool operator==(const MyWidthRange& interval) const
00255     {
00256       // if both intervals are pointing to the same instance, they are equal. 
00257       if (&interval == this)
00258         return(true);
00259       if (start != interval.start)
00260         return(false);
00261       if (min_width != interval.min_width)
00262         return(false);
00263       if (max_width != interval.max_width)
00264         return(false);
00265       return(true);
00266     }
00267 
00268   // check if two intervals are not identical. 
00269   virtual bool operator!=(const MyWidthRange& interval) const
00270     {
00271       return(!(*this == interval));
00272     }
00273   
00274   // gets and sets
00275   MyNT getStart() const
00276     {
00277       return(start);
00278     }
00279   void setStart(MyNT s)
00280     {
00281       start = s;
00282     }
00283 
00284   void setMinWidth(MyNT width)
00285     {
00286       min_width = width;
00287     }
00288   
00289   
00290   void setMaxWidth(MyNT width)
00291     {
00292       max_width = width;
00293     }
00294   
00295   
00296   virtual void print(ostream& ostr) const
00297     {
00298       ostr << "Interval is start = " << start
00299            << ", min-width = " << min_width << ", max-width = " << max_width
00300            << endl;
00301     }
00302 
00303   // check if *this contains in.
00304   virtual bool contains(const MyInterval& in) const
00305     {
00306       return(contains(in.getStart(), in.getEnd()));
00307     }
00308   
00309   // check if this range of intervals contains p.
00310   virtual bool contains(MyNT p) const
00311     {
00312       return((start + min_width <= p) && (p <= start + max_width));
00313     }
00314   
00315   // checks if this range of intervals contains [s, e].
00316   virtual bool contains(MyNT s, MyNT e) const
00317     {
00318       return((start == s) && contains(e));
00319     }
00320 
00321 //   // checks if [s, e] equals [start, end].
00322 //   virtual bool equals(MyNT s, MyNT e) const
00323 //     {
00324 //       return((start == s) && (e == end));
00325 //     }
00326   
00327   MyNT start;
00328   MyNT min_width;
00329   MyNT max_width;
00330 };
00331 
00332 
00333 
00334 #endif // _INTERVAL_H 
 All Classes Functions Variables Typedefs Friends