| 1 |
|
| 2 |
/*************************************************************************** |
| 3 |
strucpop.cpp - Librairie d'objets permettant de manipuler des données |
| 4 |
spécifiques aux StrucPops |
| 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 |
//constructeur |
| 20 |
|
| 21 |
|
| 22 |
#include"locus.h" |
| 23 |
#include"jeupop.h" |
| 24 |
#include "strucpop.h" |
| 25 |
|
| 26 |
StrucPop::StrucPop (Jeupop * pdonnees) { |
| 27 |
//cerr << "StrucPop::StrucPop (Jeupop * pdonnees) constructeur debut" << endl; |
| 28 |
|
| 29 |
_nom.assign("_"); |
| 30 |
_Pjeu = pdonnees; |
| 31 |
_nbind = 0; |
| 32 |
|
| 33 |
_nploidie = _Pjeu->get_nploidie(); |
| 34 |
_nbloc = _Pjeu->get_nbloc(); |
| 35 |
//cerr << "StrucPop::StrucPop (Jeupop * pdonnees) constructeur fin" << endl; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
StrucPop::StrucPop(const StrucPop & rval,MetaPop * Pmetapop, Jeupop * Pjeu) { |
| 41 |
// constructeur de copies |
| 42 |
// vérifié le 11/09/2000 |
| 43 |
unsigned long i; |
| 44 |
|
| 45 |
//cerr << "StrucPop(const StrucPop & rval,MetaPop * Pmetapop, Jeupop * Pjeu) début" << endl; |
| 46 |
//if (popori.get_nploidie() != _nploidie) throw Anomalie(2); |
| 47 |
// => effacer l'ancien contenu |
| 48 |
reset(); |
| 49 |
|
| 50 |
_nom = rval._nom; |
| 51 |
_Pjeu = Pjeu; |
| 52 |
_Pmetapop = Pmetapop; |
| 53 |
_nbloc = rval.get_nbloc(); |
| 54 |
_nploidie = rval.get_nploidie(); |
| 55 |
|
| 56 |
_tabAllIntrogressant.resize(rval._tabAllIntrogressant.size()); |
| 57 |
|
| 58 |
for (i=0; i < _tabAllIntrogressant.size(); i++) { |
| 59 |
_tabAllIntrogressant[i] = get_Pall(rval._tabAllIntrogressant[i]->get_NomLocus(),rval._tabAllIntrogressant[i]->get_Nom()); |
| 60 |
} |
| 61 |
//cerr << "StrucPop(const StrucPop & rval,MetaPop * Pmetapop, Jeupop * Pjeu) fin" << endl; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
StrucPop::~StrucPop () { |
| 66 |
} |
| 67 |
|
| 68 |
void StrucPop::reset() { |
| 69 |
// remise à zéro |
| 70 |
unsigned long i; |
| 71 |
|
| 72 |
_tabAllIntrogressant.resize(0); |
| 73 |
_VcalcInd.resize(0); |
| 74 |
|
| 75 |
for (i=0; i < _tabPind.size();i++) delete _tabPind[i]; |
| 76 |
|
| 77 |
_tabPind.resize(0); |
| 78 |
_nbind = 0; |
| 79 |
_nbloc = 0; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
void StrucPop::r_alldiff(int numlocus, Vecteur<Allele *> &tabPall) const { |
| 85 |
//num locus, tableau sur les Alleles differents de ce locus |
| 86 |
unsigned int i, j; |
| 87 |
unsigned int nbind(get_nbind()); |
| 88 |
|
| 89 |
tabPall.resize(0); |
| 90 |
tabPall.push_back(_tabPind[0]->_tabPall[numlocus * _nploidie]); |
| 91 |
for (i=0;i < nbind;i++) { |
| 92 |
for (j=0;j < _nploidie;j++) { |
| 93 |
if (tabPall.Existe(_tabPind[i]->_tabPall[(numlocus * _nploidie) + j])) {} |
| 94 |
else { |
| 95 |
tabPall.push_back(_tabPind[i]->_tabPall[(numlocus * _nploidie) + j]); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// return(tabPall.size()); |
| 101 |
} |
| 102 |
|
| 103 |
unsigned long StrucPop::r_nballnonnulsBootInd(unsigned long locus) const { |
| 104 |
// retourne le nb de copies d'allèles non nuls pour un locus |
| 105 |
//spécifique aux calculs avec bootstrap sur les individus |
| 106 |
unsigned long resultat(0); |
| 107 |
unsigned long nbind(_VcalcInd.size()); |
| 108 |
unsigned long nploidie(get_nploidie()); |
| 109 |
unsigned long i, j; |
| 110 |
|
| 111 |
for (i=0;i < nbind;i++) { |
| 112 |
for (j=0;j < nploidie;j++) { |
| 113 |
if (_VcalcInd[i]->_tabPall[(locus * nploidie) + j]->_miss) continue; |
| 114 |
resultat++; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return(resultat); |
| 119 |
} |
| 120 |
|
| 121 |
void StrucPop::f_bootstrap() { |
| 122 |
//bootstrap sur les individus |
| 123 |
unsigned long nbind(_tabPind.size()); |
| 124 |
unsigned long j, tirage; |
| 125 |
|
| 126 |
if (_VcalcInd.size() != nbind) _VcalcInd.resize(nbind); |
| 127 |
|
| 128 |
// tirage des individus |
| 129 |
for (j = 0; j < nbind; j ++ ) { |
| 130 |
tirage = (unsigned long) (nbind * ((long double)rand() / ((long double) RAND_MAX))); |
| 131 |
_VcalcInd[j] = _tabPind[tirage]; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
double StrucPop::r_rare(unsigned long nreduit, unsigned int numlocus) const{ |
| 136 |
//nouvelle diversité allèlique pour un échantillon |
| 137 |
// réduit: nreduit, pour un locus |
| 138 |
// cerr << "coucou"; |
| 139 |
// cerr << nreduit << " " << numlocus << " " << get_nbind() << endl; |
| 140 |
int i; |
| 141 |
long double resultat(0); |
| 142 |
long double inter; |
| 143 |
// long double inter2; |
| 144 |
int nploidie(get_nploidie()); |
| 145 |
Vecteur<Allele *> tabPallDiff; |
| 146 |
|
| 147 |
//taille de l'échantillon allèlique |
| 148 |
unsigned long nechall(get_nbind() * nploidie); |
| 149 |
nreduit *= nploidie; |
| 150 |
if (nreduit > nechall) return(resultat); |
| 151 |
|
| 152 |
//nombre d'allèles différents |
| 153 |
r_alldiff(numlocus, tabPallDiff); |
| 154 |
// cerr << "coucou"; |
| 155 |
int nball(tabPallDiff.size()); |
| 156 |
|
| 157 |
//cerr << nball << endl; |
| 158 |
// ne compte pas les allèles nuls (manquants) |
| 159 |
for (i=0;i<nball;i++) { |
| 160 |
if (tabPallDiff[i]->_miss) { |
| 161 |
nechall -= (unsigned long) r_nbcopall(tabPallDiff[i]); |
| 162 |
tabPallDiff.Suppr(i); |
| 163 |
nball--; |
| 164 |
if (nreduit > nechall) return(resultat); |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
//cerr << nball << endl; |
| 169 |
//cerr << endl; |
| 170 |
|
| 171 |
// if (nreduit == nechall) return(nball); |
| 172 |
|
| 173 |
//nombre de copies d'un allèle |
| 174 |
unsigned long ncopies; |
| 175 |
|
| 176 |
for (i=0;i<nball;i++) { |
| 177 |
ncopies = (unsigned long) r_nbcopall(tabPallDiff[i]); |
| 178 |
/* inter = comb ((nechall - ncopies) , nreduit); |
| 179 |
inter2 = comb(nechall , nreduit); |
| 180 |
inter = inter / inter2;*/ |
| 181 |
|
| 182 |
if (nreduit < ncopies) { |
| 183 |
inter = fact((int) (nechall - ncopies), (int) ((nechall - ncopies) - nreduit)) * fact((int) (nechall - nreduit) , (int) nechall); |
| 184 |
} |
| 185 |
else { |
| 186 |
inter = fact((int) (nechall - nreduit), (int) ((nechall - ncopies) - nreduit)) * fact((int) (nechall - ncopies) , (int) nechall); |
| 187 |
} |
| 188 |
resultat = resultat + (1 - inter); |
| 189 |
//cerr << nechall << " " << nreduit << endl; |
| 190 |
//cerr << inter2 << endl; |
| 191 |
//cerr << inter << endl; |
| 192 |
//cerr << inter2 << endl; |
| 193 |
//cerr << resultat << endl; |
| 194 |
//cin >> ncopies; |
| 195 |
} |
| 196 |
|
| 197 |
return((double) resultat); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
void StrucPop::ifAjouterIndividu(const Individu * Pind) { |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
Allele* StrucPop::get_Pall(unsigned long ind, unsigned long locus, unsigned int numall) const { |
| 207 |
unsigned long index; |
| 208 |
unsigned int nploidie(get_nploidie()); |
| 209 |
|
| 210 |
if (locus >= get_nbloc()) throw Anomalie(1); |
| 211 |
if (numall >= nploidie) throw Anomalie(1); |
| 212 |
|
| 213 |
index = (locus * nploidie) + numall; |
| 214 |
return(_tabPind[ind]->_tabPall[index]); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
Allele * StrucPop::get_Pall(const string & nomlocus, const string & nomall) const { |
| 219 |
//recherche l'allele correspondant dans son propre jeu de pop: |
| 220 |
/// n'utiliser que lors d'une copie |
| 221 |
Allele * res; |
| 222 |
Locus * lelocus; |
| 223 |
|
| 224 |
lelocus = _Pjeu->get_Plocus(nomlocus); |
| 225 |
if (lelocus==0) throw Anomalie (5); |
| 226 |
|
| 227 |
res = 0; |
| 228 |
//cerr << "get_Pall debut " << lelocus << " " << lelocus->get_nom() << " " << nomall << endl; |
| 229 |
res = lelocus->getPall(nomall); |
| 230 |
//cerr << "get_Pall allele fin" << res << " " << nomlocus << " " << nomall << endl; |
| 231 |
|
| 232 |
if (res==0) throw Anomalie (6); |
| 233 |
|
| 234 |
return(res); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
void StrucPop::resize_loc() { |
| 239 |
// allocation de mémoire pour un changement de nb de locus |
| 240 |
/// ATTENTION, ne marche que pour agrandir _tabPall !!! |
| 241 |
unsigned long i, j, k; |
| 242 |
vector<Allele*> tabPallnuls; |
| 243 |
|
| 244 |
if (_nbloc == _Pjeu->get_nbloc()) return; |
| 245 |
|
| 246 |
if (_Pjeu->get_nploidie() != _nploidie) throw Anomalie(2); |
| 247 |
|
| 248 |
_nbind = _tabPind.size(); |
| 249 |
// _nploidie = _Pjeu->get_nploidie(); |
| 250 |
|
| 251 |
_nbloc = _Pjeu->get_nbloc(); |
| 252 |
|
| 253 |
|
| 254 |
tabPallnuls.resize(_nbloc); |
| 255 |
for (j = 0; j < _nbloc; j++) { |
| 256 |
tabPallnuls[j] = _Pjeu->get_Plocus(j)->getPallNul(); |
| 257 |
} |
| 258 |
|
| 259 |
for (i=0;i < _nbind;i++) { |
| 260 |
//on remplit les nouveaux alleles des individus |
| 261 |
// par des alleles nuls |
| 262 |
|
| 263 |
for (j = 0; j < _nbloc; j++) { |
| 264 |
for (k = 0; k < _nploidie; k++) { |
| 265 |
if (((j * _nploidie) + k) >= _tabPind[i]->_tabPall.size()) { |
| 266 |
|
| 267 |
_tabPind[i]->_tabPall.push_back(tabPallnuls[j]); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
inline Allele* StrucPop::get_Pall(unsigned long ind,unsigned long all) const { |
| 276 |
return(_tabPind[ind]->_tabPall[all]); |
| 277 |
} |
| 278 |
|
| 279 |
const string & StrucPop::get_nomind(long nbind) const { |
| 280 |
// donne le nom de l'individu nbind |
| 281 |
//cerr << "StrucPop::get_nomind(long nbind) " << nbind << endl; |
| 282 |
return (_tabPind[nbind]->get_nom()); |
| 283 |
} |
| 284 |
|
| 285 |
void StrucPop::AjouterAllIntrogressant(Allele * Pall) { |
| 286 |
if (_tabAllIntrogressant.Existe(Pall) == false) { |
| 287 |
//cerr << "StrucPop::AjouterAllIntrogressant" << endl; |
| 288 |
_tabAllIntrogressant.push_back(Pall); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
long double StrucPop::f_heterozygotieatt(unsigned long locus, unsigned long * Pnbpopcalc) const { |
| 293 |
//Calcul de l'heterozygotie attendue pour un locus |
| 294 |
// Hs expected heterozygosity |
| 295 |
// 1- somme des frequences au carre de chaque allele |
| 296 |
// ok verifie le 11/10/2000 |
| 297 |
|
| 298 |
long unsigned i,nballloc,allnonnuls; |
| 299 |
long double sfreqcarre, freq;//, Hs; |
| 300 |
Allele * Pall; |
| 301 |
|
| 302 |
nballloc = _Pjeu->get_Plocus(locus)->get_nball(); |
| 303 |
allnonnuls = r_nballnonnuls(locus); |
| 304 |
if (allnonnuls == 0) { |
| 305 |
*Pnbpopcalc -= 1; |
| 306 |
return (0); // a verifier |
| 307 |
} |
| 308 |
|
| 309 |
for (sfreqcarre=0,i=0; i < nballloc; i++) { |
| 310 |
Pall = _Pjeu->get_Pall(locus,i); |
| 311 |
if (Pall->_miss) continue; |
| 312 |
freq = ((long double) r_nbcopall(Pall)) / ((long double) allnonnuls); |
| 313 |
sfreqcarre += (freq * freq); |
| 314 |
//cerr <<"StrucPop::f_heterozygotieatt freq" << freq << endl; |
| 315 |
} |
| 316 |
//Hs += (long double) 1 - sfreqcarre; |
| 317 |
//cerr <<"StrucPop::f_heterozygotieatt Hs" << ((long double) 1) - sfreqcarre << endl; |
| 318 |
return (((long double) 1) - sfreqcarre); |
| 319 |
} |
| 320 |
|
| 321 |
long double StrucPop::f_heterozygotieobs(unsigned long locus, unsigned long * Pnbpopcalc) const { |
| 322 |
//Calcul de l'heterozygotie observée pour un locus |
| 323 |
//frequence des hétérozygotes dans cette population à ce locus |
| 324 |
// verifie le 11/10/2000 "génétique et Evolution" Solignac |
| 325 |
|
| 326 |
long unsigned i, nbindcalc, nbind(get_nbind()),nbhetero; |
| 327 |
|
| 328 |
for (nbindcalc=1, i=0, nbhetero = 0; i < nbind; i++,nbindcalc++) { |
| 329 |
try { |
| 330 |
if (_tabPind[i]->r_esthetero(locus)) nbhetero++; |
| 331 |
} |
| 332 |
catch (Individu::Anomalie pb) { |
| 333 |
switch (pb.le_pb) { |
| 334 |
case 13: |
| 335 |
//cerr << _("tt ERROR in Individu object: ") << pb.le_pb << endl; |
| 336 |
nbindcalc--; |
| 337 |
break; |
| 338 |
default: |
| 339 |
cerr << _("ERROR in Individu object: ") << pb.le_pb << endl; |
| 340 |
break; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
nbindcalc--; |
| 346 |
// cerr << "StrucPop::f_heterozygotieobs" << endl; |
| 347 |
// cerr << locus << " nbindcalc : " << nbindcalc << " nbhetero : " << nbhetero << endl; |
| 348 |
|
| 349 |
if (nbindcalc == 0) { |
| 350 |
*Pnbpopcalc -= 1; |
| 351 |
return(0); |
| 352 |
} |
| 353 |
//cerr <<"StrucPop::f_heterozygotieobs Hi" << ((long double) nbhetero) / ((long double) nbind) << endl; |
| 354 |
//cerr <<"StrucPop::f_heterozygotieobs nbhetero" << nbhetero << " nbind" << nbind << endl; |
| 355 |
return (((long double) nbhetero) / ((long double) nbindcalc)); |
| 356 |
} |
| 357 |
|
| 358 |
long double StrucPop::f_calcfreq(Allele * Pall) const { |
| 359 |
//calcul de la fréquence de l'allèle Pall dans cette population |
| 360 |
long double nballnonnuls(r_nballnonnuls(_Pjeu->get_numloc(Pall->get_Ploc()))); |
| 361 |
|
| 362 |
// r_nballnonnuls(_Pjeu->get_numloc(Pall->get_Ploc()); |
| 363 |
|
| 364 |
return(((long double) r_nbcopall(Pall)) / nballnonnuls); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
void StrucPop::set_nom(const string & nom) { |
| 369 |
// int pos; |
| 370 |
|
| 371 |
_nom.assign(nom); |
| 372 |
|
| 373 |
/* pos = _nom.find(" ", 0); |
| 374 |
while (pos != -1) { |
| 375 |
_nom.replace(pos, _nom.size(), "_"); |
| 376 |
pos = _nom.find(" ", 0); |
| 377 |
} |
| 378 |
*/ |
| 379 |
} |
| 380 |
|
| 381 |
/* |
| 382 |
const StrucPop& StrucPop::operator= (const StrucPop & rval) { |
| 383 |
long i; |
| 384 |
long nbind(rval.get_nbind()); |
| 385 |
|
| 386 |
//cerr << "operator = pop, debut " << nbind << " " << rval._nom << endl; |
| 387 |
if (get_nploidie() != rval.get_nploidie()) throw Anomalie(2); |
| 388 |
if (get_nbloc() != rval.get_nbloc()) throw Anomalie(3); |
| 389 |
|
| 390 |
reset(_Pjeu, nbind); |
| 391 |
|
| 392 |
_nom = rval._nom; |
| 393 |
|
| 394 |
try { |
| 395 |
for (i=0; i < nbind; i++) { |
| 396 |
//cerr << "operator = pop, individu " << endl; |
| 397 |
*(_tabPind[i]) = *(rval._tabPind[i]); |
| 398 |
} |
| 399 |
} |
| 400 |
catch (Individu::Anomalie pb) { |
| 401 |
cerr << "erreur de type: " << pb.le_pb << " dans l'operateur = de Population" << endl; |
| 402 |
} |
| 403 |
//cerr << "operator = pop, coucou2 " << endl; |
| 404 |
|
| 405 |
_tabAllIntrogressant.resize(rval._tabAllIntrogressant.size()); |
| 406 |
|
| 407 |
for (i=0; i < rval._tabAllIntrogressant.size(); i++) { |
| 408 |
_tabAllIntrogressant[i] = get_Pall(rval._tabAllIntrogressant[i]->get_NomLocus(),rval._tabAllIntrogressant[i]->get_Nom()); |
| 409 |
} |
| 410 |
//cerr << "operator = pop, fin " << endl; |
| 411 |
|
| 412 |
return(*this); |
| 413 |
} |
| 414 |
|
| 415 |
*/ |
| 416 |
|
| 417 |
|
| 418 |
/** Supprime l'individu Pind du tableau */ |
| 419 |
void StrucPop::SupprtabIndividu(Individu * Pind){ |
| 420 |
//cerr << "StrucPop::SupprtabIndividu debut" << endl; |
| 421 |
unsigned long i; |
| 422 |
for (i = 0 ; i < _tabPind.size(); i++) { |
| 423 |
if (_tabPind[i] == Pind) { |
| 424 |
//cerr << "StrucPop::SupprtabIndividu" << Pind->get_nom() << endl; |
| 425 |
_tabPind.erase(_tabPind.begin() + i); |
| 426 |
_nbind--; |
| 427 |
} |
| 428 |
} |
| 429 |
if (get_niveau() !=0) _Pmetapop->SupprtabIndividu(Pind); |
| 430 |
//cerr << "StrucPop::SupprtabIndividu fin" << endl; |
| 431 |
} |
| 432 |
|
| 433 |
long StrucPop::f_calcfreqabsolue(Allele * Pall) const { |
| 434 |
//calcul de la fréquence absolue de l'allèle Pall dans cette population |
| 435 |
|
| 436 |
return(r_nbcopall(Pall)); |
| 437 |
} |
| 438 |
/** réaffectation du nombre de locus */ |
| 439 |
void StrucPop::set_nbloc(){ |
| 440 |
_nbloc = _Pjeu->get_nbloc(); |
| 441 |
} |
| 442 |
|
| 443 |
|