Version 4.1.5
Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

pmm_tree.h

00001 /* seqpp/pmm_tree.h
00002  *
00003  * Copyright (C) 2003 Laboratoire Statistique & Génome
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or (at
00008  * your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  */
00019 
00020 
00029 #ifndef SEQPP_PMM_TREE_H
00030 #define SEQPP_PMM_TREE_H
00031 
00032 #include <seqpp/pmm_node.h>
00033 #include <seqpp/pmm_leaf.h>
00034 #include <seqpp/SequenceSet.h>
00035 #include <seqpp/Partition.h>
00036 #include <string>
00037 #include <gsl/gsl_sf.h>
00038 
00039 #ifdef HAVE_LIBXML2
00040 #include <libxml/tree.h>
00041 #endif
00042 using namespace std ;
00043 
00044 
00050 class pmm_tree_rep{
00051  public:
00052 
00056   pmm_tree_rep( ){
00057     _countref=1;_alphabet_size=0;_syn_size=0;
00058     _root=NULL;_depth=0;_nbleaves=0;_postloglike=PRECISION;
00059   }
00060 
00068   pmm_tree_rep( Partition& part, short alphabet_size, int depth );
00069 
00070   pmm_tree_rep(const pmm_tree_rep & rep){
00071     cout<<"bad constr copy call"<<endl;
00072   }
00073 
00075   virtual ~pmm_tree_rep();   
00076 
00080   void tree_to_matrix( double* mat );
00081    
00082 
00083 #ifdef HAVE_LIBXML2  
00084   void save( const Translator & trans, 
00085              xmlNodePtr parent_node, const string &treename );
00086 #endif
00087   
00089   short tell_alphabet_size () const{
00090     return _alphabet_size;
00091   }
00093   const Partition& get_partition() const{
00094     return _part;
00095   }
00096 
00098   int nb_leaves() const{return _nbleaves;}
00100   void add_leaf(){_nbleaves++;}
00102   void cut_leaf(){_nbleaves--;}
00104   double complete(node_base& node) ;      
00106   void prune(pmm_node& n); 
00108   pmm_node & getRoot() const{return *_root;}
00110   pmm_leaf & getLeaf(vector<short> & rpath) const;  
00112   pmm_leaf & getLeaf(unsigned long code) const;
00114   short tellDepth() const{
00115     return _depth;
00116   }
00117 
00119   short countref() const{
00120     return _countref;
00121   }
00123   pmm_tree_rep& operator ++ (int) {
00124     _countref++;
00125     return *this;
00126   } 
00127   pmm_tree_rep& operator -- (int) {
00128     _countref--;
00129     return *this;
00130   }
00131 
00132 
00134   void set_postloglike( double ptl ){_postloglike = ptl;}
00136   void add_postloglike( double ptl ){_postloglike += ptl;}
00138   double tell_postloglike(){return _postloglike;}
00139 
00141   double tell_tree_prior(){return _curr_tree_prior;}
00143   void set_tree_prior( double x ){_curr_tree_prior = x;}
00145   void add_tree_prior( double x ){_curr_tree_prior += x;}
00146   
00147  protected:
00149   short _countref;
00150       
00152   Partition _part;  
00153   short _alphabet_size;
00154   short _syn_size;
00155   pmm_node * _root ;
00157   int _depth;
00159   int _nbleaves;
00160 
00162   double _postloglike;
00163   double _curr_tree_prior;
00164 };
00165 
00171 class pmm_tree{
00172   pmm_tree_rep * _rep;
00173  public:
00174   pmm_tree(){
00175     _rep =  new pmm_tree_rep();
00176   }
00177   pmm_tree(Partition& part, short alphabet_size, int depth){
00178     _rep = new pmm_tree_rep(part, alphabet_size, depth);
00179   }
00180   pmm_tree(const pmm_tree& p_t){
00181     _rep = p_t._rep;
00182     (*_rep)++;
00183   }
00184   pmm_tree(pmm_tree_rep& rep){
00185     _rep = &rep;
00186     (*_rep)++;
00187   }
00188   ~pmm_tree(){
00189     if (_rep->countref()>1)
00190       (*_rep)--;
00191     else
00192       delete _rep;
00193   }
00194   pmm_tree& operator=(pmm_tree & p_t){
00195     if (this != &p_t){
00196       // delete
00197       if (_rep->countref()>1)
00198         (*_rep)--;
00199       else
00200         delete _rep;
00201       // new
00202       _rep = p_t._rep;
00203       (*_rep)++;
00204     }
00205     return (*this);
00206   } 
00207   short tellDepth() const{
00208     return _rep->tellDepth();
00209   }
00210 
00214   void tree_to_matrix( double* mat ){
00215     _rep->tree_to_matrix( mat );
00216   }
00217 #ifdef HAVE_LIBXML2  
00218   void save( const Translator & trans, 
00219              xmlNodePtr parent_node, const string &treename ){
00220     _rep->save( trans, parent_node, treename );
00221   }
00222 #endif
00223 
00224   int nb_leaves() const{return _rep->nb_leaves();}
00225 
00227   pmm_node & getRoot() const{return _rep->getRoot();}
00228 
00230   void set_postloglike( double ptl ){_rep->set_postloglike(ptl);}
00232   void add_postloglike( double ptl ){_rep->add_postloglike(ptl);}
00234   double tell_postloglike(){return _rep->tell_postloglike();}
00236   double tell_tree_prior(){return _rep->tell_tree_prior();}
00238   void set_tree_prior( double x ){_rep->set_tree_prior(x);}
00239 
00240   
00241   class const_iterator{
00242   private :
00243     const pmm_tree_rep * _rep;
00244     node_base * _curr_node;
00245     // current list of non visited sons
00246     vector<vector<node_base *> *> _curr_non_visited_sons_path;
00247     // index in the list of the current non visited son
00248     vector<int> _curr_index_in_path;    
00249 
00250   protected :
00251     friend class pmm_tree;
00252     const_iterator( const pmm_tree& p_t, node_base* curr_node ){
00253       _rep=p_t._rep; _curr_node=curr_node;
00254       
00255       //_curr_non_visited_sons_path.assign(1+_rep->tellDepth(),NULL);
00256       //_curr_index_in_path.assign(_rep->tellDepth(),-1);
00257 
00258       short d = _rep->tellDepth();
00259       for (short i=0; i<d+1; i++){
00260         _curr_non_visited_sons_path.push_back(NULL);
00261         _curr_index_in_path.push_back(-1);
00262       }
00263     }
00264     const_iterator( const pmm_tree_rep& rep, node_base* curr_node ){
00265       _rep=&rep; _curr_node=curr_node;
00266       short d = _rep->tellDepth();
00267       for (short i=0; i<d+1; i++){
00268         _curr_non_visited_sons_path.push_back(NULL);
00269         _curr_index_in_path.push_back(-1);
00270       }
00271     }
00272   public:  
00273     const_iterator( ){
00274       _rep=NULL;
00275       _curr_node=NULL;
00276     }
00277     const_iterator( const const_iterator & it ){
00278       _rep=it._rep;_curr_node=it._curr_node;
00279       _curr_non_visited_sons_path=it._curr_non_visited_sons_path;
00280       _curr_index_in_path=it._curr_index_in_path;      
00281     }
00282 
00283     bool operator != ( const const_iterator & it ) const{
00284       return ((_rep!=it._rep)||(_curr_node!=it._curr_node));
00285     }
00286     bool operator == ( const const_iterator & it ) const{
00287       return ((_rep==it._rep)&&(_curr_node==it._curr_node));
00288     }
00289     const_iterator& operator = (  const const_iterator & it ){
00290       if (  it != *this ){
00291         _rep=it._rep;
00292         _curr_node=it._curr_node;
00293         _curr_non_visited_sons_path=it._curr_non_visited_sons_path;
00294         _curr_index_in_path=it._curr_index_in_path;
00295       }
00296       return *this;
00297     }
00298     bool operator++(int);      
00299     
00300     pmm_leaf & operator * () const{
00301       return  *(dynamic_cast<pmm_leaf *>(_curr_node));
00302     }
00303   };
00304   
00305   const_iterator begin() const{
00306     return const_iterator( *(this->_rep), &(this->getRoot()) );
00307   }
00308   
00309   const_iterator end() const{
00310     return const_iterator( *(this->_rep), NULL );
00311   } 
00312 };
00313 #endif



Download seq++ 4.1.5
Download previous versions
Statistique & Genome Home


Generated on Thu Aug 4 20:33:12 2005 for seqpp by doxygen 1.3.9.1