Debian Codegraph @

src/metapop.cpp

raw
1
2 /***************************************************************************
3 MetaPop.cpp - Librairie d'objets permettant de manipuler des donn�es
4 sp�cifiques aux MetaPops
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 "metapop.h"
21 #include "jeupop.h"
22
23 //constructeur du niveau racine
24 MetaPop::MetaPop(Jeupop * pdonnees):StrucPop(pdonnees) {
25
26 //cerr << "MetaPop::MetaPop(Jeupop * pdonnees) constructeur debut" << endl;
27 _Pmetapop = NULL;
28 _niveau_struc = 0;
29 _nom = "TOP_LEVEL";
30
31 _tabPstrucpop.resize(0);
32 //cerr << "MetaPop::MetaPop(Jeupop * pdonnees) constructeur fin" << endl;
33 }
34
35 //constructeur de niveau interm�diaire
36 MetaPop::MetaPop(MetaPop * pmetapopsup):StrucPop(pmetapopsup->_Pjeu) {
37 _Pmetapop = pmetapopsup;
38 _niveau_struc = pmetapopsup->_niveau_struc + 1;
39
40 _tabPstrucpop.resize(0);
41 }
42
43 //constructeur de copies
44 MetaPop::MetaPop (const MetaPop & popori, MetaPop * Pmetapop, Jeupop * Pjeu):StrucPop(popori, Pmetapop, Pjeu){
45 unsigned long i, nbpop;
46 //cerr << "MetaPop::MetaPop construct copie d�but " << endl;
47
48 //seulement pour l'objet MetaPop:
49 _niveau_struc = popori.get_niveau();
50
51 nbpop = popori.get_nbpop();
52
53 // MetaPop * Pmetapopori(&popori);
54
55 for (i=0; i < nbpop; i++) {
56 if (popori._tabPstrucpop[i]->DuType("Population")) {
57 AjouterPopulation(new Population(*(popori._tabPstrucpop[i]), this));
58 }
59 if (popori._tabPstrucpop[i]->DuType("MetaPop")) {
60 AjouterPopulation(new MetaPop(*(popori.get_Ptabmetapop(popori._tabPstrucpop[i])), this, _Pjeu));
61 }
62 }
63 //cerr << "MetaPop::MetaPop construct copie fin " << endl;
64 }
65
66 //destructeur
67 MetaPop::~MetaPop () {
68 //cerr << "MetaPop::~MetaPop debut" << endl;
69
70 unsigned long i;
71
72 for (i=0;i < _tabPstrucpop.size();i++) delete _tabPstrucpop[i];
73 //cerr << "MetaPop::~MetaPop fin" << endl;
74 }
75
76 bool MetaPop::DuType(const string & nom) const {
77 if (nom == "MetaPop") return(true);
78 else return(false);
79 }
80
81 void MetaPop::ifAjouterIndividu(const Individu * Pind) {
82 }
83
84 void MetaPop::AjouterIndividu(Individu * Pind) {
85 //ajout d'un individu dans une MetaPopulation
86
87 _tabPind.push_back(Pind);
88
89 if (_niveau_struc !=0) _Pmetapop->AjouterIndividu(Pind);
90
91 }
92
93 void MetaPop::AjouterPopulation(Population * Ppop) {
94
95 _tabPstrucpop.push_back(Ppop);
96 _Pjeu->AjouterTabPop(Ppop);
97 }
98
99 void MetaPop::AjouterPopulation(MetaPop * Pmetapop) {
100 _tabPstrucpop.push_back(Pmetapop);
101 _tabPmetapop.push_back(Pmetapop);
102 }
103
104 MetaPop * MetaPop::NewMetaPop(const biolib::vecteurs::ChaineCar & nom) {
105 MetaPop * Pmetapop;
106 unsigned long i;
107
108 for (i=0; i < _tabPmetapop.size(); i++) {
109 //si ma m�tapop existe d�j�:
110 if (_tabPmetapop[i]->get_nom() == nom) return (_tabPmetapop[i]);
111 }
112 Pmetapop = new MetaPop(this);
113 Pmetapop->set_nom(nom);
114 AjouterPopulation(Pmetapop);
115 return (Pmetapop);
116 }
117
118 void MetaPop::get_nomniveauxstruc(Titre & nom_niveaux) const{
119
120 unsigned long i;
121
122 if (nom_niveaux.size() < (_niveau_struc + 1)) nom_niveaux.push_back(_nom);
123 else nom_niveaux[_niveau_struc] = nom_niveaux[_niveau_struc] + " " + _nom;
124
125 for (i = 0; i < _tabPstrucpop.size(); i++) {
126 _tabPstrucpop[i]->get_nomniveauxstruc(nom_niveaux);
127 }
128
129 }
130
131 void MetaPop::set_nploidie() {
132 _nploidie = _Pjeu->get_nploidie();
133 _nbloc = _Pjeu->get_nbloc();
134
135 unsigned long i;
136 //cerr << "MetaPop::set_nploidie() " << _nploidie << endl;
137
138 for (i=0; i < _tabPstrucpop.size(); i++) {
139 _tabPstrucpop[i]->set_nploidie();
140 }
141 //cerr << "MetaPop::set_nploidie() fin" << _nploidie << endl;
142 }
143
144 void MetaPop::reset() {
145 // remise � z�ro
146 unsigned long i;
147
148 StrucPop::reset();
149
150 for (i=0 ; i < _tabPstrucpop.size(); i++) {
151 delete _tabPstrucpop[i];
152 }
153 _tabPstrucpop.resize(0);
154 _tabPmetapop.resize(0);
155
156 }
157
158 MetaPop * MetaPop::get_Ptabmetapop(const StrucPop * Pstrucpop) const {
159 unsigned long i;
160
161 for (i=0; i < _tabPmetapop.size(); i++) {
162 if (_tabPmetapop[i] == Pstrucpop) return (_tabPmetapop[i]);
163 }
164 // pos = _tabPmetapop.Position(Pstrucpop);
165 return (0);
166 }
167
168 void MetaPop::f_rempliTabStrucPop(Vecteur<StrucPop*> & tabStrucPop, unsigned int niveau) {
169 unsigned long i;
170
171 //cerr << "MetaPop::f_rempliTabStrucPop(Vecteur<StrucPop*> & tabStrucPop, unsigned int niveau)" << endl;
172 // if (get_niveau() > niveau) return;
173
174 if (get_niveau() == niveau) {
175 //cerr << get_niveau() << " " << niveau << endl;
176 tabStrucPop.push_back(this);
177 return;
178 }
179 for (i = 0; i < _tabPstrucpop.size(); i++) {
180 _tabPstrucpop[i]->f_rempliTabStrucPop(tabStrucPop, niveau);
181 }
182 }
183
184 long double MetaPop::f_Mheterozygotieatt(unsigned long locus) const {
185 //moyenne de l'h�t�rozygotie attendue sur les sous populations
186 //pour ce locus (Hs barre)
187 // ok verifie le 11/10/2000
188
189 long unsigned nbpop(get_nbpop()),k, nbpopcalc;
190 long double MoyHS(0);
191
192 nbpopcalc = nbpop;
193 for (k=0; k < nbpop; k++) {
194 MoyHS += _tabPstrucpop[k]->f_heterozygotieatt(locus, &nbpopcalc);
195 }
196 if ((nbpopcalc == 0) || (nbpopcalc > 100000)) return(0);
197
198 MoyHS /= (long double) nbpopcalc;
199
200 return(MoyHS);
201
202 }
203
204 long double MetaPop::f_Mheterozygotieobs(unsigned long locus) const {
205 //moyenne de l'h�t�rozygotie observ�e sur les sous populations
206 //HI h�t�rozygotie individuelle pour ce locus
207 // ok verifie le 11/10/2000
208
209 long unsigned nbpop(get_nbpop()),k, nbpopcalc;
210 long double MoyHI(0);
211
212 nbpopcalc = nbpop;
213 for (k=0; k < nbpop; k++) {
214 MoyHI += _tabPstrucpop[k]->f_heterozygotieobs(locus, &nbpopcalc);
215 }
216 if ((nbpopcalc == 0) || (nbpopcalc > 100000)) return(0);
217
218 MoyHI /= (long double) nbpopcalc;
219
220 return(MoyHI);
221
222 }
223
224 long double MetaPop::f_Mheterozygotietotale(unsigned long locus) const {
225 // h�t�rozygotie totale (Ht) attendue, � partir des fr�quences all�liques
226 // moyennes sur la m�tapopulation pour ce locus
227 // ok verifie le 11/10/2000
228
229 long unsigned i,nballloc,allnonnuls;
230 long double sfreqcarre(0), freq;//, Hs;
231 long unsigned nbpop(get_nbpop()),k;
232 Allele * Pall;
233
234 nballloc = _Pjeu->get_Plocus(locus)->get_nball();
235 for (i=0; i < nballloc; i++) {
236 for (freq=0,k=0; k < nbpop; k++) {
237 allnonnuls = _tabPstrucpop[k]->r_nballnonnuls(locus);
238 if (allnonnuls == 0) continue;
239
240 Pall = _Pjeu->get_Pall(locus,i);
241 if (Pall->r_estnul()) continue;
242 freq += ((long double) _tabPstrucpop[k]->r_nbcopall(Pall)) / ((long double) allnonnuls);
243 }
244 freq /= (long double) nbpop; //fr�quence moyenne
245 sfreqcarre += (freq * freq);
246 }
247 //Hs += (long double) 1 - sfreqcarre;
248 return ((long double) 1 - sfreqcarre);
249
250
251 }
252
253 long double MetaPop::f_M_Fis(unsigned long locus) const {
254 // indice de Wright Fis pour ce locus
255 // ok verifie le 11/10/2000
256 // Fis = (Hs barre - Hi) / Hs barre
257 if (locus > (unsigned long)get_nbloc()) throw Anomalie(7);
258
259 //cerr << "MetaPop::f_M_Fis Hi:" << f_Mheterozygotieobs(locus) << endl;
260 long double Hsbarre(f_Mheterozygotieatt(locus));
261 //cerr << "MetaPop::f_M_Fis Hsbarre:" << Hsbarre << endl;
262
263 return ((Hsbarre - f_Mheterozygotieobs(locus))/ Hsbarre);
264 }
265
266 long double MetaPop::f_M_Fst(unsigned long locus) const {
267 // indice de Wright Fst pour ce locus
268 // ok verifie le 11/10/2000
269 // Fst = (Ht - Hs barre) / Ht
270 //cerr << "MetaPop::f_M_Fst" << endl;
271 //cerr << locus << " " << get_nbloc() << endl;
272 if (locus > get_nbloc()) throw Anomalie(7);
273
274 long double Ht(f_Mheterozygotietotale(locus));
275
276 //cerr << "MetaPop::f_M_Fst Ht " << Ht << endl;
277 //cerr << "MetaPop::f_M_Fst Hsbarre " << f_Mheterozygotieatt(locus) << endl;
278
279 return ((Ht - f_Mheterozygotieatt(locus))/ Ht);
280 }
281
282
283 long double MetaPop::f_M_Fit(unsigned long locus) const {
284 // indice de Wright Fit pour ce locus
285 // ok verifie le 11/10/2000
286 // Fit = (Ht - Hi) / Ht
287 //cerr << "MetaPop::f_M_Fit" << endl;
288 //cerr << locus << " " << get_nbloc() << endl;
289
290 if (locus > get_nbloc()) throw Anomalie(7);
291
292 long double Ht(f_Mheterozygotietotale(locus));
293
294 return ((Ht - f_Mheterozygotieobs(locus))/ Ht);
295 }
296
297 /** Suppression de la Population Ppop */
298 void MetaPop::SupprPop(StrucPop * Ppop){
299 //cerr << "MetaPop::SupprPop debut" << endl;
300 unsigned long nbpop(_tabPstrucpop.size()),p;
301 bool ok(false);
302
303 for (p=0; p < nbpop; p++) {
304 if (Ppop == _tabPstrucpop[p]) {
305 //cerr << "MetaPop::SupprPop efface" << endl;
306 delete Ppop;
307 _tabPstrucpop.erase(_tabPstrucpop.begin()+p);
308 p--;
309 nbpop--;
310 ok = true;
311 }
312 }
313 //cerr << "MetaPop::SupprPop debut 2" << endl;
314
315 if (ok) {
316 nbpop = _tabPmetapop.size();
317 for (p=0; p < nbpop; p++) {
318 if (Ppop == _tabPmetapop[p]) {
319 _tabPmetapop.erase(_tabPmetapop.begin()+p);
320 }
321 }
322 //cerr << "MetaPop::SupprPop debut 3 " << "ok" << endl;
323 }
324 else {
325 for (p=0; p < nbpop; p++) _tabPstrucpop[p]->SupprPop(Ppop);
326 }
327 //cerr << "MetaPop::SupprPop fin" << endl;
328
329 }
330 /** Retourne vrai, si l'allele n'est pas pr�sent dans MetaPop */
331 bool MetaPop::r_allelenonpresent(Allele * Pall) const{
332 if (r_nbcopall(Pall) > 0) return (false);
333 else return(true);
334 }
335 /** r�affectation du nombre de locus
336 */
337 void MetaPop::set_nbloc(){
338 unsigned int i;
339 _nbloc = _Pjeu->get_nbloc();
340
341 for (i = 0; i < _tabPstrucpop.size(); i ++) _tabPstrucpop[i]->set_nbloc();
342 }
343
344 void MetaPop::oPopulationsXML(unsigned int id, ostream & sortie, ostream & infos) {
345 unsigned int i, nbpop(_tabPstrucpop.size());
346 biolib::vecteurs::ChaineCar idXML;
347
348 sortie << "<metapopulation";
349 idXML = "mp";
350 idXML.AjEntier(id);
351 idXML += "le";
352 idXML.AjEntier(get_niveau());
353 sortie << " id=\"" << idXML << "\"";
354 sortie << " name=\"" << get_nom() << "\"";
355 sortie << ">" << endl;
356
357
358 for (i=0; i < nbpop; i++) {
359 _tabPstrucpop[i]->oPopulationsXML(i, sortie, infos);
360 }
361
362 sortie << "</metapopulation>" << endl;
363
364 }
365
366 Population * MetaPop::new_population(string & nom) {
367 Population * Ppop(new Population(this));
368
369 Ppop->set_nom(nom);
370 AjouterPopulation(Ppop);
371 return (Ppop);
372 }
373