title: Implementing the Viterbi algorithm in a HMM with changing emission matrices across genomics markers
body: <p>I would like to ask for help in implementing a hidden markov approach to assigning ancestry based on SNP genotype data. Given that I have a <b>transition</b> matrix generated as such:</p>
  
  <pre><code>states &lt;- c("A1","A2","A3","A4","A5","A6","A7","A8") # Define the names of the states
  A1 &lt;- c(0.9,0.1,0.1,0.1,0.1,0.1,0.1,0.1) # Set the probabilities of switching states, where the previous state was "A1"
  A2 &lt;- c(0.1,0.9,0.1,0.1,0.1,0.1,0.1,0.1) # Set the probabilities of switching states, where the previous state was "A2"
  A3 &lt;- c(0.1,0.1,0.9,0.1,0.1,0.1,0.1,0.1) # Set the probabilities of switching states,  where the previous state was "A3"
  A4 &lt;- c(0.1,0.1,0.1,0.9,0.1,0.1,0.1,0.1) # Set the probabilities of switching states, where the previous state was "A4"
  A5 &lt;- c(0.1,0.1,0.1,0.1,0.9,0.1,0.1,0.1) # Set the probabilities of switching states, where the previous state was "A5"
  A6 &lt;- c(0.1,0.1,0.1,0.1,0.1,0.9,0.1,0.1) # Set the probabilities of switching states, where the previous state was "A6"
  A7 &lt;- c(0.1,0.1,0.1,0.1,0.1,0.1,0.9,0.1) # Set the probabilities of switching states, where the previous state was "A7"
  A8 &lt;- c(0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.9) # Set the probabilities of switching states, where the previous state was "A8"
  thetransitionmatrix &lt;- matrix(c(A1,A2,A3,A4,A5,A6,A7,A8), 8, 8, byrow = TRUE) # Create an 8 x 8 matrix
  rownames(thetransitionmatrix) &lt;- states
  colnames(thetransitionmatrix) &lt;- states
  thetransitionmatrix # Print out the transition matrix
      A1  A2  A3  A4  A5  A6  A7  A8
  A1 0.9 0.1 0.1 0.1 0.1 0.1 0.1 0.1
  A2 0.1 0.9 0.1 0.1 0.1 0.1 0.1 0.1
  A3 0.1 0.1 0.9 0.1 0.1 0.1 0.1 0.1
  A4 0.1 0.1 0.1 0.9 0.1 0.1 0.1 0.1
  A5 0.1 0.1 0.1 0.1 0.9 0.1 0.1 0.1
  A6 0.1 0.1 0.1 0.1 0.1 0.9 0.1 0.1
  A7 0.1 0.1 0.1 0.1 0.1 0.1 0.9 0.1
  A8 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.9
  </code></pre>
  
  <p>and the <b>emission</b> matrix is a list of n 8x4 matrices, with n equal to the number of SNPs/rows in the data. For example, given the following data for 8 samples (A1-A8) across 3 SNPs/rows:</p>
  
  <pre><code>A1 A2 A3 A4 A5 A6 A7 A8
  T T T T T T T C 
  T C T T T T T C
  A A A G G A A A
  </code></pre>
  
  <p>matrix 1 in the list would be</p>
  
  <pre><code>   A C G T
  A1 0 0 0 1/7
  A2 0 0 0 1/7 
  A3 0 0 0 1/7
  A4 0 0 0 1/7
  A5 0 0 0 1/7
  A6 0 0 0 1/7
  A7 0 0 0 1/7
  A8 0 1 0 0
  </code></pre>
  
  <p>since 7 of the samples possess a T in row 1, each sample has a probability of 1/7. Since only A8 possess a C, there's an 100% probability of assigning a C to A8. For row 3 the output should be </p>
  
  <pre><code>   A C G T
  A1 1/6 0 0 0
  A2 1/6 0 0 0 
  A3 1/6 0 0 0
  A4 1/2 0 0 0
  A5 1/2 0 0 0
  A6 1/6 0 0 0
  A7 1/6 0 0 0
  A8 1/6 0 0 0
  </code></pre>
  
  <p>Using the aforementioned transition matrix and the list of emission matrices, I wish to implment the Viterbi algorithm on any sequence of alleles. The code that I currently have is not able to use a different emission matrix for each row</p>
  
  <pre><code>viterbi &lt;- function(sequence, transitionmatrix, emissionmatrix)
    # This carries out the Viterbi algorithm.
    # Adapted from "Applied Statistics for Bioinformatics using R" by Wim P. Krijnen,     page 209
    # ( cran.r-project.org/doc/contrib/Krijnen-IntroBioInfStatistics.pdf )
    {
   # Get the names of the states in the HMM:
   states &lt;- rownames(theemissionmatrix)
  
   # Make the Viterbi matrix v:
   v &lt;- makeViterbimat(sequence, transitionmatrix, emissionmatrix)
   # Go through each of the rows of the matrix v (where each row represents
   # a position in the DNA sequence), and find out which column has the
   # maximum value for that row (where each column represents one state of
   # the HMM):
   mostprobablestatepath &lt;- apply(v, 1, function(x) which.max(x))
  
   # Print out the most probable state path:
   prevnucleotide &lt;- sequence[1]
   prevmostprobablestate &lt;- mostprobablestatepath[1]
   prevmostprobablestatename &lt;- states[prevmostprobablestate]
   startpos &lt;- 1
   for (i in 2:length(sequence))
   {
      nucleotide &lt;- sequence[i]
      mostprobablestate &lt;- mostprobablestatepath[i]
      mostprobablestatename &lt;- states[mostprobablestate]
      if (mostprobablestatename != prevmostprobablestatename)
      {
         print(paste("Positions",startpos,"-",(i-1), "Most probable state = ", prevmostprobablestatename))
         startpos &lt;- i
      }
      prevnucleotide &lt;- nucleotide
      prevmostprobablestatename &lt;- mostprobablestatename
   }
   print(paste("Positions",startpos,"-",i, "Most probable state = ", prevmostprobablestatename))
     }
  
  
  # the viterbi() function requires a second function makeViterbimat():
  
  makeViterbimat &lt;- function(sequence, transitionmatrix, emissionmatrix)
    # This makes the matrix v using the Viterbi algorithm.
    # Adapted from "Applied Statistics for Bioinformatics using R" by Wim P. Krijnen, page 209
    # ( cran.r-project.org/doc/contrib/Krijnen-IntroBioInfStatistics.pdf )
    {
   # Change the sequence to uppercase
   sequence &lt;- toupper(sequence)
   # Find out how many states are in the HMM
   numstates &lt;- dim(transitionmatrix)[1]
   # Make a matrix with as many rows as positions in the sequence, and as many
   # columns as states in the HMM
   v &lt;- matrix(NA, nrow = length(sequence), ncol = dim(transitionmatrix)[1])
   # Set the values in the first row of matrix v (representing the first position of the sequence) to 0
   v[1, ] &lt;- 0
   # Set the value in the first row of matrix v, first column to 1
   v[1,1] &lt;- 1
   # Fill in the matrix v:
   for (i in 2:length(sequence)) # For each position in the DNA sequence:
   {
      for (l in 1:numstates) # For each of the states of in the HMM:
      {
         # Find the probabilility, if we are in state l, of choosing the nucleotide at position in the sequence
         statelprobnucleotidei &lt;- emissionmatrix[l,sequence[i]]
  
         # v[(i-1),] gives the values of v for the (i-1)th row of v, ie. the (i-1)th position in the sequence.
         # In v[(i-1),] there are values of v at the (i-1)th row of the sequence for each possible state k.
         # v[(i-1),k] gives the value of v at the (i-1)th row of the sequence for a particular state k.
  
         # transitionmatrix[l,] gives the values in the lth row of the transition matrix, xx should not be transitionmatrix[,l]?
         # probabilities of changing from a previous state k to a current state l.
  
         # max(v[(i-1),] * transitionmatrix[l,]) is the maximum probability for the nucleotide observed
         # at the previous position in the sequence in state k, followed by a transition from previous
         # state k to current state l at the current nucleotide position.
  
         # Set the value in matrix v for row i (nucleotide position i), column l (state l) to be:
         v[i,l] &lt;-  statelprobnucleotidei * max(v[(i-1),] * transitionmatrix[,l])
      }
  }
  return(v)
    }
  </code></pre>