Debian Codegraph @

src/dr/evomodel/branchmodel/RandomBranchAssignmentModel.java

raw symbols SCIP
1 /*
2 * RandomBranchAssignmentModel.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.branchmodel;
27
28 import java.util.LinkedHashMap;
29 import java.util.List;
30
31 import dr.evomodel.substmodel.FrequencyModel;
32 import dr.evomodel.substmodel.SubstitutionModel;
33 import dr.evolution.tree.NodeRef;
34 import dr.evomodel.tree.TreeModel;
35 import dr.inference.model.AbstractModel;
36 import dr.inference.model.Model;
37 import dr.inference.model.Variable;
38 import dr.inference.model.Variable.ChangeType;
39 import dr.math.MathUtils;
40
41 @SuppressWarnings("serial")
42 public class RandomBranchAssignmentModel extends AbstractModel implements BranchModel {
43
44 public static final boolean DEBUG = true;
45
46 public static final String RANDOM_BRANCH_ASSIGNMENT_MODEL = "randomBranchAssignmentModel";
47 private final TreeModel treeModel;
48 private final List<SubstitutionModel> substitutionModels;
49
50 // private int[] order;
51 private LinkedHashMap<NodeRef, Integer> branchAssignmentMap;
52
53 public RandomBranchAssignmentModel(TreeModel treeModel,
54 List<SubstitutionModel> substitutionModels) {
55
56 super(RANDOM_BRANCH_ASSIGNMENT_MODEL);
57
58
59 this.treeModel = treeModel;
60 this.substitutionModels = substitutionModels;
61
62 int nodeCount = treeModel.getNodeCount();
63 int nModels = substitutionModels.size();
64
65 // randomly decide order, once and for all
66 branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>();
67 for (int i = 0; i < nodeCount; i++) {
68
69 NodeRef node = treeModel.getNode(i);
70 int branchClass = Integer.MAX_VALUE; //MathUtils.nextInt(nModels);
71
72 if(DEBUG) {
73
74 // System.out.println(node.toString());
75
76 // hack to get fixed indexing
77 if(node.toString().equalsIgnoreCase("node 0, height=0.0: SimSeq1") ||
78 node.toString().equalsIgnoreCase("node 1, height=0.0: SimSeq2") ||
79 node.toString().equalsIgnoreCase("node 4, height=22.0")
80 ) {
81 branchClass = 0; // 5
82 } else {
83 branchClass = 1; // 10
84 }//END: node check
85
86 } else {
87 branchClass = MathUtils.nextInt(nModels);
88 }//END: DEBUG check
89
90 branchAssignmentMap.put(node, branchClass);
91
92 }// END: nodes loop
93
94 }//END: Constructor
95
96 @Override
97 public Mapping getBranchModelMapping(NodeRef branch) {
98
99 final int branchClass = branchAssignmentMap.get(branch);
100
101 return new Mapping() {
102 public int[] getOrder() {
103 return new int[] { branchClass };
104 }
105
106 public double[] getWeights() {
107 return new double[] { 1.0 };
108 }
109 };
110 }
111
112 @Override
113 public List<SubstitutionModel> getSubstitutionModels() {
114 return substitutionModels;
115 }
116
117 @Override
118 public SubstitutionModel getRootSubstitutionModel() {
119 int rootClass = branchAssignmentMap.get(treeModel.getRoot());
120 return substitutionModels.get(rootClass);
121 }
122
123 @Override
124 public FrequencyModel getRootFrequencyModel() {
125 return getRootSubstitutionModel().getFrequencyModel();
126 }
127
128 @Override
129 public boolean requiresMatrixConvolution() {
130 return false;
131 }
132
133 @Override
134 protected void handleModelChangedEvent(Model model, Object object, int index) {
135 fireModelChanged();
136 }
137
138 @Override
139 protected void handleVariableChangedEvent(Variable variable, int index,
140 ChangeType type) {
141 }
142
143 @Override
144 protected void storeState() {
145 }
146
147 @Override
148 protected void restoreState() {
149 }
150
151 @Override
152 protected void acceptState() {
153 }
154
155
156 }//END: class