| 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 dr.evolution.continuous.ContinuousTraitLikelihood; |
| 29 |
import dr.evolution.continuous.Contrastable; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.inference.model.Likelihood; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class ContinuousLikelihood extends Likelihood.Abstract { |
| 45 |
|
| 46 |
public static final String CONTINUOUS_LIKELIHOOD = "continuousLikelihood"; |
| 47 |
public static final String TRAIT = "trait"; |
| 48 |
public static final String NAME = "name"; |
| 49 |
|
| 50 |
public ContinuousLikelihood(TreeModel treeModel, String[] traitNames) { |
| 51 |
super(treeModel); |
| 52 |
this.treeModel = treeModel; |
| 53 |
this.traitNames = traitNames; |
| 54 |
mles = new Contrastable[traitNames.length]; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public double calculateLogLikelihood() { |
| 62 |
|
| 63 |
return contTraitLikelihood.calculateLikelihood(treeModel, traitNames, mles, 1.0); |
| 64 |
} |
| 65 |
|
| 66 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 67 |
|
| 68 |
public String getParserName() { return CONTINUOUS_LIKELIHOOD; } |
| 69 |
|
| 70 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 71 |
|
| 72 |
TreeModel treeModel = (TreeModel)xo.getChild(TreeModel.class); |
| 73 |
ArrayList<String> traits = new ArrayList<String>(); |
| 74 |
|
| 75 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 76 |
|
| 77 |
Object child = xo.getChild(i); |
| 78 |
if (child instanceof XMLObject) { |
| 79 |
|
| 80 |
XMLObject cxo = (XMLObject)child; |
| 81 |
|
| 82 |
if (cxo.getName().equals(TRAIT)) { |
| 83 |
traits.add(cxo.getStringAttribute(NAME)); |
| 84 |
} |
| 85 |
|
| 86 |
} else { |
| 87 |
throw new XMLParseException("unknown child element found in continuousLikelihood"); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (treeModel == null) |
| 92 |
throw new XMLParseException("tree model element missing from continuousLikelihood element"); |
| 93 |
|
| 94 |
String[] traitNames = new String[traits.size()]; |
| 95 |
for (int i =0; i < traitNames.length; i++) { traitNames[i] = traits.get(i); } |
| 96 |
|
| 97 |
ContinuousLikelihood cl = new ContinuousLikelihood(treeModel, traitNames); |
| 98 |
|
| 99 |
return cl; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public String getParserDescription() { |
| 107 |
return "A likelihood for continuous traits."; |
| 108 |
} |
| 109 |
|
| 110 |
public Class getReturnType() { return ContinuousLikelihood.class; } |
| 111 |
|
| 112 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 113 |
|
| 114 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 115 |
new ElementRule(TreeModel.class ) |
| 116 |
}; |
| 117 |
}; |
| 118 |
|
| 119 |
private String[] traitNames = null; |
| 120 |
private Contrastable[] mles = null; |
| 121 |
private TreeModel treeModel = null; |
| 122 |
private ContinuousTraitLikelihood contTraitLikelihood = new ContinuousTraitLikelihood(); |
| 123 |
} |