Debian Codegraph @

src/arbre.h

raw
1 /***************************************************************************
2 arbre.h - description
3 -------------------
4 begin : Mon Dec 18 2000
5 copyright : (C) 2000 by Olivier Langella
6 email : [email protected]
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19 //modifications le 2/1/2001
20 //modifications le 4/6/2001
21
22 #ifndef ARBRE_H
23 #define ARBRE_H
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <fstream>
28 #include <vector>
29 #include <string>
30 #include <cmath>
31 #include <algorithm>
32 #include <functional>
33
34 #include "internat.h"
35 #include "matrices.h"
36 //#define _(String) gettext (String)
37 # ifndef _
38 # define _(String) gettext (String)
39 # endif
40
41 namespace biolib {
42 namespace arbres {
43
44
45 using namespace std;
46 using namespace biolib::vecteurs;
47
48 template <class _Tp>
49 struct moins
50 {
51 bool operator()(const _Tp& x, const _Tp& y) const { return *x < *y; }
52 };
53
54 class ArbreVectUINT:public vector<unsigned int> {
55 public:
56 void f_tri();
57
58 bool Existe (unsigned int element) const {
59 //cerr << "ArbreVectUINT::Existe " << endl;
60 //affiche();
61 unsigned int i;
62 for (i= 0 ; i < size(); i++) {
63 //cerr << "ArbreVectUINT::Existe i " << i << endl;
64 if (operator[](i) == element) return(true);
65 }
66
67 return (false);
68 };
69
70 void affiche() const;
71
72 bool operator== (const ArbreVectUINT &rval);
73
74 private:
75 void f_trishell(unsigned int n,unsigned int j);
76 void f_tribulle();
77 };
78
79 class Arbre;
80 class ArbreNdNoeud;
81 class ArbreNdOTU;
82
83 class ArbreNdBase {
84 public:
85 ArbreNdBase(Arbre * Parbre);
86 ArbreNdBase(const ArbreNdBase &) {};
87 virtual ~ArbreNdBase();
88
89 virtual void set_longueur(unsigned int pos, float taille);
90 // virtual void set_longueur(float longueur) {};
91
92 virtual void set_longueur(ArbreNdBase * Pnoeudbase, float taille);
93 virtual void oPhylipRec(ostream & fichier) const;//écriture d'arbres au format Phylip
94 virtual void oXML(ostream & fichier) const {};//écriture d'arbres au format XML
95 virtual ArbreNdBase * get_Pracine() const;
96 virtual ArbreNdBase * get_Pnoeud(unsigned int i) const {return(_tabPNoeuds[i]);};
97 virtual unsigned int get_nbbranches() const;
98 virtual double get_longueur_branche() const;
99 virtual double get_tabLgBranches(unsigned int i) const;
100 virtual bool est_racine() const ;
101 virtual void set_nom(const string & nom);
102 virtual void f_chgt_sens(ArbreNdNoeud * Pancracine, ArbreNdNoeud * Pnvsens);
103 virtual int get_posnoeuds(ArbreNdNoeud * Pnoeud) const;
104
105 void set_Pnoeud(unsigned int pos, ArbreNdBase * Pnoeud);
106
107 virtual void fCalcValBootstrap(unsigned int total_bootstrap) {return;};
108 virtual double get_longueur_max()const {return(0);} ;
109 virtual unsigned int get_nbnoeuds_max()const {return(0);} ;
110
111 virtual ArbreVectUINT* f_load_id_ind() {return (0);};
112
113 //interface pour PointPlotArbreBase
114 protected:
115
116 Arbre * _Parbre;
117 vector<ArbreNdBase *> _tabPNoeuds;
118 vector<float> _tabLgBranches;
119 };
120
121
122 class ArbreNdOTU: public ArbreNdBase {
123 public:
124 ArbreNdOTU(Arbre * Parbre, ArbreNdNoeud * Pnoeud);
125 ArbreNdOTU(Arbre * Parbre, const string & nom);
126 ArbreNdOTU(const ArbreNdOTU & original):ArbreNdBase(original) {};
127 virtual ~ArbreNdOTU();
128
129 void set_nom(const string & nom);
130 // void set_nom(const char * nom){_nom.assign(nom);};
131
132 // void set_longueur(float longueur){ArbreNdBase::set_longueur((unsigned int) 0,longueur);};
133
134 void set_reference(const string & ref) {strcpy(_reference, ref.c_str());};
135 void set_reference(const char * ref){strcpy(_reference, ref);};
136 const char * get_reference() const {return (_reference);};
137
138 void set_id(unsigned int id) { _id = id;};
139 const string & get_nom() const {return(_nom);};
140
141 inline bool operator== (const ArbreNdOTU &rval){return (_nom == rval._nom);};
142 virtual void oPhylipRec(ostream & fichier) const;//écriture d'arbres au format Phylip
143 virtual void oXML(ostream & fichier) const;//écriture d'arbres au format XML
144 virtual double get_longueur_max()const;
145 virtual unsigned int get_nbnoeuds_max()const;
146 unsigned int get_id() const {return(_id);};
147
148 virtual ArbreVectUINT* f_load_id_ind();
149
150 inline bool operator< (const ArbreNdOTU &rval){return (strcmp(_reference, rval._reference) < 0);};
151
152 private:
153
154 // char _nomchar[50];
155 char _reference[50];
156 string _nom;
157
158 unsigned int _id;
159 };
160
161
162 class ArbreNdNoeud: public ArbreNdBase {
163 public:
164 ArbreNdNoeud(Arbre * Parbre, ArbreNdNoeud * Pnoeud=0);
165 ArbreNdNoeud(const ArbreNdNoeud & original):ArbreNdBase(original) {};
166 ArbreNdNoeud(Arbre * Parbre, unsigned int nbbranches);
167 virtual ~ArbreNdNoeud();
168 inline bool operator== (const ArbreNdNoeud &rval);
169
170 void AjouterFils(ArbreNdBase * Pnoeud);
171 void AjBootstrap();
172
173 double get_val_bootstrap() const;
174 void set_force(float force);
175 void set_longueur(unsigned int pos, float taille);
176 void set_racine(bool oui) {_racine = oui;};
177 virtual void oPhylipRec(ostream & fichier) const;//écriture d'arbres au format Phylip
178 virtual void oXML(ostream & fichier) const;
179 virtual ArbreNdBase * get_Pracine() const;
180 virtual unsigned int get_nbbranches() const;
181 virtual bool est_racine() const;
182 virtual bool ExisteBranche(const ArbreVectUINT & tabInd) const;
183
184 void f_chgt_racine(ArbreNdNoeud * Pracine);
185 void f_load_vectind();
186
187 virtual ArbreVectUINT* f_load_id_ind();
188
189 void fCalcValBootstrap(unsigned int total_bootstrap);
190 virtual double get_longueur_max() const;
191 virtual unsigned int get_nbnoeuds_max()const;
192
193 protected:
194 void f_chgt_sens(ArbreNdNoeud * Pancracine, ArbreNdNoeud * Pnvsens);
195
196 bool _racine;
197 int _force;
198 // vector<VectArbreNdOTU *> _tabPPind;
199 vector<ArbreVectUINT *> _tabP_tab_id_ind;
200 unsigned int _accuvalboot;
201 };
202
203 class Arbre {
204 public:
205 Arbre();
206 // Arbre(unsigned int nbessais , unsigned int nbind, unsigned int * Ptemp, char * * PPotu);
207 Arbre(const Arbre &) {};
208
209 virtual ~Arbre();
210
211 virtual ArbreNdOTU * new_ArbreNdOTU(const string & nom);
212 virtual ArbreNdOTU * new_ArbreNdOTU(ArbreNdNoeud * PArbreNdNoeudere);
213 virtual ArbreNdNoeud * new_ArbreNdNoeud(unsigned int nbbranches);
214 virtual ArbreNdNoeud * new_ArbreNdNoeud(ArbreNdNoeud * PArbreNdNoeudere=0);
215
216 void AjBootstrap(Arbre &);
217
218 virtual void iFichier(istream &);
219
220 void iPhylip(istream &);//lecture d'arbres au format Phylip
221 void iNimbus(unsigned int nbessais , unsigned int nbind, unsigned int * Ptemp, char * * PPotu);
222
223 void iDistances(MatriceLD &, int);
224 void iDistances(MatriceLD & matrice);
225 void f_forceiDistances(MatriceLD & distancesRef,int methodeArbre);
226
227 void oFichier(ostream & fichier, unsigned int format=0) const;
228 // void iMega(istream &);//lecture d'arbres au format Mega
229 void set_Pracine(ArbreNdNoeud * Pracine);
230
231 void f_test();
232 void SquizNoeud(ArbreNdNoeud * Pndaeffacer);
233
234 int PositionInd(const char *) const;
235
236 virtual void f_load_vect_id_ind();
237
238 void reset();
239 void fCalcValBootstrap();
240 bool get_oui_bootstrap() const {return(_oui_bootstrap);};
241 bool get_oui_taille_branches() const {return(_oui_taille_branches);};
242 void set_oformat(unsigned int nbformat=1) {_oFormat = nbformat;};
243 unsigned int get_nbind() const {return(_tabPind.size());};
244 void f_tri_ind_alpha();
245
246 void operator>>(ostream& sortie);
247
248 Titre _titre;
249
250 protected:
251 ArbreNdNoeud * RechercheNoeud(const ArbreVectUINT &) const;
252 const ArbreNdOTU * RecherchePOTU(unsigned int id) const;
253
254 void iNeighborJoining(Matrice & distances);
255 void iNeighborJoiningTopo(Matrice & distances);
256 void iUPGMA(Matrice & distances);
257
258 float iPhylipRecGetTaille(istream & fichier);
259 float iPhylipRecGetForce(istream & fichier);
260 void i_PhylipRecGetNom(istream & fichier, char *);
261 void iPhylipRec(istream & fichier, ArbreNdNoeud * Pnoeud=0 );//lecture d'arbres au format Phylip
262 virtual void iPhylipDefGroupe(const string &) {}; //définition d'un groupe
263 void oPhylip(ostream &) const;//écriture d'arbres au format Phylip
264 void oXML(ostream & fichier) const;
265 virtual void oPhylipEcritGroupes(ostream &) const {};
266
267 void iNimbusRec(ArbreNdNoeud * Pnoeud, MatriceLD & tab_nimbus, unsigned int numessai, unsigned int deb, unsigned int fin, unsigned int longueur=0);
268 // void iPhylipRecPasser(istream & fichier);
269 void f_trishell_ind_alpha(unsigned int lb, unsigned int ub);
270 void f_tribulle_ind_alpha();// void f_tri_ind_alpha();
271
272 ArbreNdNoeud * _Pracine;
273 vector<ArbreNdNoeud *> _tabPnoeuds;
274 vector<ArbreNdOTU *> _tabPind;
275
276 bool _oui_taille_branches;
277 bool _oui_bootstrap;
278 bool _ind_charge;
279 unsigned int _cumulbootstrap;
280 unsigned int _oFormat; //format de sortie (1 = phylip/newick)
281
282 public:
283 struct Anomalie{
284 Anomalie (int i):le_pb(i){};
285
286 // 1-> Echec de RechercheNoeud
287 int le_pb;
288 string _message;
289
290 string& fmessage(int num){
291 switch (num) {
292 case 1:
293 _message = _("Error n°1 in Arbre: leaf not found");
294 break;
295 case 100:
296 _message = _("Error reading treefile");
297 break;
298
299 default:
300 _message = _("Error in Arbre");
301 break;
302 }
303 return(_message);
304 }
305 };
306
307 };
308
309
310 inline bool ArbreNdNoeud::operator== (const ArbreNdNoeud &rval) {
311 //=>remplir les vecteurs avant
312 //cerr << "ArbreNdNoeud::operator==" << endl;
313 unsigned int taille(_tabP_tab_id_ind.size());
314 if (taille != rval._tabP_tab_id_ind.size()) return (false);
315 if (taille == 0) cerr << "ArbreNdNoeud::operator== vide" << endl;
316 if (taille == 1) return (*_tabP_tab_id_ind[0] == *rval._tabP_tab_id_ind[0]);
317
318 unsigned int i,j;
319
320 bool ok;
321
322 // bool * tab_dejatrouve;
323 // tab_dejatrouve = new bool[taille];
324 // for (i = 0; i < taille; i++) tab_dejatrouve[i] = false;
325
326 for (i=0; i < (taille - 1); i++) {
327 ok = false;
328 for (j=0; ((ok == false) && (j < taille)); j++)
329 // if (!tab_dejatrouve[j])
330 ok = (*_tabP_tab_id_ind[i] == *rval._tabP_tab_id_ind[j]);
331 // if (ok = (*_tabPPind[i] == *rval._tabPPind[j])) tab_dejatrouve[j] = true;
332 //if (ok) cerr << "ArbreNdNoeud::operator== ok " << this << endl;
333
334 if (!ok) return (false);
335 }
336 // delete tab_dejatrouve;
337
338 return(true);
339 }
340
341 }//namespace arbres
342 }//namespace biolib
343 #endif
344
345