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 java.util.Arrays;
025import java.util.List;
026
027import org.biojava.bio.dp.BackPointer;
028import org.biojava.bio.symbol.AlphabetManager;
029import org.biojava.bio.symbol.IllegalSymbolException;
030import org.biojava.bio.symbol.Symbol;
031import org.biojava.bio.symbol.SymbolList;
032
033/**
034 * @author Matthew Pocock
035 */
036public abstract class AbstractMatrixPairDPCursor
037  implements PairDPCursor {
038    protected int[] pos;
039    protected SymbolList[] seqs;
040    protected double[][][] columns;
041    protected BackPointer[][][] bPointers;
042    protected double[][][] emissions;
043    protected int numStates;
044    protected double[] zeroCol;
045    protected BackPointer[] emptyBP;
046    protected int[] depth;
047    protected double[][][] sMatrix;
048    protected EmissionCache eCache;
049
050    public AbstractMatrixPairDPCursor(
051      SymbolList seq1,
052      SymbolList seq2,
053      int start1,
054      int start2,
055      int depth1,
056      int depth2,
057      PairDPMatrix matrix,
058      EmissionCache eCache
059    ) throws IllegalSymbolException {
060      this.numStates = matrix.states().length;
061
062      this.zeroCol = new double[this.numStates]; // don't touch this, please...
063      for (int i = 0; i < zeroCol.length; ++i) {
064        this.zeroCol[i] = Double.NaN;
065      }
066      this.emptyBP = new BackPointer[numStates];
067      
068      this.sMatrix = matrix.getScoreArray();
069
070      this.pos = new int[2];
071      this.pos[0] = start1;
072      this.pos[1] = start2;
073      this.seqs = new SymbolList[2];
074      this.seqs[0] = seq1;
075      this.seqs[1] = seq2;
076      this.depth = new int[2];
077      this.depth[0] = depth1;
078      this.depth[1] = depth2;
079      this.bPointers = new BackPointer[seq1.length()+2][seq2.length()+2][numStates];
080      this.emissions = new double[seq1.length()+2][seq2.length()+2][];
081      this.eCache = eCache;
082      
083      Symbol [] symArray = new Symbol[2];
084      List symList = Arrays.asList(symArray);
085      for(int i = 0; i <= seq1.length()+1; i++) {
086        symArray[0] = (i < 1 || i > seq1.length())
087          ? AlphabetManager.getGapSymbol()
088          : seq1.symbolAt(i);
089        double [][] ei = emissions[i];
090        for(int j = 0; j <= seq2.length()+1; j++) {
091          symArray[1] = (j < 1 || j > seq2.length())
092            ? AlphabetManager.getGapSymbol()
093            : seq2.symbolAt(j);
094          ei[j] = eCache.getEmissions(symList, !((i < 1 && j < 1) || (i > seq1.length() && j <= seq2.length())) );
095        }
096      }
097    }
098    
099    public int [] getDepth() {
100      return depth;
101    }
102
103    public Cell[][] press() {
104      Cell [][] cells = new Cell[depth[0]][depth[1]];
105      for(int i = 0; i < cells.length; i++) {
106        Cell [] ci = cells[i];
107        for(int j = 0; j < ci.length; j++) {
108          ci[j] = new Cell();
109        }
110      }
111      return cells;
112    }
113  }