| 1 |
|
| 2 |
package dr.evomodel.antigenic.phyloclustering.operators; |
| 3 |
|
| 4 |
import java.util.LinkedList; |
| 5 |
|
| 6 |
import dr.evomodel.antigenic.phyloclustering.TreeClusteringSharedRoutines; |
| 7 |
import dr.evomodel.tree.TreeModel; |
| 8 |
import dr.inference.model.MatrixParameter; |
| 9 |
import dr.inference.model.Parameter; |
| 10 |
import dr.inference.operators.AbstractAdaptableOperator; |
| 11 |
import dr.inference.operators.AdaptationMode; |
| 12 |
import dr.inference.operators.MCMCOperator; |
| 13 |
import dr.inference.operators.OperatorUtils; |
| 14 |
import dr.math.MathUtils; |
| 15 |
import dr.xml.AbstractXMLObjectParser; |
| 16 |
import dr.xml.AttributeRule; |
| 17 |
import dr.xml.ElementRule; |
| 18 |
import dr.xml.XMLObject; |
| 19 |
import dr.xml.XMLObjectParser; |
| 20 |
import dr.xml.XMLParseException; |
| 21 |
import dr.xml.XMLSyntaxRule; |
| 22 |
|
| 23 |
public class RandomWalkOnActiveMu extends AbstractAdaptableOperator { |
| 24 |
|
| 25 |
|
| 26 |
private MatrixParameter mu = null; |
| 27 |
private MatrixParameter virusLocations = null; |
| 28 |
private MatrixParameter virusLocationsTreeNode = null; |
| 29 |
private Parameter indicators; |
| 30 |
private TreeModel treeModel; |
| 31 |
|
| 32 |
private int numdata; |
| 33 |
private int numNodes; |
| 34 |
private int []correspondingTreeIndexForVirus = null; |
| 35 |
|
| 36 |
private double windowSize = 0.5; |
| 37 |
|
| 38 |
|
| 39 |
public RandomWalkOnActiveMu(double weight, MatrixParameter virusLocations, MatrixParameter mu, Parameter indicators, TreeModel treeModel_in, double windowSize, MatrixParameter virusLocationsTreeNode_in){ |
| 40 |
|
| 41 |
super(AdaptationMode.ADAPTATION_ON); |
| 42 |
|
| 43 |
setWeight(weight); |
| 44 |
this.windowSize = windowSize; |
| 45 |
|
| 46 |
this.virusLocations = virusLocations; |
| 47 |
this.mu = mu; |
| 48 |
this.indicators = indicators; |
| 49 |
this.treeModel= treeModel_in; |
| 50 |
this.virusLocationsTreeNode = virusLocationsTreeNode_in; |
| 51 |
|
| 52 |
numNodes = treeModel.getNodeCount(); |
| 53 |
numdata = virusLocations.getColumnDimension(); |
| 54 |
|
| 55 |
correspondingTreeIndexForVirus = TreeClusteringSharedRoutines.setMembershipTreeToVirusIndexes(numdata, virusLocations, numNodes, treeModel); |
| 56 |
TreeClusteringSharedRoutines.updateUndriftedVirusLocations(numNodes, numdata, treeModel, virusLocationsTreeNode, indicators, mu, virusLocations, correspondingTreeIndexForVirus); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public double doOperation() { |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
int originalNode = TreeClusteringSharedRoutines.findAnOnNodeIncludingRootRandomly(numNodes, indicators); |
| 66 |
|
| 67 |
int dimSelect = (int) Math.floor( MathUtils.nextDouble()* 2 ); |
| 68 |
double change = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize; |
| 69 |
double originalValue = mu.getParameter(originalNode).getParameterValue(dimSelect); |
| 70 |
mu.getParameter(originalNode ).setParameterValue(dimSelect, originalValue + change); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
LinkedList<Integer> childrenOriginalNode = TreeClusteringSharedRoutines.findActiveBreakpointsChildren(originalNode, numNodes, treeModel, indicators); |
| 75 |
for(int i=0; i < childrenOriginalNode.size(); i++){ |
| 76 |
int muIndexNum = childrenOriginalNode.get(i).intValue() ; |
| 77 |
Parameter curMu = mu.getParameter( muIndexNum ); |
| 78 |
double curMu_original = curMu.getParameterValue( dimSelect); |
| 79 |
mu.getParameter(muIndexNum).setParameterValue(dimSelect, curMu_original - change); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
TreeClusteringSharedRoutines.updateUndriftedVirusLocations(numNodes, numdata, treeModel, virusLocationsTreeNode, indicators, mu, virusLocations, correspondingTreeIndexForVirus); |
| 88 |
|
| 89 |
|
| 90 |
return 0.0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public double getAdaptableParameter() { |
| 99 |
return Math.log(windowSize); |
| 100 |
} |
| 101 |
|
| 102 |
public void setAdaptableParameter(double value) { |
| 103 |
windowSize = Math.exp(value); |
| 104 |
} |
| 105 |
|
| 106 |
public double getRawParameter() { |
| 107 |
return windowSize; |
| 108 |
} |
| 109 |
|
| 110 |
public String getAdaptableParameterName() { |
| 111 |
return "windowSize"; |
| 112 |
} |
| 113 |
|
| 114 |
public final static String RANDOMWALKACTIVEMU = "randomWalkOnActiveMu"; |
| 115 |
|
| 116 |
public final String getOperatorName() { |
| 117 |
return RANDOMWALKACTIVEMU; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 127 |
|
| 128 |
|
| 129 |
public final static String VIRUSLOCATIONS = "virusLocations"; |
| 130 |
public final static String VIRUSLOCATIONSTREENODE = "virusLocationsTreeNodes"; |
| 131 |
public final static String MU = "mu"; |
| 132 |
public final static String INDICATORS = "indicators"; |
| 133 |
public final static String WINDOWSIZE = "windowSize"; |
| 134 |
|
| 135 |
|
| 136 |
public String getParserName() { |
| 137 |
return RANDOMWALKACTIVEMU; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 144 |
|
| 145 |
|
| 146 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 147 |
double windowSize = xo.getDoubleAttribute(WINDOWSIZE); |
| 148 |
|
| 149 |
|
| 150 |
XMLObject cxo = xo.getChild(VIRUSLOCATIONS); |
| 151 |
MatrixParameter virusLocations = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 152 |
|
| 153 |
cxo = xo.getChild(VIRUSLOCATIONSTREENODE); |
| 154 |
MatrixParameter virusLocationsTreeNode = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 155 |
|
| 156 |
|
| 157 |
cxo = xo.getChild(MU); |
| 158 |
MatrixParameter mu = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 159 |
|
| 160 |
cxo = xo.getChild(INDICATORS); |
| 161 |
Parameter indicators = (Parameter) cxo.getChild(Parameter.class); |
| 162 |
|
| 163 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 164 |
|
| 165 |
return new RandomWalkOnActiveMu(weight, virusLocations, mu, indicators, treeModel, windowSize, virusLocationsTreeNode); |
| 166 |
|
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public String getParserDescription() { |
| 175 |
return "changes serum drift and make sure the first dimension of the active drifted mus stay the same"; |
| 176 |
} |
| 177 |
|
| 178 |
public Class getReturnType() { |
| 179 |
return RandomWalkOnActiveMu.class; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 184 |
return rules; |
| 185 |
} |
| 186 |
|
| 187 |
private final XMLSyntaxRule[] rules = { |
| 188 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 189 |
AttributeRule.newDoubleRule(WINDOWSIZE), |
| 190 |
new ElementRule(VIRUSLOCATIONS, Parameter.class), |
| 191 |
new ElementRule(VIRUSLOCATIONSTREENODE, MatrixParameter.class), |
| 192 |
new ElementRule(MU, Parameter.class), |
| 193 |
new ElementRule(INDICATORS, Parameter.class), |
| 194 |
new ElementRule(TreeModel.class), |
| 195 |
|
| 196 |
}; |
| 197 |
|
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public int getStepCount() { |
| 203 |
return 1; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
} |