001/*
002 * BioJava development code
003 * 
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 * 
009 * http://www.gnu.org/copyleft/lesser.html
010 * 
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 * 
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 * 
018 * http://www.biojava.org
019 *
020 */
021
022package org.biojava.bio.dp.twohead;
023
024import org.biojava.bio.dp.BackPointer;
025import org.biojava.bio.symbol.IllegalSymbolException;
026import org.biojava.bio.symbol.SymbolList;
027
028/**
029 * @author Matthew Pocock
030 */
031public class BackMatrixPairDPCursor
032  extends AbstractMatrixPairDPCursor {
033    public BackMatrixPairDPCursor(
034      SymbolList seq1,
035      SymbolList seq2,
036      int depth1,
037      int depth2,
038      PairDPMatrix matrix,
039      EmissionCache eCache
040    ) throws IllegalSymbolException {
041      super(
042        seq1, seq2,
043        seq1.length()+1, seq2.length()+1,
044        depth1, depth2,
045        matrix,
046        eCache
047      );
048    }
049
050    public boolean hasNext() {
051      return
052        pos[1] >= 0;
053    }
054    
055    
056    public void next(Cell[][] cells) {
057      for(int i = 0; i < depth[0]; i++) {
058        Cell[] cellI = cells[i];
059        int ii = pos[0] + i;
060        boolean outI = ii < 0 || ii > seqs[0].length()+1;
061        if(outI) {
062          for(int j = 0; j < depth[1]; j++) {
063            Cell c = cellI[j];
064            c.scores = zeroCol;
065            c.emissions = zeroCol;
066            c.backPointers = emptyBP;
067          }
068        } else {
069          double[][] sMatI = this.sMatrix[ii];
070          double[][] emisI = this.emissions[ii];
071          BackPointer[][] bPI = this.bPointers[ii];
072          for(int j = 0; j < depth[1]; j++) {
073            int jj = pos[1] + j;
074            boolean outJ = jj < 0 || jj > seqs[1].length()+1;
075            Cell c = cellI[j];
076            if(outJ) {
077              c.scores = zeroCol;
078              c.emissions = zeroCol;
079              c.backPointers = emptyBP;
080            } else {
081              c.scores = sMatI[jj];
082              c.emissions = emisI[jj];
083              c.backPointers = bPI[jj];
084            }
085          }
086        }
087      }
088      if(pos[0] > 0) {
089        pos[0]--;
090      } else {
091        pos[0] = seqs[0].length()+1;
092        pos[1]--;
093      }
094    }
095  }
096