Debian Codegraph @

src/arbre.cpp

raw
1 /***************************************************************************
2 arbre.cpp - 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 #include "arbre.h"
20
21 namespace biolib {
22 namespace arbres {
23
24 Arbre::~Arbre() {
25 //destruction des noeuds et individus
26 unsigned int i;
27
28 for (i=0; i < _tabPind.size(); i++) delete _tabPind[i];
29 for (i=0; i < _tabPnoeuds.size(); i++) delete _tabPnoeuds[i];
30
31 }
32
33 Arbre::Arbre() {
34 reset();
35 // _oui_taille_branches = false;
36 // _oui_bootstrap = false;
37 // _Pracine = 0;
38 }
39
40 ArbreNdBase::ArbreNdBase(Arbre * Parbre) {
41
42 _Parbre = Parbre;
43 }
44
45 ArbreNdBase::~ArbreNdBase() {
46 }
47
48 void ArbreNdBase::set_Pnoeud(unsigned int pos, ArbreNdBase * Pnoeud) {
49 _tabPNoeuds[pos] = Pnoeud;
50 }
51
52 unsigned int ArbreNdBase::get_nbbranches() const {
53 return(_tabPNoeuds.size());
54 }
55 bool ArbreNdBase::est_racine() const {
56 return(false);
57 }
58
59 void ArbreNdBase::set_nom(const string & nom) {
60 }
61
62 ArbreNdOTU * Arbre::new_ArbreNdOTU(const string & nom) {
63 unsigned int i, taille, j;
64 ArbreNdOTU * Pindswap;
65
66 _tabPind.push_back(new ArbreNdOTU(this, nom));
67
68
69 Pindswap = _tabPind.back();
70
71 taille = _tabPind.size();
72 if ((taille == 1) || (nom == "")) {Pindswap->set_id(taille-1);return(Pindswap);}
73 // _tabPind.back()->set_id(taille-1);
74 //classement de l'individu
75 // on avance jusqu'à son emplacement:
76 i = 0;
77 while ((i < (taille-1)) && (strcmp(_tabPind[i]->get_reference(), Pindswap->get_reference()) < 0)) i++;
78
79 _tabPind.back()->set_id(i);
80
81 if ((taille-1) == i) return (Pindswap);
82
83
84 for ( j = (taille-1); j > i; j--) {
85 _tabPind[j] = _tabPind[j-1];
86 _tabPind[j]->set_id(j);
87 }
88 _tabPind[i] = Pindswap;
89
90 return(Pindswap);
91 }
92
93 ArbreNdOTU * Arbre::new_ArbreNdOTU(ArbreNdNoeud * PArbreNdNoeudPere) {
94 _tabPind.push_back(new ArbreNdOTU(this, PArbreNdNoeudPere));
95 _tabPind.back()->set_id(_tabPind.size()-1);
96 return(_tabPind.back());
97 }
98
99 ArbreNdNoeud * Arbre::new_ArbreNdNoeud(unsigned int nbbranches){
100 //cerr << "Arbre::new_ArbreNdNoeud(unsigned int nbbranches)" << endl;
101 _tabPnoeuds.push_back(new ArbreNdNoeud(this, nbbranches));
102 //cerr << "Arbre::new_ArbreNdNoeud(unsigned int nbbranches) fin" << endl;
103 return(_tabPnoeuds.back());
104 }
105
106 ArbreNdNoeud * Arbre::new_ArbreNdNoeud(ArbreNdNoeud * PArbreNdNoeudere) {
107 _tabPnoeuds.push_back(new ArbreNdNoeud(this, PArbreNdNoeudere));
108 return(_tabPnoeuds.back());
109 }
110
111 void ArbreNdBase::f_chgt_sens(ArbreNdNoeud * Pracine, ArbreNdNoeud * Psens) {
112 //cerr << "ArbreNdBase::f_chgt_sens" << endl;
113
114 }
115
116 void ArbreNdNoeud::f_chgt_sens(ArbreNdNoeud * Pracine, ArbreNdNoeud * Psens) {
117 //Psens devient le pointeur vers la racine de l'arbre
118 // => a la place de tabPNoeuds[0]
119 unsigned int i;
120 unsigned int pos(0);
121 unsigned int taille (_tabPNoeuds.size());
122 ArbreNdBase * Pswap;
123 float lgbranche;
124
125 //cerr << "ArbreNdNoeud::f_chgt_sens debut" << endl;
126
127 Pswap = _tabPNoeuds[0];
128
129 for (i=0; i < taille; i++) if (_tabPNoeuds[i] == Psens) pos = i;
130
131 if (pos != 0) {
132
133 _tabPNoeuds[0] = _tabPNoeuds[pos];
134 _tabPNoeuds[pos] = Pswap;
135
136 lgbranche = _tabLgBranches[0];
137 set_longueur(0, _tabLgBranches[pos]);
138
139 // _tabLgBranches[0] = _tabLgBranches[pos];
140 set_longueur(pos, lgbranche);
141 // _tabLgBranches[pos] = lgbranche;
142 }
143
144 if ( Pracine == this) {
145 return;
146 // _tabLgBranches[pos] = _tabPNoeuds[pos]->get_tabLgBranches(0);
147 }
148 else {
149 Pswap->f_chgt_sens(Pracine, this);
150 }
151 //cerr << "ArbreNdNoeud::f_chgt_sens fin" << endl;
152
153 }
154
155 void ArbreNdNoeud::f_chgt_racine(ArbreNdNoeud * Pancracine) {
156 //cerr << "ArbreNdNoeud::f_chgt_racine debut" << endl;
157 if (_racine) {
158 set_longueur(0, _tabLgBranches[0]);
159 _tabPNoeuds[0]->f_chgt_sens(Pancracine, this);
160 // if (_tabLgBranches[0] != -999) _tabLgBranches[0] = -999;
161
162 //cerr << "ArbreNdNoeud::f_chgt_racine " << Pancracine->get_nbbranches() << endl;
163 if (Pancracine->get_nbbranches() == 1) { //le noeud est inutile
164 // il faut le squizer
165 _Parbre->SquizNoeud(Pancracine);
166 }
167 }
168 else {
169 if (Pancracine == this) {
170 }
171 }
172 //cerr << "ArbreNdNoeud::f_chgt_racine fin" << endl;
173 }
174
175 void ArbreNdBase::oPhylipRec(ostream & fichier) const {}//écriture d'arbres au format Phylip
176
177 ArbreNdBase * ArbreNdBase::get_Pracine() const {
178 return (_tabPNoeuds[0]);
179 }
180
181 double ArbreNdBase::get_tabLgBranches(unsigned int i) const{
182 return (_tabLgBranches[i]);
183 }
184
185 double ArbreNdBase::get_longueur_branche() const{
186 //longueur de la branche racine
187 if (est_racine()) return(0);
188 else return (_tabLgBranches[0]);
189 }
190
191 double ArbreNdNoeud::get_longueur_max() const {
192 //donne la longueur de la plus grande branche
193 double taille(-1), temp;
194 unsigned int i;
195
196 // if (_oui_taille_branche) {
197 if (est_racine()) {
198 temp = _tabPNoeuds[0]->get_longueur_max();
199 if (taille < temp) taille = temp;
200 }
201 for (i=1; i < _tabPNoeuds.size();i++) {
202 temp = _tabPNoeuds[i]->get_longueur_max();
203 if (taille < temp) taille = temp;
204 }
205 temp = get_longueur_branche();
206 if (temp < 0) return (taille);
207 else return (temp + taille);
208 // }
209 return(0);
210 }
211
212 unsigned int ArbreNdNoeud::get_nbnoeuds_max() const {
213 //donne le nombre de noeuds de la plus grande branche
214 unsigned int i, temp, nbnoeuds(0);
215
216 // if (_oui_taille_branche) {
217 if (est_racine()) {
218 temp = _tabPNoeuds[0]->get_nbnoeuds_max();
219 if (nbnoeuds < temp) nbnoeuds = temp;
220 }
221 for (i=1; i < _tabPNoeuds.size();i++) {
222 temp = _tabPNoeuds[i]->get_nbnoeuds_max();
223 if (nbnoeuds < temp) nbnoeuds = temp;
224 }
225 return (1 + nbnoeuds);
226 // }
227 }
228
229 double ArbreNdNoeud::get_val_bootstrap() const{
230 return (_force);
231 }
232
233 ArbreNdBase * ArbreNdNoeud::get_Pracine() const {
234 if (_racine) return(0);
235 else return (_tabPNoeuds[0]);
236 }
237
238 void ArbreNdNoeud::AjBootstrap() {
239 _accuvalboot++;
240 }
241
242 ArbreVectUINT* ArbreNdOTU::f_load_id_ind() {
243 ArbreVectUINT * Ptab_id_ind;
244
245 //cerr << "ArbreNdOTU::f_load_id_ind debut" <<endl;
246
247 Ptab_id_ind = new ArbreVectUINT;
248 Ptab_id_ind->push_back(_id);
249
250
251 //cerr << "ArbreNdOTU::f_load_id_ind fin" <<endl;
252 return (Ptab_id_ind);
253 }
254
255 ArbreVectUINT* ArbreNdNoeud::f_load_id_ind() {
256 //chargement du vecteur d'id d'individus
257 //à partir de la racine, déclenche une fonction récursive de remplissage
258 unsigned int i,j;
259 ArbreVectUINT * Ptab_id_ind;
260
261 //cerr << "ArbreNdNoeud::f_load_id_ind debut" <<endl;
262 for (i=0; i < _tabP_tab_id_ind.size(); i++) delete _tabP_tab_id_ind[i];
263 _tabP_tab_id_ind.resize(0);
264
265 if (_racine) {
266 _tabP_tab_id_ind.push_back(_tabPNoeuds[0]->f_load_id_ind());
267 }
268 else _tabP_tab_id_ind.push_back(new ArbreVectUINT);
269
270 for (i = 1; i < _tabPNoeuds.size(); i++) {
271 _tabP_tab_id_ind.push_back(_tabPNoeuds[i]->f_load_id_ind());
272 }
273 if (_racine) return (0);
274
275 Ptab_id_ind = new ArbreVectUINT;
276 //cerr << "ArbreNdNoeud::f_load_id_ind tri " << _tabP_tab_id_ind[0] << " " << _tabP_tab_id_ind[0]->size() << endl;
277 for (i = 1; i < _tabPNoeuds.size(); i++) {
278 //cerr << "ArbreNdNoeud::f_load_id_ind tri " << _tabP_tab_id_ind[i] << " " << _tabP_tab_id_ind[i]->size() << endl;
279 for (j = 0; j < _tabP_tab_id_ind[i]->size(); j++) {
280 Ptab_id_ind->push_back(_tabP_tab_id_ind[i]->operator[](j));
281 }
282 }
283 // => tri de Ptab_id_ind
284
285 //cerr << "ArbreNdNoeud::f_load_id_ind tri" <<endl;
286 Ptab_id_ind->f_tri();
287 //cerr << "ArbreNdNoeud::f_load_id_ind tri fin tri" <<endl;
288
289 //remplissage de _tabP_tab_id_ind[0]
290 //avec tout ce qui n'est pas dans Ptab_id_ind
291 unsigned int nbind(_Parbre->get_nbind());
292 //cerr << "ArbreNdNoeud::f_load_id_ind fin Ptab_id_ind " <<endl;
293 //Ptab_id_ind->affiche();
294 for (i=0; i < nbind; i++) if (!Ptab_id_ind->Existe(i)) _tabP_tab_id_ind[0]->push_back(i);
295 //cerr << "ArbreNdNoeud::f_load_id_ind fin" <<endl;
296 //cerr << "ArbreNdNoeud::f_load_id_ind ";
297 //Ptab_id_ind->affiche();
298 //cerr <<endl;
299 return (Ptab_id_ind);
300 }
301
302
303 void ArbreNdNoeud::set_force(float force) {
304 _force = (int) force;
305 }
306
307 bool ArbreNdNoeud::est_racine() const {
308 return(_racine);
309 }
310
311 ArbreNdNoeud::ArbreNdNoeud(Arbre * Parbre, ArbreNdNoeud * Pnoeud):ArbreNdBase(Parbre) {
312 _force = 0;
313 _accuvalboot = 0;
314 if (Pnoeud == 0) {
315 _racine = true;
316 Parbre->set_Pracine(this);
317 }
318 else {
319 _racine = false;
320 _tabPNoeuds.resize(1);
321 _tabLgBranches.resize(1);
322 _tabPNoeuds[0] = Pnoeud;
323 _tabLgBranches[0] = 0;
324 Pnoeud->AjouterFils(this);
325 }
326 }
327
328 ArbreNdNoeud::ArbreNdNoeud(Arbre * Parbre, unsigned int nbbranches):ArbreNdBase(Parbre) {
329 // création d'un noeud neutre (dont on ne connait pas encore le noeud père)
330 // avec 'nbbranches' branches
331 unsigned int i;
332
333 _force = 0;
334 _accuvalboot = 0;
335 _racine = false;
336 _tabPNoeuds.resize(nbbranches);
337 _tabLgBranches.resize(nbbranches);
338 for (i=0; i < _tabP_tab_id_ind.size(); i++) delete _tabP_tab_id_ind[i];
339 // _tabPNoeuds[0] = 0;
340 // _tabLgBranches[0] = 0;
341 // Pnoeud->AjouterFils(this);
342 }
343
344 ArbreNdNoeud::~ArbreNdNoeud() {};
345
346 unsigned int ArbreNdNoeud::get_nbbranches() const {
347 if (_racine) return(_tabPNoeuds.size());
348 else return(_tabPNoeuds.size()-1);
349 }
350
351
352 ArbreNdOTU::ArbreNdOTU(Arbre * Parbre, ArbreNdNoeud * Pnoeud):ArbreNdBase(Parbre) {
353 _tabPNoeuds.resize(1);
354 _tabLgBranches.resize(1);
355 _tabPNoeuds[0] = Pnoeud;
356 _tabLgBranches[0] = 0;
357 Pnoeud->AjouterFils(this);
358
359 // _Parbre->AjouterIndividu(this);
360 }
361
362 ArbreNdOTU::ArbreNdOTU(Arbre * Parbre, const string & nom):ArbreNdBase(Parbre) {
363 //création d'un individu orphelin de noeud
364 _tabPNoeuds.resize(1);
365 _tabLgBranches.resize(1);
366 _tabPNoeuds[0] = 0;
367 _tabLgBranches[0] = 0;
368 set_nom(nom);
369 set_reference(nom);
370 // Pnoeud->AjouterFils(this);
371 // _Parbre->AjouterIndividu(this);
372 }
373
374 ArbreNdOTU::~ArbreNdOTU() {
375 }
376
377 /*
378 bool ArbreNdOTU::operator== (const ArbreNdOTU &rval){
379 //cerr << "ArbreNdOTU::operator==" << endl;
380 return (_nom == rval._nom);
381 }
382 */
383 void ArbreNdNoeud::AjouterFils(ArbreNdBase * Pnoeud) {
384 _tabLgBranches.push_back(Pnoeud->get_tabLgBranches(0));
385 _tabPNoeuds.push_back(Pnoeud);
386 }
387
388 void ArbreNdBase::set_longueur(unsigned int pos, float taille) {
389 _tabLgBranches[pos] = taille;
390
391 //cerr << "ArbreNdBase::set_longueur" << endl;
392 if (_tabPNoeuds[pos] != 0) _tabPNoeuds[pos]->set_longueur(this,taille);
393 //cerr << "ArbreNdBase::set_longueur fin" << endl;
394
395 }
396
397 void ArbreNdNoeud::set_longueur(unsigned int pos, float taille) {
398 _tabLgBranches[pos] = taille;
399
400 //cerr << "ArbreNdNoeud::set_longueur debut" << endl1
401 if (_tabPNoeuds[pos] != 0) {
402 if ((_racine == true) && (pos == 0)) _tabPNoeuds[0]->set_longueur(this,taille);
403 else if (pos > 0) _tabPNoeuds[pos]->set_longueur(this,taille);
404 }
405 //cerr << "ArbreNdNoeud::set_longueur fin" << endl;
406
407 }
408
409 void ArbreNdNoeud::oPhylipRec(ostream & fichier) const {
410 //écriture d'arbres au format Phylip
411 unsigned int i;
412 // char car;
413 //cerr << "ArbreNdNoeud::oPhylipRec debut" << endl;
414
415 fichier << "(";
416 if (_racine) {
417 _tabPNoeuds[0]->oPhylipRec(fichier);
418 fichier << ",";
419 }
420 for (i=1; i < _tabPNoeuds.size(); i++) {
421 if (i>1) fichier << ",";
422 _tabPNoeuds[i]->oPhylipRec(fichier);
423 }
424 fichier << ")";
425 if (_Parbre->get_oui_bootstrap() && _force != -999) fichier << _force;
426 if ((_racine == false) && (_Parbre->get_oui_taille_branches())) {
427 fichier << ":";
428 fichier << _tabLgBranches[0];
429 // if ((_tabLgBranches[0] < 1e-6) || (_tabLgBranches[0] < 0)) fichier << 0;
430 // else fichier << _tabLgBranches[0];
431 }
432 }
433
434 void ArbreNdOTU::oPhylipRec(ostream & fichier) const {
435 //écriture d'arbres au format Phylip
436 fichier << _nom;
437 if ( _tabLgBranches[0]!=-999) fichier << ":" << _tabLgBranches[0];
438 }
439
440 void ArbreNdBase::set_longueur(ArbreNdBase * Pnoeudbase, float taille) {
441 unsigned int i;
442
443 for (i=0; i < _tabPNoeuds.size();i++) {
444 if (Pnoeudbase == _tabPNoeuds[i]) _tabLgBranches[i] = taille;
445 }
446 }
447
448 int ArbreNdBase::get_posnoeuds(ArbreNdNoeud * Pnoeud) const {
449 unsigned int i;
450
451 for (i=0; i < _tabPNoeuds.size();i++) {
452 if (Pnoeud == _tabPNoeuds[i]) return ((int) i);
453 }
454 return (-1);
455 }
456
457
458 int Arbre::PositionInd(const char * nom_ind) const {
459 unsigned int taille(_tabPind.size()),i;
460
461 for (i = 0; i < taille; i++) {
462 //cerr << "Arbre::PositionInd " << endl;
463 //cerr << _tabPind[i]->get_nom_c_str() << "[" << endl;
464 //cerr << nom_ind << "[" << endl ;
465 if ( strcmp(_tabPind[i]->get_reference(),nom_ind) == 0) return ((int)i);
466 }
467
468 string nom(nom_ind);
469 for (i = 0; i < taille; i++) {
470 //cerr << "Arbre::PositionInd " << endl;
471 //cerr << _tabPind[i]->get_nom_c_str() << "[" << endl;
472 //cerr << nom_ind << "[" << endl ;
473 if (_tabPind[i]->get_nom() == nom) return ((int)i);
474 }
475
476 return (-1);
477 }
478
479 void Arbre::AjBootstrap(Arbre & rval) {
480
481 // if (!_ind_charge) f_load_vectind();
482 if (!_ind_charge) f_load_vect_id_ind();
483 if (!rval._ind_charge) rval.f_load_vect_id_ind();
484 //cerr << "Arbre::AjBootstrap" << endl;
485 unsigned int i,j, taille(_tabPnoeuds.size());
486
487 bool * tab_dejatrouve;
488 tab_dejatrouve = new bool[taille];
489 for (i = 0; i < taille; i++) tab_dejatrouve[i] = false;
490
491 //cerr << "Arbre::AjBootstrap for debut" << endl;
492 for (i = 0; i < taille; i++) {
493 //cerr << "Arbre::AjBootstrap for " << i << endl;
494 for (j = 0; j < taille; j++) {
495 if (!tab_dejatrouve[j]){
496 if (*(_tabPnoeuds[i]) == *(rval._tabPnoeuds[j])) {
497 _tabPnoeuds[i]->AjBootstrap();
498 tab_dejatrouve[j] = true;
499 break;
500 }
501 }
502 }
503 }
504 //cerr << "Arbre::AjBootstrap fin" << endl;
505 _cumulbootstrap++;
506 }
507
508 void Arbre::set_Pracine(ArbreNdNoeud * Pracine) {
509 if (Pracine == 0) return;
510
511 if (_Pracine == 0) {
512 _Pracine = Pracine;
513 return;
514 }
515 if (Pracine == _Pracine) return;
516 // si il ya déjà un pointeur Pracine =>
517 // changement de racine de l'arbre
518 ArbreNdNoeud * Pancien(_Pracine);
519 _Pracine = Pracine;
520
521 Pancien->set_racine(false);
522 _Pracine->set_racine(true);
523 //cerr << "Arbre::set_Pracine" << endl;
524 // réarrangement de l'arbre par propagation dans les noeuds
525
526 _Pracine->f_chgt_racine(Pancien);
527 }
528
529 void Arbre::iFichier(istream & fichier) {
530 _oui_taille_branches = false;
531 iPhylip(fichier);
532 f_tri_ind_alpha();
533 }
534
535 void Arbre::iPhylip(istream & fichier) {
536 //lecture d'arbres au format Phylip
537
538 //cerr << "Arbre::iPhylip debut" << endl;
539 char car;
540 unsigned int i;
541
542 car = fichier.peek();
543 if (car == '[') {
544 ChaineCar ligne;
545 fichier.get(car);
546 fichier.get(car);
547
548 while (fichier.good() && (car != ']')) {
549 ligne += car;
550 fichier.get(car);
551 }
552 if (fichier.fail()) throw Anomalie(100);
553 else {
554 unsigned int nblignes(ligne.GetNbMots("\\"));
555 for (i=0; i < nblignes; i++) {
556 _titre.push_back("");
557 ligne.GetMot(i+1,_titre.back(),"\\");
558 }
559 }
560 GetLigneFlot(fichier, ligne);
561 }
562
563 fichier.get(car);
564 if (car != '(') throw Anomalie(100);
565 iPhylipRec(fichier,0);
566
567 f_tri_ind_alpha();
568 for (i=0; i < _titre.size(); i++) {
569 if (_titre[i].Position("#G") == 0) {
570 //définition d'un groupe
571 iPhylipDefGroupe(_titre[i]);
572 _titre.Suppr(i);
573 i--;
574 }
575 }
576
577 //cerr << "Arbre::iPhylip fin " << _oui_taille_branches << endl;
578
579 // if (_tabPnoeuds.size() > 0) set_Pracine(_tabPnoeuds[0]);
580 }
581
582 void Arbre::oFichier(ostream & fichier, unsigned int format) const {
583 if ((format == 0) || (format > 2)) format = _oFormat;
584
585 switch (format) {
586 case 1:
587 oPhylip(fichier);
588 break;
589 case 2:
590 oXML(fichier);
591 break;
592
593 default:
594 oPhylip(fichier);
595 break;
596 }
597
598 }
599
600 void Arbre::oPhylip(ostream & fichier) const {
601 //lecture d'arbres au format Phylip
602 unsigned int i;
603
604 if (_titre.size() > 0) {
605 fichier << "[";
606 for (i=0; i < _titre.size() ; i++) {
607 fichier << _titre[i];
608 if (i != (_titre.size()-1)) fichier << "\\";
609 }
610 oPhylipEcritGroupes(fichier);
611 fichier << "]" << endl;
612 }
613 else {
614 oPhylipEcritGroupes(fichier);
615 }
616 _Pracine->oPhylipRec(fichier);
617
618 fichier << ";" << endl;
619 }
620
621 void Arbre::iPhylipRec(istream & fichier, ArbreNdNoeud * Pnoeudracine) {
622 char nomcar[50];
623 // string individu;
624 float force, taille;
625 char car;
626 ArbreNdNoeud * Pnoeud(new_ArbreNdNoeud(Pnoeudracine));
627
628
629 // fichier.clear();
630
631 // _tabPnoeuds.push_back(new ArbreNdNoeud(this, Pnoeudracine));
632 // ArbreNdNoeud * Pnoeud(_tabPnoeuds.back());
633
634 fichier.get(car);
635 while (fichier.good()) {
636 //cerr << "Arbre::iPhylipRec deb " << car << endl;
637 switch (car) {
638 case ';': //fin de l'arbre
639 return;
640 break;
641
642 case ')': //fin du noeud
643 // force du noeud
644 // taille de la branche mere
645 // iPhylipRecPasser(fichier);
646 force = iPhylipRecGetForce(fichier);
647 taille = iPhylipRecGetTaille(fichier);
648 Pnoeud->set_force(force);
649 if (Pnoeud->est_racine() == false) Pnoeud->set_longueur(0, taille);
650 // if ((taille == -999) && (Pnoeud.est_racine())) _taille_branches = false;
651 if (force > 0) _oui_bootstrap = true;
652 return;
653 break;
654
655 case '(': //nouveau noeud
656 iPhylipRec(fichier, Pnoeud);
657 break;
658 case ',': //nouvelle branche
659 break;
660 case ' ': //rien
661 break;
662 case 13: //retour chariot CR
663 break;
664 case 9: //tabulation
665 break;
666 case 10: //LF Line Feed
667 break;
668 default: //branche terminale
669 // _tabPind.push_back(new ArbreNdOTU(this, Pnoeud));
670 //new ArbreNdOTU(this, Pnoeud);
671 new_ArbreNdOTU(Pnoeud);
672 nomcar[0] = car;
673 //cerr << "//branche terminale deb" << endl;
674 i_PhylipRecGetNom(fichier, nomcar);
675 _tabPind.back()->set_nom(nomcar);
676 _tabPind.back()->set_reference(nomcar);
677 // iPhylipRecPasser(fichier);
678 taille = iPhylipRecGetTaille(fichier);
679 _tabPind.back()->set_longueur((unsigned int) 0,taille);
680 break;
681 }
682 fichier.get(car);
683 }
684 // traitement du noeud terminé
685 }
686
687 /*void Arbre::iPhylipRecPasser(istream & fichier) {
688 char car;
689
690 car = fichier.peek();
691 // car = fichier[pos]; pos++;
692 while ((((car > 47 ) && (car < 58)) || (car == '.') || (car == ':')) && fichier.good()) {
693 fichier.get(car);
694 car = fichier.peek();
695 }
696 }
697 */
698
699 float Arbre::iPhylipRecGetForce(istream & fichier) {
700 char car;
701 char temp[10]; //ATTENTION à la taille de temp !!!!!
702 int i(0);
703
704 car = fichier.peek();
705 // car = fichier[pos]; pos++;
706
707 while ((car == 13) || (car == 10) || (car == '\'')) {//passage du retour chariot
708 fichier.get(car);
709 car = fichier.peek();
710 }
711
712 while (((car > 47 ) && (car < 58)) || (car == '.') && fichier.good() && (car != '\'')) {
713 _oui_bootstrap = true;
714 temp[i] = car;
715 i++;
716 fichier.get(car);
717 car = fichier.peek();
718 }
719 temp[i] = '\0';
720 if (car == '\'') {
721 fichier.get(car);
722 car = fichier.peek();
723 }
724 if (i == 0) return(-999);
725
726 if (fichier.fail()) cerr << "Arbre::iPhylipRecGetForce fichier.fail()" << endl;
727
728 return(atof(temp));
729 }
730
731 float Arbre::iPhylipRecGetTaille(istream & fichier) {
732 char car;
733 char temp[15]; //ATTENTION à la taille de temp !!!!!
734 int i(0);
735 // bool neg(false);
736 //cerr << "Arbre::iPhylipRecGetTaille" << endl;
737
738 car = fichier.peek();
739 while ((car == 13) || (car == 10)) {//passage du retour chariot
740 fichier.get(car);
741 car = fichier.peek();
742 }
743
744 if (car == ':') {
745 _oui_taille_branches = true;
746 fichier.get(car);
747 car = fichier.peek();
748 if (car == '-') {
749 fichier.get(car);
750 temp[i] = car;
751 i++;
752 car = fichier.peek();
753 }
754 while ((((car > 47 ) && (car < 58)) || (car == '.') || (car == 'e'))) {
755 if (car == 'e') {
756 temp[i] = car;
757 i++;
758 fichier.get(car);
759 car = fichier.peek();
760 if (car == '-') {
761 temp[i] = car;
762 i++;
763 fichier.get(car);
764 car = fichier.peek();
765 }
766 }
767 temp[i] = car;
768 i++;
769 fichier.get(car);
770 car = fichier.peek();
771 }
772 }
773 //cerr << "Arbre::iPhylipRecGetTaille " << car << endl;
774 temp[i] = '\0';
775 // if (i == 0) _oui_taille_branches = false;
776
777 if (fichier.fail()) cerr << "Arbre::iPhylipRecGetTaille fichier.fail()" << endl;
778
779 // return(0);
780 return(atof(temp));
781 }
782
783 void Arbre::i_PhylipRecGetNom(istream & fichier, char * nom) {
784 // string nom;
785 char car;
786 unsigned int i(1);
787
788 car = fichier.peek();
789
790 if (nom[0] == '\'') {
791 i = 0;
792 // nom[0] = car;
793 // fichier.get(car);
794 // car = fichier.peek();
795 }
796 while ((car != ':' ) && (car != ',' ) && (car != ')' ) && (car != '\'' )) {
797 nom[i] = car;
798 i++;
799 fichier.get(car);
800 car = fichier.peek();
801 }
802 if (car == '\'') {
803 fichier.get(car);
804 car = fichier.peek();
805 }
806 nom[i] = '\0';
807
808 // if (fichier.fail()) cerr << "Arbre::i_PhylipRecGetNom fichier.fail()" << endl;
809
810 // return(nom);
811 }
812
813 void Arbre::f_test() {
814
815 set_Pracine(_tabPnoeuds[4]);
816 }
817
818 void Arbre::SquizNoeud(ArbreNdNoeud * Pndaeffacer) {
819 //élimination d'un noeud à 1 branche
820
821 // cerr << "Arbre::SquizNoeud" << endl;
822
823 if (Pndaeffacer->get_nbbranches()!=1) {
824 cerr << "erreur Arbre::SquizNoeud" << endl;
825 return;
826 }
827
828 ArbreNdBase * Phaut;
829 ArbreNdBase * Pbas;
830 float longueur(0);
831 int pos;
832 unsigned int i;
833
834 Phaut = Pndaeffacer->get_Pnoeud(0);
835 Pbas = Pndaeffacer->get_Pnoeud(1);
836 pos = Phaut->get_posnoeuds(Pndaeffacer);
837 if (pos == -1) {
838 cerr << "Erreur Arbre::SquizNoeud" << endl;
839 return;
840 }
841 // cerr << "Arbre::SquizNoeud "<< longueur << endl;
842 if (Phaut->get_tabLgBranches(pos) != -999) longueur = Phaut->get_tabLgBranches(pos);
843 // cerr << "Arbre::SquizNoeud "<< longueur << endl;
844 if (Pbas->get_tabLgBranches(0) != -999) longueur += Pbas->get_tabLgBranches(0);
845 if ((Pbas->get_tabLgBranches(0) == -999) && (Phaut->get_tabLgBranches(pos) == -999))
846 longueur = -999;
847 Phaut->set_longueur(pos, longueur);
848 Pbas->set_longueur((unsigned int)0, longueur);
849 Phaut->set_Pnoeud(pos, Pbas);
850 Pbas->set_Pnoeud(0, Phaut);
851
852 for (i = 0; i < _tabPnoeuds.size(); i++) {
853 if (_tabPnoeuds[i] == Pndaeffacer) _tabPnoeuds.erase(_tabPnoeuds.begin()+i);
854 }
855
856 delete Pndaeffacer;
857 }
858
859 bool ArbreNdNoeud::ExisteBranche(const ArbreVectUINT & tabInd) const {
860 unsigned int i;
861
862 for (i=0; i < _tabP_tab_id_ind.size(); i++) {
863 if (tabInd == *(_tabP_tab_id_ind[i])) {
864 return (true);
865 }
866 }
867
868 return (false);
869 }
870
871
872 ArbreNdNoeud * Arbre::RechercheNoeud(const ArbreVectUINT & tabInd) const {
873 ArbreNdNoeud * Presultat(0);
874 unsigned int i;
875
876 for (i=0; i < _tabPnoeuds.size(); i++) {
877 if (_tabPnoeuds[i]->ExisteBranche(tabInd)) {
878 Presultat = _tabPnoeuds[i];
879 break;
880 }
881 }
882
883 return (Presultat);
884 }
885
886 void Arbre::f_load_vect_id_ind() {
887 //chargement du vecteur d'id d'individus
888 //à partir de la racine, déclenche une fonction récursive de remplissage
889 //cerr << "Arbre::f_load_vect_id_ind debut" <<endl;
890
891 //cerr << "Arbre::f_load_vectind " << endl;
892 // chargement descendant
893 _Pracine->f_load_id_ind();
894
895 _ind_charge = true;
896 //cerr << "Arbre::f_load_vect_id_ind fin" <<endl;
897 }
898
899 /*
900 void Arbre::f_load_vectind() {
901 //chargement du vecteur de vecteurs de pointeurs d'individus
902 //à partir de la racine, déclenche une fonction récursive de remplissage
903 unsigned int i;
904 unsigned int taille (_tabPind.size());
905 cerr << "Arbre::f_load_vectind debut" <<endl;
906
907 //cerr << "Arbre::f_load_vectind " << endl;
908 // chargement descendant
909 _Pracine->f_load_vectind();
910 // chargement ascendant
911 for (i = 0; i < taille; i++) {
912 _tabPind[i]->f_load_vectind_parent();
913 }
914 _ind_charge = true;
915 cerr << "Arbre::f_load_vectind fin" <<endl;
916 }
917 */
918
919 void Arbre::fCalcValBootstrap() {
920 //calcul de la force des noeuds
921
922 if (_cumulbootstrap != 0) _Pracine->fCalcValBootstrap(_cumulbootstrap);
923
924 _oui_bootstrap = true;
925 }
926
927 void ArbreNdNoeud::fCalcValBootstrap(unsigned int total_bootstrap) {
928
929 double resultat;
930
931 if (modf ((((double) _accuvalboot / (double) total_bootstrap) * (double) 100),&resultat) > 0.5) {
932 _force = (int)resultat + 1;
933 }
934 else _force = (int)resultat;
935
936 unsigned int i;
937
938 if (_racine) _tabPNoeuds[0]->fCalcValBootstrap(total_bootstrap);
939 for (i=1; i < _tabPNoeuds.size();i++) {
940 _tabPNoeuds[i]->fCalcValBootstrap(total_bootstrap);
941 }
942 }
943
944 void Arbre::reset() {
945 //remise à zéro de l'arbre
946 //destruction des noeuds et individus
947 unsigned int i;
948
949 for (i=0; i < _tabPind.size(); i++) delete _tabPind[i];
950 for (i=0; i < _tabPnoeuds.size(); i++) delete _tabPnoeuds[i];
951 _tabPind.resize(0);
952 _tabPnoeuds.resize(0);
953
954 _ind_charge = false;
955 _oui_taille_branches = false;
956 _oui_bootstrap = false;
957 _cumulbootstrap = 0;
958 _Pracine = 0;
959 }
960
961 void Arbre::iNeighborJoining(Matrice & distances) {
962 //cerr << "nj" << endl;
963 //pour accelerer le traitement des bootstrap:
964 // if (_PtabPnoeud == 0) _PtabPnoeud = new vector<Noeud *>;
965 // else _PtabPnoeud->resize(0);
966 reset();
967 //cerr << "Arbre::iNeighborJoining debut " << distances.GetType() << endl;
968
969 bool *etatMat;
970 // Vecteur<Branche *> tabPbranches;
971 vector<ArbreNdBase *> tabPNoeuds;
972 long nblignemat;
973 long taille(distances.GetNL());
974 long i, j, pos1(0), pos2(0), pos3, id, jd, refi, refj;
975 long double sumbr1, sumbr2;
976 long double comparer;
977 long double critere;
978 long double * Ptab(distances._tab);
979 vector<long double> accDistances;
980 long double distance;
981
982
983 ArbreNdNoeud * Pnoeud;
984
985 if (distances.GetType() != 2) throw Anomalie(4);
986
987 //cerr << "Arbre::iNeighborJoining debut 2" << endl;
988 if (taille < 3) throw Anomalie(5);
989
990 for (i = 0; i < taille; i++) {
991 for (j = 0; j < i; j++) {
992 if (distances.GetCase(i,j) < 0) throw Anomalie(6);
993 }
994 }
995 //cerr << "Arbre::iNeighborJoining debut3" << endl;
996
997 etatMat = new bool[taille];
998 // tabPbranches.resize(taille);
999 tabPNoeuds.resize(taille);
1000 // _tabPotu.resize(taille);
1001 accDistances.resize(taille);
1002
1003 for (i=0; i < taille ; i++) {
1004 etatMat[i] = true;
1005 // cerr << distances._tlig[i] << endl;
1006 // _tabPotu[i] = new Otu(distances._tlig[i]);
1007 // _tabPind[i] = new ArbreNdOTU(this, distances._tlig[i]);
1008 // tabPNoeuds[i] = _tabPind[i];
1009 tabPNoeuds[i] = new_ArbreNdOTU(distances._tlig[i]);
1010 // tabPNoeuds[i] = new ArbreNdOTU(this, distances._tlig[i]);
1011 // tabPbranches[i] = new Branche(this, _tabPotu[i]);
1012 }
1013
1014 nblignemat = taille;
1015
1016 while (nblignemat > 3) {
1017 //cerr << "Arbre::iNeighborJoining Critere Q debut" << endl;
1018 comparer = 9999999;
1019 for (i = 1; i < taille; i++) {
1020 if (etatMat[i]) {
1021 for (j = 0; j < i; j++) {
1022 if (etatMat[j]) {
1023 for (critere = 0, refj = (j * taille), refi = (i * taille),id = 0; id < taille; id++, refi++, refj++) {
1024 if (etatMat[id]) {
1025 if (id != i) critere += *(Ptab + refi);
1026
1027 if (id != j) critere += *(Ptab + refj);
1028 }
1029 }
1030 critere = ((nblignemat - 2) * *(Ptab + ((i * taille) + j))) - critere;
1031
1032 //recherche du couple ou la distance est minimum
1033 if (comparer > critere) {
1034 comparer = critere;
1035 pos1 = j;
1036 pos2 = i;
1037 }
1038 }
1039 }
1040 }
1041 }
1042 // ----- Critere Q Fin ----
1043
1044 //cerr << "Arbre::iNeighborJoining Critere Q fin" << endl;
1045
1046 //--------------calcul des longueurs de branches
1047 distance = Ptab[(pos1 * taille) + pos2];
1048 sumbr1 = 0;
1049 sumbr2 = 0;
1050 for (i = 0; i < taille; i ++) {
1051 if ((etatMat[i]) && (i != pos1) && (i != pos2)) {
1052 sumbr1 += Ptab[(pos1 * taille) + i];
1053 sumbr2 += Ptab[(pos2 * taille) + i];
1054 }
1055 }
1056 sumbr1 /= (nblignemat - 2);
1057 sumbr2 /= (nblignemat - 2);
1058
1059 tabPNoeuds[pos1]->set_longueur((unsigned int)0,((distance + sumbr1 - sumbr2) /2) - accDistances[pos1]);
1060 //cerr << "Arbre::iNeighborJoining set_longueur" << endl;
1061 //tabPbranches[pos1]->_taille = (distance + sumbr1 - sumbr2) /2;
1062 tabPNoeuds[pos2]->set_longueur((unsigned int)0,((distance + sumbr2 - sumbr1) /2) - accDistances[pos2]);
1063 //tabPbranches[pos2]->_taille = (distance + sumbr2 - sumbr1) /2;
1064
1065 accDistances[pos1] = (distance / 2);
1066 //------------fin du calcul des longueurs de branches
1067
1068 //grouper les branches dans un Noeud
1069 //creer une nouvelle Branche
1070
1071 // Pnoeud = new Noeud(new Branche(this));
1072 Pnoeud = new_ArbreNdNoeud((unsigned int) 1);
1073 //pour accelerer le traitement des bootstrap:
1074 // _PtabPnoeud->push_back(Pnoeud);
1075 // _tabPnoeuds.push_back(Pnoeud);
1076
1077 // Pnoeud->_tabPbranche.resize(2);
1078 // Pnoeud->_tabPbranche[0] = tabPbranches[pos1];
1079 // Pnoeud->_tabPbranche[1] = tabPbranches[pos2];
1080 //cerr << "Arbre::iNeighborJoining AjouterFils" << endl;
1081 Pnoeud->AjouterFils(tabPNoeuds[pos1]);
1082 tabPNoeuds[pos1]->set_Pnoeud(0,Pnoeud);
1083 Pnoeud->AjouterFils(tabPNoeuds[pos2]);
1084 tabPNoeuds[pos2]->set_Pnoeud(0,Pnoeud);
1085
1086
1087 // tabPbranches[pos1] = Pnoeud->_PbrMere;
1088 // tabPbranches[pos1]->_type = 1;
1089 tabPNoeuds[pos1] = Pnoeud;
1090 etatMat[pos2] = false;
1091 nblignemat--;
1092
1093 //recalculer les distances
1094 jd = pos1; //colonne pos1
1095 id = (pos1 * taille); //ligne pos1
1096 for (i = 0; i < taille; i ++,id ++,jd += taille) {
1097 if ((etatMat[i]) && (i != pos1)) {
1098 // distances.GetCase(pos1,i) = (distances.GetCase(pos1,i) + distances.GetCase(pos2,i)) / 2;
1099 Ptab[id] += Ptab[(pos2 * taille) + i];
1100 Ptab[id] /= (long double) 2;
1101 Ptab[jd] = Ptab[id];
1102 }
1103 }
1104 //cerr << "+" << endl;
1105
1106 }
1107
1108 pos2 = pos3 = -1;
1109 for (i = 0; i < taille; i++) {
1110 if ((etatMat[i]) && (i != pos1)) {
1111 if (pos2 < 0) pos2 = i;
1112 else if (pos3 < 0) pos3 = i;
1113 // else pos3 = i;
1114 }
1115 }
1116
1117 // _PndRoot = new Noeud(new Branche(this));
1118 //cerr << "Arbre::iNeighborJoining racine" << endl;
1119 _Pracine = new_ArbreNdNoeud();
1120 //pour accelerer le traitement des bootstrap:
1121 // _PtabPnoeud->push_back(_PndRoot);
1122 // _tabPnoeuds.push_back(_Pracine);
1123
1124 _Pracine->AjouterFils(tabPNoeuds[pos1]);
1125 _Pracine->AjouterFils(tabPNoeuds[pos2]);
1126 _Pracine->AjouterFils(tabPNoeuds[pos3]);
1127 tabPNoeuds[pos1]->set_Pnoeud(0,_Pracine);
1128 tabPNoeuds[pos2]->set_Pnoeud(0,_Pracine);
1129 tabPNoeuds[pos3]->set_Pnoeud(0,_Pracine);
1130
1131 _Pracine->set_longueur(0,((Ptab[(pos2 * taille) + pos1] + Ptab[(pos3 * taille) + pos1] - Ptab[(pos3 * taille) + pos2]) /2) - accDistances[pos1]);
1132 _Pracine->set_longueur(1,((Ptab[(pos1 * taille) + pos2] + Ptab[(pos3 * taille) + pos2] - Ptab[(pos3 * taille) + pos1]) /2) - accDistances[pos2]);
1133 _Pracine->set_longueur(2,((Ptab[(pos1 * taille) + pos3] + Ptab[(pos2 * taille) + pos3] - Ptab[(pos1 * taille) + pos2]) /2) - accDistances[pos3]);
1134
1135 delete[] etatMat;
1136
1137 _oui_bootstrap = false;
1138 _oui_taille_branches = true;
1139 //_construction = 2;
1140 }
1141
1142 void Arbre::iUPGMA(Matrice & distances) {
1143 reset();
1144
1145 Vecteur<int> etatMat;
1146 vector<long double> accDistances;
1147 Vecteur<ArbreNdBase *> tabPNoeuds;
1148 long taille(distances.GetNL());
1149 long i, j, pos1(0), pos2(0);
1150 long double comparer;
1151 ArbreNdNoeud * Pnoeud(0);
1152
1153 if (distances.GetType() != 2) throw Anomalie(4);
1154
1155 if (taille < 2) throw Anomalie(5);
1156
1157 for (i = 0; i < taille; i++) {
1158 for (j = 0; j < i; j++) {
1159 if (distances.GetCase(i,j) < 0) throw Anomalie(6);
1160 }
1161 }
1162
1163 etatMat.resize(taille);
1164 accDistances.resize(taille);
1165
1166 tabPNoeuds.resize(taille);
1167
1168 for (i=0; i < taille ; i++) {
1169 accDistances[i] = 0;
1170 etatMat[i] = 1;
1171
1172 tabPNoeuds[i] = new_ArbreNdOTU(distances._tlig[i]);
1173 }
1174
1175 while (etatMat.getNbOccurence(1) > 1) {
1176 //trouver la + courte distance
1177 comparer = -1;
1178 for (i = 1; i < taille; i++) {
1179 for (j = 0; j < i; j++) {
1180 if ((etatMat[i] == 1)&&(etatMat[j] == 1)) {
1181 if (comparer == -1) {
1182 comparer = distances.GetCase(i,j);
1183 pos1 = j;
1184 pos2 = i;
1185 }
1186 else {
1187 if (comparer > distances.GetCase(i,j)) {
1188 comparer = distances.GetCase(i,j);
1189 pos1 = j;
1190 pos2 = i;
1191 }
1192 }
1193 }
1194 }
1195 }
1196 //grouper les branches dans un Noeud
1197 Pnoeud = new_ArbreNdNoeud((unsigned int) 1);
1198 Pnoeud->AjouterFils(tabPNoeuds[pos1]);
1199 tabPNoeuds[pos1]->set_Pnoeud(0,Pnoeud);
1200 Pnoeud->AjouterFils(tabPNoeuds[pos2]);
1201 tabPNoeuds[pos2]->set_Pnoeud(0,Pnoeud);
1202
1203
1204
1205 tabPNoeuds[pos1]->set_longueur((unsigned int)0,(comparer/2) - accDistances[pos1]);
1206 tabPNoeuds[pos2]->set_longueur((unsigned int)0,(comparer/2) - accDistances[pos2]);
1207 accDistances[pos1] = (comparer/2);
1208
1209 tabPNoeuds[pos1] = Pnoeud;
1210 etatMat[pos2] = 0;
1211
1212 //recalculer les distances
1213 for (i = 0; i < taille; i ++) {
1214 if ((etatMat[i] == 1 ) && (i != pos1)) {
1215 distances.GetCase(pos1,i) = (distances.GetCase(pos1,i) + distances.GetCase(pos2,i))/2;
1216 }
1217 }
1218
1219 }
1220
1221 _Pracine = Pnoeud;
1222 _oui_bootstrap = false;
1223 _oui_taille_branches = true;
1224 }
1225
1226 void Arbre::iNeighborJoiningTopo(Matrice & distances) {
1227 reset();
1228 //cerr << "Arbre::iNeighborJoiningTopo debut" << endl;
1229
1230 bool *etatMat;
1231 vector<ArbreNdBase *> tabPNoeuds;
1232 long nblignemat;
1233 long taille(distances.GetNL());
1234 long i, j, pos1(0), pos2(0), pos3, id, jd, refi, refj;
1235 long double comparer;
1236 long double critere;
1237 long double * Ptab(distances._tab);
1238 vector<long double> accDistances;
1239
1240
1241 ArbreNdNoeud * Pnoeud;
1242
1243 if (distances.GetType() != 2) throw Anomalie(4);
1244
1245 if (taille < 3) throw Anomalie(5);
1246
1247 for (i = 0; i < (taille * taille); i++) {
1248 if (Ptab[i] < 0) throw Anomalie(6);
1249 }
1250 /* for (i = 0; i < taille; i++) {
1251 for (j = 0; j < i; j++) {
1252 if (distances.GetCase(i,j) < 0) throw Anomalie(6);
1253 }
1254 }
1255 */
1256 etatMat = new bool[taille];
1257 tabPNoeuds.resize(taille);
1258 accDistances.resize(taille);
1259
1260 for (i=0; i < taille ; i++) {
1261 etatMat[i] = true;
1262 tabPNoeuds[i] = new_ArbreNdOTU(distances._tlig[i]);
1263 }
1264
1265 nblignemat = taille;
1266
1267 while (nblignemat > 3) {
1268 //cerr << "Arbre::iNeighborJoiningTopo Critere Q debut" << endl;
1269 comparer = 9999999;
1270 for (i = 1; i < taille; i++) {
1271 if (etatMat[i]) {
1272 for (j = 0; j < i; j++) {
1273 if (etatMat[j]) {
1274 for (critere = 0, refj = (j * taille), refi = (i * taille),id = 0; id < taille; id++, refi++, refj++) {
1275 if (etatMat[id]) {
1276 if (id != i) critere += *(Ptab + refi);
1277
1278 if (id != j) critere += *(Ptab + refj);
1279 }
1280 }
1281 critere = ((nblignemat - 2) * *(Ptab + ((i * taille) + j))) - critere;
1282
1283 //recherche du couple ou la distance est minimum
1284 if (comparer > critere) {
1285 comparer = critere;
1286 pos1 = j;
1287 pos2 = i;
1288 }
1289 }
1290 }
1291 }
1292 }
1293 // ----- Critere Q Fin ----
1294
1295 //cerr << "Arbre::iNeighborJoining Critere Q fin" << endl;
1296
1297 //grouper les branches dans un Noeud
1298 //creer une nouvelle Branche
1299
1300 // Pnoeud = new Noeud(new Branche(this));
1301 Pnoeud = new_ArbreNdNoeud((unsigned int) 1);
1302 Pnoeud->AjouterFils(tabPNoeuds[pos1]);
1303 tabPNoeuds[pos1]->set_Pnoeud(0,Pnoeud);
1304 Pnoeud->AjouterFils(tabPNoeuds[pos2]);
1305 tabPNoeuds[pos2]->set_Pnoeud(0,Pnoeud);
1306
1307
1308 tabPNoeuds[pos1] = Pnoeud;
1309 etatMat[pos2] = false;
1310 nblignemat--;
1311
1312 //recalculer les distances
1313 jd = pos1; //colonne pos1
1314 id = (pos1 * taille); //ligne pos1
1315 for (i = 0; i < taille; i ++,id ++,jd += taille) {
1316 if ((etatMat[i]) && (i != pos1)) {
1317 Ptab[id] += Ptab[(pos2 * taille) + i];
1318 Ptab[id] /= (long double) 2;
1319 Ptab[jd] = Ptab[id];
1320 }
1321 }
1322 //cerr << "+" << endl;
1323
1324 }
1325
1326 pos2 = pos3 = -1;
1327 for (i = 0; i < taille; i++) {
1328 if ((etatMat[i]) && (i != pos1)) {
1329 if (pos2 < 0) pos2 = i;
1330 else if (pos3 < 0) pos3 = i;
1331 // else pos3 = i;
1332 }
1333 }
1334
1335 // _PndRoot = new Noeud(new Branche(this));
1336 //cerr << "Arbre::iNeighborJoining racine" << endl;
1337 _Pracine = new_ArbreNdNoeud();
1338
1339 _Pracine->AjouterFils(tabPNoeuds[pos1]);
1340 _Pracine->AjouterFils(tabPNoeuds[pos2]);
1341 _Pracine->AjouterFils(tabPNoeuds[pos3]);
1342 tabPNoeuds[pos1]->set_Pnoeud(0,_Pracine);
1343 tabPNoeuds[pos2]->set_Pnoeud(0,_Pracine);
1344 tabPNoeuds[pos3]->set_Pnoeud(0,_Pracine);
1345
1346
1347 delete[] etatMat;
1348
1349 _oui_bootstrap = false;
1350 _oui_taille_branches = false;
1351 //_construction = 2;
1352 }
1353
1354 double ArbreNdOTU::get_longueur_max()const {
1355 if (get_longueur_branche() < 0) return (0);
1356 else return (get_longueur_branche());
1357 }
1358
1359 unsigned int ArbreNdOTU::get_nbnoeuds_max()const {
1360 return (1);
1361 }
1362
1363
1364 void Arbre::f_tri_ind_alpha() {
1365
1366 unsigned int i;
1367
1368 //cerr << "Arbre::f_tri_ind_alpha debut " << _tabPind.size() << endl;
1369 if (_tabPind.size() < 2) return;
1370
1371 //f_tribulle_ind_alpha();
1372 sort (_tabPind.begin(),_tabPind.end(),moins<ArbreNdOTU *> ());
1373 /* if (_tabPind.size() < 4) f_tribulle_ind_alpha();
1374 // if (size() < 4) return;
1375 else f_trishell_ind_alpha(0, _tabPind.size()-1);
1376 //cerr << "Arbre::f_tri_ind_alpha num"<< endl;
1377 */
1378 for (i=0; i < _tabPind.size(); i++) {
1379 _tabPind[i]->set_id(i);
1380
1381 //cerr << _tabPind[i]->get_nom_c_str() << endl;
1382 }
1383 //cerr << "Arbre::f_tri_ind_alpha fin"<< endl;
1384 }
1385
1386
1387 void Arbre::f_tribulle_ind_alpha() {
1388 unsigned int ok,i;
1389 ArbreNdOTU * swap;
1390 unsigned int n(_tabPind.size());
1391
1392 do{
1393 ok=1;
1394 for(i=1;i<n;i++) if (strcmp(_tabPind[i-1]->get_reference(), _tabPind[i]->get_reference()) > 0) {
1395 ok=0;
1396 swap = _tabPind[i];
1397 _tabPind[i] = _tabPind[i-1];
1398 _tabPind[i-1] = swap;
1399 }
1400 }while(!ok);
1401 }
1402
1403 void Arbre::f_trishell_ind_alpha(unsigned int lb, unsigned int ub) {
1404 //tri de _tabPind en fonction du nom des individus
1405 //T *a,
1406 unsigned int n, h, i, j;
1407 // unsigned int t;
1408 ArbreNdOTU * Pswap;
1409
1410 /**************************
1411 * sort array a[lb..ub] *
1412 **************************/
1413 //cerr << "Arbre::f_trishell_ind_alpha debut "<< lb << " " << ub << endl;
1414
1415 /* compute largest increment */
1416 n = ub - lb + 1;
1417 h = 1;
1418 if (n < 14) h = 1;
1419 // else if (sizeof(unsigned int) == 2 && n > 29524)
1420 // h = 3280;
1421 else {
1422 while (h < n) h = 3*h + 1;
1423 h /= 3;
1424 h /= 3;
1425 }
1426
1427 while (h > 0) {
1428
1429 /* sort-by-insertion in increments of h */
1430 for (i = lb + h; i <= ub; i++) {
1431 //cerr << "Arbre::f_trishell_ind_alpha i "<< i << endl;
1432 Pswap = _tabPind[i];
1433 //cerr << "Arbre::f_trishell_ind_alpha ok " << endl;
1434 //cerr << "i-h " << (i-h) << endl;
1435 for (j = i-h; ((j >= lb) && (strcmp(_tabPind[j]->get_reference(), Pswap->get_reference()) > 0)); j -= h)
1436 _tabPind[j+h] = _tabPind[j];
1437 _tabPind[j+h] = Pswap;
1438 }
1439
1440 /* compute next increment */
1441 h /= 3;
1442 }
1443 //cerr << "Arbre::f_trishell_ind_alpha fin "<< lb << " " << ub << endl;
1444 }
1445
1446 void ArbreVectUINT::f_tri() {
1447 //tri du vecteur
1448 sort(begin(), end(), less<unsigned int>());
1449 //return;
1450 //vector<unsigned int>::sort();
1451 //return;
1452 //cerr << "ArbreVectUINT::f_tri debut " << endl;
1453 //if (size() < 2) return;
1454 //if (size() == 2) {
1455 // unsigned int swap;
1456 // if (operator[](0) > operator[](1)) {
1457 // swap = operator[](0);
1458 // operator[](0) = operator[](1);
1459 // operator[](1) = swap;
1460 // }
1461 // return;
1462 //}
1463 //if (size() == 3) f_tribulle();
1464 // if (size() < 4) return;
1465 //else f_tribulle();
1466 // else f_trishell(0,size()-1);
1467 //cerr << "ArbreVectUINT::f_tri fin " << endl;
1468 }
1469
1470
1471 void ArbreVectUINT::f_tribulle() {
1472 unsigned int ok,i, swap;
1473 unsigned int n(size());
1474
1475 do{
1476 ok=1;
1477 for(i=1;i<n;i++) if (operator[](i-1)>operator[](i)) {
1478 ok=0;
1479 swap = operator[](i-1);
1480 operator[](i-1) = operator[](i);
1481 operator[](i) = swap;
1482 }
1483 } while(!ok);
1484 }
1485
1486 void ArbreVectUINT::f_trishell(unsigned int lb, unsigned int ub) {
1487 //T *a,
1488 unsigned int n, h, i, j;
1489 unsigned int t;
1490
1491 /**************************
1492 * sort array a[lb..ub] *
1493 **************************/
1494
1495 /* compute largest increment */
1496 n = ub - lb + 1;
1497 h = 1;
1498 if (n < 14)
1499 h = 1;
1500 // else if (sizeof(unsigned int) == 2 && n > 29524)
1501 // h = 3280;
1502 else {
1503 while (h < n) h = 3*h + 1;
1504 h /= 3;
1505 h /= 3;
1506 }
1507
1508 while (h > 0) {
1509
1510 /* sort-by-insertion in increments of h */
1511 for (i = lb + h; i <= ub; i++) {
1512 t = operator[](i);
1513 // for (j = i-h; j >= lb && compGT(operator[](j), t); j -= h)
1514 for (j = i-h; ((j >= lb) && (operator[](j)> t)); j -= h)
1515 operator[](j+h) = operator[](j);
1516 operator[](j+h) = t;
1517 }
1518
1519 /* compute next increment */
1520 h /= 3;
1521 }
1522 }
1523
1524 bool ArbreVectUINT::operator== (const ArbreVectUINT &rval) {
1525 //=>remplir les vecteurs avant
1526 //cerr << "ArbreNdNoeud::operator==" << endl;
1527 unsigned int i, taille (size());
1528
1529 if (taille != rval.size()) return (false);
1530
1531 for (i = 0; i < taille; i ++) {
1532 if (operator[](i) != rval[i]) return (false);
1533 }
1534 return (true);
1535 }
1536
1537 void ArbreVectUINT::affiche() const {
1538
1539 string affichage;
1540 unsigned int i, taille (size());
1541
1542 for (i = 0; i < taille; i ++) {
1543 cout << " " << operator[](i);
1544 }
1545
1546 }
1547
1548 void Arbre::iDistances(MatriceLD & matrice) {
1549 iNeighborJoining(matrice);
1550 // f_tri_ind_alpha();
1551 }
1552
1553 void Arbre::iDistances(MatriceLD & distances, int methode) {
1554 //reconstruction d'arbres a partir d'une matrice de distances:
1555 // int -> methode de reconstruction
1556 // 1 -> UPGMA
1557 // 2 -> Neighbor Joining
1558 //on efface l'arbre si il yen a deja un
1559 //cerr << "Arbre::iDistances(MatriceLD & distances, int methode) debut" << endl;
1560 reset();
1561
1562 switch (methode) {
1563 case 1: // UPGMA
1564 iUPGMA(distances);
1565 break;
1566 case 101: // UPGMA, topologie seulement
1567 iUPGMA(distances);
1568 break;
1569 case 2: // Neighbor Joining
1570 iNeighborJoining(distances);
1571 break;
1572 case 102: // Neighbor Joining, topologie seulement
1573 iNeighborJoiningTopo(distances);
1574 break;
1575 }
1576 //cerr << "Arbre::iDistances(MatriceLD & distances, int methode) tri" << endl;
1577 // f_tri_ind_alpha();
1578 }
1579
1580 void Arbre::f_forceiDistances(MatriceLD & distancesRef,int methodeArbre) {
1581 bool continuer(true);
1582
1583 while (continuer) {
1584 try {
1585 continuer = false;
1586 iDistances(distancesRef,methodeArbre);
1587 }
1588 catch (Anomalie pb) {
1589 cout << _("Error while building tree...") << endl;
1590 switch (pb.le_pb) {
1591 case 4:
1592 cout << _("Inconsistant matrix file format") << endl;
1593 throw Anomalie(4);
1594 break;
1595 case 5:
1596 cout << _("the matrix is too small") << endl;
1597 throw Anomalie(5);
1598 break;
1599 case 6:
1600 cout << _("Negative distances in the matrix...") << endl;
1601 cout << _("the smallest value is: ") << distancesRef.get_ppvaleur() << endl;
1602 cout << _("all negative values are replaced by zero to proceed") << endl;
1603 distancesRef.f_neg2zero();
1604 continuer = true;
1605 break;
1606 default:
1607 throw Anomalie(pb.le_pb);
1608 break;
1609 }
1610 }
1611 }
1612 }
1613
1614 void Arbre::iNimbus(unsigned int nbessais , unsigned int nbind, unsigned int * Ptemp, char * * PPotu) {
1615 unsigned int i, j;
1616 ChaineCar nom;
1617 MatriceLD tab_nimbus(nbind, nbessais) ;
1618 unsigned int * Puint(Ptemp);
1619 //cerr << "Arbre::iNimbus debut" << endl;
1620 // Visu.reset((long unsigned int) nbind, (long unsigned int) nbessais);
1621 for (i=0; i < nbind ; i++) {
1622 tab_nimbus._tlig[i].assign(PPotu[i]);
1623 for (j=0; j < nbessais ; j++) {
1624 tab_nimbus.GetCase(i, j) = *Puint;
1625 Puint++;
1626 }
1627 }
1628 tab_nimbus.SetFlag(1);
1629 // tab_nimbus.oExcel(cerr);
1630
1631 reset();
1632
1633 for (i=0; i < nbind ; i++) {
1634 new_ArbreNdOTU("");
1635 _tabPind[i]->set_nom(tab_nimbus._tlig[i]);
1636 }
1637
1638 _Pracine = new_ArbreNdNoeud();
1639
1640 iNimbusRec(_Pracine, tab_nimbus, 0, 0, nbind);
1641
1642 _Pracine->AjouterFils(new_ArbreNdOTU(""));
1643 _tabPind.back()->set_nom("____");
1644 _tabPind.back()->set_longueur((unsigned int) 0, (float) nbessais);
1645
1646 _oui_bootstrap = false;
1647 _oui_taille_branches = true;
1648
1649 //cerr << "Arbre::iNimbus fin " << _tabPind.size() << endl;
1650
1651 }
1652
1653 void Arbre::iNimbusRec(ArbreNdNoeud * Pnoeud, MatriceLD & tab_nimbus, unsigned int numessai, unsigned int deb, unsigned int fin, unsigned int longueur) {
1654 // pos: position en nb d'essais
1655 // branche regroupant les otus de 'deb' a 'fin'
1656 unsigned int i, nbessais(tab_nimbus.GetNC());
1657 unsigned int pos;
1658 // ArbreNdNoeud * Pnoeud (_tabPnoeud.back());
1659
1660 //cerr << "Arbre::iNimbusRec debut" << endl;
1661 if ((numessai >= nbessais)&&(numessai != 0)) { //dernière colonne
1662 //cerr << "Arbre::iNimbusRec derniere col numessai " << numessai << endl;
1663 Pnoeud->AjouterFils(new_ArbreNdNoeud((unsigned int) 1));
1664 //cerr << "coucou1 " << Pnoeud->get_nbbranches() << endl;
1665 // PPnoeudnov = _tabPnoeuds.back();
1666 //cerr << "coucou2 nbb " << _tabPnoeuds.back()->get_nbbranches() << endl;
1667 _tabPnoeuds.back()->set_longueur(0, longueur);
1668 //cerr << "coucou3" << endl;
1669 //cerr << "Arbre::iNimbusRec dernier deb " << _tabPind[deb]->get_nom() << endl;
1670 _tabPnoeuds.back()->AjouterFils(_tabPind[deb]);
1671 _tabPind[deb]->set_longueur((unsigned int) 0, 0.25);
1672 for (i = deb+1; i < fin; i++) {
1673 //cerr << "coucou4" << endl;
1674 //cerr << "Arbre::iNimbusRec dernier " << _tabPind[i]->get_nom() << endl;
1675 _tabPnoeuds.back()->AjouterFils(_tabPind[i]);
1676 _tabPind[i]->set_longueur((unsigned int) 0, 0.25);
1677 }
1678 return;
1679 }
1680
1681 if ((fin - deb) == 1) {
1682 //cerr << "Arbre::iNimbusRec one " << _tabPind[deb]->get_nom() << endl;
1683 // branche terminale
1684 Pnoeud->AjouterFils(_tabPind[deb]);
1685 _tabPind[deb]->set_longueur((unsigned int) 0, nbessais - numessai);
1686 return;
1687 }
1688
1689 for (pos = numessai; pos < nbessais; pos ++) {
1690
1691 //cerr << "Arbre::iNimbusRec pos "<< pos << " numessai " << numessai << endl;
1692 for (i = deb+1; i < fin; i ++) {
1693 if (tab_nimbus.GetCase(i-1, pos) != tab_nimbus.GetCase(i, pos)) {
1694 //nouveau noeud
1695 Pnoeud->AjouterFils(new_ArbreNdNoeud((unsigned int) 1));
1696 Pnoeud = _tabPnoeuds.back();
1697 Pnoeud->set_longueur(0, pos-numessai);
1698 iNimbusRec(Pnoeud, tab_nimbus, pos, deb,i,pos-numessai);
1699 //les autres
1700 deb = i;
1701 i++;
1702 while (i < fin) {
1703 if (tab_nimbus.GetCase(i-1, pos) != tab_nimbus.GetCase(i, pos)){
1704 iNimbusRec(Pnoeud, tab_nimbus, pos, deb,i,pos-numessai);
1705 deb = i;
1706 }
1707 i ++;
1708 }
1709 iNimbusRec(Pnoeud, tab_nimbus, pos, deb,fin,pos-numessai);
1710 return;
1711 }
1712
1713 }
1714
1715 }
1716
1717 iNimbusRec(Pnoeud, tab_nimbus, pos, deb,fin,pos-numessai);
1718 }
1719
1720 void Arbre::operator>>(ostream& sortie) {
1721 switch (_oFormat) {
1722 case 1:
1723 oPhylip(sortie);
1724 break;
1725 default:
1726 oPhylip(sortie);
1727 break;
1728 }
1729 }
1730
1731 const ArbreNdOTU * Arbre::RecherchePOTU(unsigned int id) const {
1732
1733 unsigned int i;
1734
1735 for (i=0 ; i < _tabPind.size(); i++) {
1736 if (_tabPind[i]->get_id() == id) return(_tabPind[i]);
1737 }
1738 return (0);
1739 }
1740
1741 void ArbreNdOTU::set_nom(const string & nom) {
1742 //a faire : vérifier l'unicité du nom
1743 _nom.assign(nom);
1744 }
1745
1746 void Arbre::oXML(ostream & fichier) const {
1747 //écriture d'arbres au format XML
1748 /*
1749 <!ELEMENT trees (tree+)>
1750 <!ELEMENT tree (comments?,branch,branch,branch?,length?)>
1751 <!ELEMENT branch (node,length?)>
1752 <!ELEMENT node ((branch,branch)|specie)>
1753 <!ELEMENT length (#PCDATA)>
1754 <!ELEMENT specie (#PCDATA)>
1755
1756 */
1757 fichier << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << endl;
1758 fichier << "<!DOCTYPE trees SYSTEM \"treefile.dtd\">" << endl;
1759 fichier << "<!-- " << endl;
1760 fichier << "created by biolib library" << endl;
1761 fichier << "Olivier Langella <[email protected]>" << endl;
1762 fichier << "http://www.pge.cnrs-gif.fr/bioinfo" << endl;
1763 fichier << "-->" << endl;
1764
1765 fichier << "<trees>" << endl;
1766 fichier << "<tree>" << endl;
1767 unsigned int i;
1768
1769 if (_titre.size() > 0) {
1770 fichier << "<comments>" << endl;
1771 for (i=0; i < _titre.size() ; i++) {
1772 fichier << _titre[i];
1773 }
1774 //oPhylipEcritGroupes(fichier);
1775 fichier << "</comments>" << endl;
1776 }
1777
1778 _Pracine->oXML(fichier);
1779
1780 fichier << "</tree>" << endl;
1781 fichier << "</trees>" << endl;
1782 }
1783
1784
1785 void ArbreNdNoeud::oXML(ostream & fichier) const {
1786 //écriture d'arbres au format Phylip
1787 unsigned int i;
1788 // char car;
1789 //cerr << "ArbreNdNoeud::oPhylipRec debut" << endl;
1790
1791 if (_racine) {
1792 fichier << "<branch>" << endl;
1793 _tabPNoeuds[0]->oXML(fichier);
1794 fichier << "</branch>" << endl;
1795 }
1796 else fichier << "<node>" << endl;
1797
1798 for (i=1; i < _tabPNoeuds.size(); i++) {
1799 fichier << "<branch>" << endl;
1800 _tabPNoeuds[i]->oXML(fichier);
1801 fichier << "</branch>" << endl;
1802 }
1803 if (!(_racine)) fichier << "</node>" << endl;
1804 if (_Parbre->get_oui_bootstrap() && _force != -999) {
1805 fichier << "<force>" << endl;
1806 fichier << _force;
1807 fichier << "<force>" << endl;
1808 }
1809 if ((_racine == false) && (_Parbre->get_oui_taille_branches())) {
1810 fichier << "<length>" << endl;
1811 fichier << _tabLgBranches[0] << endl;
1812 fichier << "</length>" << endl;
1813 // if ((_tabLgBranches[0] < 1e-6) || (_tabLgBranches[0] < 0)) fichier << 0;
1814 // else fichier << _tabLgBranches[0];
1815 }
1816
1817 }
1818
1819 void ArbreNdOTU::oXML(ostream & fichier) const {
1820 //écriture d'arbres au format Phylip
1821 // char car;
1822 //cerr << "ArbreNdNoeud::oPhylipRec debut" << endl;
1823
1824 fichier << "<branch>" << endl;
1825 fichier << "<species>" << endl;
1826 fichier << get_nom() << endl;
1827 fichier << "</species>" << endl;
1828 if (_Parbre->get_oui_taille_branches()) {
1829 fichier << "<length>" << endl;
1830 fichier << _tabLgBranches[0] << endl;
1831 fichier << "</length>" << endl;
1832 }
1833 fichier << "</branch>" << endl;
1834 }
1835
1836 } //namespace biolib {
1837 } //namespace arbres {
1838