| 1 |
package dr.evomodel.antigenic.phyloclustering.statistics; |
| 2 |
|
| 3 |
|
| 4 |
import dr.evolution.tree.NodeRef; |
| 5 |
import dr.evolution.tree.Tree; |
| 6 |
import dr.evolution.tree.TreeTrait; |
| 7 |
import dr.evolution.tree.TreeTraitProvider; |
| 8 |
import dr.evomodel.tree.TreeModel; |
| 9 |
import dr.inference.model.MatrixParameter; |
| 10 |
import dr.inference.model.Parameter; |
| 11 |
import dr.xml.AbstractXMLObjectParser; |
| 12 |
import dr.xml.AttributeRule; |
| 13 |
import dr.xml.ElementRule; |
| 14 |
import dr.xml.XMLObject; |
| 15 |
import dr.xml.XMLObjectParser; |
| 16 |
import dr.xml.XMLParseException; |
| 17 |
import dr.xml.XMLSyntaxRule; |
| 18 |
|
| 19 |
|
| 20 |
public class AnnotateMuTreeTrait implements TreeTraitProvider { |
| 21 |
|
| 22 |
protected Helper treeTraits = new Helper(); |
| 23 |
private TreeModel treeModel; |
| 24 |
public static final String ANNOTATEMUPARAMETERTREETRAIT = "annotateMuTreeTrait"; |
| 25 |
public final static String muParameterStr = "mu"; |
| 26 |
|
| 27 |
public final static String indicatorsStr = "indicators"; |
| 28 |
public final static String hasDriftStr = "hasDrift"; |
| 29 |
|
| 30 |
public final static String MU1_SCALE_PARAMETER = "mu1Scale"; |
| 31 |
public final static String MU2_SCALE_PARAMETER = "mu2Scale"; |
| 32 |
|
| 33 |
public final static String MUMEAN_PARAMETER = "muMean"; |
| 34 |
|
| 35 |
|
| 36 |
private MatrixParameter muParameter; |
| 37 |
private Parameter indicators; |
| 38 |
|
| 39 |
private Parameter mu1ScaleParameter; |
| 40 |
private Parameter mu2ScaleParameter; |
| 41 |
private Parameter muMeanParameter; |
| 42 |
private boolean hasDrift; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public AnnotateMuTreeTrait(TreeModel treeModel_in, MatrixParameter muTreeNode_in, Parameter indicators_in, boolean driftTreeClusterMu, Parameter mu1Scale, Parameter mu2Scale, Parameter muMean) { |
| 49 |
|
| 50 |
this.treeModel = treeModel_in; |
| 51 |
this.muParameter = muTreeNode_in; |
| 52 |
this.indicators = indicators_in; |
| 53 |
|
| 54 |
this.mu1ScaleParameter = mu1Scale; |
| 55 |
this.mu2ScaleParameter = mu2Scale; |
| 56 |
this.muMeanParameter = muMean; |
| 57 |
this.hasDrift = driftTreeClusterMu; |
| 58 |
|
| 59 |
treeTraits.addTrait(new TreeTrait.IA() { |
| 60 |
|
| 61 |
public String getTraitName() { |
| 62 |
|
| 63 |
|
| 64 |
return "mu"; |
| 65 |
} |
| 66 |
|
| 67 |
public String getTraitString(Tree tree, NodeRef node) { |
| 68 |
|
| 69 |
if(tree != treeModel){ |
| 70 |
System.out.println("Something is wrong. Why is tree not equal to treeModel?"); |
| 71 |
System.exit(0); |
| 72 |
} |
| 73 |
|
| 74 |
String outputStr = "{0,0}"; |
| 75 |
if( (int)indicators.getParameterValue(node.getNumber() ) ==1 ){ |
| 76 |
double firstCoord = muParameter.getParameter(node.getNumber()).getParameterValue(0); |
| 77 |
double secondCoord = muParameter.getParameter(node.getNumber()).getParameterValue(1); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if(hasDrift && mu1ScaleParameter != null && muMeanParameter != null ){ |
| 85 |
|
| 86 |
firstCoord = firstCoord * mu1ScaleParameter.getParameterValue(0); |
| 87 |
} |
| 88 |
if(hasDrift && mu2ScaleParameter != null ){ |
| 89 |
|
| 90 |
secondCoord = secondCoord * mu2ScaleParameter.getParameterValue(0); |
| 91 |
} |
| 92 |
|
| 93 |
outputStr = "{"+ firstCoord + "," + secondCoord +"}"; |
| 94 |
} |
| 95 |
return outputStr; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
public Intent getIntent() { |
| 100 |
|
| 101 |
return Intent.NODE; |
| 102 |
} |
| 103 |
|
| 104 |
public Class getTraitClass() { |
| 105 |
System.out.println("getTraitClass ran. Not expected. Quit now"); |
| 106 |
System.exit(0); |
| 107 |
return int[].class; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
public int[] getTrait(Tree tree, NodeRef node) { |
| 112 |
|
| 113 |
System.out.println("getTrait ran. Not expected. Quit now"); |
| 114 |
System.exit(0); |
| 115 |
|
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
}); |
| 121 |
|
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public TreeTrait[] getTreeTraits() { |
| 128 |
|
| 129 |
return treeTraits.getTreeTraits(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
public TreeTrait getTreeTrait(String key) { |
| 134 |
System.out.println("not expected to run getTreeTrait. Quit now"); |
| 135 |
System.exit(0); |
| 136 |
return treeTraits.getTreeTrait(key); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 143 |
|
| 144 |
|
| 145 |
public String getParserName() { |
| 146 |
return ANNOTATEMUPARAMETERTREETRAIT; |
| 147 |
} |
| 148 |
|
| 149 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 150 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 151 |
|
| 152 |
XMLObject cxo = xo.getChild(muParameterStr); |
| 153 |
MatrixParameter muParameter = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 154 |
|
| 155 |
cxo = xo.getChild(indicatorsStr); |
| 156 |
Parameter indicators = (Parameter) cxo.getChild(Parameter.class); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
boolean hasDrift = xo.getAttribute(hasDriftStr, false); |
| 162 |
|
| 163 |
Parameter mu1Scale = null; |
| 164 |
if (xo.hasChildNamed(MU1_SCALE_PARAMETER)) { |
| 165 |
mu1Scale = (Parameter) xo.getElementFirstChild(MU1_SCALE_PARAMETER); |
| 166 |
} |
| 167 |
|
| 168 |
Parameter mu2Scale = null; |
| 169 |
if (xo.hasChildNamed(MU2_SCALE_PARAMETER)) { |
| 170 |
mu2Scale = (Parameter) xo.getElementFirstChild(MU2_SCALE_PARAMETER); |
| 171 |
} |
| 172 |
|
| 173 |
Parameter muMean = null; |
| 174 |
if(xo.hasChildNamed(MUMEAN_PARAMETER)){ |
| 175 |
muMean = (Parameter) xo.getElementFirstChild(MUMEAN_PARAMETER); |
| 176 |
} |
| 177 |
|
| 178 |
return new AnnotateMuTreeTrait( treeModel, muParameter, indicators, hasDrift, mu1Scale, mu2Scale, muMean); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public String getParserDescription() { |
| 187 |
return "Integrate mu coordinates into the tree."; |
| 188 |
} |
| 189 |
|
| 190 |
public Class getReturnType() { |
| 191 |
return AnnotateMuTreeTrait.class; |
| 192 |
} |
| 193 |
|
| 194 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 195 |
return rules; |
| 196 |
} |
| 197 |
|
| 198 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 199 |
new ElementRule(TreeModel.class), |
| 200 |
new ElementRule(muParameterStr, MatrixParameter.class), |
| 201 |
new ElementRule(indicatorsStr,Parameter.class), |
| 202 |
|
| 203 |
new ElementRule(MU1_SCALE_PARAMETER, Parameter.class, "Optional parameter for scaling the first dimension of mu"), |
| 204 |
new ElementRule(MU2_SCALE_PARAMETER, Parameter.class, "Optional parameter for scaling the second dimension of mu"), |
| 205 |
new ElementRule(MUMEAN_PARAMETER, Parameter.class), |
| 206 |
AttributeRule.newBooleanRule(hasDriftStr, true, "whether to multiple the mu by the drift term"), |
| 207 |
|
| 208 |
}; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
} |