Debian Codegraph @

src/dr/oldevomodel/ibd/IBDReporter.java

raw symbols SCIP
1 /*
2 * IBDReporter.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.oldevomodel.ibd;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeTrait;
31 import dr.evolution.tree.TreeTraitProvider;
32 import dr.evomodel.branchratemodel.BranchRateModel;
33 import dr.evomodel.branchratemodel.DefaultBranchRateModel;
34 import dr.oldevomodel.substmodel.AbstractSubstitutionModel;
35 import dr.oldevomodel.substmodel.HKY;
36 import dr.evomodel.tree.TreeModel;
37 import dr.inference.model.*;
38 import dr.xml.*;
39
40 /**
41 * Package: dr.evomodel.ibd
42 * Description:
43 * <p/>
44 * <p/>
45 * Created by
46 * avaleks ([email protected])
47 * Date: 04-Aug-2008
48 * Time: 13:46:33
49 */
50 public class IBDReporter extends AbstractModel implements TreeTraitProvider {
51
52 protected double[] ibdweights;
53 protected double[][] ibdForward;
54 protected double[][] ibdBackward;
55 protected double[] diag;
56 protected boolean weightsKnown;
57 protected HKY substitutionModel;
58 protected TreeModel treeModel;
59 protected BranchRateModel branchRateModel;
60 protected Parameter mutationParameter;
61
62 IBDReporter(Parameter mutationParameter, TreeModel treeModel, BranchRateModel branchRateModel, AbstractSubstitutionModel substitutionModel) {
63 super("IBDReporter");
64 this.substitutionModel = (HKY) substitutionModel;
65 addModel(this.substitutionModel);
66 this.treeModel = treeModel;
67 addModel(this.treeModel);
68 this.branchRateModel = branchRateModel;
69 addModel(this.branchRateModel);
70 this.mutationParameter = mutationParameter;
71 addVariable(this.mutationParameter);
72 }
73
74 public void forwardIBD() {
75 int numNodes = treeModel.getNodeCount();
76 int stateCount = substitutionModel.getStateCount();
77 getDiagonalRates(diag);
78 for (int nodeId = 0; nodeId < numNodes; ++nodeId) {
79 NodeRef node = treeModel.getNode(nodeId);
80 NodeRef parent = treeModel.getParent(node);
81 if (parent == null) { // handle the root
82
83 } else if (treeModel.isExternal(node)) { // Handle the tip
84 double branchTime = branchRateModel.getBranchRate(treeModel, node) * (treeModel.getNodeHeight(parent) - treeModel.getNodeHeight(node));
85
86 for (int state = 0; state < stateCount; ++state) {
87 ibdForward[nodeId][state] = Math.exp(-diag[state] * branchTime);
88 }
89 } else { // Handle internal node
90 double branchTime = branchRateModel.getBranchRate(treeModel, node) * (treeModel.getNodeHeight(parent) - treeModel.getNodeHeight(node));
91
92 int childCount = treeModel.getChildCount(node);
93 for (int state = 0; state < stateCount; ++state) {
94 ibdForward[nodeId][state] = 0;
95 for (int child = 0; child < childCount; ++child) {
96 int childNodeId = treeModel.getChild(node, child).getNumber();
97 ibdForward[nodeId][state] += ibdForward[childNodeId][state];
98 }
99 ibdForward[nodeId][state] *= Math.exp(-diag[state] * branchTime);
100 }
101 }
102 }
103 }
104
105 public void backwardIBD(NodeRef node) {
106 int stateCount = substitutionModel.getStateCount();
107 if (node == null) {
108 node = treeModel.getRoot();
109 int nodeId = node.getNumber();
110 for (int state = 0; state < stateCount; ++state) {
111 ibdBackward[nodeId][state] = 0;
112 }
113 }
114 getDiagonalRates(diag);
115 int childCount = treeModel.getChildCount(node);
116 int nodeId = node.getNumber();
117 for (int child = 0; child < childCount; ++child) {
118 NodeRef childNode = treeModel.getChild(node, child);
119 int childNodeId = childNode.getNumber();
120 double branchTime = branchRateModel.getBranchRate(treeModel, childNode) * (treeModel.getNodeHeight(node) - treeModel.getNodeHeight(childNode));
121 for (int state = 0; state < stateCount; ++state) {
122 ibdBackward[childNodeId][state] = ibdBackward[nodeId][state];
123 for (int sibling = 0; sibling < childCount; ++sibling) {
124 if (sibling != child) {
125 int siblingId = treeModel.getChild(node, sibling).getNumber();
126 ibdBackward[childNodeId][state] += ibdForward[siblingId][state];
127 }
128 }
129 ibdBackward[childNodeId][state] *= Math.exp(-diag[state] * branchTime);
130 }
131 }
132 for (int child = 0; child < childCount; ++child) {
133 NodeRef childNode = treeModel.getChild(node, child);
134 backwardIBD(childNode);
135 }
136
137 }
138
139 public void expectedIBD() {
140 int stateCount = substitutionModel.getStateCount();
141 int nodeCount = treeModel.getNodeCount();
142 if (ibdweights == null) {
143 ibdweights = new double[treeModel.getExternalNodeCount()];
144 ibdForward = new double[nodeCount][stateCount];
145 ibdBackward = new double[nodeCount][stateCount];
146 diag = new double[stateCount];
147 }
148
149 double[] freq = substitutionModel.getFrequencyModel().getFrequencies();
150 forwardIBD();
151 backwardIBD(null);
152 int numTips = treeModel.getExternalNodeCount();
153 for (int i = 0; i < numTips; ++i) {
154 ibdweights[i] = 0;
155 for (int j = 0; j < stateCount; ++j) {
156 ibdweights[i] += ibdBackward[i][j] * freq[j];
157 }
158 }
159 }
160
161 protected void getDiagonalRates(double[] diagonalRates) {
162 double kappa = substitutionModel.getKappa();
163 double[] freq = substitutionModel.getFrequencyModel().getFrequencies();
164 double mutationRate = mutationParameter.getParameterValue(0);
165 double beta = 0.5 / ((freq[0] + freq[2]) * (freq[1] + freq[3]) + kappa * (freq[0] * freq[2] + freq[1] * freq[3]));
166
167 diagonalRates[0] = ((freq[1] + freq[3]) + freq[2] * kappa) * mutationRate * beta;
168 diagonalRates[1] = ((freq[0] + freq[2]) + freq[3] * kappa) * mutationRate * beta;
169 diagonalRates[2] = ((freq[1] + freq[3]) + freq[0] * kappa) * mutationRate * beta;
170 diagonalRates[3] = ((freq[0] + freq[2]) + freq[1] * kappa) * mutationRate * beta;
171 }
172
173 TreeTrait ibdWeight = new TreeTrait.D() {
174 public String getTraitName() {
175 return "IBDWeight";
176 }
177
178 public Intent getIntent() {
179 return Intent.NODE;
180 }
181
182 public Double getTrait(Tree tree, NodeRef node) {
183 if (!weightsKnown) {
184 expectedIBD();
185 weightsKnown = true;
186 }
187 if (tree.isExternal(node)) {
188 return getIBDWeight(tree, node);
189 }
190 return null;
191 }
192 };
193
194 public TreeTrait[] getTreeTraits() {
195 return new TreeTrait[] { ibdWeight };
196 }
197
198 public TreeTrait getTreeTrait(String key) {
199 // ignore the key - it must be the one they wanted, no?
200 return ibdWeight;
201 }
202
203 private double getIBDWeight(Tree tree, NodeRef node) {
204 if (!weightsKnown) {
205 expectedIBD();
206 weightsKnown = true;
207 }
208 if (tree.isExternal(node)) {
209 int nodeNum = node.getNumber();
210 return ibdweights[nodeNum] + 1;
211 }
212 return 0;
213 }
214
215 /**
216 * The XML parser
217 */
218
219 public static final String IBD_REPORTER_LIKELIHOOD = "ibdReporter";
220 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
221
222 public String getParserName() {
223 return IBD_REPORTER_LIKELIHOOD;
224 }
225
226 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
227
228
229 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
230 Parameter mutationParameter = (Parameter) xo.getChild(Parameter.class);
231 AbstractSubstitutionModel substitutionModel =
232 (AbstractSubstitutionModel) xo.getChild(AbstractSubstitutionModel.class);
233
234 BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
235 if (branchRateModel == null) {
236 branchRateModel = new DefaultBranchRateModel();
237 }
238
239 return new IBDReporter(mutationParameter, treeModel, branchRateModel, substitutionModel);
240 }
241
242 //************************************************************************
243 // AbstractXMLObjectParser implementation
244 //************************************************************************
245
246 public String getParserDescription() {
247 return "This element represents a reporter for expected number of tips ibd.";
248 }
249
250 public Class getReturnType() {
251 return Likelihood.class;
252 }
253
254 public XMLSyntaxRule[] getSyntaxRules() {
255 return rules;
256 }
257
258 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
259 new ElementRule(TreeModel.class),
260 new ElementRule(BranchRateModel.class, true),
261 new ElementRule(AbstractSubstitutionModel.class),
262 new ElementRule(Parameter.class)
263 };
264 };
265
266 protected void handleModelChangedEvent(Model model, Object object, int index) {
267 if (model == branchRateModel || model == treeModel || model == substitutionModel) {
268 weightsKnown = false;
269 } else {
270 System.err.println("Weird call back to IBDReporter from " + model.getModelName());
271 }
272 }
273
274 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
275 if (variable == mutationParameter) {
276 weightsKnown = false;
277 } else {
278 System.err.println("Weird call back to IBDReporter from " + variable.getVariableName());
279 }
280 }
281
282 protected void storeState() {
283 }
284
285 protected void restoreState() {
286 }
287
288 protected void acceptState() {
289 }
290
291 }