Debian Codegraph @

src/dr/evomodel/branchmodel/BranchAssignmentModel.java

raw symbols SCIP
1 package dr.evomodel.branchmodel;
2
3 import dr.evomodelxml.branchmodel.BranchAssignmentModelParser;
4 import dr.evomodel.substmodel.FrequencyModel;
5 import dr.evomodel.substmodel.SubstitutionModel;
6 import dr.evolution.tree.NodeRef;
7 import dr.evomodel.tree.TreeModel;
8 import dr.inference.model.AbstractModel;
9 import dr.inference.model.Model;
10 import dr.inference.model.Variable;
11 import dr.inference.model.Variable.ChangeType;
12
13 import java.util.LinkedHashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16
17 @SuppressWarnings("serial")
18 public class BranchAssignmentModel extends AbstractModel implements BranchModel {
19
20 public static final String BRANCH_ASSIGNMENT_MODEL = "branchAssignmentModel";
21
22 private TreeModel treeModel;
23 private final String annotation;
24 private final LinkedHashMap<Integer, SubstitutionModel> modelIndexMap;
25 private final SubstitutionModel baseModel;
26
27 private Integer baseModelIndex;
28 private LinkedHashMap<NodeRef, Integer> branchAssignmentMap;
29 private LinkedList<SubstitutionModel> substitutionModels;
30
31 public BranchAssignmentModel(
32 TreeModel treeModel, //
33 String annotation, //
34 LinkedHashMap<Integer, SubstitutionModel> modelIndexMap, //
35 SubstitutionModel baseModel//
36 ) {
37
38 super(BRANCH_ASSIGNMENT_MODEL);
39
40 this.treeModel = treeModel;
41 this.annotation = annotation;
42 this.modelIndexMap = modelIndexMap;
43 this.baseModel = baseModel;
44
45 this.substitutionModels = new LinkedList<SubstitutionModel>();
46 this.branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>();
47 // base model comes last
48 this.baseModelIndex = modelIndexMap.size();
49
50 setup();
51
52 }// END: Constructor
53
54 private void setup() {
55
56 // for (int i = 0; i < modelIndexMap.size() + 1; i++) {
57 // substitutionModels.add(null);
58 // }
59
60 // try {
61 //
62 // File file = new File("/home/filip/Dropbox/BeagleSequenceSimulator/branchSpecificSimulations/annotated_tree.nexus");
63 // BufferedReader reader;
64 //
65 // reader = new BufferedReader(new FileReader(file));
66 // NexusImporter importer = new NexusImporter(reader);
67 // Tree tree = importer.importTree(null);
68 // this.treeModel = new TreeModel(tree);
69 //
70 // } catch ( Exception e) {
71 // e.printStackTrace();
72 // }
73
74 for (NodeRef node : this.treeModel.getNodes()) {
75 if (!treeModel.isRoot(node)) {
76
77 Integer modelIndex = Integer.MAX_VALUE;
78 SubstitutionModel model = null;
79 Object nodeAttribute = treeModel.getNodeAttribute(node,
80 annotation);
81
82 if (nodeAttribute == null) {
83
84 System.out
85 .println("Attribute "
86 + annotation
87 + " missing from node. Using base model as branch model.");
88
89 modelIndex = this.baseModelIndex;
90 model = this.baseModel;
91
92 } else {
93
94 modelIndex = (Integer) nodeAttribute;
95 model = this.modelIndexMap.get(modelIndex);
96
97 }
98
99 branchAssignmentMap.put(node, modelIndex);
100
101 // if (substitutionModels.get(modelIndex) == null) {
102 // substitutionModels.set(modelIndex, model);
103 // }
104
105 substitutionModels.add(model);
106
107 }// END: root check
108 }// END: nodes loop
109
110 }//END: setup
111
112 @Override
113 public Mapping getBranchModelMapping(NodeRef branch) {
114
115 final int modelIndex = branchAssignmentMap.get(branch);
116
117 return new Mapping() {
118 public int[] getOrder() {
119 return new int[] { modelIndex };
120 }
121
122 public double[] getWeights() {
123 return new double[] { 1.0 };
124 }
125 };
126 }// END: getBranchModelMapping
127
128 @Override
129 public List<SubstitutionModel> getSubstitutionModels() {
130 return substitutionModels;
131 }// END: getSubstitutionModels
132
133 @Override
134 public SubstitutionModel getRootSubstitutionModel() {
135
136 Object nodeAttribute = treeModel.getNodeAttribute(treeModel.getRoot(),
137 BranchAssignmentModelParser.ANNOTATION_VALUE);
138 SubstitutionModel model = null;
139
140 if (nodeAttribute == null) {
141
142 model = this.baseModel;
143
144 } else {
145
146 Integer modelIndex = (Integer) nodeAttribute;
147 model = this.modelIndexMap.get(modelIndex);
148
149 }
150
151 return model;
152 }// END: getRootSubstitutionModel
153
154 @Override
155 public FrequencyModel getRootFrequencyModel() {
156 return getRootSubstitutionModel().getFrequencyModel();
157 }
158
159 @Override
160 public boolean requiresMatrixConvolution() {
161 return false;
162 }
163
164 @Override
165 protected void handleModelChangedEvent(Model model, Object object, int index) {
166 fireModelChanged();
167 }
168
169 @Override
170 protected void handleVariableChangedEvent(@SuppressWarnings("rawtypes") Variable variable, int index,
171 ChangeType type) {
172 }
173
174 @Override
175 protected void storeState() {
176 }
177
178 @Override
179 protected void restoreState() {
180 }
181
182 @Override
183 protected void acceptState() {
184 }
185
186 }// END: class