| 1 |
|
| 2 |
/*************************************************************************** |
| 3 |
population.cpp - Librairie d'objets permettant de manipuler des données |
| 4 |
spécifiques aux populations |
| 5 |
------------------- |
| 6 |
begin : ven sep 01 10:25:55 CEST 2000 |
| 7 |
copyright : (C) 2000 by Olivier Langella CNRS UPR9034 |
| 8 |
email : [email protected] |
| 9 |
***************************************************************************/ |
| 10 |
|
| 11 |
/*************************************************************************** |
| 12 |
* * |
| 13 |
* This program is free software; you can redistribute it and/or modify * |
| 14 |
* it under the terms of the GNU General Public License as published by * |
| 15 |
* the Free Software Foundation; either version 2 of the License, or * |
| 16 |
* (at your option) any later version. * |
| 17 |
* * |
| 18 |
***************************************************************************/ |
| 19 |
|
| 20 |
#include "population.h" |
| 21 |
#include "jeupop.h" |
| 22 |
|
| 23 |
//constructeur |
| 24 |
Population::Population(Jeupop * pdonnees):StrucPop(pdonnees){ |
| 25 |
// ******************** Traitement des Metapops *********** |
| 26 |
// *********** 5/9/2000 |
| 27 |
_Pmetapop = pdonnees->_Pracinepops; //par défaut |
| 28 |
// |
| 29 |
// |
| 30 |
// |
| 31 |
//_Pmetapop->AjouterPopulation(this); |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
//constructeur |
| 36 |
Population::Population(MetaPop * Pmetapop):StrucPop(Pmetapop->get_Pjeu()){ |
| 37 |
// ******************** Traitement des Metapops *********** |
| 38 |
// *********** 5/9/2000 |
| 39 |
_Pmetapop = Pmetapop; |
| 40 |
// |
| 41 |
// |
| 42 |
//_Pmetapop->AjouterPopulation(this); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
//constructeur de copies |
| 48 |
Population::Population (const StrucPop & popori, MetaPop * Pmetapop):StrucPop(popori, Pmetapop, Pmetapop->get_Pjeu()){ |
| 49 |
unsigned long i; |
| 50 |
//cerr << "Population::Population construct copie début " << _nom << endl; |
| 51 |
Individu * Poriind; |
| 52 |
|
| 53 |
//seulement pour l'objet Population: |
| 54 |
// _nbind = popori.get_nbind(); |
| 55 |
//cerr << "Population::Population construct copie _nbind " << popori.get_nbind() << endl; |
| 56 |
|
| 57 |
for (i=0; i < popori.get_nbind(); i++) { |
| 58 |
Poriind = popori.get_Pind(i); |
| 59 |
//cerr << "Population::Population construct copie _nomind " << Poriind->get_nom() << endl; |
| 60 |
Poriind = new Individu(*Poriind, this); |
| 61 |
//cerr << "Population::Population construct copie _nomind " << Poriind->get_nom() << endl; |
| 62 |
|
| 63 |
AjouterIndividu(Poriind); |
| 64 |
} |
| 65 |
//cerr << "Population::Population construct copie fin " << endl; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
//destructeur |
| 70 |
Population::~Population () { |
| 71 |
|
| 72 |
while (_tabPind.size() > 0) { |
| 73 |
//cerr << "Population::~Population " <<_tabPind[0]->get_nom() << endl; |
| 74 |
SupprIndividu(_tabPind[0]); |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
bool Population::DuType(const string & nom) const { |
| 80 |
if (nom == "Population") return(true); |
| 81 |
else return(false); |
| 82 |
} |
| 83 |
|
| 84 |
void Population::AjouterIndividu(Individu * Pind) { |
| 85 |
//ajout d'un individu dans une Population |
| 86 |
//cerr << "Population::AjouterIndividu(Individu * Pind) début" << endl; |
| 87 |
_tabPind.push_back(Pind); |
| 88 |
|
| 89 |
_nbind++; |
| 90 |
//cerr << "Population::AjouterIndividu " << _nbind << endl; |
| 91 |
_Pmetapop->AjouterIndividu(Pind); |
| 92 |
//cerr << "Population::AjouterIndividu(Individu * Pind) fin" << endl; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
void Population::ifAjouterIndividu(const Individu * Pind) { |
| 97 |
//ajout d'un individu dans une Population, avec les alleles correspondants |
| 98 |
// => recherche d'un individu deja définis |
| 99 |
long i, taille(get_nbind()), nbind(-1); |
| 100 |
const string nomind(Pind->get_nom()); |
| 101 |
|
| 102 |
_nbloc = _Pjeu->get_nbloc(); |
| 103 |
|
| 104 |
for (i = 0 ; i < taille; i++) { |
| 105 |
if (nomind == get_nomind(i)) { |
| 106 |
nbind = i; |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if (nbind >= 0) { //l'individu existe deja |
| 112 |
//il faut ajouter les locus (les alleles) |
| 113 |
// que l'original n'a pas |
| 114 |
// pb à résoudre: incohérence entre alleles d'un meme locus, |
| 115 |
// d'un meme individu... |
| 116 |
get_Pind(nbind)->ifFusionnerIndividu(*Pind); |
| 117 |
} |
| 118 |
else { //il faut creer un nouvel individu |
| 119 |
_tabPind.push_back(new Individu(this, nomind)); |
| 120 |
// et le remplir |
| 121 |
_tabPind.back()->ifFusionnerIndividu(*Pind); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void Population::get_nomniveauxstruc(Titre & nom_niveaux) const{ |
| 126 |
|
| 127 |
unsigned long niveau(get_niveau()); |
| 128 |
|
| 129 |
if (nom_niveaux.size() < (niveau + 1)) nom_niveaux.push_back(_nom); |
| 130 |
else nom_niveaux[niveau] = nom_niveaux[niveau] + " " + _nom; |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
unsigned int Population::get_niveau() const { |
| 135 |
return (_Pmetapop->get_niveau()+1); |
| 136 |
} |
| 137 |
|
| 138 |
void Population::set_nploidie() { |
| 139 |
_nploidie = _Pjeu->get_nploidie(); |
| 140 |
_nbloc = _Pjeu->get_nbloc(); |
| 141 |
|
| 142 |
unsigned long i; |
| 143 |
|
| 144 |
//cerr << "Population::set_nploidie() " << _nploidie << endl; |
| 145 |
for (i=0; i < _tabPind.size(); i++) { |
| 146 |
_tabPind[i]->resize_alleles(); |
| 147 |
} |
| 148 |
//cerr << "Population::set_nploidie() fin" << _nploidie << endl; |
| 149 |
//cin >> i; |
| 150 |
} |
| 151 |
|
| 152 |
void Population::set_nploidie(unsigned int nploidie) { |
| 153 |
if (_nploidie == 0) { |
| 154 |
_nploidie = nploidie; |
| 155 |
_Pjeu->set_nploidie(nploidie); |
| 156 |
} |
| 157 |
else if (_nploidie != nploidie) throw Anomalie(2); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
string Population::get_nom_chemin() const { |
| 162 |
MetaPop * Pmetapop(_Pmetapop); |
| 163 |
|
| 164 |
string nom_complet(_nom); |
| 165 |
string separateur("/"); |
| 166 |
|
| 167 |
while ((Pmetapop != 0) && (Pmetapop->get_niveau() != 0)) { |
| 168 |
nom_complet = Pmetapop->get_nom() + separateur + nom_complet; |
| 169 |
Pmetapop = Pmetapop->get_Pmetapop(); |
| 170 |
} |
| 171 |
|
| 172 |
return (nom_complet); |
| 173 |
} |
| 174 |
|
| 175 |
void Population::f_rempliTabStrucPop(Vecteur<StrucPop*> & tabStrucPop, unsigned int niveau) { |
| 176 |
tabStrucPop.push_back(this); |
| 177 |
} |
| 178 |
/** Suppression de la Population */ |
| 179 |
void Population::SupprPop(StrucPop * Ppop){ |
| 180 |
if (Ppop != this) return; |
| 181 |
|
| 182 |
unsigned long nbind (get_nbind()), i; |
| 183 |
|
| 184 |
for (i = 0; i < nbind; i++) { |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
/** Suppression d'un individu dans une Population */ |
| 189 |
void Population::SupprIndividu(Individu * Pind) { |
| 190 |
SupprtabIndividu(Pind); |
| 191 |
delete (Pind); |
| 192 |
//cerr << "Population::SupprIndividu fin" << endl; |
| 193 |
} |
| 194 |
|
| 195 |
void Population::oPopulationsXML(unsigned int id, ostream & sortie, ostream & infos) { |
| 196 |
unsigned int i, nbind(get_nbind()); |
| 197 |
biolib::vecteurs::ChaineCar idXML; |
| 198 |
|
| 199 |
sortie << "<population"; |
| 200 |
idXML = "p"; |
| 201 |
idXML.AjEntier(id); |
| 202 |
idXML += "le"; |
| 203 |
idXML.AjEntier(get_niveau()); |
| 204 |
set_idXML(idXML); |
| 205 |
sortie << " id=\"" << idXML << "\""; |
| 206 |
sortie << " name=\"" << get_nom() << "\""; |
| 207 |
sortie << ">" << endl; |
| 208 |
|
| 209 |
|
| 210 |
for (i=0; i < nbind; i++) { |
| 211 |
get_Pind(i)->oPopulationsXML(i, sortie, infos); |
| 212 |
} |
| 213 |
|
| 214 |
sortie << "</population>" << endl; |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
Individu * Population::new_individual(string & name, unsigned int nploidie) { |
| 219 |
if (get_nploidie() == 0) set_nploidie(nploidie); |
| 220 |
else if (get_nploidie() != nploidie) { |
| 221 |
throw Anomalie(2); |
| 222 |
} |
| 223 |
|
| 224 |
Individu * Pind(new Individu(this, name)); |
| 225 |
|
| 226 |
AjouterIndividu(Pind); |
| 227 |
|
| 228 |
|
| 229 |
return (Pind); |
| 230 |
} |
| 231 |
|