Debian Codegraph @

src/individu.cpp

raw
1 /***************************************************************************
2 individu.cpp - description
3 -------------------
4 begin : Thu Sep 14 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
19 #include "individu.h"
20 #include "population.h"
21 #include "jeupop.h"
22
23
24 void Individu::creation (Population * Ppop) {
25 _Ppop = Ppop;
26 _nploidie = Ppop->get_nploidie();
27 if (Ppop->get_nploidie() == 0) _nploidie = 0;
28 else _nploidie = Ppop->get_nploidie();
29 _tabPall.resize(Ppop->get_nbloc() * _nploidie);
30 }
31
32 //constructeur
33 Individu::Individu (Population * Ppop) {
34
35 creation(Ppop);
36 _nom.assign("");
37 }
38
39 Individu::Individu (Population * Ppop, const string& nom) {
40
41 creation(Ppop);
42 _nom.assign(nom);
43 }
44
45 //destructeur
46 Individu::~Individu () {
47 //cerr << "Individu::~Individu ()" << endl;
48 // delete [] _tabPall;
49 }
50
51 void Individu::resize_alleles() {
52
53 _nploidie = _Ppop->get_nploidie();
54 _tabPall.resize(_Ppop->get_nbloc() * _nploidie);
55 }
56
57 const Individu& Individu::operator= (const Individu & rval) {
58 unsigned long i; //index
59 unsigned long j; //locus
60 string nomlocus;
61 unsigned long nball(rval._tabPall.size());
62 unsigned long nploidie(_Ppop->get_nploidie());
63 unsigned long nbloc(_Ppop->get_nbloc());
64
65 if (_tabPall.size() != nball) throw Anomalie (1);
66
67
68 _nom = rval._nom;
69
70 //cerr << "operator= individu debut " << _nom << endl;
71 for (j=0; j < nbloc; j ++) {
72 nomlocus = rval._Ppop->_Pjeu->get_nomlocus(j);
73 for (i=0; i < nploidie; i ++) {
74 //cerr << "operator= individu numall " << (j*nploidie) + i << endl;
75 if (rval._tabPall[(j * nploidie) + i] == 0) {
76 _tabPall[(j * nploidie) + i] = (_Ppop->_Pjeu->get_Plocus(nomlocus))->getPallNul();
77 }
78 else {
79 //cerr << "operator= individu nom de l'allele " << rval._tabPall[(j * nploidie) + i]->_nom << endl;
80 _tabPall[(j * nploidie) + i] = _Ppop->get_Pall(nomlocus, rval._tabPall[(j * nploidie) + i]->_nom);
81 }
82 }
83 }
84 //cerr << "operator= individu fin" << endl;
85
86 return(*this);
87 }
88
89 bool Individu::r_estnul() const {
90 long i; //index
91 long nball(_tabPall.size());
92
93 for (i=0; i < nball; i ++) if (_tabPall[i]->_miss == false ) return (false);
94 return (true);
95 }
96
97 bool Individu::r_esthetero (unsigned long locus) const {
98 //lancer une exception si l'individu n'est pas diploide !!!!
99
100 unsigned long nploidie(_Ppop->get_nploidie());
101 unsigned long nbloc(_Ppop->get_nbloc());
102
103 if (locus >= nbloc) throw Anomalie(11);
104 if (nploidie != 2) throw Anomalie(12);
105
106 if ((_tabPall[(locus * 2)]->_miss)||(_tabPall[(locus * 2)+1]->_miss)) {
107 throw Anomalie(13);
108 return(false);
109 }
110 if (_tabPall[(locus * 2)] == _tabPall[(locus * 2) + 1]) return(false);
111 else return (true);
112
113 }
114
115 bool Individu::operator== (const Individu & rval) const {
116
117 //pas operationnel !!!!
118
119 unsigned long i; //index
120 unsigned long j; //locus
121 string nomlocus;
122 unsigned long nball(rval._tabPall.size());
123 unsigned long nploidie(_Ppop->get_nploidie());
124 unsigned long nbloc(_Ppop->get_nbloc());
125
126 // cerr << "truc";
127 if (rval == 0) {
128 // cerr << "truc";
129 nball = _tabPall.size();
130 for (i=0; i < nball; i ++) if (_tabPall[i]->_miss == false ) return (false);
131 return (true);
132 }
133
134 if (_tabPall.size() != nball) return (false);
135
136
137 // _nom = rval._nom;
138
139 for (j=0; j < nbloc; j ++) {
140 // nomlocus = rval._Ppop->_Pjeu->get_nomlocus(j);
141 for (i=0; i < nploidie; i ++) {
142 if (_tabPall[(j*nploidie) + i] != rval._tabPall[(j*nploidie) + i]) return (false);
143 // _tabPall[(j*nploidie) + i] = _Ppop->get_Pall(nomlocus, rval._tabPall[(j*nploidie) + i]->_nom);
144 }
145 }
146
147 return(true);
148 }
149
150
151 Individu::Individu (const Individu & rval, Population * Ppop){
152 unsigned long i; //index
153 unsigned long j; //locus
154 string nomlocus;
155 unsigned long nball(rval._tabPall.size());
156 //cerr << "Individu::Individu construct copie début "<< nball << endl;
157 unsigned long nploidie(Ppop->get_nploidie());
158 //cerr << "Individu::Individu construct copie début "<< nball << endl;
159 unsigned long nbloc(Ppop->get_nbloc());
160
161 //cerr << "Individu::Individu construct copie début " << endl;
162 _Ppop = Ppop;
163 resize_alleles();
164 if (_tabPall.size() != nball) throw Anomalie (1);
165
166 _nom = rval._nom;
167
168 //cerr << "operator= individu debut " << _nom << endl;
169 for (j=0; j < nbloc; j ++) {
170 nomlocus = rval._Ppop->_Pjeu->get_nomlocus(j);
171 for (i=0; i < nploidie; i ++) {
172 //cerr << "operator= individu numall " << (j*nploidie) + i << endl;
173 if (rval._tabPall[(j * nploidie) + i] == 0) {
174 _tabPall[(j * nploidie) + i] = (_Ppop->_Pjeu->get_Plocus(nomlocus))->getPallNul();
175 }
176 else {
177 //cerr << "operator= individu nom de l'allele " << rval._tabPall[(j * nploidie) + i]->_nom << endl;
178 _tabPall[(j * nploidie) + i] = _Ppop->get_Pall(nomlocus, rval._tabPall[(j * nploidie) + i]->_nom);
179 }
180 }
181 }
182 //cerr << "operator= individu fin" << endl;
183 //cerr << "Individu::Individu construct copie fin " << endl;
184
185 }
186
187
188 void Individu::ifPlacerAllele(Locus* Ploc, long position, const Allele * Pall) {
189 // numall < nploidie
190 // long position((Ploc->get_numloc()
191 //cerr << "coucou 3" << endl;
192 if ((_tabPall[position] == 0) || (_tabPall[position]->_miss)) {
193 //on remplace, normal
194 //cerr << "coucou 3 if" << endl;
195 _tabPall[position] = Ploc->getPall(Pall->get_nom());
196 }
197 else {
198 if (*_tabPall[position] == *Pall) {
199 //c'est bon
200 return;
201 }
202 else {
203 // ca veut dire qu'il y a un allele, et qu'il est different
204 // => pb, mais on ignore pour l'instant
205 return;
206 }
207 }
208 }
209
210 void Individu::ifFusionnerIndividu(const Individu& individu) {
211 unsigned long nbloc(_Ppop->get_nbloc());
212 unsigned long nploidie(_Ppop->get_nploidie());
213 unsigned long taille,i,j;
214 Locus * Ploc;
215 // string nomloc;
216
217 taille = nbloc * nploidie;
218
219 while (_tabPall.size() < taille) {
220 _tabPall.push_back(0);
221 }
222 //cerr << "coucou 1" << endl;
223
224 taille = individu._tabPall.size();
225 for (i = 0; i < taille; i += nploidie) {
226 Ploc = _Ppop->_Pjeu->get_Plocus(individu._tabPall[i]->get_NomLocus());
227 //cerr << "coucou 2" << Ploc->get_nom() << endl;
228 for (j=0; j < nploidie; j++) {
229 ifPlacerAllele(Ploc,((Ploc->get_numloc() * nploidie) + j),individu._tabPall[i+j]);
230 }
231 }
232 }
233
234
235 unsigned long Individu::r_nballnonnuls(unsigned long locus, unsigned int nploidie) const {
236 // retourne le nb de copies d'allèles non nuls pour un locus
237 unsigned long resultat(0);
238 unsigned int j;
239
240 for (j=0;j < nploidie;j++) {
241 if (_tabPall[(locus * nploidie) + j]->_miss) continue;
242 resultat++;
243 }
244
245 return(resultat);
246 }
247
248
249 const string& Individu::get_nom_all(unsigned long numlocus, unsigned long numall) const {
250 // unsigned long nploidie(_Ppop->get_nploidie());
251 unsigned long nbloc(_Ppop->get_nbloc());
252
253 if (numlocus >= nbloc) throw Anomalie(11);
254 if (numall >= _nploidie) throw Anomalie(12);
255
256 return(_tabPall[(numlocus * _nploidie) + numall]->get_Nom());
257 }
258
259
260
261 void Individu::affiche (ostream& sortie, int choix) const {
262 unsigned long i,j;
263 unsigned int nploidie(_Ppop->get_nploidie());
264
265 switch (choix) {
266 case 1:
267 //affichage d'un individu au format genepop
268 sortie << _nom << " ,";
269
270 for (i=0;i< _tabPall.size(); i+=2) {
271 sortie << " " << _tabPall[i]->_nom << _tabPall[i+1]->_nom;
272 }
273 break;
274
275 case 2:
276 //affichage d'un individu au format genepop en tenant compte des introgressions
277 sortie << _nom << " ,";
278
279 for (i=0;i< _tabPall.size(); i+=2) {
280 sortie << " ";
281 if (_Ppop->_tabAllIntrogressant.Existe(_tabPall[i])) {
282 sortie << "00";
283 }
284 else {
285 sortie << _tabPall[i]->_nom;
286 }
287 if (_Ppop->_tabAllIntrogressant.Existe(_tabPall[i+1])) {
288 sortie << "00";
289 }
290 else {
291 sortie << _tabPall[i+1]->_nom;
292 }
293 }
294 break;
295
296 case 3:
297 //affichage d'un individu au format populations en tenant compte des introgressions
298 sortie << _nom << " ,";
299
300 for (i=0;i< _tabPall.size(); i+=nploidie) {
301 for (j=0; j < nploidie; j++) {
302 if (j==0) sortie << " ";
303 else sortie << ":";
304 if (_Ppop->_tabAllIntrogressant.Existe(_tabPall[i+j])) {
305 sortie << "*";
306 }
307 if (_tabPall[i+j]->r_estnul()) {
308 sortie << "00";
309 }
310 else {
311 sortie << _tabPall[i+j]->_nom;
312 }
313 // sortie << _tabPall[i+j]->_nom;
314 }
315 }
316 break;
317 case 4:
318 //affichage d'un individu au format genetix
319 sortie << _nom;
320 for (i=0; i < (10 - _nom.size());i ++) sortie << " ";
321
322 for (i=0;i< _tabPall.size(); i+=2) {
323 if ((_tabPall[i]->r_estnul()) || (_tabPall[i+1]->r_estnul())) sortie << " 000000";
324 else {
325 sortie << " ";
326 if (_tabPall[i]->_nom.size() == 2) sortie << "0";
327 else if (_tabPall[i]->_nom.size() == 1) sortie << "00";
328 sortie << _tabPall[i]->_nom;
329 if (_tabPall[i+1]->_nom.size() == 2) sortie << "0";
330 else if (_tabPall[i+1]->_nom.size() == 1) sortie << "00";
331 sortie << _tabPall[i+1]->_nom;
332
333 }
334 }
335 break;
336 case 5:
337 //affichage d'un individu au format Fstat
338
339 for (i=0;i< _tabPall.size(); i+=2) {
340 if ((_tabPall[i]->r_estnul()) || (_tabPall[i+1]->r_estnul())) sortie << "\t0";
341 else sortie << "\t" << _tabPall[i]->_nom << _tabPall[i+1]->_nom;
342 }
343 break;
344 default:
345 break;
346 }
347
348 }
349
350
351 unsigned long Individu::get_nballnuls (unsigned long locus) const {
352 //retourne le nombre d'alleles nuls de l'individu pour le locus
353 // long taille (_tabPall.size());
354 unsigned long i;
355 // unsigned long nploidie(_Ppop->get_nploidie());
356 unsigned long resultat(0);
357
358 for (i = 0; i < _nploidie; i++) {
359 if (_tabPall[(locus * _nploidie) + i]->_miss) resultat++;
360 }
361 return(resultat);
362 }
363 /** Suppresssion d'un locus */
364 void Individu::supprLocus(unsigned int numlocus){
365 unsigned int nbloc(_Ppop->get_nbloc());
366 unsigned int i;
367
368 if (numlocus >= nbloc) throw Anomalie(11);
369
370 for (i = 0; i < _nploidie; i ++) {
371 _tabPall.Suppr(numlocus * _nploidie);
372 }
373 }
374
375 void Individu::oPopulationsXML(unsigned int id, ostream & sortie, ostream & infos) const {
376 unsigned int i, j, nploidie(_Ppop->get_nploidie()), nbloc(_Ppop->get_nbloc());
377 biolib::vecteurs::ChaineCar idXML;
378
379 sortie << "<individual";
380 idXML = _Ppop->get_idXML();
381 idXML += "i";
382 idXML.AjEntier(id);
383 //set_idXML(idXML);
384 sortie << " id=\"" << idXML << "\"";
385 sortie << " nploidy=\"" << nploidie << "\"";
386 sortie << " name=\"" << get_nom() << "\"";
387 sortie << ">" << endl;
388
389 for (i=0; i < nbloc; i++) {
390 sortie << "<locus";
391 sortie << " allid=\"";
392 for (j=0; j < nploidie; j++) {
393 sortie << _tabPall[(i*nploidie)+j]->get_idXML();
394 if (j < (nploidie-1)) sortie << " ";
395 }
396 sortie << "\">";
397 sortie << "</locus>" << endl;
398 }
399
400 sortie << "</individual>" << endl;
401
402
403
404 }
405
406 void Individu::set_Pallele (Allele * Pall, unsigned int pos) {
407 unsigned int nploidie(get_nploidie());
408
409 if (pos >= nploidie) throw Anomalie(12);
410
411 pos = (Pall->get_Ploc()->get_numloc() * nploidie) + pos;
412
413 _tabPall[pos] = Pall;
414
415 }