| 1 |
/*************************************************************************** |
| 2 |
jeupopexp.cpp - description |
| 3 |
------------------- |
| 4 |
begin : Tue Oct 24 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 |
//#include "strucpop.h" |
| 19 |
#include "jeupopexp.h" |
| 20 |
|
| 21 |
JeuPopExp::JeuPopExp(Jeupop * Pjeu):_Pjeu(Pjeu) { |
| 22 |
|
| 23 |
_Pcopie = 0; |
| 24 |
} |
| 25 |
|
| 26 |
JeuPopExp::~JeuPopExp(){ |
| 27 |
if (_Pcopie != 0) delete _Pcopie; |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
void JeuPopExp::oLea(unsigned long P1,unsigned long P2,unsigned long H, ostream & sortie, ostream & infos) { |
| 33 |
f_prepP1P2H(P1,P2,H,infos); |
| 34 |
|
| 35 |
unsigned long nbpop; |
| 36 |
unsigned long nbloc(_Pcopie->get_nbloc()); |
| 37 |
unsigned long nball; //nb d'alleles pour chaque locus |
| 38 |
unsigned long i,l,p; |
| 39 |
Locus * Plocus; |
| 40 |
bool proms; |
| 41 |
|
| 42 |
nbpop = _Pcopie->get_nbpop(); |
| 43 |
|
| 44 |
sortie << "0" << endl; |
| 45 |
sortie << nbpop << endl; |
| 46 |
sortie << nbloc << endl; |
| 47 |
|
| 48 |
for (l=0; l < nbloc; l++) { |
| 49 |
Plocus = _Pcopie->get_Plocus(l); |
| 50 |
nball = Plocus->get_nball(); |
| 51 |
sortie << Plocus->get_nballnonnuls() <<endl; |
| 52 |
for (p=0; p < nbpop; p++) { |
| 53 |
proms = true; |
| 54 |
for (i=0; i < nball; i++) { |
| 55 |
if (!proms) sortie << " "; |
| 56 |
if (Plocus->getPall(i)->r_nonnul()) { |
| 57 |
// sortie << _Pcopie->get_Ppop(p)->f_calcfreq(Plocus->getPall(i)); |
| 58 |
sortie << _Pcopie->get_Ppop(p)->f_calcfreqabsolue(Plocus->getPall(i)); |
| 59 |
if (proms) proms = false; |
| 60 |
} |
| 61 |
} |
| 62 |
sortie << endl; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
/** Exportation au format "admix" de G. Bertorelle |
| 68 |
voir le site: |
| 69 |
http://www.unife.it/genetica/Giorgio/giorgio.html |
| 70 |
*/ |
| 71 |
void JeuPopExp::oAdmix_dat(unsigned long P1,unsigned long P2,unsigned long H, ostream & sortie, ostream & infos){ |
| 72 |
|
| 73 |
f_prepP1P2H(H,P1,P2,infos); //inversion de l'ordre pour corriger le bug trouvé par Andrea Taylor (15/5/2002) |
| 74 |
|
| 75 |
|
| 76 |
unsigned long nbpop; |
| 77 |
unsigned long nbloc(_Pcopie->get_nbloc()); |
| 78 |
unsigned long nball; //nb d'alleles pour chaque locus |
| 79 |
unsigned long i,l,p; |
| 80 |
Locus * Plocus; |
| 81 |
bool proms; |
| 82 |
|
| 83 |
nbpop = _Pcopie->get_nbpop(); |
| 84 |
|
| 85 |
for (l=0; l < nbloc; l++) { |
| 86 |
Plocus = _Pcopie->get_Plocus(l); |
| 87 |
sortie << Plocus->get_nom() << endl; |
| 88 |
proms = true; |
| 89 |
for (p=0; p < nbpop; p++) { |
| 90 |
// affichage du total (nb d'alles non nuls de chaque pop pour ce locus) |
| 91 |
if (!proms) sortie << " "; |
| 92 |
sortie << _Pcopie->get_Ppop(p)->r_nballnonnuls(l); |
| 93 |
if (proms) proms = false; |
| 94 |
} |
| 95 |
sortie << endl; |
| 96 |
|
| 97 |
nball = Plocus->get_nball(); |
| 98 |
for (i=0; i < nball; i++) { |
| 99 |
if (Plocus->getPall(i)->r_nonnul()) { |
| 100 |
proms = true; |
| 101 |
for (p=0; p < nbpop; p++) { |
| 102 |
if (!proms) sortie << " "; |
| 103 |
sortie << _Pcopie->get_Ppop(p)->f_calcfreqabsolue(Plocus->getPall(i)); |
| 104 |
if (proms) proms = false; |
| 105 |
} |
| 106 |
sortie << endl; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void JeuPopExp::oAdmix_mtx(ostream & sortie, ostream & infos){ |
| 113 |
// f_prepP1P2H(P1,P2,H,infos); |
| 114 |
|
| 115 |
unsigned long nbpop; |
| 116 |
unsigned long nbloc(_Pcopie->get_nbloc()); |
| 117 |
unsigned long nball; //nb d'alleles pour chaque locus |
| 118 |
unsigned long i,l,p; |
| 119 |
unsigned long diffcarre; |
| 120 |
Locus * Plocus; |
| 121 |
// bool proms; |
| 122 |
|
| 123 |
if (_Pcopie == 0) throw Anomalie(1, "Admix_mtx"); |
| 124 |
nbpop = _Pcopie->get_nbpop(); |
| 125 |
if (nbpop != 3) throw Anomalie(1, "Admix_mtx"); |
| 126 |
|
| 127 |
sortie << nbloc << endl; |
| 128 |
for (l=0; l < nbloc; l++) { |
| 129 |
Plocus = _Pcopie->get_Plocus(l); |
| 130 |
sortie << Plocus->get_nom() << endl; |
| 131 |
nball = Plocus->get_nballnonnuls(); |
| 132 |
// nb d'alleles différents non nuls pour ce locus |
| 133 |
sortie << nball << endl; |
| 134 |
// matrice de distance "squared difference in allele size" |
| 135 |
for (i=0; i < Plocus->get_nball(); i++) { |
| 136 |
if (Plocus->getPall(i)->r_estnul()) continue; |
| 137 |
for (p=0; p < i; p++) { |
| 138 |
if (Plocus->getPall(p)->r_estnul()) continue; |
| 139 |
diffcarre =(Plocus->getPall(p)->get_nbrepet() - Plocus->getPall(i)->get_nbrepet()); |
| 140 |
diffcarre *= diffcarre; |
| 141 |
sortie << diffcarre << " "; |
| 142 |
//cerr << ",nrepet:" << Plocus->getPall(p)->get_nbrepet(); |
| 143 |
} |
| 144 |
sortie << "0" << endl; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** Prépare une copie de jeupop avec 3 |
| 150 |
populations P1, P2, H (special admixture) */ |
| 151 |
void JeuPopExp::f_prepP1P2H(unsigned long P1,unsigned long P2,unsigned long H, ostream & infos){ |
| 152 |
unsigned long nbpop(_Pjeu->get_nbpop()); |
| 153 |
unsigned long p; |
| 154 |
Population * PP1; |
| 155 |
Population * PP2; |
| 156 |
Population * PH; |
| 157 |
Population * Ppop; |
| 158 |
//-------préparation des données: |
| 159 |
if (_Pcopie != 0) delete (_Pcopie); |
| 160 |
_Pcopie = new Jeupop(*_Pjeu); |
| 161 |
|
| 162 |
PP1 = _Pcopie->get_Ppop(P1); |
| 163 |
PP2 = _Pcopie->get_Ppop(P2); |
| 164 |
PH = _Pcopie->get_Ppop(H); |
| 165 |
|
| 166 |
_Pcopie->f_deplacePop(PP1,0); |
| 167 |
_Pcopie->f_deplacePop(PP2,1); |
| 168 |
_Pcopie->f_deplacePop(PH,2); |
| 169 |
|
| 170 |
|
| 171 |
//cerr << "JeuPopExp::oAile" << endl; |
| 172 |
//cerr << _Pcopie->get_nbpop() << " " <<_Pcopie->get_nbind() << endl; |
| 173 |
|
| 174 |
p=nbpop; |
| 175 |
while (nbpop > 3) { |
| 176 |
p--; |
| 177 |
Ppop = _Pcopie->get_Ppop(p); |
| 178 |
if (Ppop == PP1) continue; |
| 179 |
if (Ppop == PP2) continue; |
| 180 |
if (Ppop == PH) continue; |
| 181 |
_Pcopie->SupprPop(Ppop); |
| 182 |
nbpop--; |
| 183 |
//cerr << _Pcopie->get_nbpop() << " " <<_Pcopie->get_nbind() << endl; |
| 184 |
} |
| 185 |
|
| 186 |
// _Pcopie->oGenepop(cout); |
| 187 |
// elimination des alleles inutiles (non présents dans l'échantillon) |
| 188 |
_Pcopie->f_nettoieAlleles(); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
void JeuPopExp::oGenetix(ostream & sortie, ostream & infos){ |
| 193 |
// sortie au format Genetix |
| 194 |
// http://www.univ-montp2.fr/~genetix/genetix.htm |
| 195 |
// 1ere ligne : nb de locus |
| 196 |
// 2eme ligne : nb de populations |
| 197 |
//description des locus: |
| 198 |
// nom de locus sur une ligne (5 caracteres max) |
| 199 |
// nombre d'alleles, et numéros d'allèles sur 3 chiffres |
| 200 |
// description des populations: |
| 201 |
// nom de population sur une ligne |
| 202 |
// taille de l'échantillon sur une ligne |
| 203 |
// Identifiant de l'individu sur 10 caractères, suivi du génotype |
| 204 |
// retour chariot : CRLF |
| 205 |
|
| 206 |
//cerr << "JeuPopExp::oGenetix(ostream & sortie, ostream & infos)" << endl; |
| 207 |
unsigned long nbpop; |
| 208 |
unsigned long nbind; |
| 209 |
unsigned long nbloc(_Pjeu->get_nbloc()); |
| 210 |
unsigned long nball; //nb d'alleles pour chaque locus |
| 211 |
unsigned long i,l,p; |
| 212 |
Locus * Plocus; |
| 213 |
Population * Ppop; |
| 214 |
Individu * Pind; |
| 215 |
|
| 216 |
char crlf[3]; |
| 217 |
|
| 218 |
crlf[0] = 13; |
| 219 |
crlf[1] = 10; |
| 220 |
crlf[2] = '\0'; |
| 221 |
// bool proms; |
| 222 |
|
| 223 |
if (_Pjeu == 0) throw Anomalie(1, "Genetix"); |
| 224 |
|
| 225 |
if (_Pjeu->get_nploidie() != 2) { |
| 226 |
//le format Genetix ne permet que le stockage de pop diploïdes |
| 227 |
throw Anomalie(100, "Genetix"); |
| 228 |
} |
| 229 |
|
| 230 |
if (!(_Pjeu->f_verifnum(3))) { |
| 231 |
//il faut renuméroter les allèles pour le format génétix |
| 232 |
throw Anomalie(2, "Genetix"); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
nbpop = _Pjeu->get_nbpop(); |
| 237 |
// if (nbpop != 3) throw Anomalie(1); |
| 238 |
|
| 239 |
sortie << nbloc << crlf; |
| 240 |
sortie << nbpop << crlf; |
| 241 |
|
| 242 |
for (l=0; l < nbloc; l++) { |
| 243 |
Plocus = _Pjeu->get_Plocus(l); |
| 244 |
sortie << Plocus->get_nom() << crlf; //attention ! pas plus de 5 caractères |
| 245 |
if (Plocus->get_nom().size() > 5) throw Anomalie(1000, "Genetix"); |
| 246 |
nball = Plocus->get_nball(); |
| 247 |
// nb d'alleles pour ce locus |
| 248 |
sortie << Plocus->get_nballnonnuls(); |
| 249 |
// matrice de distance "squared difference in allele size" |
| 250 |
for (i=0; i < nball; i++) { |
| 251 |
if (Plocus->getPall(i)->r_estnul() != true) { |
| 252 |
sortie << " "; |
| 253 |
if (Plocus->getPall(i)->get_nom().size() == 2) sortie << "0"; |
| 254 |
else if (Plocus->getPall(i)->get_nom().size() == 1) sortie << "00"; |
| 255 |
sortie << Plocus->getPall(i)->get_nom(); |
| 256 |
} |
| 257 |
} |
| 258 |
sortie << crlf; |
| 259 |
} |
| 260 |
|
| 261 |
for (p=0; p < nbpop; p++) { |
| 262 |
// _Pcopie->get_Ppop(p); |
| 263 |
Ppop = _Pjeu->get_Ppop(p); |
| 264 |
sortie << Ppop->get_nom() << crlf; |
| 265 |
nbind = Ppop->get_nbind(); |
| 266 |
sortie << nbind << crlf; |
| 267 |
|
| 268 |
for (i=0; i < nbind; i++) { |
| 269 |
Pind = Ppop->get_Pind(i); |
| 270 |
if (Pind->get_nom().size() > 10) throw Anomalie(1001, "Genetix"); |
| 271 |
Pind->affiche (sortie, 4); //affichage de l'individu au format genetix |
| 272 |
sortie << crlf; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
void JeuPopExp::oFstat(ostream & sortie, ostream & infos){ |
| 278 |
// sortie au format FSTAT 2.91 |
| 279 |
// http://www.unil.ch/izea/softwares/fstat.html |
| 280 |
// 1ere ligne : nb de pop, nb de locus, plus grand numero d'allele, nb de digits |
| 281 |
// nom des locus sur n lignes |
| 282 |
|
| 283 |
unsigned long nbpop; |
| 284 |
unsigned long nbind; |
| 285 |
unsigned long nbloc(_Pjeu->get_nbloc()); |
| 286 |
unsigned long i,l,p; |
| 287 |
Locus * Plocus; |
| 288 |
Population * Ppop; |
| 289 |
Individu * Pind; |
| 290 |
char crlf[3]; |
| 291 |
|
| 292 |
crlf[0] = 13; |
| 293 |
crlf[1] = 10; |
| 294 |
crlf[2] = '\0'; |
| 295 |
// bool proms; |
| 296 |
|
| 297 |
if (_Pjeu == 0) throw Anomalie(1, "Fstat"); |
| 298 |
|
| 299 |
if (_Pjeu->get_nploidie() != 2) { |
| 300 |
//le format Fstat ne permet que le stockage de pop diploïdes |
| 301 |
throw Anomalie(100, "Fstat"); |
| 302 |
} |
| 303 |
|
| 304 |
if (!(_Pjeu->f_verifnum(2))) { |
| 305 |
//il faut renuméroter les allèles pour le format Fstat |
| 306 |
throw Anomalie(2, "Fstat"); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
nbpop = _Pjeu->get_nbpop(); |
| 311 |
// if (nbpop != 3) throw Anomalie(1); |
| 312 |
|
| 313 |
p = 0; |
| 314 |
for (l=0; l < nbloc; l++) { |
| 315 |
if (_Pjeu->get_Plocus(l)->get_nballnonnuls() > p) p = _Pjeu->get_Plocus(l)->get_nballnonnuls(); |
| 316 |
} |
| 317 |
|
| 318 |
//sortie << nbpop << "\t" << nbloc << "\t" << p << "\t2"<< crlf; |
| 319 |
sortie << nbpop << "\t" << nbloc << "\t" << "99" << "\t2"<< crlf; |
| 320 |
|
| 321 |
for (l=0; l < nbloc; l++) { |
| 322 |
Plocus = _Pjeu->get_Plocus(l); |
| 323 |
sortie << Plocus->get_nom() << crlf; |
| 324 |
} |
| 325 |
|
| 326 |
for (p=0; p < nbpop; p++) { |
| 327 |
// _Pcopie->get_Ppop(p); |
| 328 |
Ppop = _Pjeu->get_Ppop(p); |
| 329 |
nbind = Ppop->get_nbind(); |
| 330 |
|
| 331 |
for (i=0; i < nbind; i++) { |
| 332 |
if (p < 9) sortie << " "; |
| 333 |
else if (p < 99) sortie << " "; |
| 334 |
else if (p < 999) sortie << " "; |
| 335 |
|
| 336 |
sortie << (p+1); |
| 337 |
Pind = Ppop->get_Pind(i); |
| 338 |
//if (Pind->get_nom().size() > 10) throw Anomalie(1001, "Fstat"); |
| 339 |
Pind->affiche (sortie, 5); //affichage de l'individu au format Fstat |
| 340 |
sortie << crlf; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
void JeuPopExp::oPopulationsXML(ostream & sortie, ostream & infos) const{ |
| 346 |
unsigned int l, i, nball(0), nbloc(_Pjeu->get_nbloc()); |
| 347 |
Locus * Plocus; |
| 348 |
biolib::vecteurs::ChaineCar idXML; |
| 349 |
|
| 350 |
//export into "populations" XML format |
| 351 |
// pas fini |
| 352 |
sortie << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << endl; |
| 353 |
sortie << "<!-- " << endl; |
| 354 |
sortie << "created by Populations" << endl; |
| 355 |
sortie << "http://www.pge.cnrs-gif.fr/bioinfo/populations" << endl; |
| 356 |
sortie << "-->" << endl; |
| 357 |
|
| 358 |
sortie << "<populations version=\"0.1\">" << endl; |
| 359 |
sortie << "<comments>" << endl; |
| 360 |
for (l=0; l < _Pjeu->get_commentaires().size(); l++) { |
| 361 |
sortie << _Pjeu->get_commentaires().operator[](l) << endl; |
| 362 |
} |
| 363 |
sortie << "</comments>" << endl; |
| 364 |
|
| 365 |
sortie << "<loci>" << endl; |
| 366 |
for (l=0; l < nbloc; l++) { |
| 367 |
Plocus = _Pjeu->get_Plocus(l); |
| 368 |
sortie << "<locus"; |
| 369 |
sortie << " id=\"l" << l << "\""; |
| 370 |
sortie << " name=\"" << Plocus->get_nom() << "\""; |
| 371 |
sortie << ">" << endl; |
| 372 |
//if (Plocus->get_nom().size() > 5) throw Anomalie(1000, "Genetix"); |
| 373 |
nball = Plocus->get_nball(); |
| 374 |
// nb d'alleles pour ce locus |
| 375 |
for (i=0; i < nball; i++) { |
| 376 |
sortie << "<allele"; |
| 377 |
idXML = "l"; |
| 378 |
idXML.AjEntier(l); |
| 379 |
idXML += "a"; |
| 380 |
idXML.AjEntier(i); |
| 381 |
Plocus->getPall(i)->set_idXML((string) idXML); |
| 382 |
sortie << " id=\"" << Plocus->getPall(i)->get_idXML() << "\""; |
| 383 |
if (Plocus->getPall(i)->r_estnul()) sortie << " name=\"missing\""; |
| 384 |
else { |
| 385 |
sortie << " name=\"" << Plocus->getPall(i)->get_nom() << "\""; |
| 386 |
sortie << " nbrepeat=\"" << Plocus->getPall(i)->get_nbrepet() << "\""; |
| 387 |
} |
| 388 |
sortie << ">" << endl; |
| 389 |
|
| 390 |
sortie << "</allele>" << endl; |
| 391 |
} |
| 392 |
sortie << "</locus>" << endl; |
| 393 |
} |
| 394 |
sortie << "</loci>" << endl; |
| 395 |
|
| 396 |
_Pjeu->get_Pracinepop()->oPopulationsXML(0, sortie, infos); |
| 397 |
|
| 398 |
sortie << "</populations>" << endl; |
| 399 |
|
| 400 |
} |
| 401 |
|