Advertisement
MariusSemeon

genetic drift sim

Sep 14th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Random;
  4.  
  5. public class GeneticDrift_RUN_SIM {
  6.  
  7. public static void main(String[] args){
  8.  
  9. GeneticDriftSim sim = new GeneticDriftSim();
  10.  
  11. for(int i=0; i<5; i++){  // Number of times you wish to run simulation
  12. sim.go();
  13. }
  14.  
  15.   }
  16. }
  17.  
  18. ///////////////////////////////////////////////////
  19.  
  20. class GeneticDriftSim {
  21.  
  22. public void go() {
  23.  
  24. int numOfAnimals = 1000; // total number of males (females will be the same)
  25.                         // i.e number you input will be HALF the starting population
  26.  
  27. int numOfMatings = 10000; // i.e total number of generations
  28.  
  29. ArrayList<Animal> males = new ArrayList<Animal>();
  30. ArrayList<Animal> females = new ArrayList<Animal>();
  31. ArrayList<Animal> nextgenerationMales = new ArrayList<Animal>();
  32. ArrayList<Animal> nextgenerationFemales = new ArrayList<Animal>();
  33. ArrayList<Animal> lastGeneration = new ArrayList<Animal>();
  34.  
  35. for(int i=0; i<numOfAnimals; i++){
  36.   males.add(new Animal('M'));
  37.   females.add(new Animal('F'));
  38. }
  39.  
  40. for(int j=0; j<numOfMatings; j++){
  41.  
  42. for(int i=0; i<males.size(); i++){   // male array same size as females anyway
  43.   Animal offspring1 = males.get(i).mate(females.get(i));
  44.   Animal offspring2 = males.get(i).mate(females.get(i));
  45.   nextgenerationMales.add(offspring1);  //no need to check for M/F since first
  46.   nextgenerationFemales.add(offspring2); //child always M, second always F
  47. }
  48. males.clear();
  49. females.clear();
  50. males.addAll(nextgenerationMales);
  51. females.addAll(nextgenerationFemales);
  52. nextgenerationMales.clear();
  53. nextgenerationFemales.clear();
  54.  
  55. /*System.out.println(males);       // Uncomment this and next section if you
  56. System.out.println(females); */    // wish to see each generation and the
  57. Collections.shuffle(males);       // phenotypic distribution
  58. Collections.shuffle(females);
  59.  
  60. /*System.out.println();
  61. System.out.println("-----NEXT GEN-----");
  62. System.out.println(); */
  63.  
  64. }
  65.  
  66. lastGeneration.addAll(males);
  67. lastGeneration.addAll(females);
  68.  
  69. int totalA1Count = 0;
  70. int totalA2Count = 0;
  71. for(Animal a : lastGeneration){
  72.   if(a.allele1 == "A1"){
  73.     totalA1Count++;
  74.   } else {
  75.     totalA2Count++;
  76.   }
  77.   if(a.allele2 == "A1"){
  78.     totalA1Count++;
  79.   } else {
  80.     totalA2Count++;
  81.   }
  82. } //closes for loop
  83.  
  84. System.out.println("totalA1Count: "+totalA1Count +" totalA2Count: " +totalA2Count);
  85.  
  86. }
  87.  
  88. }
  89.  
  90. ///////////////////////////////////////////////
  91.  
  92. class Animal {
  93.  
  94. public String allele1;
  95. public String allele2;
  96. private char gender;
  97. private int numOfChildren;
  98.  
  99. public Animal(char gender){
  100.   this.gender = gender;       // Use no-arg constructor to make the
  101.   this.allele1 = "A1";          // starting Animals so equal frequencies of A1 & A2
  102.   this.allele2 = "A2";
  103.   this.numOfChildren = 0;         // all A1/A2 heterozygotes
  104. }
  105.  
  106. public Animal(char gender, String allele1, String allele2){
  107.   this.gender = gender;
  108.   this.allele1 = allele1;            // Use to create all future Animals once
  109.   this.allele2 = allele2;            // program has started
  110.   this.numOfChildren = 0;
  111. }
  112.  
  113. public Animal mate(Animal parentTwo){
  114.   char genderToPassOn;
  115.   if(this.numOfChildren == 0){
  116.     genderToPassOn = 'M';
  117.   } else if(this.numOfChildren == 1){
  118.     genderToPassOn = 'F';
  119.   } else {
  120.     genderToPassOn = 'N'; // N = neutral. But shouldn't be possible anyway
  121.   }                       // since not allowed to have >2 children
  122.  
  123.   String parent1AlleleToPassOn;
  124.   String parent2AlleleToPassOn;
  125.  
  126.   Random rand = new Random();
  127.   int y = rand.nextInt(2);
  128.   if(y ==0){
  129.     parent1AlleleToPassOn = this.allele1;
  130.   } else {
  131.     parent1AlleleToPassOn = this.allele2;
  132.   }
  133.   int z = rand.nextInt(2);
  134.   if(z ==0){
  135.     parent2AlleleToPassOn = parentTwo.allele1;
  136.   } else {
  137.     parent2AlleleToPassOn = parentTwo.allele2;
  138.   }
  139.  
  140.   this.numOfChildren++;
  141.  
  142.   return new Animal(genderToPassOn, parent1AlleleToPassOn, parent2AlleleToPassOn);
  143.   }
  144.  
  145. @Override
  146. public String toString(){
  147.   return this.allele1 + this.allele2 +"("+this.gender+")";
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement