| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 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 |
|
| 99 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 100 |
final Object child = xo.getChild(i); |
| 101 |
|
| 102 |
|
| 103 |
if (child instanceof Tree) { |
| 104 |
|
| 105 |
} else if (child instanceof TaxonList) { |
| 106 |
|
| 107 |
taxonLists.add((Taxa) child); |
| 108 |
|
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
try { |
| 114 |
|
| 115 |
Tree[] trees = new Tree[taxonLists.size()]; |
| 116 |
|
| 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 |
|
| 126 |
treeModel.beginTreeEdit(); |
| 127 |
|
| 128 |
treeModel.adoptTreeStructure(simTree); |
| 129 |
|
| 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 |
|
| 139 |
e.printStackTrace(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 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 |
|
| 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 |
} |