Debian Codegraph @

src/dr/evomodel/substmodel/GeneralSubstitutionModel.java

raw symbols SCIP
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.evomodel.substmodel;
27
28 import dr.evolution.datatype.DataType;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.DuplicatedParameter;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 /**
36 * <b>A general model of sequence substitution</b>. A general reversible class for any
37 * data type.
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 * @author Marc Suchard
42 */
43 public class GeneralSubstitutionModel extends BaseSubstitutionModel {
44
45 /**
46 * the rate which the others are set relative to
47 */
48 protected int ratesRelativeTo;
49
50 public GeneralSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel,
51 Parameter ratesParameter, int relativeTo) {
52 this(name, dataType, freqModel, ratesParameter, relativeTo, null);
53
54 }
55
56 /**
57 * constructor
58 *
59 * @param dataType the data type
60 */
61 public GeneralSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel,
62 Parameter ratesParameter, int relativeTo, EigenSystem eigenSystem) {
63
64 super(name, dataType, freqModel, eigenSystem);
65
66 this.ratesParameter = ratesParameter;
67 if (ratesParameter != null) {
68 addVariable(ratesParameter);
69 if (!(ratesParameter instanceof DuplicatedParameter))
70 ratesParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
71 ratesParameter.getDimension()));
72
73 setupDimensionNames(relativeTo);
74 }
75 setRatesRelativeTo(relativeTo);
76 }
77
78 protected void frequenciesChanged() {
79 // Nothing to precalculate
80 }
81
82 protected void ratesChanged() {
83 // Nothing to precalculate
84 }
85
86 protected void setupRelativeRates(double[] rates) {
87 for (int i = 0; i < rates.length; i++) {
88 if (i == ratesRelativeTo) {
89 rates[i] = 1.0;
90 } else if (ratesRelativeTo < 0 || i < ratesRelativeTo) {
91 rates[i] = ratesParameter.getParameterValue(i);
92 } else {
93 rates[i] = ratesParameter.getParameterValue(i - 1);
94 }
95 }
96 }
97
98 protected void setupDimensionNames(int relativeTo) {
99 List<String> rateNames = new ArrayList<String>();
100
101 String ratePrefix = ratesParameter.getParameterName();
102
103 int index = 0;
104
105 for (int i = 0; i < dataType.getStateCount(); ++i) {
106 for (int j = i + 1; j < dataType.getStateCount(); ++j) {
107 if (index != relativeTo) {
108 rateNames.add(getDimensionString(i, j, ratePrefix));
109 }
110 }
111 index++;
112 }
113
114 String[] tmp = new String[0];
115 ratesParameter.setDimensionNames(rateNames.toArray(tmp));
116 }
117
118 protected String getDimensionString(int i, int j, String prefix) {
119 String codes = dataType.getCode(i) + "." + dataType.getCode(j);
120 if (prefix == null) {
121 return codes;
122 } else {
123 return prefix + "." + codes;
124 }
125 }
126
127 /**
128 * set which rate the others are relative to
129 */
130 public void setRatesRelativeTo(int ratesRelativeTo) {
131 this.ratesRelativeTo = ratesRelativeTo;
132 }
133
134 // *****************************************************************
135 // Interface Model
136 // *****************************************************************
137
138
139 protected void storeState() {
140 } // nothing to do
141
142 /**
143 * Restore the additional stored state
144 */
145 protected void restoreState() {
146 updateMatrix = true;
147 }
148
149 protected void acceptState() {
150 } // nothing to do
151
152 /**
153 * Parses an element from an DOM document into a DemographicModel. Recognises
154 * ConstantPopulation and ExponentialGrowth.
155 */
156
157 protected Parameter ratesParameter = null;
158 }