| 1 | /* |
| 2 | * GeneralSubstitutionModel.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.oldevomodel.substmodel; |
| 27 | |
| 28 | import dr.evolution.datatype.DataType; |
| 29 | import dr.oldevomodelxml.substmodel.GeneralSubstitutionModelParser; |
| 30 | import dr.inference.model.DuplicatedParameter; |
| 31 | import dr.inference.model.Parameter; |
| 32 | |
| 33 | /** |
| 34 | * <b>A general model of sequence substitution</b>. A general reversible class for any |
| 35 | * data type. |
| 36 | * |
| 37 | * @author Andrew Rambaut |
| 38 | * @author Alexei Drummond |
| 39 | * @version $Id: GeneralSubstitutionModel.java,v 1.37 2006/05/05 03:05:10 alexei Exp $ |
| 40 | */ |
| 41 | public class GeneralSubstitutionModel extends AbstractSubstitutionModel implements dr.util.XHTMLable { |
| 42 | |
| 43 | /** |
| 44 | * the rate which the others are set relative to |
| 45 | */ |
| 46 | protected int ratesRelativeTo; |
| 47 | |
| 48 | /** |
| 49 | * constructor |
| 50 | * |
| 51 | * @param dataType the data type |
| 52 | * @param freqModel the equilibrium frequency model - this must match the data type |
| 53 | * @param parameter the rates parameter, minus the rate that they are specified relative to |
| 54 | * @param relativeTo the index of the rate that all other are specified relative to |
| 55 | */ |
| 56 | public GeneralSubstitutionModel( |
| 57 | DataType dataType, |
| 58 | FrequencyModel freqModel, |
| 59 | Parameter parameter, |
| 60 | int relativeTo) { |
| 61 | |
| 62 | super(GeneralSubstitutionModelParser.GENERAL_SUBSTITUTION_MODEL, dataType, freqModel); |
| 63 | |
| 64 | ratesParameter = parameter; |
| 65 | if (ratesParameter != null) { |
| 66 | addVariable(ratesParameter); |
| 67 | if (!(ratesParameter instanceof DuplicatedParameter)) |
| 68 | ratesParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, ratesParameter.getDimension())); |
| 69 | } |
| 70 | setRatesRelativeTo(relativeTo); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * constructor |
| 75 | * |
| 76 | * @param name a name to give the substitution model |
| 77 | * @param dataType the data type |
| 78 | * @param freqModel the equilibrium frequency model - this must match the data type |
| 79 | * @param relativeTo the index of the rate that all other are specified relative to |
| 80 | */ |
| 81 | protected GeneralSubstitutionModel( |
| 82 | String name, |
| 83 | DataType dataType, |
| 84 | FrequencyModel freqModel, |
| 85 | int relativeTo) { |
| 86 | |
| 87 | super(name, dataType, freqModel); |
| 88 | |
| 89 | setRatesRelativeTo(relativeTo); |
| 90 | } |
| 91 | |
| 92 | protected void frequenciesChanged() { |
| 93 | // Nothing to precalculate |
| 94 | } |
| 95 | |
| 96 | protected void ratesChanged() { |
| 97 | // Nothing to precalculate |
| 98 | } |
| 99 | |
| 100 | protected void setupRelativeRates() { |
| 101 | |
| 102 | for (int i = 0; i < relativeRates.length; i++) { |
| 103 | if (i == ratesRelativeTo) { |
| 104 | relativeRates[i] = 1.0; |
| 105 | } else if (i < ratesRelativeTo) { |
| 106 | relativeRates[i] = ratesParameter.getParameterValue(i); |
| 107 | } else { |
| 108 | relativeRates[i] = ratesParameter.getParameterValue(i - 1); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * set which rate the others are relative to |
| 115 | * |
| 116 | * @param ratesRelativeTo the index of the rate in the matrix that all other |
| 117 | * rates are parameterized relative to. |
| 118 | */ |
| 119 | public void setRatesRelativeTo(int ratesRelativeTo) { |
| 120 | this.ratesRelativeTo = ratesRelativeTo; |
| 121 | } |
| 122 | |
| 123 | // ***************************************************************** |
| 124 | // Interface Model |
| 125 | // ***************************************************************** |
| 126 | |
| 127 | |
| 128 | protected void storeState() { |
| 129 | } // nothing to do |
| 130 | |
| 131 | /** |
| 132 | * Restore the additional stored state |
| 133 | */ |
| 134 | protected void restoreState() { |
| 135 | updateMatrix = true; |
| 136 | } |
| 137 | |
| 138 | protected void acceptState() { |
| 139 | } // nothing to do |
| 140 | |
| 141 | // ************************************************************** |
| 142 | // XHTMLable IMPLEMENTATION |
| 143 | // ************************************************************** |
| 144 | |
| 145 | public String toXHTML() { |
| 146 | StringBuffer buffer = new StringBuffer(); |
| 147 | |
| 148 | buffer.append("<em>General Model</em>"); |
| 149 | |
| 150 | return buffer.toString(); |
| 151 | } |
| 152 | |
| 153 | protected Parameter ratesParameter = null; |
| 154 | } |