Debian Codegraph @

src/dr/evomodel/continuous/IndependentCoalescentSampler.java

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