Debian Codegraph @

src/dr/evomodel/tipstatesmodel/TipStatesModel.java

raw symbols SCIP
1 /*
2 * TipStatesModel.java
3 *
4 * Copyright (c) 2002-2016 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.tipstatesmodel;
27
28 import dr.evolution.alignment.PatternList;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.util.TaxonList;
31 import dr.inference.model.AbstractModel;
32 import dr.inference.model.Model;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 /**
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 * @version $Id$
43 */
44 public abstract class TipStatesModel extends AbstractModel {
45
46 // an enum which specifies if the model emits tip states or partials
47 public enum Type {
48 PARTIALS,
49 STATES
50 };
51
52 /**
53 * @param name Model Name
54 */
55 public TipStatesModel(String name, TaxonList includeTaxa, TaxonList excludeTaxa) {
56 super(name);
57
58 this.includeTaxa = includeTaxa;
59 this.excludeTaxa = excludeTaxa;
60 }
61
62 public final void setTree(Tree tree) {
63 this.tree = tree;
64
65 int extNodeCount = tree.getExternalNodeCount();
66
67 excluded = new boolean[extNodeCount];
68 if (includeTaxa != null) {
69 for (int i = 0; i < extNodeCount; i++) {
70 if (includeTaxa.getTaxonIndex(tree.getNodeTaxon(tree.getExternalNode(i))) == -1) {
71 excluded[i] = true;
72 }
73 }
74 }
75
76 if (excludeTaxa != null) {
77 for (int i = 0; i < extNodeCount; i++) {
78 if (excludeTaxa.getTaxonIndex(tree.getNodeTaxon(tree.getExternalNode(i))) != -1) {
79 excluded[i] = true;
80 }
81 }
82
83 }
84
85 states = new int[extNodeCount][];
86
87 taxaChanged();
88 }
89
90 protected abstract void taxaChanged();
91
92 public final void setStates(PatternList patternList, int sequenceIndex, int nodeIndex, String taxonId) {
93 if (this.patternList == null) {
94 this.patternList = patternList;
95 patternCount = patternList.getPatternCount();
96 stateCount = patternList.getDataType().getStateCount();
97 } else if (patternList != this.patternList) {
98 throw new RuntimeException("The TipStatesModel with id, " + getId() + ", has already been associated with a patternList.");
99 }
100 if (this.states[nodeIndex] == null) {
101 this.states[nodeIndex] = new int[patternCount];
102 }
103
104 for (int i = 0; i < patternCount; i++) {
105 this.states[nodeIndex][i] = patternList.getPatternState(sequenceIndex, i);
106 }
107
108 taxonMap.put(nodeIndex, taxonId);
109 }
110
111 protected void handleModelChangedEvent(Model model, Object object, int index) {
112 fireModelChanged();
113 }
114
115 /**
116 * This method is called whenever a parameter is changed.
117 * <p/>
118 * It is strongly recommended that the model component sets a "dirty" flag and does no
119 * further calculations. Recalculation is typically done when the model component is asked for
120 * some information that requires them. This mechanism is 'lazy' so that this method
121 * can be safely called multiple times with minimal computational cost.
122 */
123 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
124 fireModelChanged();
125 }
126
127 /**
128 * Additional state information, outside of the sub-model is stored by this call.
129 */
130 protected void storeState() {
131 }
132
133 /**
134 * After this call the model is guaranteed to have returned its extra state information to
135 * the values coinciding with the last storeState call.
136 * Sub-models are handled automatically and do not need to be considered in this method.
137 */
138 protected void restoreState() {
139 }
140
141 /**
142 * This call specifies that the current state is accept. Most models will not need to do anything.
143 * Sub-models are handled automatically and do not need to be considered in this method.
144 */
145 protected void acceptState() {
146 }
147
148 public PatternList getPatternList() {
149 return patternList;
150 }
151
152 public abstract Type getModelType();
153
154 public abstract void getTipPartials(int nodeIndex, double[] tipPartials);
155
156 public abstract void getTipStates(int nodeIndex, int[] tipStates);
157
158 protected int[][] states;
159 protected boolean[] excluded;
160
161 protected int patternCount = 0;
162 protected int stateCount;
163
164 protected TaxonList includeTaxa;
165 protected TaxonList excludeTaxa;
166
167 protected Tree tree;
168
169 private PatternList patternList = null;
170
171 protected Map<Integer, String> taxonMap = new HashMap<Integer, String>();
172 }