Debian Codegraph @

src/dr/evomodel/continuous/GibbsIndependentCoalescentOperator.java

raw symbols SCIP
1 /*
2 * GibbsIndependentCoalescentOperator.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.continuous;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import dr.evolution.tree.SimpleTree;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.util.Taxa;
34 import dr.evolution.util.TaxonList;
35 import dr.evomodel.coalescent.CoalescentLikelihood;
36 import dr.evomodel.coalescent.CoalescentSimulator;
37 import dr.evomodel.coalescent.DemographicModel;
38 import dr.evomodel.tree.TreeModel;
39 import dr.inference.operators.GibbsOperator;
40 import dr.inference.operators.MCMCOperator;
41 import dr.inference.operators.SimpleMCMCOperator;
42 import dr.xml.AttributeRule;
43 import dr.xml.ElementRule;
44 import dr.xml.XMLObject;
45 import dr.xml.XMLParseException;
46 import dr.xml.XMLSyntaxRule;
47
48 /**
49 * An independent coalescent sampler, based on the coalescent simulator parser code.
50 *
51 * @author Guy Baele
52 *
53 */
54 public class GibbsIndependentCoalescentOperator extends SimpleMCMCOperator implements GibbsOperator {
55
56 public static final String OPERATOR_NAME = "GibbsIndependentCoalescentOperator";
57
58 private TreeModel treeModel;
59 private DemographicModel demoModel;
60 private CoalescentLikelihood coalescent;
61 private XMLObject xo;
62
63 public GibbsIndependentCoalescentOperator(XMLObject xo, TreeModel treeModel, DemographicModel demoModel, CoalescentLikelihood coalescent, double weight) {
64
65 this.xo = xo;
66 this.treeModel = treeModel;
67 this.demoModel = demoModel;
68 this.coalescent = coalescent;
69 setWeight(weight);
70
71 }
72
73 public String getPerformanceSuggestion() {
74 return "";
75 }
76
77 public String getOperatorName() {
78 return "GibbsIndependentCoalescent(" + treeModel.getModelName() + ")";
79 }
80
81 public int getStepCount() {
82 return 1;
83 }
84
85 /**
86 * change the parameter and return the hastings ratio.
87 */
88 public double doOperation() {
89
90 CoalescentSimulator simulator = new CoalescentSimulator();
91
92 List<TaxonList> taxonLists = new ArrayList<TaxonList>();
93
94 double rootHeight = -1.0;
95 double oldLikelihood = 0.0;
96 double newLikelihood = 0.0;
97
98 // should have one child that is node
99 for (int i = 0; i < xo.getChildCount(); i++) {
100 final Object child = xo.getChild(i);
101
102 //careful: Trees are TaxonLists ... (AER); see OldCoalescentSimulatorParser
103 if (child instanceof Tree) {
104 //do nothing
105 } else if (child instanceof TaxonList) {
106 //taxonLists.add((TaxonList) child);
107 taxonLists.add((Taxa) child);
108 //taxa added
109 break;
110 }
111 }
112
113 try {
114
115 Tree[] trees = new Tree[taxonLists.size()];
116 // simulate each taxonList separately
117 for (int i = 0; i < taxonLists.size(); i++) {
118 trees[i] = simulator.simulateTree(taxonLists.get(i), demoModel);
119 }
120
121 oldLikelihood = coalescent.getLogLikelihood();
122
123 SimpleTree simTree = simulator.simulateTree(trees, demoModel, rootHeight, trees.length != 1);
124
125 //this would be the normal way to do it
126 treeModel.beginTreeEdit();
127 //now it's allowed to adjust the tree structure
128 treeModel.adoptTreeStructure(simTree);
129 //endTreeEdit() would then fire the events
130 treeModel.endTreeEdit();
131
132 newLikelihood = coalescent.getLogLikelihood();
133
134 } catch (IllegalArgumentException iae) {
135 try {
136 throw new XMLParseException(iae.getMessage());
137 } catch (XMLParseException e) {
138 // TODO Auto-generated catch block
139 e.printStackTrace();
140 }
141 }
142
143 //return oldLikelihood - newLikelihood;
144 return 0;
145 }
146
147 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
148
149 public String getParserName() {
150 return OPERATOR_NAME;
151 }
152
153 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
154
155 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
156 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
157 DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class);
158 CoalescentLikelihood coalescent = (CoalescentLikelihood) xo.getChild(CoalescentLikelihood.class);
159
160 return new GibbsIndependentCoalescentOperator(xo, treeModel, demoModel, coalescent, weight);
161
162 }
163
164 //************************************************************************
165 // AbstractXMLObjectParser implementation
166 //************************************************************************
167
168 public XMLSyntaxRule[] getSyntaxRules() {
169 return rules;
170 }
171
172 private final XMLSyntaxRule[] rules = {
173 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
174 new ElementRule(Taxa.class),
175 new ElementRule(TreeModel.class),
176 new ElementRule(DemographicModel.class),
177 new ElementRule(CoalescentLikelihood.class)
178 };
179
180 public String getParserDescription() {
181 return "This element returns an independence coalescent sampler, disguised as a Gibbs operator, from a demographic model.";
182 }
183
184 public Class getReturnType() {
185 return MCMCOperator.class;
186 }
187
188 };
189
190 }