| 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.antigenic; |
| 27 |
|
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.HashSet; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Set; |
| 32 |
import java.util.logging.Logger; |
| 33 |
|
| 34 |
import dr.evolution.tree.NodeRef; |
| 35 |
import dr.evolution.tree.TreeUtils; |
| 36 |
import dr.evomodel.tree.TreeModel; |
| 37 |
import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities; |
| 38 |
import dr.inference.model.AbstractModelLikelihood; |
| 39 |
import dr.inference.model.CompoundParameter; |
| 40 |
import dr.inference.model.Model; |
| 41 |
import dr.inference.model.Parameter; |
| 42 |
import dr.inference.model.Variable; |
| 43 |
import dr.math.GammaFunction; |
| 44 |
import dr.xml.AbstractXMLObjectParser; |
| 45 |
import dr.xml.ElementRule; |
| 46 |
import dr.xml.StringAttributeRule; |
| 47 |
import dr.xml.XMLObject; |
| 48 |
import dr.xml.XMLObjectParser; |
| 49 |
import dr.xml.XMLParseException; |
| 50 |
import dr.xml.XMLSyntaxRule; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class NPAntigenicLikelihood extends AbstractModelLikelihood { |
| 57 |
|
| 58 |
public static final String NP_ANTIGENIC_LIKELIHOOD = "NPAntigenicLikelihood"; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public NPAntigenicLikelihood (TreeModel treeModel,CompoundParameter traitParameter , Parameter assignments, Parameter links, |
| 63 |
Parameter chi, Parameter clusterPrec, Parameter priorMean, Parameter priorPrec, |
| 64 |
Parameter transformFactor,Parameter means1, Parameter means2, Parameter locationDrift, |
| 65 |
Parameter offsets, Boolean hasDrift){ |
| 66 |
super(NP_ANTIGENIC_LIKELIHOOD); |
| 67 |
|
| 68 |
|
| 69 |
this.assignments = assignments; |
| 70 |
this.links = links; |
| 71 |
this.clusterPrec = clusterPrec; |
| 72 |
this.priorPrec = priorPrec; |
| 73 |
this.priorMean = priorMean; |
| 74 |
this.treeModel= treeModel; |
| 75 |
this.traitParameter= traitParameter; |
| 76 |
this.transformFactor=transformFactor; |
| 77 |
this.means1=means1; |
| 78 |
this.means2=means2; |
| 79 |
this.alpha= chi; |
| 80 |
this.locationDrift=locationDrift; |
| 81 |
this.offsets=offsets; |
| 82 |
|
| 83 |
this.hasDrift=false; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
addVariable(traitParameter); |
| 88 |
addVariable(assignments); |
| 89 |
addVariable(links); |
| 90 |
addModel(treeModel); |
| 91 |
addVariable(chi); |
| 92 |
addVariable(transformFactor); |
| 93 |
addVariable(alpha); |
| 94 |
|
| 95 |
addVariable(offsets); |
| 96 |
|
| 97 |
numdata = traitParameter.getParameterCount(); |
| 98 |
this.allTips= TreeUtils.getExternalNodes(treeModel,treeModel.getRoot()); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
setDepMatrix(); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
for (int i=0; i<numdata; i++){ |
| 109 |
assignments.setParameterValue(i, i); |
| 110 |
links.setParameterValue(i,i); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
this.logLikelihoodsVector = new double[links.getDimension()+1]; |
| 117 |
this.logLikelihoodsVectorKnown = new boolean[links.getDimension()+1]; |
| 118 |
this.storedLogLikelihoodsVector = new double[links.getDimension()+1]; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
this.m = new double[2]; |
| 123 |
m[0]= priorMean.getParameterValue(0); |
| 124 |
m[1]= priorMean.getParameterValue(1); |
| 125 |
|
| 126 |
|
| 127 |
this.v0 = 2; |
| 128 |
|
| 129 |
|
| 130 |
this.k0= priorPrec.getParameterValue(0)/clusterPrec.getParameterValue(0); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
this.T0Inv= new double[2][2]; |
| 135 |
T0Inv[0][0]= v0/clusterPrec.getParameterValue(0); |
| 136 |
T0Inv[1][1]= v0/clusterPrec.getParameterValue(0); |
| 137 |
T0Inv[1][0]= 0.0; |
| 138 |
T0Inv[0][1]= 0.0; |
| 139 |
|
| 140 |
|
| 141 |
this.logDetT0= -Math.log(T0Inv[0][0]*T0Inv[1][1]); |
| 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 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private void setDepMatrix(){ |
| 297 |
|
| 298 |
depMatrixKnown = true; |
| 299 |
|
| 300 |
depMatrix=new double[numdata][numdata]; |
| 301 |
List<NodeRef> childList = new ArrayList<NodeRef>(); |
| 302 |
|
| 303 |
recursion(treeModel.getRoot(),childList); |
| 304 |
logCorrectMatrix(transformFactor.getParameterValue(0)); |
| 305 |
logDepMatrix = new double[numdata][numdata]; |
| 306 |
for(int i=0;i<numdata;i++){ |
| 307 |
for(int j=0;j<i;j++){ |
| 308 |
logDepMatrix[i][j]=Math.log(depMatrix[i][j]); |
| 309 |
logDepMatrix[j][i]=logDepMatrix[j][i]; |
| 310 |
|
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public double getLogLikGroup(int groupNumber){ |
| 321 |
double L =0.0; |
| 322 |
|
| 323 |
|
| 324 |
int ngroup=0; |
| 325 |
for (int i=0;i<assignments.getDimension(); i++){ |
| 326 |
if((int) assignments.getParameterValue(i) == groupNumber){ |
| 327 |
ngroup++;}} |
| 328 |
|
| 329 |
|
| 330 |
if (ngroup != 0){ |
| 331 |
double[][] group = new double[ngroup][2]; |
| 332 |
|
| 333 |
|
| 334 |
double mean[]=new double[2]; |
| 335 |
|
| 336 |
int count = 0; |
| 337 |
for (int i=0;i<assignments.getDimension(); i++){ |
| 338 |
if((int) assignments.getParameterValue(i) == groupNumber){ |
| 339 |
group[count][0] = getData(i,0); |
| 340 |
group[count][1] = getData(i,0); |
| 341 |
mean[0]+=group[count][0]; |
| 342 |
mean[1]+=group[count][1]; |
| 343 |
count++;}} |
| 344 |
|
| 345 |
mean[0]/=ngroup; |
| 346 |
mean[1]/=ngroup; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
double kn= k0+ngroup; |
| 351 |
double vn= v0+ngroup; |
| 352 |
|
| 353 |
|
| 354 |
double[][] sumdif=new double[2][2]; |
| 355 |
|
| 356 |
for(int i=0;i<ngroup;i++){ |
| 357 |
sumdif[0][0]+= (group[i][0]-mean[0])*(group[i][0]-mean[0]); |
| 358 |
sumdif[0][1]+= (group[i][0]-mean[0])*(group[i][1]-mean[1]); |
| 359 |
sumdif[1][0]+= (group[i][0]-mean[0])*(group[i][1]-mean[1]); |
| 360 |
sumdif[1][1]+= (group[i][1]-mean[1])*(group[i][1]-mean[1]); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
double[][] TnInv = new double[2][2]; |
| 366 |
TnInv[0][0]=T0Inv[0][0]+ngroup*(k0/kn)*(mean[0]-m[0])*(mean[0]-m[0])+sumdif[0][0]; |
| 367 |
TnInv[0][1]=T0Inv[0][1]+ngroup*(k0/kn)*(mean[1]-m[1])*(mean[0]-m[0])+sumdif[0][1]; |
| 368 |
TnInv[1][0]=T0Inv[1][0]+ngroup*(k0/kn)* (mean[0]-m[0])*(mean[1]-m[1])+sumdif[1][0]; |
| 369 |
TnInv[1][1]=T0Inv[1][1]+ngroup*(k0/kn)* (mean[1]-m[1])*(mean[1]-m[1])+sumdif[1][1]; |
| 370 |
|
| 371 |
|
| 372 |
double logDetTn=-Math.log(TnInv[0][0]*TnInv[1][1]-TnInv[0][1]*TnInv[1][0]); |
| 373 |
|
| 374 |
|
| 375 |
L+= -(ngroup)*Math.log(Math.PI); |
| 376 |
L+= Math.log(k0) - Math.log(kn); |
| 377 |
L+= (vn/2)*logDetTn - (v0/2)*logDetT0; |
| 378 |
L+= GammaFunction.lnGamma(vn/2)+ GammaFunction.lnGamma((vn/2)-0.5); |
| 379 |
L+=-GammaFunction.lnGamma(v0/2)- GammaFunction.lnGamma((v0/2)-0.5); |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
} |
| 387 |
logLikelihoodsVectorKnown[groupNumber]=true; |
| 388 |
return L; |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public Model getModel() { |
| 399 |
return this; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public double[] getLogLikelihoodsVector(){ |
| 405 |
return logLikelihoodsVector; |
| 406 |
} |
| 407 |
|
| 408 |
public Parameter getLinks(){ |
| 409 |
return links; |
| 410 |
} |
| 411 |
|
| 412 |
public Parameter getAssignments(){ |
| 413 |
return assignments; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
public double getData(int virus, int dim){ |
| 418 |
return traitParameter.getParameter(virus).getParameterValue(dim); |
| 419 |
} |
| 420 |
|
| 421 |
public double[][] getDepMatrix(){ |
| 422 |
return depMatrix; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
public double[][] getLogDepMatrix(){ |
| 427 |
return logDepMatrix; |
| 428 |
} |
| 429 |
|
| 430 |
public Parameter getPriorMean(){ |
| 431 |
return priorMean; |
| 432 |
} |
| 433 |
public Parameter getPriorPrec(){ |
| 434 |
return priorPrec; |
| 435 |
} |
| 436 |
public Parameter getClusterPrec(){ |
| 437 |
return clusterPrec; |
| 438 |
} |
| 439 |
|
| 440 |
public void setLogLikelihoodsVector(int pos, double value){ |
| 441 |
logLikelihoodsVector[pos]=value; |
| 442 |
} |
| 443 |
|
| 444 |
public void setAssingments(int pos, double value){ |
| 445 |
assignments.setParameterValue(pos,value); |
| 446 |
} |
| 447 |
|
| 448 |
public void setLinks(int pos, double value){ |
| 449 |
links.setParameterValue(pos,value); |
| 450 |
} |
| 451 |
|
| 452 |
public void setMeans(int pos, double[] value){ |
| 453 |
means1.setParameterValue(pos,value[0]); |
| 454 |
means2.setParameterValue(pos,value[1]); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
public double getLogLikelihood() { |
| 459 |
if (!logLikelihoodKnown) { |
| 460 |
|
| 461 |
logLikelihood = computeLogLikelihood(); |
| 462 |
} |
| 463 |
|
| 464 |
return logLikelihood; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public double computeLogLikelihood() { |
| 471 |
|
| 472 |
|
| 473 |
if (!depMatrixKnown ){ |
| 474 |
setDepMatrix(); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
double logL = 0.0; |
| 481 |
for (int j=0 ; j<logLikelihoodsVector.length;j++){ |
| 482 |
|
| 483 |
if(!logLikelihoodsVectorKnown[j]){ |
| 484 |
logLikelihoodsVector[j]=getLogLikGroup(j); |
| 485 |
} |
| 486 |
logL +=logLikelihoodsVector[j]; |
| 487 |
|
| 488 |
} |
| 489 |
|
| 490 |
for (int j=0 ; j<links.getDimension();j++){ |
| 491 |
if(links.getParameterValue(j)==j){ |
| 492 |
logL += Math.log(alpha.getParameterValue(0)); |
| 493 |
} |
| 494 |
else{logL += Math.log(depMatrix[j][(int) links.getParameterValue(j)]); |
| 495 |
|
| 496 |
} |
| 497 |
|
| 498 |
double sumDist=0.0; |
| 499 |
for (int i=0;i<numdata;i++){ |
| 500 |
if(i!=j){sumDist += depMatrix[i][j]; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
logL-= Math.log(alpha.getParameterValue(0)+sumDist); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
logLikelihoodKnown=true; |
| 510 |
|
| 511 |
|
| 512 |
return logL; |
| 513 |
|
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
void recursion( NodeRef node, List childList){ |
| 522 |
|
| 523 |
|
| 524 |
List<NodeRef> leftChildTipList = new ArrayList<NodeRef>(); |
| 525 |
List<NodeRef> rightChildTipList = new ArrayList<NodeRef>(); |
| 526 |
|
| 527 |
if(!treeModel.isExternal(node)){ |
| 528 |
recursion(treeModel.getChild(node, 0),leftChildTipList); |
| 529 |
recursion(treeModel.getChild(node, 1),rightChildTipList); |
| 530 |
|
| 531 |
|
| 532 |
double lBranch = treeModel.getBranchLength(treeModel.getChild(node, 0)); |
| 533 |
double rBranch = treeModel.getBranchLength(treeModel.getChild(node, 1)); |
| 534 |
|
| 535 |
|
| 536 |
Set<NodeRef> notLeftChildList = new HashSet<NodeRef>(); |
| 537 |
notLeftChildList.addAll(allTips); |
| 538 |
for (NodeRef i :leftChildTipList){ |
| 539 |
notLeftChildList.remove(i); |
| 540 |
} |
| 541 |
Set<NodeRef> notRightChildList = new HashSet<NodeRef>(); |
| 542 |
notRightChildList.addAll(allTips); |
| 543 |
for (NodeRef i :rightChildTipList){ |
| 544 |
notRightChildList.remove(i); |
| 545 |
} |
| 546 |
|
| 547 |
for (NodeRef lChild : leftChildTipList){ |
| 548 |
for (NodeRef Child : notLeftChildList){ |
| 549 |
depMatrix[Child.getNumber()][lChild.getNumber()] += lBranch; |
| 550 |
depMatrix[lChild.getNumber()][Child.getNumber()] += lBranch; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
for (NodeRef rChild : rightChildTipList){ |
| 556 |
for (NodeRef Child : notRightChildList){ |
| 557 |
depMatrix[Child.getNumber()][rChild.getNumber()] += rBranch; |
| 558 |
depMatrix[rChild.getNumber()][Child.getNumber()] += rBranch; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
childList.addAll(leftChildTipList); |
| 564 |
childList.addAll(rightChildTipList); |
| 565 |
} |
| 566 |
else{ |
| 567 |
childList.add(node); |
| 568 |
} |
| 569 |
|
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
void logCorrectMatrix(double p){ |
| 575 |
for (int i=0; i<numdata; i++){ |
| 576 |
for (int j=0; j<i; j++){ |
| 577 |
depMatrix[i][j]=1/Math.pow(depMatrix[i][j],p); |
| 578 |
depMatrix[j][i]=depMatrix[i][j]; |
| 579 |
}} |
| 580 |
|
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public double getTreeDist(int i, int j){ |
| 591 |
double dist=0; |
| 592 |
|
| 593 |
NodeRef MRCA = findMRCA(i,j); |
| 594 |
|
| 595 |
NodeRef Parent = treeModel.getExternalNode(i); |
| 596 |
while (Parent!=MRCA){ |
| 597 |
dist+=treeModel.getBranchLength(Parent); |
| 598 |
Parent = treeModel.getParent(Parent); |
| 599 |
} |
| 600 |
|
| 601 |
Parent = treeModel.getExternalNode(j); |
| 602 |
while (Parent!=MRCA){ |
| 603 |
dist+=treeModel.getBranchLength(Parent); |
| 604 |
Parent = treeModel.getParent(Parent); |
| 605 |
} |
| 606 |
|
| 607 |
return dist; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
private NodeRef findMRCA(int iTip, int jTip) { |
| 612 |
Set<String> leafNames = new HashSet<String>(); |
| 613 |
leafNames.add(treeModel.getTaxonId(iTip)); |
| 614 |
leafNames.add(treeModel.getTaxonId(jTip)); |
| 615 |
return TreeUtils.getCommonAncestorNode(treeModel, leafNames); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
public void printInformtion(double[][] Mat) { |
| 620 |
StringBuffer sb = new StringBuffer("matrix \n"); |
| 621 |
for(int i=0;i <numdata; i++){ |
| 622 |
sb.append(" \n"); |
| 623 |
for(int j=0; j<numdata; j++){ |
| 624 |
sb.append(Mat[i][j]+" \t"); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 629 |
|
| 630 |
|
| 631 |
public void printInformation(Parameter Vec) { |
| 632 |
StringBuffer sb = new StringBuffer("Vector \n"); |
| 633 |
|
| 634 |
for (int i = 0; i<numdata; i++){ |
| 635 |
sb.append(Vec.getParameterValue(i)+" \t"); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public void printInformation(int[] Vec) { |
| 645 |
StringBuffer sb = new StringBuffer("Vector \n"); |
| 646 |
for(int i=0;i <numdata; i++){ |
| 647 |
|
| 648 |
sb.append(Vec[i]+" \t"); |
| 649 |
|
| 650 |
} |
| 651 |
|
| 652 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
public void printOrder() { |
| 658 |
StringBuffer sb = new StringBuffer("taxa \n"); |
| 659 |
for(int i=0;i <numdata; i++){ |
| 660 |
sb.append(" \n"); |
| 661 |
|
| 662 |
sb.append(treeModel.getTaxonId(i)); |
| 663 |
} |
| 664 |
|
| 665 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
public void printInformation(double x) { |
| 672 |
StringBuffer sb = new StringBuffer("Info \n"); |
| 673 |
sb.append(x); |
| 674 |
|
| 675 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 676 |
|
| 677 |
public void printInformation(String x) { |
| 678 |
StringBuffer sb = new StringBuffer("Info \n"); |
| 679 |
sb.append(x); |
| 680 |
|
| 681 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 682 |
|
| 683 |
public void printInformation(String x, String y) { |
| 684 |
StringBuffer sb = new StringBuffer("Info \n"); |
| 685 |
sb.append(x + " and " + y); |
| 686 |
|
| 687 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
@Override |
| 692 |
protected void storeState() { |
| 693 |
System.arraycopy(logLikelihoodsVector, 0, storedLogLikelihoodsVector, 0, logLikelihoodsVector.length); |
| 694 |
} |
| 695 |
|
| 696 |
@Override |
| 697 |
protected void restoreState() { |
| 698 |
double[] tmp = logLikelihoodsVector; |
| 699 |
logLikelihoodsVector = storedLogLikelihoodsVector; |
| 700 |
storedLogLikelihoodsVector = tmp; |
| 701 |
|
| 702 |
depMatrixKnown = !proposedChangeDepMatrix; |
| 703 |
proposedChangeDepMatrix =false; |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
logLikelihoodKnown = false; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public void makeDirty() { |
| 716 |
} |
| 717 |
|
| 718 |
public void acceptState() { |
| 719 |
|
| 720 |
proposedChangeDepMatrix =false; |
| 721 |
proposedChangeDataMatrix =false; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 726 |
if (model == treeModel) |
| 727 |
depMatrixKnown=false; |
| 728 |
logLikelihoodKnown=false; |
| 729 |
return; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 734 |
logLikelihoodKnown = false; |
| 735 |
|
| 736 |
if (variable == transformFactor) { |
| 737 |
depMatrixKnown=false; |
| 738 |
proposedChangeDepMatrix=true; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
if (variable == traitParameter){ |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
int loc= index / 2; |
| 747 |
int changedGroup=(int)assignments.getParameterValue(loc); |
| 748 |
logLikelihoodsVectorKnown[changedGroup]=false; |
| 749 |
} |
| 750 |
|
| 751 |
} |
| 752 |
|
| 753 |
Set<NodeRef> allTips; |
| 754 |
CompoundParameter traitParameter; |
| 755 |
Parameter alpha; |
| 756 |
Parameter clusterPrec ; |
| 757 |
Parameter priorPrec ; |
| 758 |
Parameter priorMean ; |
| 759 |
Parameter assignments; |
| 760 |
Parameter links; |
| 761 |
Parameter means2; |
| 762 |
Parameter means1; |
| 763 |
Parameter locationDrift; |
| 764 |
Parameter offsets; |
| 765 |
boolean hasDrift; |
| 766 |
|
| 767 |
private boolean depMatrixKnown= false; |
| 768 |
private boolean[] dataMatrixKnown; |
| 769 |
private boolean logLikelihoodKnown=false; |
| 770 |
private double logLikelihood =0.0; |
| 771 |
private boolean[] logLikelihoodsVectorKnown; |
| 772 |
|
| 773 |
|
| 774 |
boolean proposedChangeDepMatrix=false; |
| 775 |
boolean proposedChangeDataMatrix=false; |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
TreeModel treeModel; |
| 781 |
String traitName; |
| 782 |
|
| 783 |
double[][] depMatrix; |
| 784 |
double[][] logDepMatrix; |
| 785 |
double[] logLikelihoodsVector; |
| 786 |
double[] storedLogLikelihoodsVector; |
| 787 |
int numdata; |
| 788 |
Parameter transformFactor; |
| 789 |
double k0; |
| 790 |
double v0; |
| 791 |
double[][] T0Inv; |
| 792 |
double[] m; |
| 793 |
double logDetT0; |
| 794 |
|
| 795 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 796 |
public final static String CLUSTER_PREC = "clusterPrec"; |
| 797 |
public final static String PRIOR_PREC = "priorPrec"; |
| 798 |
public final static String PRIOR_MEAN = "priorMean"; |
| 799 |
public final static String ASSIGNMENTS = "assignments"; |
| 800 |
public final static String LINKS = "links"; |
| 801 |
public final static String MEANS_1 = "clusterMeans1"; |
| 802 |
public final static String MEANS_2 = "clusterMeans2"; |
| 803 |
public final static String TRANSFORM_FACTOR = "transformFactor"; |
| 804 |
public final static String CHI = "chi"; |
| 805 |
public final static String OFFSETS = "offsets"; |
| 806 |
public final static String LOCATION_DRIFT = "locationDrift"; |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
boolean integrate = false; |
| 812 |
|
| 813 |
|
| 814 |
public String getParserName() { |
| 815 |
return NP_ANTIGENIC_LIKELIHOOD; |
| 816 |
} |
| 817 |
|
| 818 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 819 |
|
| 820 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 821 |
|
| 822 |
|
| 823 |
XMLObject cxo = xo.getChild(CLUSTER_PREC); |
| 824 |
Parameter clusterPrec = (Parameter) cxo.getChild(Parameter.class); |
| 825 |
|
| 826 |
cxo = xo.getChild(PRIOR_PREC); |
| 827 |
Parameter priorPrec = (Parameter) cxo.getChild(Parameter.class); |
| 828 |
|
| 829 |
cxo = xo.getChild(PRIOR_MEAN); |
| 830 |
Parameter priorMean = (Parameter) cxo.getChild(Parameter.class); |
| 831 |
|
| 832 |
cxo = xo.getChild(ASSIGNMENTS); |
| 833 |
Parameter assignments = (Parameter) cxo.getChild(Parameter.class); |
| 834 |
|
| 835 |
|
| 836 |
cxo = xo.getChild(LINKS); |
| 837 |
Parameter links = (Parameter) cxo.getChild(Parameter.class); |
| 838 |
|
| 839 |
cxo = xo.getChild(MEANS_2); |
| 840 |
Parameter means2 = (Parameter) cxo.getChild(Parameter.class); |
| 841 |
|
| 842 |
cxo = xo.getChild(MEANS_1); |
| 843 |
Parameter means1 = (Parameter) cxo.getChild(Parameter.class); |
| 844 |
|
| 845 |
cxo = xo.getChild(CHI); |
| 846 |
Parameter chi = (Parameter) cxo.getChild(Parameter.class); |
| 847 |
|
| 848 |
cxo = xo.getChild(TRANSFORM_FACTOR); |
| 849 |
Parameter transformFactor = (Parameter) cxo.getChild(Parameter.class); |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
cxo=xo.getChild(LOCATION_DRIFT) ; |
| 854 |
Parameter locationDrift= (Parameter) cxo.getChild(Parameter.class); |
| 855 |
|
| 856 |
cxo=xo.getChild(OFFSETS); |
| 857 |
Parameter offsets =(Parameter) cxo.getChild(Parameter.class); |
| 858 |
|
| 859 |
boolean hasDrift = false; |
| 860 |
if (offsets.getDimension()>1){ |
| 861 |
hasDrift=true; |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
TreeTraitParserUtilities utilities = new TreeTraitParserUtilities(); |
| 867 |
String traitName = TreeTraitParserUtilities.DEFAULT_TRAIT_NAME; |
| 868 |
|
| 869 |
|
| 870 |
TreeTraitParserUtilities.TraitsAndMissingIndices returnValue = |
| 871 |
utilities.parseTraitsFromTaxonAttributes(xo, traitName, treeModel, integrate); |
| 872 |
|
| 873 |
CompoundParameter traitParameter = returnValue.traitParameter; |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
return new NPAntigenicLikelihood(treeModel,traitParameter, assignments, links, chi,clusterPrec, priorMean,priorPrec, |
| 880 |
transformFactor, means1,means2,locationDrift,offsets,hasDrift); |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
public String getParserDescription() { |
| 888 |
return "conditional likelihood ddCRP"; |
| 889 |
} |
| 890 |
|
| 891 |
public Class getReturnType() { |
| 892 |
return NPAntigenicLikelihood.class; |
| 893 |
} |
| 894 |
|
| 895 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 896 |
return rules; |
| 897 |
} |
| 898 |
|
| 899 |
private final XMLSyntaxRule[] rules = { |
| 900 |
new StringAttributeRule(TreeTraitParserUtilities.TRAIT_NAME, "The name of the trait for which a likelihood should be calculated"), |
| 901 |
|
| 902 |
new ElementRule(TreeTraitParserUtilities.TRAIT_PARAMETER, new XMLSyntaxRule[]{ |
| 903 |
new ElementRule(Parameter.class) |
| 904 |
}), |
| 905 |
new ElementRule(PRIOR_PREC, |
| 906 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 907 |
new ElementRule(CLUSTER_PREC, |
| 908 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 909 |
new ElementRule(PRIOR_MEAN, |
| 910 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 911 |
new ElementRule(ASSIGNMENTS, |
| 912 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 913 |
new ElementRule(LINKS, |
| 914 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 915 |
new ElementRule(TRANSFORM_FACTOR, |
| 916 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 917 |
|
| 918 |
new ElementRule(MEANS_1, |
| 919 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 920 |
new ElementRule(MEANS_2, |
| 921 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 922 |
new ElementRule(CHI, new XMLSyntaxRule[] { |
| 923 |
new ElementRule(Parameter.class), |
| 924 |
}), |
| 925 |
new ElementRule(OFFSETS, Parameter.class), |
| 926 |
new ElementRule(LOCATION_DRIFT, Parameter.class), |
| 927 |
|
| 928 |
new ElementRule(TreeModel.class), |
| 929 |
|
| 930 |
}; |
| 931 |
}; |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
} |