ALMaSS Vole ODDox  1.1
The vole model description following ODdox protocol
GeneticMaterial.cpp
Go to the documentation of this file.
1 /*
2 *******************************************************************************************************
3 Copyright (c) 2011, Christopher John Topping, University of Aarhus
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided
7 that the following conditions are met:
8 
9 Redistributions of source code must retain the above copyright notice, this list of conditions and the
10 following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12 the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
17 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 ********************************************************************************************************
23 */
38 //---------------------------------------------------------------------------
39 //
40 
41 //#include <stdio.h>
42 #include "../Landscape/ls.h"
43 #include "../Vole/GeneticMaterial.h"
44 #include "../BatchALMaSS/BoostRandomGenerators.h"
45 
46 extern boost::variate_generator<base_generator_type&, boost::uniform_real<> > g_rand_uni;
47 
48 
49 
50 //---------------------------------------------------------------------------
51 
53 unsigned char g_MaxAllele;
54 
56  FILE * FreqFile = fopen("GeneticFrequencies.txt", "r" );
57  int data;
58  if ( !FreqFile ) {
59  g_msg->Warn( "GeneticFrequencies File missing", "" );
60  exit( 0 );
61  }
62  for ( int i = 0; i < 16; i++ ) {
63  for ( int j = 0; j < 4; j++ ) {
64  fscanf( FreqFile, "%d", & data );
65  AlleleNumber[ i ] [ j ] = data;
66  }
67  }
68  for ( int i = 16; i < 32; i++ ) {
69  for ( int j = 0; j < 16; j++ ) {
70  fscanf( FreqFile, "%d", & data );
71  AlleleNumber[ i ] [ j ] = data;
72  }
73  }
74  fclose( FreqFile );
75 }
76 
77 //---------------------------------------------------------------------------
78 
79 /*
80 
81 void AlleleFreq::Flush() {
82  for ( int loc = 0; loc < 16; loc++ ) {
83  HO[ loc ] = 0;
84  HE[ loc ] = 0;
85  NoAlleles[ loc ] = 0;
86  for ( int al = 0; al < 4; al++ ) {
87  AlleleFrequency[ loc ] [ al ] = 0;
88  AlleleNumber[ loc ] [ al ] = 0;
89  }
90  }
91  for ( int loc = 16; loc < 32; loc++ ) {
92  HO[ loc ] = 0;
93  HE[ loc ] = 0;
94  NoAlleles[ loc ] = 0;
95  for ( int al = 0; al < 16; al++ ) {
96  AlleleFrequency[ loc ] [ al ] = 0;
97  AlleleNumber[ loc ] [ al ] = 0;
98  }
99  }
100 }
101 
102 //---------------------------------------------------------------------------
103 
104 
105 void AlleleFreq::CalcNoAlleles() {
106  // Counts the number of alleles existing for each locus
107  for ( int loc = 0; loc < 16; loc++ ) {
108  for ( int al = 0; al < 4; al++ ) {
109  if ( AlleleNumber[ loc ] [ al ] > 0 ) NoAlleles[ loc ] ++;
110  }
111  }
112  for ( int loc = 16; loc < 32; loc++ ) {
113  for ( int al = 0; al < 16; al++ ) {
114  if ( AlleleNumber[ loc ] [ al ] > 0 ) NoAlleles[ loc ] ++;
115  }
116  }
117 }
118 
119 //---------------------------------------------------------------------------
120 
121 
122 void AlleleFreq::CalcHE() {
123  for ( int loc = 0; loc < 16; loc++ ) {
124  float NAlleles = 0;
125  // How many alleles for each locus
126  for ( int al = 0; al < 4; al++ ) {
127  NAlleles += ( float )AlleleNumber[ loc ] [ al ];
128  }
129  // Calculated the proportion
130  for ( int al = 0; al < 4; al++ ) {
131  AlleleFrequency[ loc ] [ al ] = ( float )AlleleNumber[ loc ] [ al ] / NAlleles;
132  }
133  //HE=2ab+2bc+2bd+2ac+2ad+2cd
134  HE[ loc ] = ( 2 * AlleleFrequency[ loc ] [ 0 ] * AlleleFrequency[ loc ] [ 1 ] )
135  + ( 2 * AlleleFrequency[ loc ] [ 1 ] * AlleleFrequency[ loc ] [ 2 ] )
136  + ( 2 * AlleleFrequency[ loc ] [ 1 ] * AlleleFrequency[ loc ] [ 3 ] )
137  + ( 2 * AlleleFrequency[ loc ] [ 0 ] * AlleleFrequency[ loc ] [ 2 ] )
138  + ( 2 * AlleleFrequency[ loc ] [ 0 ] * AlleleFrequency[ loc ] [ 3 ] )
139  + ( 2 * AlleleFrequency[ loc ] [ 2 ] * AlleleFrequency[ loc ] [ 3 ] );
140  }
141 }
142 
143 //---------------------------------------------------------------------------
144 
145 
146 void AlleleFreq::CalcHO( int si ) {
147  for ( int loc = 0; loc < 16; loc++ ) {
148  HO[ loc ] /= ( float )si;
149  }
150 }
151 
152 //---------------------------------------------------------------------------
153 
154 
155 void AlleleFreq::CalcAF() {
156  for ( int loc = 0; loc < 16; loc++ ) {
157  int NoAlleles = 0;
158  for ( int al = 0; al < 4; al++ ) NoAlleles += AlleleNumber[ loc ] [ al ];
159  for ( int al = 0; al < 4; al++ ) {
160  AlleleFrequency[ loc ] [ al ] = AlleleNumber[ loc ] [ al ] / ( float )NoAlleles;
161  }
162  }
163 }
164 
165 */
166 
167 //---------------------------------------------------------------------------
168 
170  SetAllele(0,1,0);
171 }
172 //---------------------------------------------------------------------------
174  SetAllele(0,1,1);
175 }
176 //---------------------------------------------------------------------------
177 
179  SetAllele(0,0,0);
180 }
181 //---------------------------------------------------------------------------
183  SetAllele(0,0,1);
184 }
185 //---------------------------------------------------------------------------
186 
188  return GetAllele(0,0);
189 }
190 //---------------------------------------------------------------------------
192  return GetAllele(0,1);
193 }
194 //---------------------------------------------------------------------------
195 
196 void GeneticMaterial::SetAllele( int locus, uint32 value, int Chromo ) {
197  Chromo*=3; // now 0 or 3
198  uint32 mask;
199  if (locus<16) {
200  // Get the right chromosome
201  // Create the mask
202  // Does it twice because 32 bits coding for 16 loci
203  mask = 0x03 << locus;
204  mask = mask << locus;
205  // just to make make sure it is 0-3
206  value = value & 0x03;
207  // create the value mask
208  value = value << locus;
209  value = value << locus;
210  // clear the locus
211  Chromosome[ Chromo ] &= ~mask;
212  // write the value
213  Chromosome[ Chromo ] |= value;
214  } else {
215  Chromo++; // now 1 or 4
216  locus-=16;
217  if (locus>=8) {
218  Chromo++;
219  locus-=8;
220  }
221  mask = 0x0F << (locus*4);
222  value = value & 0x0f; // make sure there was no extra stuff added!
223  // create the value mask
224  value = value << (locus*4);
225  Chromosome[ Chromo ] &= ~mask;
226  // write the value
227  Chromosome[ Chromo ] |= value;
228  }
229 }
230 
231 //---------------------------------------------------------------------------
232 
233 uint32 GeneticMaterial::GetAllele( int locus, int Chromo ) {
234  uint32 value;
235  // Get the right chromosome
236  // if Chromo==0 then 0-2, else 3-5
237  Chromo *= 3; // 0 or 3
238  if ( locus < 16 ) {
239  // Shift it so the locus is in the last two bits
240  // Does it twice because 32 bis coding for 16 loci
241  value = Chromosome[ Chromo ] >> locus;
242  value = ( value >> locus ) & 0x03;
243  } else {
244  Chromo++; // 1 or 4
245  locus -= 16; // Now 0 to 16
246  if ( locus >= 8 ) {
247  Chromo++; // 2 or 5
248  locus -= 8;
249  }
250  value = Chromosome[ Chromo ] >> ( locus * 4 );
251  value = value & 0x0f;
252  }
253  return value;
254 }
255 
256 //---------------------------------------------------------------------------
257 
258 void GeneticMaterial::PrintChromosome( char * C, int Chromo ) {
259  for ( int i = 0; i < 16; i++ ) {
260  uint32 allele = GetAllele( i, Chromo );
261  switch ( allele ) {
262  case 0:
263  C[ i ] = 'a';
264  break;
265  case 1:
266  C[ i ] = 'b';
267  break;
268  case 2:
269  C[ i ] = 'c';
270  break;
271  case 3:
272  C[ i ] = 'd';
273  break;
274  case 4:
275  C[ i ] = 'e';
276  break;
277  case 5:
278  C[ i ] = 'f';
279  break;
280  case 6:
281  C[ i ] = 'g';
282  break;
283  case 7:
284  C[ i ] = 'h';
285  break;
286  case 8:
287  C[ i ] = 'i';
288  break;
289  case 9:
290  C[ i ] = 'j';
291  break;
292  case 10:
293  C[ i ] = 'k';
294  break;
295  case 11:
296  C[ i ] = 'l';
297  break;
298  case 12:
299  C[ i ] = 'm';
300  break;
301  case 13:
302  C[ i ] = 'n';
303  break;
304  case 14:
305  C[ i ] = 'o';
306  break;
307  case 15:
308  C[ i ] = 'p';
309  break;
310  }
311  }
312  C[ 16 ] = 0;
313 }
314 
315 //---------------------------------------------------------------------------
316 
318  // OK OK there is an easy way to do this by calling HeterozygosityCount and
319  // subtracting this from 32, but just is case that little bit of saved time is useful:
320  int homozyg=0;
321  for ( int i = 0; i < 32; i++ ) {
322  if ( GetAllele( i, 0 ) == GetAllele( i, 1 ) ) homozyg++;
323  }
324  return homozyg;
325 }
326 //---------------------------------------------------------------------------
327 
329  int heterozyg = 0;
330  for ( int i = 0; i < 32; i++ ) {
331  if ( GetAllele( i, 0 ) != GetAllele( i, 1 ) ) heterozyg++;
332  }
333  return heterozyg;
334 }
335 
336 //---------------------------------------------------------------------------
337 
339  for ( int i = 0; i < 32; i++ ) {
340  // For each locus
341  // Choose which chromosome for each parent
342  int g0 = random( 2 );
343  int g1 = random( 2 );
344  // get the two alleles
345  uint32 a0 = Gene1->GetAllele( i, g0 );
346  uint32 a1 = Gene2->GetAllele( i, g1 );
347  // put a0 into chromo0 & a1 to chromo1 & vice versa
348  SetAllele( i, a0, 0 );
349  SetAllele( i, a1, 1 );
350  }
351 }
352 
353 //---------------------------------------------------------------------------
354 
356  // ensure zeros in all loci
357  for ( int i = 0; i < 6; i++ ) Chromosome[ i ] = 0;
358 }
359 
360 //---------------------------------------------------------------------------
361 
367  uint32 value;
368  for ( int l = 0; l < 32; l++ ) {
369  //if ( l < 16 ) c = 0; else if ( l < 24 ) c = 1; else c = 2;
370 
371  int chance = random( 1000 );
372  uint32 index = 0;
373  while ( chance > Al->SupplyAN( l, index ) ) {
374  index++;
375  }
376  value = index;
377  // set the value
378  SetAllele( l, value, 0 );
379  chance = random( 1000 );
380  index = 0;
381  while ( chance > Al->SupplyAN( l, index ) ) {
382  index++;
383  }
384  value = index;
385  // set the value
386  SetAllele( l, value, 1 );
387  }
388 }
389 
390 //---------------------------------------------------------------------------
395  return 1.0;
396  /* OLD CODE OUTDATED uint32 allele0a = GetAllele(0,0); // loci 0 uint32 allele1a = GetAllele(1,0); // loci 1
397  uint32 allele0b = GetAllele(0,1); // loci 0 uint32 allele1b = GetAllele(1,1); // loci 1
398  // Initial rules are that locus 0 and 1 are ac or bd then OK // likewise they may be ca or db
399  // any other combination is bad // Same for loci 1 bool IsOK0=false; switch(allele0a) { case 0: //a
400  if (allele1a==2) IsOK0=true; break; case 1: //b if (allele1a==3) IsOK0=true; break; case 2: //c
401  if (allele1a==0) IsOK0=true; break; case 3: //d if (allele1a==1) IsOK0=true; break; default:
402  FILE* errfile=fopen("GeneticErrorFile.Txt","w"); fprintf(errfile,"Unknown Allele Number\n"); fclose(errfile); exit(10);
403  break; } bool IsOK1=false; switch(allele0b) { case 0: if (allele1b==2) IsOK1=true; break; case 1:
404  if (allele1b==3) IsOK1=true; break; case 2: if (allele1b==0) IsOK1=true; break; case 3: if (allele1b==1) IsOK1=true; break;
405  default: FILE* errfile=fopen("GeneticErrorFile.Txt","w"); fprintf(errfile,"Unknown Allele Number\n"); fclose(errfile);
406  exit(11); break; } // determine the effect of the genetics // In the simple case it is good or bad
407  if (IsOK0 && IsOK1) return 1.0; else return 0.05; */
408 }
409 
410 //---------------------------------------------------------------------------
415  return 1.0;
416  // OLD CODE OUTDATED
417  /* Ditte's Simulation Version uint32 allele0a = GetAllele(0,0); // loci 0 uint32 allele1a = GetAllele(1,0); // loci 1
418  uint32 allele0b = GetAllele(0,1); // loci 0 uint32 allele1b = GetAllele(1,1); // loci 1
419  // Initial rules are that if 0a and 1a are 0 & 2 or 2 & 0 then OK (a,c)(c,a)
420  // likewise they may be 1 & 3 or 3 & 1 (b,d) (d,b) // any other combination is bad // Same for loci 1 bool IsOK0=false;
421  switch(allele0a) { case 0: if (allele1a==2) IsOK0=true; break; case 1: if (allele1a==3) IsOK0=true; break; case 2:
422  if (allele1a==0) IsOK0=true; break; case 3: if (allele1a==1) IsOK0=true; break; default: assert(NULL); break; }
423  bool IsOK1=false; switch(allele0b) { case 0: if (allele1b==2) IsOK1=true; break; case 1: if (allele1b==3) IsOK1=true; break;
424  case 2: if (allele1b==0) IsOK1=true; break; case 3: if (allele1b==1) IsOK1=true; break; default: assert(NULL); break; }
425  // determine the effect of the genetics // In the simple case it is good or bad
426  if (IsOK0 && IsOK1) return 1.0; else return 0.9;
427 
428  // Lar Bach's version uint32 allele2a = GetAllele(2,0); // loci 0 uint32 allele2b = GetAllele(2,1); // loci 0
429  float result= -0.5; switch (allele2a) { case 0: case 2: case 3: break; default: result+=0.5; } switch (allele2b) { case 0:
430  case 2: case 3: break; default: result+=0.5; break; } return result;
431  // returns -0.5 if homozygous aa, 0.5 if homozygous bb & het=0
432 
433  */
434 }
435 
436 //---------------------------------------------------------------------------
437 
441 // Each locus is checked and if mutation chance equals 1 the allele and chromosome
442 //is chosen at random
443 
445 {
446  for ( int i = 0; i < 16; i++ ) {
447  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance
448  {
449  SetAllele( i, random( 4 ), random( 2 ) );
450  }
451  }
452  for ( int i = 16; i < 32; i++ ) {
453  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance
454  {
455  SetAllele( i, random( 16 ), random( 2 ) );
456  }
457  }
458 }
459 
460 //---------------------------------------------------------------------------
461 
465 void GeneticMaterial::Mutation_1ab() // random allele choice
466 {
467  // Only used when all loci have only two alleles!
468  for ( int i = 0; i < 32; i++ ) {
469  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance
470  {
471  SetAllele( i, random( 2 ), random( 2 ) );
472  }
473  }
474 }
475 
476 //---------------------------------------------------------------------------
477 
481 void GeneticMaterial::Mutation_2() // move one allele +/-
482 {
483  for ( int i = 0; i < 16; i++ ) {
484  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance
485  {
486  int strand = random( 2 );
487  int allele = GetAllele( i, strand );
488  if ( random( 2 ) == 1 ) allele++; else allele--;
489  if ( allele == -1 ) allele = 3; else if ( allele == 4 ) allele = 0;
490  SetAllele( i, allele, strand );
491  }
492  }
493  for ( int i = 16; i < 32; i++ ) {
494  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance
495  {
496  int strand = random( 2 );
497  int allele = GetAllele( i, strand );
498  if ( random( 2 ) == 1 ) allele++; else allele--;
499  if ( allele == -1 ) allele = 15; else if ( allele == 16 ) allele = 0;
500  SetAllele( i, allele, strand );
501  }
502  }
503 }
504 
505 //---------------------------------------------------------------------------
506 
511 {
512  // NB Only works for the first 16 loci
513  for ( int i = 0; i < 16; i++ ) {
514  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance
515  {
516  int strand = random( 2 );
517  int allele = GetAllele( i, strand );
518  switch ( allele ) {
519  case 0:
520  allele = 1;
521  break;
522  case 1:
523  allele = 0;
524  break;
525  case 2:
526  allele = 3;
527  break;
528  case 3:
529  allele = 2;
530  break;
531  }
532  SetAllele( i, allele, strand );
533  }
534  }
535 }
536 //---------------------------------------------------------------------------
537 
539 {
541  if (g_rand_uni() < MutationChance) // one chance in Mutation Chance
542  {
543  int strand = random(2);
544  int allele = GetAllele(0, strand);
545  allele = (allele + 1) && 1; // only 1 or 0 is allowed
546  }
547 }
548 
549 //---------------------------------------------------------------------------
550 
551 
552 //---------------------------------------------------------------------------
553 // GeneticMaterial1616
554 //---------------------------------------------------------------------------
555 
556 // const int MutChance = 2000;
557 
559  FILE * FreqFile = fopen("GeneticFrequencies_1616_Mut.txt", "r" );
560  int data;
561  if ( !FreqFile ) {
562  g_msg->Warn( "GeneticFrequencies_1616_Mut.txt File missing - AllelFreq1616", "" );
563  exit( 0 );
564  }
565  for ( int i = 0; i < 16; i++ ) {
566  for ( int j = 0; j < 16; j++ ) {
567  fscanf( FreqFile, "%d", & data );
568  AlleleNumber[ i ] [ j ] = data;
569  }
570  }
571 
572  fclose( FreqFile );
573 }
574 //---------------------------------------------------------------------------
576  FILE * FreqFile = fopen("GeneticFrequencies_256_16_Mut.txt", "r" );
577  int data;
578  if ( !FreqFile ) {
579  g_msg->Warn( "GeneticFrequencies_256_16_Mut.txt File missing - AllelFreq1616", "" );
580  exit( 0 );
581  }
582  for ( int i = 0; i < 256; i++ ) {
583  for ( int j = 0; j < 16; j++ ) {
584  fscanf( FreqFile, "%d", & data );
585  AlleleNumber[ i ] [ j ] = data;
586  }
587  }
588 
589  fclose( FreqFile );
590 }
591 //---------------------------------------------------------------------------
593  // ensure zeros in all loci
594  for ( int i = 0; i < 4; i++ ) Chromosome[ i ] = 0;
595 }
596 
597 //---------------------------------------------------------------------------
598 
599 uint32 GeneticMaterial1616::GetAllele( unsigned int locus, unsigned int Chromo ) {
600  // This is for 32 bit machines, 64bit is easier
601  // locus must be 0 to 15
602  //Chromo must be either 0 or 1
603  // These debug tests below are costly so turn off in release code
604  #ifdef __GENDEBUG
605  if (Chromo>1) {
606  g_msg->Warn( "Chromo > 1 in GeneticMaterial1616 - get allele", NULL );
607  exit( 0 );
608  }
609  if (locus>15) {
610  g_msg->Warn( "locus > 15 in GeneticMaterial1616 - get allele", NULL );
611  exit( 0 );
612  }
613  #endif
614 
615  uint32 segment=((Chromo<<1) | ((locus & 0x08)>>3)); // locates on which segment in the comosome we are in
616  uint32 allele=0x0F & (Chromosome[segment]>>((locus & 0x07)<<2)); //Locates the allele and shifts it down to the first postition to read
617  return allele;
618 }
619 //---------------------------------------------------------------------------
620 
621 void GeneticMaterial1616::SetAllele( unsigned int locus, uint32 value, unsigned int Chromo ) {
622  // This is for 32 bit machines, 64bit is easier
623  // locus must be 0 to 15
624  //Chromo must be either 0 or 1
625  // These debug tests below are costly so turn off in release code
626  #ifdef __GENDEBUG
627  if (Chromo>1) {
628  g_msg->Warn( "Chromo > 1 in GeneticMaterial1616 - set allele", NULL );
629  exit( 0 );
630  }
631  if (locus>15) {
632  g_msg->Warn( "locus > 15 in GeneticMaterial1616 - set allele", NULL );
633  exit( 0 );
634  }
635  #endif
636 
637  uint32 segment=((Chromo<<1) | ((locus & 0x08)>>3));
638  uint32 mask = 0x0F; //0000 1111F
639  // Need to shift the mask over the correct allele
640  mask=mask<<((locus&7)<<2);
641  value = value & 0x0f; // make sure there was no extra stuff added!
642  // create the value mask
643  value = value << ((locus&7)<<2);
644  Chromosome[ segment ] &= ~mask; // get rid of the current info
645  Chromosome[ segment ] |= value; // write the value
646 }
647 //---------------------------------------------------------------------------
648 
649 void GeneticMaterial1616::PrintChromosome( char * C, unsigned int Chromo ) {
650  for ( int i = 0; i < 16; i++ ) {
651  uint32 allele = GetAllele( i, Chromo );
652  switch ( allele ) {
653  case 0:
654  C[ i ] = 'a';
655  break;
656  case 1:
657  C[ i ] = 'b';
658  break;
659  case 2:
660  C[ i ] = 'c';
661  break;
662  case 3:
663  C[ i ] = 'd';
664  break;
665  case 4:
666  C[ i ] = 'e';
667  break;
668  case 5:
669  C[ i ] = 'f';
670  break;
671  case 6:
672  C[ i ] = 'g';
673  break;
674  case 7:
675  C[ i ] = 'h';
676  break;
677  case 8:
678  C[ i ] = 'i';
679  break;
680  case 9:
681  C[ i ] = 'j';
682  break;
683  case 10:
684  C[ i ] = 'k';
685  break;
686  case 11:
687  C[ i ] = 'l';
688  break;
689  case 12:
690  C[ i ] = 'm';
691  break;
692  case 13:
693  C[ i ] = 'n';
694  break;
695  case 14:
696  C[ i ] = 'o';
697  break;
698  case 15:
699  C[ i ] = 'p';
700  break;
701  }
702  }
703  C[ 16 ] = 0;
704 }
705 //---------------------------------------------------------------------------
706 
708  // OK OK there is an easy way to do this by calling HeterozygosityCount and
709  // subtracting this from 32, but just is case that little bit of saved time is useful:
710  int homozyg=0;
711  for ( int i = 0; i < 16; i++ ) {
712  if ( GetAllele( i, 0 ) == GetAllele( i, 1 ) ) homozyg++;
713  }
714  return homozyg;
715 }
716 //---------------------------------------------------------------------------
717 
719  int heterozyg = 0;
720  for ( int i = 0; i < 16; i++ ) {
721  if ( GetAllele( i, 0 ) != GetAllele( i, 1 ) ) heterozyg++;
722  }
723  return heterozyg;
724 }
725 //---------------------------------------------------------------------------------------
726 
728  // Is called with hers and his genes
729  for ( int i = 0; i < 16; i++ ) {
730  // For each locus
731  // Choose which chromosome for each parent
732  int g0 = random(2);
733  int g1 = random(2);
734  // get the two alleles
735  uint32 a0 = Gene1->GetAllele( i, g0 );
736  uint32 a1 = Gene2->GetAllele( i, g1 );
737  // put a0 into chromo0 & a1 to chromo1 & vice versa
738  SetAllele( i, a0, 0 );
739  SetAllele( i, a1, 1 );
740  }
741 }
742 
743 //---------------------------------------------------------------------------
745  SetAllele(0,1,0);
746 }
747 //---------------------------------------------------------------------------
749  SetAllele(0,1,1);
750 }
751 //---------------------------------------------------------------------------
752 
754  SetAllele(0,0,0);
755 }
756 //---------------------------------------------------------------------------
758  SetAllele(0,0,1);
759 }
760 //---------------------------------------------------------------------------
761 
763  return GetAllele(0,0);
764 }
765 //---------------------------------------------------------------------------
767  return GetAllele(0,1);
768 }
769 //---------------------------------------------------------------------------
770 
776  uint32 value; //, c;
777  for ( int l = 0; l < 16; l++ ) {
778  // if ( l < 16 ) c = 0; else c = 2;
779 
780  int chance = random( 1000 );
781  uint32 index = 0;
782  while ( chance >= Al->SupplyAN( l, index ) ) {
783  index++;
784  }
785  value = index;
786  // set the value
787  SetAllele( l, value, 0 );
788 
789  chance = random( 1000 );
790  index = 0;
791  while ( chance >= Al->SupplyAN( l, index ) ) {
792  index++;
793  }
794  value = index;
795  // set the value
796  SetAllele( l, value, 1 );
797  }
798 }
799 //---------------------------------------------------------------------------
800 
804 // Each locus is checked and if mutation chance equals 1 the allel and chromosome
805 //is chosen at random
806 
808 {
809  if (MutationChance != 0)
810  {
811  for ( int i = 0; i < 16; i++ )
812  {
813  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance
814  {
815  SetAllele( i, random( 16 ), random( 2 ) );
816  }
817  }
818  }
819 }
820 
821 //---------------------------------------------------------------------------
822 
826 void GeneticMaterial1616::Mutation_2() // move one allele +
827 {
828  if (MutationChance != 0){
829  for ( int i = 0; i < 16; i++ ) {
830  if (g_rand_uni() < MutationChance) // one chance in Mutation Chance for the locus
831  {
832  unsigned int strand = random( 2 ); // kromosom 0 or 1
833  unsigned int allele = GetAllele( i, strand );
834  allele++;
835  allele&=0x0f; //and with 1111 - allels curls up 15 -> 0
836  SetAllele( i, allele, strand );
837  }
838  }
839 }
840 }
841 
842 //---------------------------------------------------------------------------
843 
847 void GeneticMaterial1616::Mutation_3() // move one allele + or -
848 {
849  if (MutationChance != 0){
850  for ( int i = 0; i < 16; i++ ) {
851  if ( g_rand_uni() < MutationChance) // one chance in Mutation Chance for the locus
852  {
853  unsigned strand = random( 2 ); // kromosom 0 or 1
854  unsigned allele = GetAllele( i, strand );
855  //if (strand == 1) allele++; else allele--;
856  if ( random( 2 ) == 1 ) allele++; else allele--;
857 
858  //For mutations less than 0 and more than 15 the mutation should result in 1 or 14
859 
860  if (allele > 0x0f) // Test to see if mutation to negative or above 15
861  {
862  allele |= 0x1C;
863  if(allele > 0x01C) { allele &= 0x01;}
864  else allele >>=1;
865  //allele|= 0xF0; // (240) Sets neg to 0, and 16 to 15 -> to avoid curling up
866  //allele = (~allele);
867  }
868 
869  SetAllele( i, allele, strand );
870  }
871  }
872 }
873 }
874 
875 //---------------------------------------------------------------------------
877  // ensure zeros in all loci
878  for ( int i = 0; i < 32; i++ ) Chromosome[ i ] = 0;
879 }
880 
881 //---------------------------------------------------------------------------
882 
883 void GeneticMaterial256_16::SetAllele( unsigned int locus, uint32 value, unsigned int Chromo ) {
884  if (Chromo==1) locus +=16;
885  Chromosome[ locus ] = (unsigned char) value;
886 }
887 //---------------------------------------------------------------------------
888 
889 uint32 GeneticMaterial256_16::GetAllele( unsigned int locus, unsigned int Chromo ) {
890  if (Chromo==1) locus +=16;
891  return (uint32) Chromosome[ locus ];
892 }
893 //---------------------------------------------------------------------------
894 
898 void GeneticMaterial256_16::Mutation_3() // move one allele + or -
899 {
900  if (MutationChance != 0){
901  for ( int i = 0; i < 16; i++ ) {
902  if ( g_rand_uni() < MutationChance ) // one chance in Mutation Chance for the locus
903  {
904  unsigned strand = random( 2 ); // kromosom 0 or 1
905  int allele = GetAllele( i, strand );
906  if ( random( 2 ) == 1 ) allele++; else allele--;
907  //For mutations less than 0 and more than 256 the mutation should result in 1 or g_MaxAllele-1
908  if (allele > g_MaxAllele) allele-=2;
909  else if (allele < 0) allele = 1;
910  SetAllele( i, (uint32) allele, strand );
911  }
912  }
913  }
914 }
915 //---------------------------------------------------------------------------
916 
918  SetAllele(0,1,0);
919 }
920 //---------------------------------------------------------------------------
922  SetAllele(0,1,1);
923 }
924 //---------------------------------------------------------------------------
925 
927  SetAllele(0,0,0);
928 }
929 //---------------------------------------------------------------------------
931  SetAllele(0,0,1);
932 }
933 //---------------------------------------------------------------------------
934 
936  return GetAllele(0,0);
937 }
938 //---------------------------------------------------------------------------
940  return GetAllele(0,1);
941 }
942 //---------------------------------------------------------------------------
943 
945  // Is called with hers and his genes
946  for ( int i = 0; i < 16; i++ ) {
947  // For each locus
948  // Choose which chromosome for each parent
949  int g0 = random(2);
950  int g1 = random(2);
951  // get the two alleles
952  uint32 a0 = Gene1->GetAllele( i, g0 );
953  uint32 a1 = Gene2->GetAllele( i, g1 );
954  // put a0 into chromo0 & a1 to chromo1 & vice versa
955  SetAllele( i, a0, 0 );
956  SetAllele( i, a1, 1 );
957  }
958 }
959 //---------------------------------------------------------------------------
960 
966  uint32 value; //, c;
967  for ( int l = 0; l < 16; l++ ) {
968  int chance = random( 1000 );
969  uint32 index = 0;
970  while ( chance >= Al->SupplyAN( l, index ) ) {
971  index++;
972  }
973  value = index;
974  // set the value
975  SetAllele( l, value, 0 );
976  chance = random( 1000 );
977  index = 0;
978  while ( chance >= Al->SupplyAN( l, index ) ) {
979  index++;
980  }
981  value = index;
982  // set the value
983  SetAllele( l, value, 1 );
984  }
985 }
986 //---------------------------------------------------------------------------
GeneticMaterial::UnsetGeneticFlag
void UnsetGeneticFlag()
Definition: GeneticMaterial.cpp:178
GeneticMaterial1616::Mutation_3
void Mutation_3()
Definition: GeneticMaterial.cpp:847
GeneticMaterial::HeterozygosityCount
int HeterozygosityCount()
Definition: GeneticMaterial.cpp:328
GeneticMaterial256_16::Initiation
void Initiation(AlleleFreq256_16 *Al)
Definition: GeneticMaterial.cpp:965
GeneticMaterial::GeneticMaterial
GeneticMaterial()
Definition: GeneticMaterial.cpp:355
GeneticMaterial::Chromosome
uint32 Chromosome[6]
Definition: GeneticMaterial.h:97
GeneticMaterial1616::UnsetDirectFlag
void UnsetDirectFlag()
Definition: GeneticMaterial.cpp:757
GeneticMaterial::ScoreHQThreshold
float ScoreHQThreshold()
Definition: GeneticMaterial.cpp:414
GeneticMaterial1616::Recombine
void Recombine(GeneticMaterial1616 *Gene1, GeneticMaterial1616 *Gene2)
Definition: GeneticMaterial.cpp:727
GeneticMaterial::UnsetDirectFlag
void UnsetDirectFlag()
Definition: GeneticMaterial.cpp:182
AlleleFreq256_16::SupplyAN
int SupplyAN(int loc, int al)
Definition: GeneticMaterial.h:189
GeneticMaterial256_16::Mutation_3
void Mutation_3()
Definition: GeneticMaterial.cpp:898
GeneticMaterial::GetDirectFlag
uint32 GetDirectFlag()
Definition: GeneticMaterial.cpp:191
GeneticMaterial256_16::SetAllele
void SetAllele(unsigned int locus, uint32 value, unsigned int Chromo)
Definition: GeneticMaterial.cpp:883
GeneticMaterial::SetDirectFlag
void SetDirectFlag()
Definition: GeneticMaterial.cpp:173
AlleleFreq1616::AlleleNumber
int AlleleNumber[16][16]
Definition: GeneticMaterial.h:128
GeneticMaterial::Mutation_2
void Mutation_2()
Definition: GeneticMaterial.cpp:481
GeneticMaterial1616::Mutation_2
void Mutation_2()
Definition: GeneticMaterial.cpp:826
AlleleFreq1616::AlleleFreq1616
AlleleFreq1616()
Definition: GeneticMaterial.cpp:558
AlleleFreq::AlleleNumber
int AlleleNumber[32][16]
Definition: GeneticMaterial.h:64
GeneticMaterial256_16::UnsetGeneticFlag
void UnsetGeneticFlag()
Definition: GeneticMaterial.cpp:926
GeneticMaterial
Class for the genetic material optionally carried by animals in ALMaSS.
Definition: GeneticMaterial.h:94
GeneticMaterial1616::GetGeneticFlag
uint32 GetGeneticFlag()
Definition: GeneticMaterial.cpp:762
GeneticMaterial256_16::Chromosome
unsigned char Chromosome[32]
Definition: GeneticMaterial.h:195
GeneticMaterial256_16::GeneticMaterial256_16
GeneticMaterial256_16()
Definition: GeneticMaterial.cpp:876
GeneticMaterial1616::SetGeneticFlag
void SetGeneticFlag()
Definition: GeneticMaterial.cpp:744
GeneticMaterial::Mutation_1
void Mutation_1()
Definition: GeneticMaterial.cpp:444
GeneticMaterial256_16::SetDirectFlag
void SetDirectFlag()
Definition: GeneticMaterial.cpp:921
AlleleFreq
Class to handle statistics and constructs based on allele frequencies.
Definition: GeneticMaterial.h:61
g_MaxAllele
unsigned char g_MaxAllele
Definition: GeneticMaterial.cpp:53
GeneticMaterial::GetGeneticFlag
uint32 GetGeneticFlag()
Definition: GeneticMaterial.cpp:187
AlleleFreq256_16::AlleleFreq256_16
AlleleFreq256_16()
Definition: GeneticMaterial.cpp:575
GeneticMaterial1616::GeneticMaterial1616
GeneticMaterial1616()
Definition: GeneticMaterial.cpp:592
GeneticMaterial::SetAllele
void SetAllele(int pos, uint32 value, int Chromosome)
Definition: GeneticMaterial.cpp:196
GeneticMaterial::Mutation_3
void Mutation_3()
Definition: GeneticMaterial.cpp:510
MutationChance
double MutationChance
Definition: GeneticMaterial.cpp:52
GeneticMaterial::GetAllele
uint32 GetAllele(int pos, int Chromosome)
Definition: GeneticMaterial.cpp:233
GeneticMaterial1616::Mutation_1
void Mutation_1()
Definition: GeneticMaterial.cpp:807
GeneticMaterial1616::HomozygosityCount
int HomozygosityCount()
Definition: GeneticMaterial.cpp:707
GeneticMaterial1616::SetAllele
void SetAllele(unsigned int locus, uint32 value, unsigned int Chromo)
Definition: GeneticMaterial.cpp:621
GeneticMaterial1616::Initiation
void Initiation(AlleleFreq1616 *Al)
Definition: GeneticMaterial.cpp:775
GeneticMaterial256_16::GetGeneticFlag
uint32 GetGeneticFlag()
Definition: GeneticMaterial.cpp:935
GeneticMaterial::Initiation
void Initiation(AlleleFreq *Al)
Definition: GeneticMaterial.cpp:366
GeneticMaterial256_16
Definition: GeneticMaterial.h:192
g_rand_uni
boost::variate_generator< base_generator_type &, boost::uniform_real<> > g_rand_uni
GeneticMaterial256_16::GetAllele
uint32 GetAllele(unsigned int locus, unsigned int Chromo)
Definition: GeneticMaterial.cpp:889
AlleleFreq::SupplyAN
int SupplyAN(int loc, int al)
Definition: GeneticMaterial.h:71
GeneticMaterial::PrintChromosome
void PrintChromosome(char *C, int Chromosome)
Definition: GeneticMaterial.cpp:258
GeneticMaterial256_16::GetDirectFlag
uint32 GetDirectFlag()
Definition: GeneticMaterial.cpp:939
GeneticMaterial1616::GetDirectFlag
uint32 GetDirectFlag()
Definition: GeneticMaterial.cpp:766
GeneticMaterial1616::HeterozygosityCount
int HeterozygosityCount()
Definition: GeneticMaterial.cpp:718
AlleleFreq1616::SupplyAN
int SupplyAN(int loc, int al)
Definition: GeneticMaterial.h:135
GeneticMaterial::Mutation_4
void Mutation_4()
Definition: GeneticMaterial.cpp:538
GeneticMaterial1616::GetAllele
uint32 GetAllele(unsigned int locus, unsigned int Chromo)
Definition: GeneticMaterial.cpp:599
AlleleFreq::AlleleFreq
AlleleFreq()
Definition: GeneticMaterial.cpp:55
GeneticMaterial256_16::Recombine
void Recombine(GeneticMaterial256_16 *Gene1, GeneticMaterial256_16 *Gene2)
Definition: GeneticMaterial.cpp:944
GeneticMaterial::SetGeneticFlag
void SetGeneticFlag()
Definition: GeneticMaterial.cpp:169
GeneticMaterial1616::UnsetGeneticFlag
void UnsetGeneticFlag()
Definition: GeneticMaterial.cpp:753
GeneticMaterial::HomozygosityCount
int HomozygosityCount()
Definition: GeneticMaterial.cpp:317
GeneticMaterial1616::PrintChromosome
void PrintChromosome(char *C, unsigned int Chromosome)
Definition: GeneticMaterial.cpp:649
AlleleFreq256_16::AlleleNumber
int AlleleNumber[256][16]
Definition: GeneticMaterial.h:182
GeneticMaterial256_16::SetGeneticFlag
void SetGeneticFlag()
Definition: GeneticMaterial.cpp:917
GeneticMaterial::Recombine
void Recombine(GeneticMaterial *Gen21, GeneticMaterial *Gene2)
Definition: GeneticMaterial.cpp:338
AlleleFreq1616
Definition: GeneticMaterial.h:125
GeneticMaterial1616::SetDirectFlag
void SetDirectFlag()
Definition: GeneticMaterial.cpp:748
AlleleFreq256_16
Definition: GeneticMaterial.h:179
GeneticMaterial::Mutation_1ab
void Mutation_1ab()
Definition: GeneticMaterial.cpp:465
GeneticMaterial256_16::UnsetDirectFlag
void UnsetDirectFlag()
Definition: GeneticMaterial.cpp:930
GeneticMaterial::ScoreReproduction
float ScoreReproduction()
Definition: GeneticMaterial.cpp:394
GeneticMaterial1616
Definition: GeneticMaterial.h:153
GeneticMaterial1616::Chromosome
uint32 Chromosome[4]
Definition: GeneticMaterial.h:156