| 1 |
package dr.evomodel.antigenic.phyloclustering.misc.obsolete; |
| 2 |
|
| 3 |
import dr.evomodel.antigenic.NPAntigenicLikelihood; |
| 4 |
import dr.inference.model.Likelihood; |
| 5 |
import dr.inference.model.MatrixParameter; |
| 6 |
import dr.inference.model.Parameter; |
| 7 |
import dr.inference.operators.MCMCOperator; |
| 8 |
import dr.inference.operators.SimpleMCMCOperator; |
| 9 |
import dr.math.MathUtils; |
| 10 |
import dr.xml.*; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class ClusterOperator extends SimpleMCMCOperator { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public final static String CLUSTER_OPERATOR = "clusterOperator"; |
| 19 |
public static final String WINDOW_SIZE = "windowSize"; |
| 20 |
|
| 21 |
|
| 22 |
public NPAntigenicLikelihood modelLikelihood; |
| 23 |
private Parameter assignments = null; |
| 24 |
private Parameter links = null; |
| 25 |
|
| 26 |
private MatrixParameter virusLocations = null; |
| 27 |
private double windowSize = 1; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public ClusterOperator(Parameter assignments, MatrixParameter virusLocations, double weight, double windowSize, NPAntigenicLikelihood Likelihood, Parameter links) { |
| 32 |
|
| 33 |
this.links = links; |
| 34 |
this.assignments = assignments; |
| 35 |
this.virusLocations = virusLocations; |
| 36 |
this.windowSize = windowSize; |
| 37 |
this.modelLikelihood = Likelihood; |
| 38 |
|
| 39 |
setWeight(weight); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
System.out.println((int)assignments.getParameterValue(0)); |
| 44 |
Parameter vLoc = virusLocations.getParameter(0); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public final double doOperation() { |
| 63 |
|
| 64 |
double moveWholeClusterOrChangeOnePoint = MathUtils.nextDouble(); |
| 65 |
|
| 66 |
|
| 67 |
double moveWholeClusterProb = 0.5; |
| 68 |
|
| 69 |
int numSamples = assignments.getDimension(); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
int target = MathUtils.nextInt(numSamples); |
| 75 |
int targetCluster = (int) assignments.getParameterValue(target); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
int ranDim= MathUtils.nextInt(virusLocations.getParameter(0).getDimension()); |
| 87 |
|
| 88 |
double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
for(int i=0; i < numSamples; i++){ |
| 93 |
if((int) assignments.getParameterValue(i)== targetCluster ){ |
| 94 |
|
| 95 |
Parameter vLoc = virusLocations.getParameter(i); |
| 96 |
|
| 97 |
double newValue = vLoc.getParameterValue(ranDim) + draw; |
| 98 |
vLoc.setParameterValue(ranDim, newValue); |
| 99 |
|
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
return 0.0; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public int minEmpty(double[] logLikVector){ |
| 210 |
int isEmpty=0; |
| 211 |
int i =0; |
| 212 |
while (isEmpty==0){ |
| 213 |
if(logLikVector[i]==0){ |
| 214 |
isEmpty=1;} |
| 215 |
else { |
| 216 |
if(i==logLikVector.length-1){isEmpty=1;} |
| 217 |
i++;} |
| 218 |
} |
| 219 |
return i; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public int maxFull(double[] logLikVector){ |
| 230 |
int isEmpty=1; |
| 231 |
int i =logLikVector.length-1; |
| 232 |
while (isEmpty==1){ |
| 233 |
if(logLikVector[i]!=0){ |
| 234 |
isEmpty=0;} |
| 235 |
else {i--;} |
| 236 |
} |
| 237 |
return i; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public int[] connected(int i, Parameter clusteringParameter){ |
| 244 |
int n = clusteringParameter.getDimension(); |
| 245 |
int[] visited = new int[n+1]; |
| 246 |
visited[0]=i+1; |
| 247 |
int tv=1; |
| 248 |
|
| 249 |
for(int j=0;j<n;j++){ |
| 250 |
if(visited[j]!=0){ |
| 251 |
int curr = visited[j]-1; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
int forward = (int) clusteringParameter.getParameterValue(curr); |
| 257 |
visited[tv] = forward+1; |
| 258 |
tv++; |
| 259 |
|
| 260 |
|
| 261 |
for(int ii=0; ii<tv-1; ii++){ |
| 262 |
if(visited[ii]==forward+1){ |
| 263 |
tv--; |
| 264 |
visited[tv]=0; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
for (int jj=0; jj<n;jj++){ |
| 272 |
if((int)clusteringParameter.getParameterValue(jj)==curr){ |
| 273 |
visited[tv]= jj+1; |
| 274 |
tv++; |
| 275 |
|
| 276 |
for(int ii=0; ii<tv-1; ii++){ |
| 277 |
if(visited[ii]==jj+1){ |
| 278 |
tv--; |
| 279 |
visited[tv]=0; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
}} |
| 287 |
return visited; |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public void accept(double deviation) { |
| 294 |
super.accept(deviation); |
| 295 |
|
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
public void reject(){ |
| 300 |
super.reject(); |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public final String getOperatorName() { |
| 308 |
return CLUSTER_OPERATOR; |
| 309 |
} |
| 310 |
|
| 311 |
public final void optimize(double targetProb) { |
| 312 |
|
| 313 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 314 |
} |
| 315 |
|
| 316 |
public boolean isOptimizing() { |
| 317 |
return false; |
| 318 |
} |
| 319 |
|
| 320 |
public void setOptimizing(boolean opt) { |
| 321 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 322 |
} |
| 323 |
|
| 324 |
public double getMinimumAcceptanceLevel() { |
| 325 |
return 0.1; |
| 326 |
} |
| 327 |
|
| 328 |
public double getMaximumAcceptanceLevel() { |
| 329 |
return 0.4; |
| 330 |
} |
| 331 |
|
| 332 |
public double getMinimumGoodAcceptanceLevel() { |
| 333 |
return 0.20; |
| 334 |
} |
| 335 |
|
| 336 |
public double getMaximumGoodAcceptanceLevel() { |
| 337 |
return 0.30; |
| 338 |
} |
| 339 |
|
| 340 |
public String getPerformanceSuggestion() { |
| 341 |
if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) { |
| 342 |
return ""; |
| 343 |
} else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) { |
| 344 |
return ""; |
| 345 |
} else { |
| 346 |
return ""; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 354 |
public final static String ASSIGNMENTS = "assignments"; |
| 355 |
public final static String VIRUS_LOCATIONS = "virusLocations"; |
| 356 |
|
| 357 |
public final static String LIKELIHOOD = "likelihood"; |
| 358 |
public final static String LINKS = "links"; |
| 359 |
|
| 360 |
|
| 361 |
public String getParserName() { |
| 362 |
return CLUSTER_OPERATOR; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 369 |
|
| 370 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
XMLObject cxo = xo.getChild(ASSIGNMENTS); |
| 375 |
Parameter assignments = (Parameter) cxo.getChild(Parameter.class); |
| 376 |
|
| 377 |
MatrixParameter virusLocationsParameter = null; |
| 378 |
if (xo.hasChildNamed(VIRUS_LOCATIONS)) { |
| 379 |
virusLocationsParameter = (MatrixParameter) xo.getElementFirstChild(VIRUS_LOCATIONS); |
| 380 |
} |
| 381 |
|
| 382 |
cxo = xo.getChild(LINKS); |
| 383 |
Parameter links = (Parameter) cxo.getChild(Parameter.class); |
| 384 |
|
| 385 |
cxo = xo.getChild(LIKELIHOOD); |
| 386 |
NPAntigenicLikelihood likelihood = (NPAntigenicLikelihood)cxo.getChild(NPAntigenicLikelihood.class); |
| 387 |
|
| 388 |
|
| 389 |
return new ClusterOperator(assignments, virusLocationsParameter, weight, 1.0, likelihood, links); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public String getParserDescription() { |
| 400 |
return "An operator that picks a new allocation of an item to a cluster under the Dirichlet process."; |
| 401 |
} |
| 402 |
|
| 403 |
public Class getReturnType() { |
| 404 |
return ClusterOperator.class; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 409 |
return rules; |
| 410 |
} |
| 411 |
|
| 412 |
private final XMLSyntaxRule[] rules = { |
| 413 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 414 |
|
| 415 |
new ElementRule(ASSIGNMENTS, |
| 416 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 417 |
new ElementRule(VIRUS_LOCATIONS, MatrixParameter.class, "Parameter of locations of all virus"), |
| 418 |
new ElementRule(LINKS, |
| 419 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 420 |
new ElementRule(LIKELIHOOD, new XMLSyntaxRule[] { |
| 421 |
new ElementRule(Likelihood.class),}, true), |
| 422 |
|
| 423 |
}; |
| 424 |
|
| 425 |
}; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public int getStepCount() { |
| 431 |
return 1; |
| 432 |
} |
| 433 |
|
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|