1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  nodes.h
  5  *
  6  *    Description:  description of phylogentic nodes as per 
  7  *    Fitch and Sannkoff as describd by Felenstein
  8  *
  9  *        Version:  1.0
 10  *       Created: Nov  4 21:15:49 EDT 2016
 11  *       Revision:  none
 12  *       Compiler:  gcc
 13  *
 14  *         Author:  Ruben Safir,
 15  *        Company:  LIU Thesis 
 16  *
 17  * =====================================================================================
 18  */
 19 #ifndef NODES_H
 20 #define NODES_H
 21 #include<iostream>
 22 #include<string>
 23 #include<vector>
 24 namespace tree {
 25 
 26 
 27 /* ==============================================================================
 28  * Class NODE - 
 29  * Description - This is a node in the tree that must undergo parsimony evaluation
 30  * ================================================================================
 31  */
 32 template<class unk>
 33 class NODE{
 34    public:
 35    ¦  //=========================LIFECYCLE=========================================
 36    ¦  /* Constructor */
 37       NODE( std::string  xt, const unk &x , NODE<unk> *xcl= 0, NODE<unk> *xcr = 0, NODE<unk> *xp = 0 ):_trait{xt},_states{x}, _cl{xcl}, _cr{xcr}, _p{xp} {//child left, child right etc
 38       std::cout << std::endl <<"This node has "<< trait() << std::endl << " node " << "has the following states => " << states() << std::endl;
 39       };
 40 
 41       NODE(const NODE &other); //copy constructor
 42    ¦  ~NODE();
 43    ¦
 44    ¦  /* ====================  ACCESSORS     ======================================= */
 45 //no need for inline when you are defineing functions within the class definition
 46       std::string trait()const{
 47          return _trait;
 48       }
 49       unk states()const{
 50          std::cout << std::endl << __LINE__ << " sending _states" << std::endl;
 51    ¦  ¦  return _states;
 52    ¦  };
 53       NODE<unk> * cl()const{
 54    ¦  ¦  return _cl;
 55    ¦  };
 56       NODE<unk> * cr()const{
 57    ¦  ¦  return _cr;
 58    ¦  };
 59       NODE<unk> * p()const{
 60    ¦  ¦  return _p;
 61    ¦  };
 62       void states(const unk &x){
 63    ¦  ¦  _states = x;
 64    ¦  }
 65       void trait(std::string xt){
 66          ¦_trait = xt; //With genetics this is a A C G T although it is just a label
 67       ¦}
 68       void cl(NODE<unk> *xcl){ //This is the left child node in the tree
 69    ¦  ¦  _cl = xcl;
 70    ¦  }
 71       void cr(NODE<unk> *xcr){//This is the right node child node in the tree
 72    ¦  ¦  _cr = xcr;
 73    ¦  }
 74       void p(NODE<unk> *xp){//This is the parent node in the tree
 75    ¦  ¦  _p = xp;
 76    ¦  }
 77    ¦  /* ====================  MUTATORS      ======================================= */
 78    ¦
 79    ¦  /* ====================  OPERATORS     ======================================= */
 80       template <class T>
 81       friend std::ostream& operator<<(std::ostream& os, const T&);
 82 
 83       template<class T>
 84       friend std::ostream& operator<<(std::ostream& os, const NODE<T> &);
 85 
 86    protected:
 87    ¦  /* ====================  DATA MEMBERS  ======================================= */
 88    ¦
 89    ¦
 90    private:
 91    ¦  /* ====================  DATA MEMBERS  ======================================= */
 92       ¦std::string _trait;
 93       ¦unk _states;
 94       ¦NODE<unk> * _cl, * _cr, * _p;
 95 
 96 
 97 }; /* -----  end of template class NODE  ----- */
 98 
 99 
100 
101 
102 template<class unk>
103    tree::NODE<unk>::~NODE<unk>(){
104 }
105 
106 /*
107  * =====================================================================================
108  *        Class:  State
109  *  Description:  This describes a possible individual state or charactoristic 
110  * =====================================================================================
111  */
112 template<class cost>
113 class STATE
114 {
115    public:
116       /* ====================  LIFECYCLE     ======================================= */
117       ¦  ¦  ¦  ¦  ¦  ¦  ¦  ¦/* constructor */
118       //r is the minimum cost of a single state when compared to the child nodes
119       explicit STATE<cost>(std::string const xa, cost xr)
120       :_nam{xa}, _r{xr}
121       {
122          std::cout << std::endl << "Building a state pair" << std::endl;
123          std::cout << "nam => " << nam()  << "\tcost=> " << r() << std::endl;
124       };
125 
126 
127       /* ====================  ACCESSORS     ======================================= */
128       std::string nam()const {return _nam;};
129       cost r() const {return _r;};
130       void r(cost b ){_r = b;};
131       /* ====================  MUTATORS      ======================================= */
132 
133       void change(cost a, cost b){
134          _nam = a;
135          r(b);
136       }
137       /* ====================  OPERATORS     ======================================= */
138       friend std::ostream & operator << ( std::ostream & os, const STATE & obj )
139          {
140             os << std::endl << "name => " << obj.nam() << ":cost=> " << obj.r() ;
141             return os;
142          }     /* -----  end of function operator <<  ----- */
143       STATE & operator = (cost b)
144       {
145          r(b);
146          return *this;
147       }
148 
149       /* ====================  DATA MEMBERS  ======================================= */
150    protected:
151 
152    private:
153       std::string _nam;
154       cost _r;
155 
156 }; /* -----  end of class State  ----- */
157 
158 /*
159  * =====================================================================================
160  *        Class:  Pstates
161  *  Description:  vector of all possible states
162  * =====================================================================================
163  */
164 template<class cost_type>
165 class Pstates
166 {
167    public:
168 
169       /* ====================  LIFECYCLE     ======================================= */
170       /* constructor      */
171       Pstates (std::vector< STATE<cost_type>  > x)
172          : _vstate{x}
173       {
174             for( auto& y : _vstate)
175                std::cout << std::endl <<"adding state " << y << std::endl;
176       };
177 
178       ~Pstates (){};                            /* destructor       */
179 
180       /* ====================  ACCESSORS     ======================================= */
181       const std::vector< STATE<cost_type> >& vstate() const{
182          return _vstate;
183       }
184       void vstate(std::vector< STATE<cost_type> > in ){
185          _vstate = in;
186       }
187 
188       /* ====================  MUTATORS      ======================================= */
189 
190       /* ====================  OPERATORS     ======================================= */
191 
192       const Pstates& operator = ( const Pstates &other ); /* assignment operator */
193       const Pstates& operator = ( const std::vector< STATE<cost_type > >  &other ){
194          vstate(other);
195          return *this;
196       } ; /* assignment operator */
197       STATE<cost_type>& operator [] ( unsigned int const index ){
198          return (_vstate[index]);
199       } ; /* [] operator */
200       ¦
201       friend std::ostream & operator << ( std::ostream & os, const Pstates & obj )
202          {
203             for(auto state_pair : obj.vstate() )
204                std::cout << std::endl << "state pair" << state_pair;
205             return os;
206          }     /* -----  end of function operator <<  ----- */
207 
208       /* ====================  DATA MEMBERS  ======================================= */
209    protected:
210 
211    private:
212       std::vector< STATE <cost_type> > _vstate;
213 
214 }; /* -----  end of class Pstates  ----- */
215 
216 
217 /* 
218  * =====================================================================================
219  *        Class:  CRAWLER
220  *  Description:  Tree for phylogenetic nodes with overideable algorithms
221  * =====================================================================================
222  */
223 
224 template < class T >
225 class CRAWLER
226 {
227    public:
228 
229       // ====================  LIFECYCLE     =======================================
230       CRAWLER ();                           /* constructor      */
231       /* If you are constructing with a single node check the children and parents and make them part of the tree */
232       CRAWLER (NODE<T> start):pself{&start}, pleft{nullptr}, pright{nullptr}, pparent{nullptr}{
233          if(start.cl() != nullptr)
234             right(start.cl() );
235          if(start.cl() !=nullptr)
236             left(start.cr() );
237          if(start.p() != nullptr)
238             parent( start.p() );
239       };
240       /* if sending pointers, assume the node will take the properties of the arguments for the tree */
241       CRAWLER(NODE<T> * self, NODE<T> *left = nullptr, NODE<T> *right = nullptr, NODE<T> *parent = nullptr ): pself{self}, pleft{left}, pright{right}, pparent{parent} {
242          self->cr(right);
243          self->cl(left);
244          self->p(parent);
245       };
246       CRAWLER ( const CRAWLER &other ); /* copy constructor */
247       ~CRAWLER ();                          /* destructor       */
248 
249       /* ====================  ACCESSORS     ======================================= */
250       NODE<T> * left(){
251          return pleft;
252       }
253       NODE<T> * right(){
254          return pright;
255       }
256       NODE<T> * parent(){
257          return pparent;
258       }
259       NODE<T> * left(NODE<T> *in){
260          return pleft = in;
261       }
262       NODE<T> * right(NODE<T>  *in){
263          return pright = in;
264       }
265       NODE<T> * parent(NODE<T> *in){
266          return pparent = in;
267       }
268 
269       /* ====================  MUTATORS      ======================================= */
270 
271       /* ====================  OPERATORS     ======================================= */
272 
273       const CRAWLER& operator = ( const CRAWLER &other ); // assignment operator
274 
275       /* ====================  DATA MEMBERS  ======================================= */
276    protected:
277       NODE<T> *pleft;
278       NODE<T> *pright;
279       NODE<T> *pparent;
280       NODE<T> *pself;
281 
282    private:
283 
284 
285 }; /* -----  end of template class CRAWLER  ----- */
286 
287 
288 }        /*---End of Namespace TREE---*/
289 #endif