/* * HomologyRecursion.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.oldevomodel.indel; import dr.evolution.alignment.Alignment; import dr.evolution.datatype.DataType; import dr.evolution.tree.NodeRef; import dr.evolution.tree.Tree; import dr.oldevomodel.substmodel.SubstitutionModel; import dr.math.BFloat; import java.util.HashMap; public class HomologyRecursion { /** Array of 0-1 vectors, encoding the alignment as columns */ IntMathVec[] iAlignment; /** Array of arrays, encoding the sequences (in rows). No gaps. Range [0,iNumNucs). */ int[][] iSequences; /** * Array encoding the tree. * iParent[iChild] is the parent of iChild, except -1 = root's parent. * Parent must be > child, and leaf indices must correspond to indices into alignment vectors */ private int[] iParent; /** Length of incoming branches to each tree node. Root is assumed to be infinity */ private double[] iTau; /** Parameters of the TKF91 model */ double iLambda; double iMu; /** Number of nucleotides */ int iNumNucs; /** Transition probability matrices, iTrans[node][from][to], for each tree node except root */ double[][][] iTrans; /** Equilibrium probabilities for nucleotides. */ double[] iEquil; /** Intermediate results for TKF91 model, initialised in recursion(), used in treeRecursion() */ private double iH[], iN[], iB[], iE[]; /** TKF91-related result for initialising the DP table */ private double iInitial; /** Class that implements a fast version of treeLikelihood */ private NativeTreeLikelihood iNativeMethod; /** Labels used in local array iState[] */ static private final int eFree = 0, ePossible = 1, eEdgeUsed = 2, eUsed = 3; /** Max dimension of unalignable region */ static private final int cMaxUnalignDimension = 10; /** Minimum edge length */ static private final double MIN_EDGE_LENGTH = 1e-3; /** Error count */ static int sBigUnalignableRegion = 0; String PrintDouble(double[] d) { String str = ""; for(double aD : d) { str = str + aD + " "; } return str; } void checkConsistency() { // Checks whether variables are init'ed properly int iCols = iAlignment.length; int iRows = iAlignment[0].iV.length; String iErr = ""; IntMathVec iPos = new IntMathVec( iRows ); for (int i=0; i=iNumNucs)) { iErr = "Nucleotide codes in iSequences not in range, " + iSequences[j][i] + "not in [0,"+(iNumNucs-1)+"]"; } } } int[] iChildren = new int[ iParent.length ]; for (int i=0; i= numStates) ? 0 : 1); } iAlignment[i] = new IntMathVec(column); } } /** * Initializes the iSequence array from the given alignment. */ private void initSequences(Alignment alignment, int[] treeIndex) { int numSeqs = alignment.getSequenceCount(); DataType dataType = alignment.getDataType(); int numStates = dataType.getStateCount(); iSequences = new int[numSeqs][]; for (int i = 0; i < numSeqs; i++) { int seqLength = 0; for (int j =0; j < alignment.getSiteCount(); j++) { int state = alignment.getState(i, j); if (state>=0 && state=0 && state