Debian Codegraph @

src/jeupop.cpp

raw
1 /***************************************************************************
2 jeupop.cpp - Librairie d'objets permettant de manipuler des donn�es
3 sp�cifiques aux populations
4 -------------------
5 begin : ven sep 06 10:25:55 CEST 2000
6 copyright : (C) 2000 by Olivier Langella CNRS UPR9034
7 email : [email protected]
8 ***************************************************************************/
9
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19
20
21 #include "jeupop.h"
22 #include "distgnt.h"
23
24 //constructeur
25 Jeupop::Jeupop () {
26
27 //cerr << "Jeupop::Jeupop () debut" << endl;
28
29
30 _nbpop = 0;
31 _nbloc = 0;
32 _nploidie = 0;
33
34 _tabPpop.resize(0);
35 _tabPloc.resize(0);
36
37 _Pracinepops = new MetaPop(this);
38 //cerr << "Jeupop::Jeupop () fin" << endl;
39 }
40
41 /*Jeupop::Jeupop (long nbpop, long nbloc) {
42 long i;
43
44
45 _nbpop = nbpop;
46 _nbloc = nbloc;
47 _nploidie = 0;
48
49 _tabPpop.resize(0);
50 _tabPloc.resize(0);
51 // _tabPpop = new (Population*[nbpop]);
52 // _tabPloc = new (Locus*[nbloc]);
53
54 for (i=0;i < nbpop;i++) _tabPpop.push_back(new Population (this));
55 for (i=0;i < nbloc;i++) _tabPloc.push_back(new Locus (this,0));
56
57 _Pracinepops = new MetaPop(this);
58
59 }
60 */
61 Jeupop::Jeupop(const Jeupop& original) {
62 //constructeur de copies
63 unsigned long i;
64
65 //cerr << "Jeupop::Jeupop construct copie debut " << endl;
66 _nploidie = original.get_nploidie();
67 _commentaires = original._commentaires;
68
69 //cerr << "Jeupop::Jeupop construct delete _Pracine " << endl;
70 // delete _Pracinepops;
71 //cerr << "Jeupop::Jeupop construct delete _Pracine " << endl;
72 _tabPpop.resize(0);
73 //cerr << "Jeupop::Jeupop construct delete _Pracine " << endl;
74 // for (i=0;i < _tabPloc.size();i++) delete (_tabPloc[i]);
75 _tabPloc.resize(0);
76
77
78 _nbloc = original.get_nbloc();
79 for (i=0;i < _nbloc;i++) {
80 //cerr << "Jeupop::Jeupop construct copie locus " << endl;
81 _tabPloc.push_back(new Locus (*original._tabPloc[i], this));
82 }
83
84 //cerr << "Jeupop::Jeupop construct copie metapop " << endl;
85 _Pracinepops = new MetaPop(*original._Pracinepops,0, this);
86 //cerr << "Jeupop::Jeupop construct copie fin " << endl;
87 }
88
89 //destructeur
90 Jeupop::~Jeupop () {
91 unsigned long i;
92
93 //for (i=0;i < _tabPpop.size();i++) delete (_tabPpop[i]);
94 for (i=0;i < _tabPloc.size();i++) delete (_tabPloc[i]);
95
96 delete _Pracinepops;
97 }
98
99
100 void Jeupop::reset() {
101
102 unsigned long i;
103
104 //cerr << "Jeupop::reset(long nbpop, long nbloc) debut"<< endl;
105 _nbloc = _tabPloc.size();
106 delete _Pracinepops;
107 _tabPpop.resize(0);
108 _nbpop = 0;
109
110 for (i=0;i < _nbloc;i++) delete (_tabPloc[i]);
111 _nbloc = 0;
112 _tabPloc.resize(0);
113 _VcalcStrucPop.resize(0);
114 _VcalcLocus.resize(0);
115
116
117 _Pracinepops = new MetaPop(this);
118
119 //cerr << "Jeupop::reset(long nbpop, long nbloc) fin" << endl;
120
121 }
122
123
124 void Jeupop::oGenepopCano(ostream& sortie) const{
125 //�criture de fichiers au format genepop
126 // type canonique (avec Pop nomdepop)
127 // pas adapt�e � un traitement sur fichiers haploides
128 unsigned long i,j;
129 char car(0);
130 unsigned long nbpop(get_nbpop());
131 unsigned long nbloc(get_nbloc());
132 unsigned long nbind;
133 int format(1);
134
135 if (_nploidie != 2) {
136 //le format Genepop ne permet que le stockage de pop diplo�des
137 throw Anomalie(1);
138 }
139
140 if (!(f_verifnum(1))) {
141 //il faut renum�roter les all�les pour le format genepop
142 throw Anomalie(2);
143 }
144
145
146 //commentaires
147 car = '"';
148 if (_commentaires.size() == 0) sortie << car << endl;
149 else {
150 sortie << _commentaires.get_titre(0) << endl;
151 for (i=1; i < _commentaires.size(); i++) {
152 sortie << car << _commentaires.get_titre(i) << endl;
153 }
154 }
155
156 //nom des locus
157 for (i=0; i < nbloc;i++) {
158 sortie << _tabPloc[i]->get_nom() << endl ;
159 }
160
161 // populations
162 for (i=0; i < nbpop;i++) {
163 sortie << "POP " << _tabPpop[i]->get_nom_chemin() << endl ;
164 // individus
165 if (_tabPpop[i]->_tabAllIntrogressant.size() > 0) format = 2;
166 else format = 1;
167
168
169 nbind = _tabPpop[i]->get_nbind();
170 for (j=0; j < nbind;j++) {
171 _tabPpop[i]->_tabPind[j]->affiche(sortie,format);
172 sortie << endl;
173 }
174 }
175
176 }
177
178 void Jeupop::oGenepop(ostream& sortie) const{
179 //�criture de fichiers au format genepop
180 // pas adapt�e � un traitement sur fichiers haploides
181 long i,j;
182 char car;
183 long nbpop(get_nbpop());
184 long nbloc(get_nbloc());
185 long nbind;
186 int format(1);
187
188 if (_nploidie != 2) {
189 //le format Genepop ne permet que le stockage de pop diplo�des
190 throw Anomalie(1);
191 }
192
193 if (!(f_verifnum(1))) {
194 //il faut renum�roter les all�les pour le format genepop
195 throw Anomalie(2);
196 }
197
198
199 //nom des populations
200 car = '"';
201 sortie << car;
202
203 i = 0;
204 for (i=0; i < (nbpop-1);i++) {
205 sortie << _tabPpop[i]->get_nom() << ";" ;
206 }
207 sortie << _tabPpop[i]->get_nom() << car << endl;
208
209
210 //nom des locus
211 for (i=0; i < nbloc;i++) {
212 sortie << _tabPloc[i]->get_nom() << endl ;
213 }
214
215 // populations
216 for (i=0; i < nbpop;i++) {
217 sortie << "POP" << endl ;
218 // individus
219 if (_tabPpop[i]->_tabAllIntrogressant.size() > 0) format = 2;
220 else format = 1;
221
222 nbind = _tabPpop[i]->get_nbind();
223 for (j=0; j < nbind;j++) {
224 _tabPpop[i]->_tabPind[j]->affiche(sortie,format);
225 sortie << endl;
226 }
227 }
228
229 }
230
231 void Jeupop::oPopulations(ostream& sortie) const{
232 //�criture de fichiers au format populations
233 // adapt� � toutes les ploidies
234 unsigned long i,j;
235 char car;
236 unsigned long nbpop(get_nbpop());
237 unsigned long nbloc(get_nbloc());
238 unsigned long nbind;
239 int format(3);
240
241 //commentaires
242 //cerr << "Jeupop::oPopulations(ostream& sortie)" << _commentaires.size() << "comm size" << endl;
243 car = '"';
244 if (_commentaires.size() == 0) sortie << car << endl;
245 else {
246 for (i=0; i < _commentaires.size(); i++) {
247 sortie << car << _commentaires.get_titre(i) << endl;
248 }
249 }
250
251 // sortie << _tabPpop[i]->get_nom() << car << endl;
252
253
254 //nom des locus
255 for (i=0; i < nbloc;i++) {
256 sortie << _tabPloc[i]->get_nom() << endl ;
257 }
258 //cerr << "Jeupop::oPopulations(ostream& sortie) 2" << endl;
259
260 // populations
261 for (i=0; i < nbpop;i++) {
262 sortie << "POP " << _tabPpop[i]->get_nom_chemin() << endl ;
263 //cerr << "Jeupop::oPopulations(ostream& sortie) pop " << _tabPpop[i]->get_nom_chemin() << endl;
264 // individus
265 nbind = _tabPpop[i]->get_nbind();
266 //cerr << "Jeupop::oPopulations(ostream& sortie) nbind " << nbind << endl;
267 for (j=0; j < nbind;j++) {
268 _tabPpop[i]->_tabPind[j]->affiche(sortie,format);
269 sortie << endl;
270 }
271 }
272 //cerr << "Jeupop::oPopulations(ostream& sortie) 3" << endl;
273
274 }
275
276 void Jeupop::oImmanc(ostream& sortie) const{
277 //format d'entr�e pour :
278 //Immanc - detects individual that are immigrants or have recent immigrant ancestry
279 //by using multilocus genotypes.
280 //Version 5.0, released October 8, 1998
281 //Reference: Rannala, B., and J.L., Mountain. 1997. Detecting immigration by using
282 //multilocus genotypes. Proceedings of the National Academy of Sciences USA 94:
283 //9197-9201.
284
285 // pas adapt�e � un traitement sur fichiers haploides
286 if (_nploidie != 2) {
287 //le format Immanc ne permet que le stockage de pop diplo�des
288 throw Anomalie(1);
289 }
290
291 if (!(f_verifnum(1))) {
292 //il faut renum�roter les all�les pour le format Immanc
293 throw Anomalie(2);
294 }
295
296 long i,j,k, nbind, nbloc(get_nbloc()), nbpop(get_nbpop());
297 string nomloc;
298 string nompop;
299 for (i=0; i< nbloc-1;i++) {//scan de chaque locus
300 nomloc = _tabPloc[i]->_nom;
301 for (j=0; j< nbpop;j++) {//scan des pops
302 nompop = _tabPpop[j]->get_nom();
303 nbind = _tabPpop[j]->get_nbind();
304 for (k=0; k < nbind;k++) {// scan des individus
305 //nom de l'ind, nom de la pop, nom du locus, les 2 all�les
306 sortie << " " << _tabPpop[j]->_tabPind[k]->_nom;
307 sortie << " " << nompop;
308 sortie << " " << nomloc;
309 sortie << " " << _tabPpop[j]->_tabPind[k]->_tabPall[i * 2]->_nom;
310 sortie << " " << _tabPpop[j]->_tabPind[k]->_tabPall[(i * 2) + 1]->_nom;
311 sortie << " " << endl;
312 }
313 }
314 }
315 }
316
317 void Jeupop::oMsat(ostream& sortie) const{
318 //format d'entr�e pour :
319 //distances microsat de D. Goldstein
320
321 if (!(f_verifnum(2))) {
322 //les noms d'alleles ne correspondent pas a des tailles de microsat
323 throw Anomalie(2);
324 }
325
326 // 1 ligne = 1 allele, d'un locus d'un individu
327
328 long i,j,k,l , nbind, nbloc(get_nbloc()), nbpop(get_nbpop());
329 long nploidie(get_nploidie());
330 string nomind;
331 string nomloc;
332 string nomall;
333
334 Individu * Pind;
335
336 for (i=0; i< nbpop;i++) {//scan des pops
337 nbind = _tabPpop[i]->get_nbind();
338 for (j=0; j< nbind;j++) {// scan des individus
339 // nomind = _tabPpop[i]->_tabPind[j]->get_nom();
340 Pind = _tabPpop[i]->_tabPind[j];
341 nomind = Pind->get_nom();
342
343 for (k=0; k < nbloc;k++) {// scan des locus
344 nomloc = _tabPloc[k]->_nom;
345
346 for (l=0; l < nploidie;l++) {// scan des alleles
347 nomall = Pind->get_nom_all(k,l);
348 //nom de l'ind, nom de la pop, nom du locus, les 2 all�les
349 sortie << nomind << " " << nomloc << " " << nomall << endl;
350 }
351 }
352 }
353 }
354 }
355
356 void Jeupop::oMsatFreq(ostream& sortie) const{
357 //format d'entr�e pour :
358 //distances microsat de D. Goldstein
359
360 if (!(f_verifnum(2))) {
361 //les noms d'alleles ne correspondent pas a des tailles de microsat
362 throw Anomalie(2);
363 }
364
365 // 1 ligne = 1 allele, d'un locus d'un individu
366
367 long i,k,l , nball, nbloc(get_nbloc()), nbpop(get_nbpop());
368 // long nploidie(get_nploidie());
369 string nompop;
370 string nomloc;
371 string nomall;
372
373 // Individu * Pind;
374
375 for (i=0; i< nbpop;i++) {//scan des pops
376 nompop = _tabPpop[i]->get_nom();
377
378
379 for (k=0; k < nbloc;k++) {// scan des locus
380 nomloc = _tabPloc[k]->_nom;
381 nball = _tabPloc[k]->get_nball();
382
383 for (l=0; l < nball;l++) {// scan des alleles
384 nomall = _tabPloc[k]->_tabPall[l]->get_Nom();
385 //nom de l'ind, nom de la pop, nom du locus, les 2 all�les
386 sortie << nompop << " " << nomloc << " " << nomall << endl;
387 sortie << " " << _tabPpop[i]->r_nbcopall(_tabPloc[k]->_tabPall[l]) << endl;
388 }
389 }
390 }
391 }
392
393 /*int Jeupop::r_allcommun(Individu * pind1, Individu * pind2, unsigned int numlocus) const{
394 //renvoi du nombre d'all�les en commun
395 // fonctionne pour N ploides
396 Vecteur<Allele*> tabPall1;
397 unsigned int i,j;
398 long pos;
399
400 j = numlocus * _nploidie;
401
402 for (i = 0; i < _nploidie; i++) {
403 tabPall1.push_back(pind1->_tabPall[i+j]);
404 }
405
406 for (i = 0; i < _nploidie; i++) {
407 pos = tabPall1.Position(pind2->_tabPall[i+j]);
408 if (pos != -1) tabPall1.Suppr(pos);
409 }
410
411 return (_nploidie - tabPall1.size());
412 }
413 */
414 /*
415 int Jeupop::r_allcommunnul(Individu * pind1, Individu * pind2, int numlocus) const{
416 //renvoi du nombre d'all�les en commun
417 // saute les all�les nuls
418 // fonctionne pour N ploides
419 Vecteur<Allele*> tabPall1;
420 unsigned int i,j;
421 int pos;
422
423 j = numlocus * _nploidie;
424
425 for (i = 0; i < _nploidie; i++) {
426 tabPall1.push_back(pind1->_tabPall[i+j]);
427 }
428
429 for (i = 0; i < _nploidie; i++) {
430 if (pind2->_tabPall[i+j]->_nul) {}
431 else {
432 pos = tabPall1.Position(pind2->_tabPall[i+j]);
433 if (pos != -1) tabPall1.Suppr(pos);
434 }
435 }
436
437 return (_nploidie - tabPall1.size());
438 }
439 */
440 /*
441 long Jeupop::r_allcommunnul(Individu * pind1, Individu * pind2) const{
442 //renvoi du nombre d'all�les en commun
443 // saute les all�les nuls
444 // fonctionne pour N ploides
445 Vecteur<Allele*> tabPall1;
446 int i,pos;
447 long taille;
448
449 taille = pind2->_tabPall.size();
450
451 tabPall1 = pind1->_tabPall;
452
453 for (i = 0; i < taille; i++) {
454 if (pind2->_tabPall[i]->_nul) {}
455 else {
456 pos = tabPall1.Position(pind2->_tabPall[i]);
457 if (pos != -1) tabPall1.Suppr(pos);
458 }
459 }
460
461 return (taille - tabPall1.size());
462 }
463 */
464
465 void Jeupop::f_rarefaction(MatriceLD& resultat, int effectif) const {
466
467 //calcul de la nouvelle diversit� all�lique, pour un �chantillon
468 // r�duit: r�sultats stock�s dans Matrice, int => effectif r�duit
469 long i,j;
470 // char temp[10];
471 int nbloc(get_nbloc());
472 int nbpop(get_nbpop());
473
474 resultat.resize(nbpop,nbloc);
475
476 resultat._titre.push_back("Nouvelle diversit� all�lique pour un effectif r�duit de : ");
477
478 // resultat._titre[resultat._titre.size()-1] += itoa(effectif,temp,10);
479 resultat._titre[resultat._titre.size()-1].AjEntier(effectif);
480 resultat.SetType(1);
481 resultat.SetFlag(3);
482
483 for (i=0;i<nbloc;i++) {
484 resultat._tcol[i] = _tabPloc[i]->_nom;
485 }
486
487 for (j=0;j<nbpop;j++) {
488 resultat._tlig[j] = _tabPpop[j]->_nom;
489
490 for (i=0;i<nbloc;i++) {
491 resultat.GetCase(j,i) = _tabPpop[j]->r_rare(effectif,i);
492 // cerr << "coucou";
493 }
494 }
495 }
496
497
498
499 const string & Jeupop::get_nomlocus(long nblocus) const {
500 // donne le nom du locus nblocus
501 return (_tabPloc[nblocus]->get_nom());
502 }
503
504 const string & Jeupop::get_nompop(long nbpop) const{
505 // donne le nom de la populations nbpop
506 return (_tabPpop[nbpop]->get_nom());
507
508 }
509
510 Locus* Jeupop::get_Plocus(const string & nom) const{
511 // donne un pointeur sur le locus du nom 'nom'
512 Locus* res;
513 unsigned long i;
514
515 res = 0;
516 for (i=0; i < _tabPloc.size(); i++) {
517 if(_tabPloc[i]->_nom == nom) {
518 res = _tabPloc[i];
519 break;
520 }
521 }
522 if (res == 0) throw Anomalie(8);
523
524 return (res);
525 }
526
527 Locus* Jeupop::get_Plocus(unsigned long i) const {
528 // donne le pointeur sur le locus i
529 if (i >= _tabPloc.size()) throw Anomalie(8);
530
531 return (_tabPloc[i]);
532 }
533
534 Population* Jeupop::get_Ppop(string & nom) const{
535 // donne un pointeur sur le locus du nom 'nom'
536 Population* res;
537 unsigned long i;
538
539 res = 0;
540 for (i=0; i < _tabPpop.size(); i++) {
541 if(_tabPpop[i]->_nom == nom) {
542 res = _tabPpop[i];
543 break;
544 }
545 }
546 if (res == 0) throw Anomalie(9);
547
548 return (res);
549 }
550
551 Population* Jeupop::get_Ppop(unsigned long p) const{
552 // donne un pointeur sur la population p
553 if (p >= _tabPpop.size()) throw Anomalie(9);
554
555 return (_tabPpop[p]);
556 }
557
558 /*
559 long double Jeupop::r_dist2pop(long double ** tabFreq1, long double ** tabFreq2, long * tabNbAll, Population * ppop1, Population * ppop2, int methode) const {
560 //renvoi de la distance entre 2 Populations
561 long double resultat(0);
562 long double somme;
563 long nbpop1(ppop1->get_nbind()), nbpop2(ppop2->get_nbind());
564 long calcsomme(ppop1->get_nbind() * ppop2->get_nbind());
565 long i,j;
566
567 if (methode < 100) {
568 //distance entre populations bas�e sur distances entre individus
569 somme = 0;
570 for (i=0; i < nbpop1; i++) { //scan des individus de la pop1
571 for (j=0; j < nbpop2; j++) { //scan des individus de la pop2
572 try {
573 somme += r_dist2ind(ppop1->get_Pind(i), ppop2->get_Pind(j), methode);
574 }
575 catch (Anomalie erreur) {
576 if (erreur.le_pb == 6) calcsomme--;
577 }
578 }
579 }
580
581 if (calcsomme < 1) return (0);
582 resultat = somme / calcsomme;
583 }
584 else {
585 switch (methode) {
586 case 101:
587 // Nei's minimum genetic distance (1972)
588 resultat = r_dpopNei(tabFreq1, tabFreq2, tabNbAll, false);
589 break;
590 case 102:
591 // Nei's standard genetic distance (1972)
592 resultat = r_dpopNei(tabFreq1, tabFreq2, tabNbAll, true);
593 break;
594 case 103:
595 // Cavalli-Sforza and Edwards' (1967)
596 resultat = r_dpopCavalli(tabFreq1, tabFreq2, tabNbAll);
597 break;
598
599 case 104:
600 // Nei et al's, Da (1983)
601 resultat = r_dpopNeiDa(tabFreq1, tabFreq2, tabNbAll);
602 break;
603 case 106:
604 // Goldstein et al. (1995a)
605 resultat = r_dpopGoldsteinMu(tabFreq1, tabFreq2, tabNbAll);
606 break;
607 case 107:
608 // Latter's Fst (1972)
609 resultat = r_dpopFst(tabFreq1, tabFreq2, tabNbAll);
610 break;
611 case 108:
612 // Prevosti et al.'s, Cp (1975)
613 resultat = r_dpopPrevosti(tabFreq1, tabFreq2, tabNbAll);
614 break;
615 case 109:
616 // Rogers', Dr (1972)
617 resultat = r_dpopRoger(tabFreq1, tabFreq2, tabNbAll);
618 break;
619 case 110:
620 // Goldstein et al. (1995b) Slatkin 1995
621 resultat = r_dpopGoldsteinASD(tabFreq1, tabFreq2, tabNbAll);
622 break;
623 case 111:
624 // Shriver et al's, Dsw (1995)
625 resultat = r_dpopShriver(tabFreq1, tabFreq2, tabNbAll);
626 break;
627
628 }
629 }
630
631 return resultat;
632 }
633 */
634
635
636 istream& operator>>(istream& ientree,Jeupop& LesPops) {
637 int nbformat(5),i(1);
638 char car;
639 bool autre(true);
640
641 ientree.get(car);
642 if (ientree.fail()) {
643 cerr << _("The file is not readable... ") << endl;
644 throw Jeupop::Anomalie(5);
645 }
646 while ((autre) && (i < nbformat)) {
647 try {
648 ientree.clear();
649 ientree.seekg(0);
650 LesPops.iFichier(ientree,i);
651
652 autre = false;
653 }
654 catch (Jeupop::Anomalie erreur) {
655 if (erreur.le_pb > 0) {
656 autre = true;
657 if (i == (nbformat-1)) {
658 cerr << _("unknown file format") << endl;
659 autre = false;
660 throw Jeupop::Anomalie(5);
661 }
662 erreur.le_pb = 0;
663 }
664 }
665 i++;
666 }
667
668 return (ientree);
669 }
670
671 void Jeupop::iFichier(istream & entree, int i) {
672 //cerr << "Jeupop::iFichier debut " << i << endl;
673 switch (i) {
674 case 1:
675 iPopulationsXML(entree);
676 // set_nploidie(_nploidie);
677 return;
678 case 2:
679 iGenepop(entree);
680 // set_nploidie(_nploidie);
681 return;
682 case 3:
683 iPopulations(entree);
684 // set_nploidie(_nploidie);
685 return;
686 case 4:
687 iGenetix(entree);
688 // set_nploidie(_nploidie);
689 return;
690 default:
691 return;
692 }
693 }
694
695
696 void Jeupop::iGenepop(istream &fichier){
697 //lecture de fichiers au format genepop
698 // pas adapt�e � un traitement sur fichiers haploides
699
700 //cerr << "Jeupop::iGenepop debut" << endl;
701 ChaineFicPop lecturePop(fichier);
702 biolib::vecteurs::ChaineCar mot;
703 biolib::vecteurs::ChaineCar nomall1;
704 biolib::vecteurs::ChaineCar nomall2;
705
706 Titre titres;
707
708 Population * Ppop;
709 Individu * Pind;
710
711 unsigned int nbloc,i,j;
712 unsigned int nbdigits(0);
713
714 //cerr << "Jeupop::iGenepop debut2" << endl;
715 // lecture des commentaires
716 try {
717 lecturePop.get_titre(_commentaires);
718 }
719 catch (ChaineFicPop::Anomalie pb) {
720 throw Anomalie(4);
721 }
722 //cerr << "Jeupop::iGenepop get_titre " << _commentaires[0] << endl;
723
724 reset();
725
726 // lecture des locus
727 try {
728 lecturePop.get_locus(titres);
729 }
730 catch (ChaineFicPop::Anomalie pb) {
731 throw Anomalie(4);
732 }
733 nbloc = titres.size();
734
735 for (i=0; i < nbloc; i++) {
736 _tabPloc.push_back(new Locus(this, 0));
737 _tabPloc.back()->set_nom(titres[i]);
738 }
739 //cerr << "Jeupop::iGenepop locus " << titres[0] << " " << nbloc << " " << _tabPloc.size() << endl;
740
741
742 //lecture des populations
743 // _nploidie = 2;
744 set_nploidie(2);
745
746 while (lecturePop.estunepop()) {
747 //creation d'une nouvelle population
748 //cerr << "Jeupop::iGenepop get_nompop(mot) " << mot << endl;
749 lecturePop.get_nompop(mot);
750 //cerr << "Jeupop::iGenepop get_nompop(mot) " << mot << endl;
751
752 Ppop = NewPop(mot);
753 //cerr << "Jeupop::iGenepop get_nompop(mot) " << mot << endl;
754
755 try {
756 if (lecturePop.get_lignepop()==1) break;
757 }
758 catch (ChaineFicPop::Anomalie pb) {
759 throw Anomalie(4);
760 }
761 while (lecturePop.estunindividu()) {
762 //creation d'un nouvel individu
763 lecturePop.get_nomind(mot);
764 Pind = new Individu(_tabPpop.back(), mot);
765 Ppop->AjouterIndividu(Pind);
766
767 lecturePop.get_alleles(titres);
768
769 if (titres.size() != nbloc) {//erreur
770 cout << endl;
771 cout << _("error in data file.") << endl;
772 cout << _("wrong number of locus at line : ") << lecturePop.get_numeroligne() << endl;
773
774 throw Anomalie(4);
775 }
776
777 //enregistrement de chaque locus
778 for (i=0; i < nbloc; i++) {
779 mot = titres[i];
780 if (nbdigits == 0) nbdigits = (mot.size() / 2);
781 if (nbdigits == 1) {
782 throw Anomalie(4);
783 }
784 if (mot.size() != (nbdigits * 2)) {
785 throw Anomalie(4);
786 }
787 if (mot.EstUnChiffre() == false) {
788 throw Anomalie(4);
789 }
790
791 nomall1.assigner(mot, 0, nbdigits - 1);
792 nomall2.assigner(mot, nbdigits, (nbdigits * 2) - 1);
793
794 Pind->_tabPall[i*2] = _tabPloc[i]->getPall(nomall1);
795 if (Pind->_tabPall[i*2] == 0) {
796 Pind->_tabPall[i*2] = new Allele(_tabPloc[i], nomall1);
797 _tabPloc[i]->_tabPall.push_back(Pind->_tabPall[i*2]);
798 }
799
800 Pind->_tabPall[i*2 + 1] = _tabPloc[i]->getPall(nomall2);
801 if (Pind->_tabPall[i*2 + 1] == 0) {
802 Pind->_tabPall[i*2 + 1] = new Allele(_tabPloc[i], nomall2);
803 _tabPloc[i]->_tabPall.push_back(Pind->_tabPall[i*2 + 1]);
804 }
805 }
806
807 try {
808 if (lecturePop.get_lignepop()==1) break;
809 // lecturePop.get_lignepop();
810 }
811 catch (ChaineFicPop::Anomalie pb) {
812 throw Anomalie(4);
813 }
814 }//lecture est un individu
815 }
816
817 // traitement sp�cifique au cas des fichiers JM Cornuet
818 if (lecturePop.get_boolnompop() == false) { //il n'y avait pas de noms de pop
819 // derriere le mot cle pop => tentative d'extraction style JMC
820 //cerr << "coucou";
821
822 // lecture des noms de populations dans les commentaires
823 for (i=0,j=0; i < _commentaires.size(); i++) {
824 //cerr << "coucou " << _commentaires[i].GetNbMots(",") << " " << _tabPpop.size();
825 if (_commentaires[i].GetNbMots(",") == _tabPpop.size()) {
826 //extraction des noms;
827 for (j=0; j < _tabPpop.size(); j++) {
828 _commentaires[i].GetMot(j+1,_tabPpop[j]->_nom,",");
829 }
830 break;
831 }
832 }
833
834 if (j == 0) {
835 //il n'y a vraiment pas de noms... on num�rote
836 for (i=0; i < _tabPpop.size(); i++) {
837 if (_tabPpop[i]->_nom == "") {
838 ostringstream tampon;
839 tampon << "POP_";
840 tampon << (i+1);
841 _tabPpop[i]->_nom = tampon.str();
842 }
843 }
844 }
845 }
846 }
847
848
849 void Jeupop::iPopulations(istream &fichier){
850 //lecture de fichiers au format Populations
851 // adapt�s � un traitement sur fichiers haploides
852 //
853 // modification du 31/07/2000:
854 // rajout du mot cl� "DIFF" (nom d'all�le) qui permet de compter comme diff�rents 2 all�les
855 // utile pour le traitement de donn�es RFLP
856 //
857 // refait le 07/9/2000
858
859 ChaineFicPop lecturePop(fichier);
860 biolib::vecteurs::ChaineCar mot;
861 biolib::vecteurs::ChaineCar nomall;
862
863 Titre titres;
864
865 Population * Ppop;
866 Individu * Pind;
867 Allele * Palldiff;
868 Allele * Pall;
869
870 bool allestintrogressant(false);
871
872 unsigned int nbloc,i, j;
873
874 // lecture des commentaires
875 try {
876 lecturePop.get_titre(_commentaires);
877 }
878 catch (ChaineFicPop::Anomalie pb) {
879 throw Anomalie(4);
880 }
881 //cerr << "Jeupop::iGenepop get_titre " << _commentaires[0] << endl;
882
883 reset();
884
885 // lecture des locus
886 try {
887 lecturePop.get_locus(titres);
888 }
889 catch (ChaineFicPop::Anomalie pb) {
890 throw Anomalie(4);
891 }
892 _nbloc = nbloc = titres.size();
893
894 for (i=0; i < nbloc; i++) {
895 _tabPloc.push_back(new Locus(this, 0));
896 _tabPloc.back()->set_nom(titres[i]);
897 }
898 //cerr << "Jeupop::iGenepop locus " << titres[0] << " " << nbloc << " " << _tabPloc.size() << endl;
899
900
901 //lecture des populations
902 _nploidie = 0; //pas encore d�termin�
903
904 while (lecturePop.estunepop()) {
905 //creation d'une nouvelle population
906 lecturePop.get_nompop(mot);
907 Ppop = NewPop(mot);
908
909 try {
910 if (lecturePop.get_lignepop()==1) break;
911 // lecturePop.get_lignepop();
912 }
913 catch (ChaineFicPop::Anomalie pb) {
914 throw Anomalie(4);
915 }
916 while (lecturePop.estunindividu()) {
917 //creation d'un nouvel individu
918 //
919 lecturePop.get_nomind(mot);
920
921 Pind = new Individu(Ppop, mot);
922 Ppop->AjouterIndividu(Pind);
923
924 //cerr << "Jeupop::iGenepop lecture: " <<lecturePop << endl;
925 // lecturePop.get_nomind(Pind->_nom);
926 //cerr << "Jeupop::iGenepop individu: " << Pind->_nom << endl;
927
928
929 lecturePop.get_alleles(titres);
930 //cerr << "Jeupop::iGenepop alleles : " << titres.size() << endl;
931 //for (j =0 ; j < titres.size(); j++) cerr << titres[j] << " ";
932 if (titres.size() != nbloc) {//erreur
933 cout << endl;
934 cout << _("error in data file.") << endl;
935 cout << _("wrong number of locus at line : ") << lecturePop.get_numeroligne() << endl;
936
937 throw Anomalie(4);
938 }
939 //cerr << "Jeupop::iPopulations nbloc " <<nbloc << endl;
940 //enregistrement de chaque locus
941 for (i=0; i < nbloc; i++) {
942 mot = titres[i];
943 //cerr << "Jeupop::iPopulations mot " << mot << endl;
944 if (_nploidie == 0) {
945 set_nploidie(mot.GetNbMots(":"));
946 //Ppop->set_nploidie();
947 }
948
949 //cerr << "Jeupop::iPopulations set_nploidie : " << mot.GetNbMots(":") << " "<< _nploidie << endl;
950 if (mot.GetNbMots(":") != _nploidie) {//erreur
951 cout << endl;
952 cout << _("error in data file.") << endl;
953 cout << _("wrong number of alleles at line : ") << lecturePop.get_numeroligne() << endl;
954
955 throw Anomalie(4);
956 }
957
958 Palldiff = 0;
959 for (j = 0; j < _nploidie; j++) { //boucle pour chaque allele d'un locus
960 Pall = 0;
961 allestintrogressant = false;
962
963 mot.GetMot(j+1 ,nomall, ":");
964 //cas des alleles introgressants:
965 // marqu�s par une *
966 if (nomall[0] == '*') {
967 //cerr << "Jeupop::iPopulations all introgressant" << endl;
968 allestintrogressant = true;
969 nomall.erase(0,1);
970 }
971 //cerr << "Jeupop::iPopulations nomall" << nomall << endl;
972
973 if ((Palldiff == 0) && (nomall == "DIFF")) {
974 // creation d'un allele diff�rent pour ce locus pour cet individu
975 Pall = Palldiff = new Allele(_tabPloc[i], "DIFF");
976 Pind->_tabPall[i*_nploidie + j] = Palldiff;
977 _tabPloc[i]->_tabPall.push_back(Palldiff);
978 }
979 else {
980 if (nomall == "DIFF") {
981 // assignation de l'all�le diff�rent
982 Pind->_tabPall[i*_nploidie + j] = Pall = Palldiff;
983 }
984 else {
985 // recherche de l'allele
986 Pall = Pind->_tabPall[i*_nploidie + j] = _tabPloc[i]->getPall(nomall);
987
988 if (Pall == 0) {
989 // si la recherche n'a rien donner: creation de l'all�le
990 Pall = Pind->_tabPall[i*_nploidie + j] = new Allele(_tabPloc[i], nomall);
991 _tabPloc[i]->_tabPall.push_back(Pall);
992
993 }
994 }
995 }
996
997 if (allestintrogressant) {
998 // si il n'est pas deja not� dans le tableau de la pop, l'indiquer
999 //cerr << "Jeupop::iPopulations all introgressant" << endl;
1000 Ppop->AjouterAllIntrogressant(Pall);
1001 // allestintrogressant = false;
1002 }
1003 }
1004
1005 }
1006
1007 //Pind->affiche(cout,1);
1008
1009 try {
1010 if (lecturePop.get_lignepop()==1) break;
1011 // lecturePop.get_lignepop();
1012 }
1013 catch (ChaineFicPop::Anomalie pb) {
1014 throw Anomalie(4);
1015 }
1016 } // boucle while (lecturePop.estunepop())
1017 } // boucle while (lecturePop.estunepop())
1018
1019 // lecture des noms de populations dans les commentaires
1020
1021 /* for (i=0; i < _tabPpop.size(); i++) {
1022 if (_tabPpop[i]->_nom == "_") {
1023 _tabPpop[i]->_nom = "POP_";
1024 _tabPpop[i]->_nom.AjEntier(i+1);
1025 }
1026 }*/
1027 }
1028
1029 bool Jeupop::f_verifnum(int typedenum) const {
1030 // V�rification du type de num�rotation des all�les
1031 // 1 => type Genepop stricte (2digits)
1032 // 2 => nom d'alleles = num�ros
1033 // 3 => nom d'alleles = num�ros < 999
1034 //Anomalie 2-> les num�ros d'all�les format genepop ne sont pas pr�sents
1035
1036 long nbloc(get_nbloc());
1037 long i;
1038
1039 for (i=0; i < nbloc; i++) {
1040 if (!(_tabPloc[i]->f_verifnum(typedenum))) return(false);
1041 }
1042
1043 return(true);
1044 }
1045
1046 void Jeupop::f_rempliVcalcStrucPop(unsigned int niveau) {
1047 // remplir le tableau _VcalcStrucPop,
1048 // en vue de calcul sur des m�tapopulations (ex: DistGnt)
1049 unsigned long i;
1050
1051 f_resetVcalcStrucPop();
1052 //cerr << "Jeupop::f_rempliVcalcStrucPop(unsigned int niveau) debut" << endl;
1053 // if (_VcalcStrucPop.size() != 0) return;
1054
1055 if (niveau == 100) {
1056 //Populations normales
1057 _VcalcStrucPop.resize(_tabPpop.size());
1058
1059 for (i = 0; i < _VcalcStrucPop.size(); i++) _VcalcStrucPop[i] = _tabPpop[i];
1060 return;
1061
1062 }
1063 //cerr << "Jeupop::f_rempliVcalcStrucPop(unsigned int niveau) " << niveau << endl;
1064 _Pracinepops->f_rempliTabStrucPop(_VcalcStrucPop, niveau);
1065
1066 //cerr << "Jeupop::f_rempliVcalcStrucPop(unsigned int niveau)" << endl;
1067 //for (i = 0; i < _VcalcStrucPop.size(); i++) cerr << _VcalcStrucPop[i]->get_nom();
1068
1069 }
1070
1071 void Jeupop::f_trad2Gpop(ostream& tableCorrespondances) {
1072
1073 long nbloc(get_nbloc());
1074 long i;
1075
1076 // 1-> op�ration possible seulement sur les diploides
1077 if (get_nploidie() != 2) throw Anomalie(1);
1078
1079 //verification sur le nombre d'alleles maximum (99)
1080 //pas fait
1081 tableCorrespondances << "correspondances Anciens_Noms <---> Noms_Genepop" << endl;
1082 for (i=0; i < nbloc; i++) {
1083 _tabPloc[i]->f_trad2Gpop(tableCorrespondances);
1084 }
1085 }
1086
1087 void Jeupop::ifAjouterLocus(const Locus * Ploc) {
1088 //ajout d'un locus dans une pop, avec les alleles correspondants
1089 // => recherche un locus preexistant
1090 long i, taille(_tabPloc.size()), nbloc(-1);
1091 string nomloc;
1092 nomloc = Ploc->get_nom();
1093
1094 for (i = 0 ; i < taille; i++) {
1095 if (nomloc == get_nomlocus(i)) {
1096 nbloc = i;
1097 break;
1098 }
1099 }
1100 if (nbloc >= 0) { //le locus existe deja
1101 //il faut ajouter les alleles que l'original n'a pas
1102 taille = Ploc->get_nball();
1103 for (i = 0; i < taille; i++ ){
1104 get_Plocus(nbloc)->ifAjouterAllele(Ploc->getPall(i));
1105 }
1106 }
1107 else { //il faut le creer, avec les alleles du nouveau locus
1108 _tabPloc.push_back(new Locus(*Ploc, this));
1109 }
1110
1111 _nbloc = _tabPloc.size(); //precaution
1112 }
1113
1114 /*void Jeupop::ifAjouterPop(const Population * Ppop) {
1115 //ajout d'un population dans un jeupop, avec les individus correspondants
1116 // => recherche d'une population preexistante
1117 long i, taille(get_nbpop()), nbpop(-1);
1118 const string nompop(Ppop->get_nom());
1119
1120 for (i = 0 ; i < taille; i++) {
1121 if (nompop == get_nompop(i)) {
1122 nbpop = i;
1123 break;
1124 }
1125 }
1126 if (nbpop >= 0) { //la population existe deja
1127 //il faut ajouter les individus que l'original n'a pas
1128 // et completer les alleles des individus preexistants
1129 // pb � r�soudre: incoh�rence entre alleles d'un meme locus,
1130 // d'un meme individu...
1131 get_pop(nbpop)->_nploidie = get_nploidie();
1132 get_pop(nbpop)->_nbloc = 0;// _Pjeu->get_nbloc();
1133
1134 get_pop(nbpop)->resize_loc();
1135
1136 taille = Ppop->get_nbind();
1137 for (i = 0; i < taille; i++ ){
1138
1139 //cerr << "if ajouterpop" << endl;
1140 get_pop(nbpop)->ifAjouterIndividu(Ppop->get_Pind(i));
1141 }
1142 }
1143 else { //il faut creer une nouvelle population
1144 _tabPpop.push_back(new Population(this, *Ppop));
1145 }
1146 }
1147
1148 */
1149 /*
1150 Jeupop Jeupop::operator+ (const Jeupop &rval) const {
1151 long i , taille;
1152
1153 if (get_nploidie() != rval.get_nploidie()) throw Anomalie(7);
1154 // if (_nc != rval._nc) throw Anomalie(4);
1155 // for (i=0; i < rval._commentaires.size(); i++) {
1156 //cerr << rval.GetTitre(i);
1157 // _commentaires.push_back(rval._commentaires.GetTitre(i));
1158 // }
1159
1160 //cerr << _commentaires[0] << " " << _commentaires[1];
1161
1162 Jeupop Resultat;
1163 //on peut fixer la ploidie, et commencer � remplir Resultat
1164 // avec le 1er jeu de populations:
1165 //cerr << "operator + debut " << endl;
1166 Resultat = *this;
1167
1168 Resultat._commentaires = _commentaires + rval._commentaires;
1169 // Il faut savoir combien de locus differents comportera le r�sultat
1170 // => parcours du tableau de locus du 2eme JeuPop
1171 taille = rval.get_nbloc();
1172 for (i = 0; i < taille; i++) {
1173 //cerr << "ajout locus " << i << endl;
1174 Resultat.ifAjouterLocus(rval.get_Plocus(i));
1175 }
1176
1177 // A ce stade, Resultat contient tout le 1er jeupop
1178 // et tous les locus et alleles du 2eme
1179 // Il faut maintenant remplir les populations de resultat
1180 taille = rval.get_nbpop();
1181 for (i = 0; i < taille; i++) {
1182 //cerr << "ajout pop " << i << endl;
1183 Resultat.ifAjouterPop(rval.get_pop(i));
1184 }
1185
1186 taille = Resultat.get_nbpop();
1187 for (i = 0; i < taille; i++) {
1188 //cerr << "resize " << i << endl;
1189 Resultat.get_pop(i)->resize_loc();
1190 }
1191
1192 //cerr << "operator + fin " << endl;
1193 return Resultat;
1194
1195 }*/
1196
1197
1198 void Jeupop::f_BootstrapLocus(Arbre &arbreRef, int methodeArbre, int methodeDist, bool square_distance, int nbrepet,bool sur_ind, Vecteur<unsigned int> * PVcalcLocus) {
1199 // Boostrap sur les locus => arbre d'individus si sur_ind est vrai
1200 // Boostrap sur les locus => arbre de populations si sur_ind est faux
1201 // ********creer des exceptions pour v�rifier l'arbreRef *********
1202
1203 DistancesGnt distgnt(methodeDist, square_distance, this);
1204 unsigned long i;
1205
1206 // pr�paration de _VcalcLoc:
1207 if (PVcalcLocus == NULL) {
1208 PVcalcLocus = new Vecteur<unsigned int>;
1209 _VcalcLocus.resize(get_nbloc());
1210 PVcalcLocus->resize(get_nbloc());
1211 for (i = 0; i < _VcalcLocus.size(); i ++) {
1212 _VcalcLocus[i] = i;
1213 PVcalcLocus->operator[](i) = i;
1214 }
1215 }
1216
1217 try {
1218 distgnt.f_bootstrapLocus(arbreRef, methodeArbre, nbrepet, PVcalcLocus, sur_ind);
1219 }
1220 catch (DistancesGnt::Anomalie lepb) {
1221 cerr << endl << lepb.fmessage() << endl;
1222 throw Anomalie(101);
1223 }
1224 }
1225
1226 void Jeupop::f_BootstrapIndividus(Arbre &arbreRef, int methodeArbre, int methodeDist, bool square_distance, int nbrepet, Vecteur<unsigned int> * PVcalcLocus) {
1227 // Boostrap sur les locus => arbre d'individus si sur_ind est vrai
1228 // Boostrap sur les locus => arbre de populations si sur_ind est faux
1229 // ********creer des exceptions pour v�rifier l'arbreRef *********
1230
1231 DistancesGnt distgnt(methodeDist, square_distance, this);
1232 unsigned long i;
1233
1234 // pr�paration de _VcalcLoc:
1235 if (PVcalcLocus == NULL) {
1236 PVcalcLocus = new Vecteur<unsigned int>;
1237 _VcalcLocus.resize(get_nbloc());
1238 PVcalcLocus->resize(get_nbloc());
1239 for (i = 0; i < _VcalcLocus.size(); i ++) {
1240 _VcalcLocus[i] = i;
1241 PVcalcLocus->operator[](i) = i;
1242 }
1243 }
1244
1245 try {
1246 distgnt.f_bootstrapIndividus(arbreRef, methodeArbre, nbrepet, PVcalcLocus);
1247 }
1248 catch (DistancesGnt::Anomalie lepb) {
1249 cerr << endl << lepb.fmessage() << endl;
1250 throw Anomalie(101);
1251 }
1252 }
1253
1254 void Jeupop::f_distgntpop (MatriceLD &resultat,int methode, bool square_distance, unsigned int niveau, Vecteur<unsigned int> *PVcalcLocus) {
1255 //cerr << "Jeupop::f_distgntpop debut" << endl;
1256 //distances G�n�tiques entre Populations
1257 DistancesGnt distgntpop(methode, square_distance, this, niveau);
1258 unsigned long i;
1259
1260 // pr�paration de _VcalcLoc:
1261 if (PVcalcLocus == NULL) {
1262 _VcalcLocus.resize(get_nbloc());
1263 for (i = 0; i < _VcalcLocus.size(); i ++) {
1264 _VcalcLocus[i] = i;
1265 }
1266 }
1267 else {
1268 _VcalcLocus = *PVcalcLocus;
1269 }
1270 //cerr << "Jeupop::f_distgntpop 1" << endl;
1271
1272 try {
1273 distgntpop.f_distgntpop(resultat, &_VcalcLocus);
1274 }
1275 catch (DistancesGnt::Anomalie lepb) {
1276 cerr << endl << lepb.fmessage() << endl;
1277 throw Anomalie(101);
1278 }
1279
1280 //cerr << "Jeupop::f_distgntpop fin" << endl;
1281
1282 }
1283
1284 void Jeupop::f_distgnt (MatriceLD &resultat,int methode, bool square_distance, Vecteur<unsigned int> *PVcalcLocus) {
1285 //distances G�n�tiques entre individus
1286 DistancesGnt distgntind(methode, square_distance, this);
1287 unsigned long i;
1288 // pr�paration de _VcalcLoc:
1289 //cerr << "f_distgnt (jeupop) deb" << endl;
1290 if (PVcalcLocus == NULL) {
1291 _VcalcLocus.resize(get_nbloc());
1292 for (i = 0; i < _VcalcLocus.size(); i ++) {
1293 _VcalcLocus[i] = i;
1294 }
1295 }
1296 else {
1297 _VcalcLocus = *PVcalcLocus;
1298 }
1299
1300 // f_distgntVcalc (resultat, methode);
1301
1302 try {
1303 distgntind.f_distgntInd(resultat, &_VcalcLocus);
1304 }
1305 catch (DistancesGnt::Anomalie lepb) {
1306 cerr << endl << lepb.fmessage() << endl;
1307 throw Anomalie(101);
1308 }
1309 //cerr << "f_distgnt (jeupop) fin" << endl;
1310
1311 }
1312
1313
1314 /*long Jeupop::get_numloc(Locus * Ploc) const {
1315 long i;
1316
1317 for (i= 0; i < _nbloc; i++) {
1318 // cerr << "getnumloc" << endl;
1319 if (_tabPloc[i] == Ploc) return (i);
1320 }
1321 return (-1);
1322 }*/
1323
1324
1325
1326 Population* Jeupop::get_pop(unsigned long numpop) const{
1327 if (numpop < get_nbpop()) { //ok
1328 return(_tabPpop[numpop]);
1329 }
1330 else {
1331 return (0);
1332 }
1333 }
1334
1335 unsigned int Jeupop::get_nploidie() const {
1336 return(_nploidie);
1337 }
1338
1339 unsigned long Jeupop::get_nbloc() const {
1340 return(_tabPloc.size());
1341 }
1342
1343 unsigned long Jeupop::get_nbpop() const {
1344 return(_tabPpop.size());
1345 }
1346
1347 unsigned long Jeupop::get_nbind() const {
1348 return(_Pracinepops->get_nbind());
1349 }
1350
1351 /*Allele* Jeupop::get_Pall(int loc, int all) const {
1352 return(_tabPloc[loc]->_tabPall[all]);
1353 }
1354 */
1355
1356 Allele* Jeupop::get_Pall(unsigned long loc, unsigned long all) const {
1357 return(_tabPloc[loc]->_tabPall[all]);
1358 }
1359
1360 void Jeupop::get_nomniveauxstruc(Titre & nom_niveaux) const {
1361
1362 _Pracinepops->get_nomniveauxstruc(nom_niveaux);
1363
1364 }
1365
1366
1367 void Jeupop::set_nploidie(unsigned int nploidie) {
1368 _nploidie = nploidie;
1369 _nbloc = get_nbloc();
1370
1371 //cerr << "Jeupop::set_nploidie " << _nploidie << " " << _nbloc << endl;
1372 _Pracinepops->set_nploidie();
1373 //cerr << "Jeupop::set_nploidie fin" << _nploidie << " " << _nbloc << endl;
1374 }
1375
1376
1377 void Jeupop::AjouterPopulation(Population * Ppop) {
1378 // _tabPpop.push_back(Ppop);
1379 //AjouterTabPop(Ppop);
1380 _Pracinepops->AjouterPopulation(Ppop);
1381 }
1382
1383 void Jeupop::AjouterTabPop(Population * Ppop) {
1384 _tabPpop.push_back(Ppop);
1385 _nbpop++;
1386 // _Pracinepops->AjouterPopulation(Ppop);
1387 }
1388
1389 Locus * Jeupop::new_locus(const string & name) {
1390 Locus * Plocus (new Locus(this, 0));
1391 Plocus->set_nom(name);
1392 _tabPloc.push_back(Plocus);
1393 return (Plocus);
1394 }
1395
1396 Population * Jeupop::NewPop(const biolib::vecteurs::ChaineCar & chemin) {
1397 // cr�ation de Population et de MetaPop en fonction du chemin
1398 Titre tabchemin;
1399 biolib::vecteurs::ChaineCar mot;
1400 unsigned long i;
1401 MetaPop * Pmetapop(_Pracinepops);
1402 Population * Ppop;
1403
1404 //cerr << "Jeupop::NewPop(ChaineCar & chemin) d�but " << endl;
1405
1406 mot.assign("");
1407
1408 for (i = 0; i < chemin.GetNbMots("/"); i ++) {
1409 chemin.GetMot(i+1,mot, "/");
1410 if (mot == "") continue;
1411 tabchemin.push_back(mot);
1412 }
1413
1414 if (tabchemin.size() > 0) {
1415 for (i = 0; i < tabchemin.size()-1; i ++) {
1416 //cr�er une m�tapopulation
1417 Pmetapop = Pmetapop->NewMetaPop(tabchemin[i]);
1418 mot = tabchemin[tabchemin.size()-1];
1419 }
1420 }
1421
1422 Ppop = new Population(Pmetapop);
1423 //cerr << "Jeupop::NewPop(ChaineCar & chemin) set_nom " << endl;
1424 Ppop->set_nom(mot);
1425
1426 Pmetapop->AjouterPopulation(Ppop);
1427
1428 //cerr << "Jeupop::NewPop(ChaineCar & chemin) fin " << endl;
1429
1430 return (Ppop);
1431 }
1432
1433
1434 void Jeupop::affind (int numpop, int numind) const {
1435 _tabPpop[numpop]->_tabPind[numind]->affiche(cout,1);
1436 }
1437
1438 /** Suppression de la population p du Jeupop */
1439 void Jeupop::SupprPop(StrucPop * Ppop){
1440 //cerr << "Jeupop::SupprPop debut" << endl;
1441 unsigned long i;
1442
1443 _Pracinepops->SupprPop(Ppop);
1444 for (i = 0; i < _tabPpop.size(); i++) {
1445 if (_tabPpop[i] == Ppop) _tabPpop.Suppr(i);
1446 }
1447 _nbpop--;
1448 //cerr << "Jeupop::SupprPop fin" << endl;
1449 }
1450 /** D�place la pop Ppop a � la position b */
1451 void Jeupop::f_deplacePop(Population * Ppop, unsigned long b){
1452 signed long a;
1453 a = _tabPpop.Position(Ppop);
1454 if (a < 0) return;
1455 if (b > _tabPpop.size()) return;
1456 if ((unsigned long)a == b) return;
1457
1458 _tabPpop[a] = _tabPpop[b];
1459 _tabPpop[b] = Ppop;
1460 }
1461 /** enl�ve les all�les qui ne sont pas repr�sent�s dans les populations
1462 */
1463 void Jeupop::f_nettoieAlleles(){
1464 unsigned long l;
1465
1466 for (l = 0; l < _tabPloc.size(); l++) _tabPloc[l]->f_nettoieAlleles();
1467 }
1468
1469 /** Retourne vrai, si l'allele n'est pas pr�sent dans jeupop */
1470 bool Jeupop::r_allelenonpresent(Allele * Pall) const{
1471
1472 return(_Pracinepops->r_allelenonpresent(Pall));
1473 }
1474 /** construit le vecteur Vlocus avec les numeros de tous les locus */
1475 void Jeupop::f_selectlocustous(vector<unsigned int> & Vlocus) const{
1476 unsigned int nblocus(get_nbloc());
1477 unsigned int i;
1478
1479 Vlocus.resize(0);
1480
1481 for (i = 0; i < nblocus; i ++) Vlocus.push_back(i);
1482 }
1483 /** Suppression des locus qui ne sont pas pr�sents dans Vnumloc */
1484 void Jeupop::GarderVlocus(Vecteur<unsigned int> & Vnumloc){
1485
1486 unsigned int i;
1487 _nbloc = _tabPloc.size();
1488
1489 //cerr << "Jeupop::GarderVlocus " << _nbloc << endl ;
1490 for (i = _nbloc; i > 0 ; i--) {
1491 //cerr << "Jeupop::GarderVlocus " << i-1 << endl ;
1492 // si i-1 n'est pas pr�sent dans Vnumloc: on supprime le locus
1493 if (!Vnumloc.Existe(i-1)) supprLocus(i-1);
1494 }
1495 }
1496 /** Suppression d'un locus */
1497 void Jeupop::supprLocus(unsigned int numlocus){
1498 unsigned long i;
1499
1500 if (numlocus >= _tabPloc.size()) throw Anomalie(8);
1501 //suppression du locus chez tous les individus
1502 for (i = 0; i < _Pracinepops->_tabPind.size(); i++) _Pracinepops->_tabPind[i]->supprLocus(numlocus);
1503
1504 delete _tabPloc[numlocus];
1505 _tabPloc.Suppr(numlocus);
1506
1507 set_nbloc();
1508 }
1509 /** R�affectation du nombre de locus */
1510 void Jeupop::set_nbloc(){
1511
1512 _nbloc = _tabPloc.size();
1513 _Pracinepops->set_nbloc();
1514
1515 }
1516 /** obtenir le num�ro d'un locus avec son nom */
1517 signed int Jeupop::get_numloc(const string & nomlocus) const{
1518 signed int numloc(-1);
1519 unsigned int nbloc(get_nbloc()), i;
1520
1521 for (i=0; i < nbloc; i++) {
1522 //cerr << "Jeupop::get_numloc " << nomlocus << " " << _tabPloc[i]->get_nom()<<endl;
1523 if (nomlocus == _tabPloc[i]->get_nom()) {
1524 numloc = i;
1525 break;
1526 }
1527 }
1528
1529 return (numloc);
1530 }
1531
1532
1533 void Jeupop::iGenetix(istream &fichier){
1534 //lecture de fichiers au format Genetix
1535 // pas adapt�e � un traitement sur fichiers haploides ou nploides
1536
1537 //cerr << "Jeupop::iGenetix debut" << endl;
1538 ChaineFicPop objetGenetix(fichier);
1539 Titre titres;
1540 biolib::vecteurs::ChaineCar mot, nomall1, nomall2;
1541
1542 Population * Ppop;
1543 Individu * Pind;
1544
1545 unsigned int nbpop, nbloc, nball(0), nbind, i,j, k;
1546 unsigned int nbdigits(3);
1547
1548 reset();
1549
1550 set_nploidie(2);
1551
1552 objetGenetix.get_lignepop();
1553 objetGenetix.GetMot(1, mot);
1554 nbloc = (int) mot;
1555 if ((nbloc < 1) || (nbloc > 100)) throw Anomalie(5);
1556 _commentaires.push_back(objetGenetix);
1557
1558
1559 //cerr << "Jeupop::iGenetix nbloc " << nbloc << endl;
1560 objetGenetix.get_lignepop();
1561 objetGenetix.GetMot(1, mot);
1562 nbpop = (int) mot;
1563 // fichier >> nbpop;
1564 if ((nbpop < 1) || (nbpop > 1000)) throw Anomalie(5);
1565 _commentaires.push_back(objetGenetix);
1566
1567 //cerr << "Jeupop::iGenetix nbpop " << nbpop << endl;
1568
1569 //lecture et cr�ation des locus et des all�les
1570 for (i=0; i < nbloc; i++) {
1571 _tabPloc.push_back(new Locus(this, 0));
1572 objetGenetix.get_lignepop();
1573 _tabPloc.back()->set_nom(objetGenetix);
1574 //cerr << "Jeupop::iGenetix locus " << objetGenetix << endl;
1575 fichier >> nball;
1576 objetGenetix.get_lignepop();
1577 if (objetGenetix.GetNbMots() != (int) nball) throw Anomalie(5);
1578 for (j=0; j < nball; j++) {
1579 objetGenetix.GetMot(j+1, nomall1);
1580 _tabPloc[i]->_tabPall.push_back(new Allele(_tabPloc[i], nomall1));
1581 }
1582 }
1583 set_nbloc();
1584
1585 for (i=0; i < nbpop; i++) {
1586 objetGenetix.get_lignepop();
1587
1588 Ppop = NewPop(objetGenetix);
1589
1590 //cerr << "Jeupop::iGenetix pop " << objetGenetix << endl;
1591 objetGenetix.get_lignepop();
1592 if (objetGenetix.EstUnChiffre() == false) {
1593 cout << endl;
1594 cout << _("error in Genetix data file.") << endl;
1595 cout << _("wrong number of individuals at line : ") << objetGenetix.get_numeroligne() << endl;
1596
1597 throw Anomalie(201);
1598 }
1599 else nbind = (unsigned int) objetGenetix;
1600
1601 //cerr << "Jeupop::iGenetix nbind " << nbind << endl;
1602 for (j=0; j < nbind; j++) {
1603 //creation d'un nouvel individu
1604 fichier >> mot;
1605 Pind = new Individu(_tabPpop.back(), mot);
1606 Ppop->AjouterIndividu(Pind);
1607
1608 //cerr << "Jeupop::iGenetix ind " << mot << endl;
1609
1610 objetGenetix.get_lignepop();
1611 if (objetGenetix.GetNbMots() != (int) nbloc){
1612 cout << endl;
1613 cout << _("error in Genetix data file.") << endl;
1614 cout << _("wrong number of locus at line : ") << objetGenetix.get_numeroligne() << endl;
1615
1616 throw Anomalie(201);
1617 }
1618
1619 for (k=0; k < nbloc; k++) {
1620 //cerr << "Jeupop::iGenetix nbloc nbmots " << nbloc << " " << objetGenetix.GetNbMots() << endl;
1621 objetGenetix.GetMot(k+1, mot);
1622 if (mot.EstUnChiffre() == false) {
1623 throw Anomalie(5);
1624 }
1625 //cerr << "Jeupop::iGenetix mot " << mot << endl;
1626 nomall1.assigner(mot, 0, nbdigits - 1);
1627 nomall2.assigner(mot, nbdigits, (nbdigits * 2) - 1);
1628
1629 //cerr << "Jeupop::iGenetix all " << nomall1 << " " << nomall2 << endl;
1630 if (nomall1 == "000") Pind->_tabPall[k*2] = _tabPloc[k]->getPallNul();
1631 else Pind->_tabPall[k*2] = _tabPloc[k]->getPall(nomall1);
1632
1633 if (nomall2 == "000") Pind->_tabPall[k*2 + 1] = _tabPloc[k]->getPallNul();
1634 else Pind->_tabPall[k*2 + 1] = _tabPloc[k]->getPall(nomall2);
1635 //cerr << "Jeupop::iGenetix Pall " << Pind->_tabPall[k*2] << " " << Pind->_tabPall[k*2 + 1] << endl;
1636 }
1637 }
1638 }
1639 //cerr << "Jeupop::iGenetix fin "<< endl;
1640 }
1641
1642 void Jeupop::iPopulationsXML(istream &fichier){
1643 #ifdef XMLREADPOPULATIONS_H
1644 //cerr << "Jeupop::iPopulationsXML debut "<< endl;
1645 // string mot;
1646
1647 // fichier >> mot;
1648 //cerr << "Jeupop::iPopulationsXML mot " << mot << endl;
1649 // if (mot != "<?xml") {
1650 // fichier.seekg(0);
1651 // throw Jeupop::Anomalie(5);
1652 // return;
1653 // }
1654 // fichier.seekg(0);
1655
1656 try {
1657
1658 XMLreadPopulations * Plecture;
1659 Plecture = new XMLreadPopulations(*this);
1660 Plecture->iParseFlux(fichier);
1661 cerr << "Jeupop::iPopulationsXML(istream &fichier)" << endl;
1662 }
1663 catch (XMLreadPopulations::Anomalie pb) {
1664 cout << endl << pb.message << endl << endl;
1665 throw Jeupop::Anomalie(5);
1666 }
1667
1668 #else
1669 throw Anomalie(5);
1670 #endif
1671 }
1672
1673 void Jeupop::set_commentaires(const string & commentaires){
1674 _commentaires.push_back(commentaires);
1675 }
1676