Debian Codegraph @

src/dr/evomodel/substmodel/SVSComplexSubstitutionModel.java

raw symbols SCIP
1 /*
2 * SVSComplexSubstitutionModel.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.inference.model.*;
29 import dr.evolution.datatype.DataType;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35 * @author Marc Suchard
36 */
37 public class SVSComplexSubstitutionModel extends ComplexSubstitutionModel implements Likelihood,
38 BayesianStochasticSearchVariableSelection {
39
40 public SVSComplexSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel,
41 Parameter ratesParameter, Parameter indicatorsParameter) {
42 super(name, dataType, freqModel, ratesParameter);
43
44 if (indicatorsParameter == null) {
45 this.indicatorsParameter = new Parameter.Default(ratesParameter.getDimension(), 1.0);
46 } else {
47 this.indicatorsParameter = indicatorsParameter;
48 addVariable(indicatorsParameter);
49 }
50
51 setupIndicatorDimensionNames(-1);
52 }
53
54 @Override
55 protected void setupRelativeRates(double[] rates) {
56 for (int i = 0; i < rates.length; i++) {
57 rates[i] = ratesParameter.getParameterValue(i) * indicatorsParameter.getParameterValue(i);
58 }
59 }
60
61 protected void setupIndicatorDimensionNames(int relativeTo) {
62 List<String> indicatorNames = new ArrayList<String>();
63
64 String indicatorPrefix = indicatorsParameter.getParameterName();
65
66 for (int i = 0; i < dataType.getStateCount(); ++i) {
67 for (int j = i + 1; j < dataType.getStateCount(); ++j) {
68 indicatorNames.add(getDimensionString(i, j, indicatorPrefix));
69 }
70 }
71
72 for (int j = 0; j < dataType.getStateCount(); ++j) {
73 for (int i = j + 1; i < dataType.getStateCount(); ++i) {
74 indicatorNames.add(getDimensionString(i, j, indicatorPrefix));
75 }
76 }
77
78 String[] tmp = new String[0];
79 indicatorsParameter.setDimensionNames(indicatorNames.toArray(tmp));
80 }
81
82 public Parameter getIndicators() {
83 return indicatorsParameter;
84 }
85
86 public boolean validState() {
87 return !updateMatrix ||
88 BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this);
89 }
90
91 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
92 if (variable == ratesParameter && indicatorsParameter.getParameterValue(index) == 0)
93 return; // Does not affect likelihood
94 super.handleVariableChangedEvent(variable,index,type);
95 }
96
97 public Model getModel() {
98 return this;
99 }
100
101 /**
102 * Forces a complete recalculation of the likelihood next time getLikelihood is called
103 */
104 public void makeDirty() {
105 updateMatrix = true;
106 }
107
108 /**
109 * @return A detailed name of likelihood for debugging.
110 */
111 // public String prettyName() {
112 // return "SVSComplexSubstitutionModel-connectedness";
113 // }
114
115 @Override
116 public boolean isUsed() {
117 return super.isUsed() && isUsed;
118 }
119
120 public void setUsed() {
121 isUsed = true;
122 }
123
124 private boolean isUsed = false;
125
126 private double[] probability = null;
127
128 private final Parameter indicatorsParameter;
129 }