Difference between revisions of "830 - Haplotyper::SampleHaplotypes()"

From Genome Analysis Wiki
Jump to navigationJump to search
Line 1: Line 1:
 
<source lang="cpp">
 
<source lang="cpp">
  
void Haplotyper::SampleChromosomes(Random * rand)
+
float Haplotyper::SampleHaplotypes(float * vector, int & first, int & second)
 
   {
 
   {
   RewindMemoryPool();
+
   first = 0;
   RetrieveMemoryBlock(markers - 1);
+
   second = 0;
  
   int first = 0, second = 0;
+
   float sum = 0.0;
  SampleHaplotypes(leftMatrices[markers - 1], first, second);
 
  
   float left[2][2], right[2][2];
+
   // Calculate sum over all states
  for (int j = markers - 2; j >= 0; j--)
+
  for (int i = 0; i < states; i++)
      {
+
      for (int j = 0; j <= i; j++)
      RecordHaplotype(j + 1, first, second);
+
        {
 +
        sum += *probability;
 +
        probability++;
 +
        }
  
      RetrieveMemoryBlock(j);
+
  // Sample number and select state
 +
  float choice = rand->Uniform(0, sum);
  
      float sum = SummarizeOptions(leftMatrices[j], left, right, thetas[j], first, second);
+
  sum = 0.0;
      float choice = rand->Uniform(0, sum);
 
  
      // The most likely outcome is that no changes occur ...
+
  for (probability = leftMatrices[markers - 1]; first < states; first++)
      choice -= left[0][0] * right[0][0];
+
      {
      if (choice <= 0.0) continue;
+
       for (second = 0; second <= first; second++)
 
 
       // Otherwise, check if the second haplotype changed ...
 
      choice -= left[0][1] * right[0][1];
 
      if (choice <= 0.0)
 
 
         {
 
         {
         SampleOneHaplotype(leftMatrices[j], first, second, -choice / right[0][1]);
+
         sum += *probability;
         continue;
+
         probability++;
        }
 
  
      // Otherwise, check if the first haplotype changed ...
+
        if (sum >= choice) break;
      choice -= left[1][0] * right[1][0];
 
      if (choice <= 0.0)
 
        {
 
        SampleOneHaplotype(leftMatrices[j], second, first, -choice / right[1][0]);
 
        continue;
 
 
         }
 
         }
  
       // And sometimes, both haplotypes recombine and we start fresh
+
       if (second <= first) break;
      SampleHaplotypes(leftMatrices[j], first, second);
 
 
       }
 
       }
 
+
 
   RecordHaplotypes(0, first, second);
+
   if (rand->Binary()) SWAP(first, second);
 
   }
 
   }
 
</source>
 
</source>

Revision as of 14:08, 2 October 2013

float Haplotyper::SampleHaplotypes(float * vector, int & first, int & second)
   {
   first = 0;
   second = 0;

   float sum = 0.0;

   // Calculate sum over all states
   for (int i = 0; i < states; i++)
      for (int j = 0; j <= i; j++)
         {
         sum += *probability;
         probability++;
         }

   // Sample number and select state
   float choice = rand->Uniform(0, sum);

   sum = 0.0;

   for (probability = leftMatrices[markers - 1]; first < states; first++)
      {
      for (second = 0; second <= first; second++)
         {
         sum += *probability;
         probability++;

         if (sum >= choice) break;
         }

      if (second <= first) break;
      }

   if (rand->Binary()) SWAP(first, second);
   }